chirpnest/utils.go

146 lines
3.4 KiB
Go
Raw Normal View History

2019-05-03 05:58:36 +02:00
package main
import (
2020-01-14 03:38:42 +01:00
"encoding/json"
2019-05-09 11:23:13 +02:00
"errors"
2020-01-14 03:39:53 +01:00
"io/ioutil"
2019-05-03 05:58:36 +02:00
"log"
2019-05-09 11:19:34 +02:00
"regexp"
"strconv"
"time"
2019-05-03 05:58:36 +02:00
)
func failOnError(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", msg, err)
}
}
2019-05-03 09:45:25 +02:00
func logOnError(err error, msg string) {
if err != nil {
log.Printf("%s: %s", msg, err)
}
}
2019-05-09 11:19:02 +02:00
2020-02-01 08:00:33 +01:00
func MinInt64(a int64, b int64) int64 {
2019-05-19 05:12:07 +02:00
if a < b {
return a
} else {
return b
}
}
2020-02-01 08:21:43 +01:00
func MaxInt64(a int64, b int64) int64 {
2019-05-19 05:12:07 +02:00
if a > b {
return a
} else {
return b
}
}
2020-01-14 03:35:20 +01:00
func ReadConfig(path string) error {
2020-01-14 03:34:46 +01:00
b, err := Asset("data/config.json")
logOnError(err, "readConfig : load data/config.json")
if err != nil {
return err
}
err = json.Unmarshal(b, &cfg)
logOnError(err, "readConfig : Unmarshal(data/config.json)")
b, err = ioutil.ReadFile(path)
logOnError(err, "readConfig : ReadFile("+path+")")
if err != nil {
return err
}
err = json.Unmarshal(b, &cfg)
logOnError(err, "readConfig : Unmarshal("+path+")")
2020-01-14 03:35:20 +01:00
return nil
2020-01-14 03:34:46 +01:00
}
2019-05-09 11:19:02 +02:00
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++ {
2020-06-27 12:41:29 +02:00
if i == 1060 {
days += 365
2020-06-27 12:41:52 +02:00
} else if i%4 == 0 {
2020-06-27 12:35:01 +02:00
days += 366
2020-06-27 12:27:16 +02:00
} else {
days += 365
}
2019-05-09 11:19:02 +02:00
}
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
}
2019-06-03 03:19:06 +02:00
func RndInt64() int64 {
2019-07-31 08:41:48 +02:00
RndMux.Lock()
2019-06-03 03:21:31 +02:00
i := RndSrc.Int63()
2019-07-31 08:41:48 +02:00
RndMux.Unlock()
2019-06-03 03:19:06 +02:00
return i
}
2019-12-20 10:34:03 +01:00
2020-06-17 12:06:05 +02:00
func RndIntn(n int) int {
RndMux.Lock()
i := RndSrc.Intn(n)
RndMux.Unlock()
return i
}
2019-12-20 10:34:03 +01:00
func hammerTimeNow(time string, weather string) bool {
if time == `🌞Morning` && weather == `🌧` ||
time == `🌙Evening` && weather == `🌤` ||
time == `🌙Night` && weather == `` {
return true
} else {
return false
}
}
func hammerTimeNext(time string, weather string) bool {
if time == `🌙Night` && weather == `🌧` ||
time == `🌞Day` && weather == `🌤` ||
time == `🌙Evening` && weather == `` {
return true
} else {
return false
}
}