vyvanse/cmd/vyvanse/spla2n.go

115 lines
2.5 KiB
Go

package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/bwmarrin/discordgo"
opentracing "github.com/opentracing/opentracing-go"
otlog "github.com/opentracing/opentracing-go/log"
)
func spla2nMaps(ctx context.Context, s *discordgo.Session, msg *discordgo.Message, parv []string) error {
sp, ctx := opentracing.StartSpanFromContext(ctx, "spla2nMaps")
defer sp.Finish()
resp, err := http.Get("https://splatoon.ink/schedule2")
if err != nil {
sp.LogFields(otlog.Error(err), otlog.String("step", "http get"))
return err
}
st := &splattus{}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
sp.LogFields(otlog.Error(err), otlog.String("step", "http response read"))
return err
}
json.Unmarshal(body, st)
var modeInfo []string
for _, mode := range st.Modes.Regular {
if mode.Active() {
modeInfo = append(modeInfo, mode.String())
}
}
for _, mode := range st.Modes.Gachi {
if mode.Active() {
modeInfo = append(modeInfo, mode.String())
}
}
for _, mode := range st.Modes.League {
if mode.Active() {
modeInfo = append(modeInfo, mode.String())
}
}
text := strings.Join(modeInfo, "\n")
_, err = s.ChannelMessageSend(msg.ChannelID, text)
return err
}
type splatoonMode struct {
StartTime int64 `json:"startTime"`
EndTime int64 `json:"endTime"`
Maps []string `json:"maps"`
Rule splatoonRule `json:"rule"`
Mode splatoonGameMode `json:"mode"`
}
func (sm splatoonMode) Active() bool {
beg := time.Unix(sm.StartTime, 0)
end := time.Unix(sm.EndTime, 0)
now := time.Now()
return now.After(beg) && now.Before(end)
}
func (sm splatoonMode) String() string {
maps := strings.Join(sm.Maps, ", ")
end := time.Unix(sm.EndTime, 0)
now := time.Now()
diff := end.Sub(now)
return fmt.Sprintf("%s:\nRotation ends at %s (in %s)\nMaps: %s\nRule: %s\n", sm.Mode, end.Format(time.RFC3339), diff, maps, sm.Rule)
}
type splatoonGameMode struct {
Key string `json:"key"`
Name string `json:"name"`
}
func (sgm splatoonGameMode) String() string {
return sgm.Name
}
type splatoonRule struct {
Key string `json:"key"`
MultilineName string `json:"multiline_name"`
Name string `json:"name"`
}
func (sr splatoonRule) String() string {
return sr.Name
}
type splattus struct {
UpdateTime int64 `json:"updateTime"`
Modes struct {
League []splatoonMode `json:"league"`
Regular []splatoonMode `json:"regular"`
Gachi []splatoonMode `json:"gachi"`
} `json:"modes"`
}