chirpnest/utils.go
2019-05-19 20:02:38 +08:00

116 lines
2.7 KiB
Go

package main
import (
"errors"
"log"
"regexp"
"strconv"
"time"
)
func failOnError(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", msg, err)
}
}
func logOnError(err error, msg string) {
if err != nil {
log.Printf("%s: %s", msg, err)
}
}
func Min(a int, b int) int {
if a < b {
return a
} else {
return b
}
}
func Max(a int, b int) int {
if a > b {
return a
} else {
return b
}
}
func CastleID(c string) int {
switch c {
case "Deerhorn":
return castleDeer
case "Dragonscale":
return castleDragon
case "Highnest":
return castleHighnest
case "Moonlight":
return castleMoon
case "Potato":
return castlePotato
case "Sharkteeth":
return castleShark
case "Wolfpack":
return castleWolf
case "🦌":
return castleDeer
case "🐉":
return castleDragon
case "🦅":
return castleHighnest
case "🌑":
return castleMoon
case "🥔":
return castlePotato
case "🦈":
return castleShark
case "🐺":
return castleWolf
default:
return 0
}
return 0
}
func fromChatWarsDate(d string) (t time.Time, err error) {
r := regexp.MustCompile(`(?P<Day>[0-9]{2}) (?P<Month>(Wintar|Hornung|Lenzin|Ōstar|Winni|Brāh|Hewi|Aran|Witu|Wīndume|Herbist|Hailag)) (?P<Year>[0-9]{4})( (?P<Hour>[0-9]{2}):(?P<Minute>[0-9]{2})){0,1}`)
if r.FindStringSubmatch(d) != nil {
// log.Printf("fromChatWarsDate : Day : %s\n", r.ReplaceAllString(d, "${Day}"))
// log.Printf("fromChatWarsDate : Month : %s\n", r.ReplaceAllString(d, "${Month}"))
// log.Printf("fromChatWarsDate : Year : %s\n", r.ReplaceAllString(d, "${Year}"))
// log.Printf("fromChatWarsDate : Hour : %s\n", r.ReplaceAllString(d, "${Hour}"))
// log.Printf("fromChatWarsDate : Minute : %s\n", r.ReplaceAllString(d, "${Minute}"))
year, _ := strconv.Atoi(r.ReplaceAllString(d, "${Year}"))
day, _ := strconv.Atoi(r.ReplaceAllString(d, "${Day}"))
hour, _ := strconv.Atoi(r.ReplaceAllString(d, "${Hour}"))
minute, _ := strconv.Atoi(r.ReplaceAllString(d, "${Minute}"))
days := int(0)
for i := 1059; i < year; i++ {
days = days + 365
}
for i := 1; i < chatWarsMonth[r.ReplaceAllString(d, "${Month}")]; i++ {
if _, special := chatWarsDaysSpecial[year][i]; special {
days = days + chatWarsDaysSpecial[year][i]
} else {
days = days + chatWarsDays[i]
}
}
for i := 1; i < day; i++ {
days++
}
t, _ := time.Parse(time.RFC3339, "2017-11-18T21:00:00+00:00")
t = t.AddDate(0, 0, (days / 3))
t = t.Add(time.Hour * time.Duration(days%3) * 8)
t = t.Add(time.Minute * time.Duration(hour) * 20)
t = t.Add(time.Second * time.Duration(minute) * 20)
return t, nil
} else {
return time.Now(), errors.New("Wrong format")
}
}
func toChatWarsDate(t time.Time) (s string, err error) {
return "test", nil
}