160 lines
5.3 KiB
Go
160 lines
5.3 KiB
Go
package main
|
|
|
|
import (
|
|
"berkutschi/berkutschi"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/rs/zerolog"
|
|
"github.com/rs/zerolog/log"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const event = 3858
|
|
|
|
var Judges = [5]JugdeRaceInfo{}
|
|
|
|
func main() {
|
|
zerolog.TimeFieldFormat = time.RFC1123Z
|
|
f, err := os.OpenFile("my.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
|
|
consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC1123Z}
|
|
multi := zerolog.MultiLevelWriter(consoleWriter, f)
|
|
log.Logger = zerolog.New(multi).With().Timestamp().Caller().Logger()
|
|
|
|
pollData, err := berkutschi.Poll(event)
|
|
if err != nil {
|
|
log.Panic().Err(fmt.Errorf("Error while polling: %s", err)).Send()
|
|
}
|
|
fillJudgeraceinfo(pollData)
|
|
|
|
b := berkutschi.Init(event)
|
|
|
|
go func() {
|
|
jumperInfo := JumperInfo{}
|
|
jumperscore := JumperScore{}
|
|
for {
|
|
message := <-b.RX
|
|
var jumpMessage BerkutschiJumpUpdateMessages
|
|
err := json.Unmarshal(message, &jumpMessage)
|
|
if err == nil && jumpMessage[0].Data.Current.Lastname != "" {
|
|
jumper, err := fillJumperInfo(jumpMessage)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if newJumperInfoData(jumperInfo, jumper) {
|
|
jumperInfo = jumper
|
|
log.Info().Msgf("New jumper info: %+v", jumperInfo)
|
|
}
|
|
score, _ := fillJumperScore(jumpMessage)
|
|
if score.Rank != 0 && newJumperScoreData(jumperscore, score) {
|
|
jumperscore = score
|
|
log.Info().Msgf("New jumper score: %+v", jumperscore)
|
|
}
|
|
} else if err != nil {
|
|
log.Error().RawJSON("receivedMessage", message).Err(fmt.Errorf("%v", err))
|
|
} else {
|
|
if jumpMessage[0].Data.Next.Lastname == "" {
|
|
log.Debug().RawJSON("receivedMessage", message).Msg("Received message i cant decode")
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
|
|
ctx, _ := context.WithCancel(context.Background())
|
|
<-ctx.Done()
|
|
}
|
|
|
|
func fillJumperInfo(m BerkutschiJumpUpdateMessages) (JumperInfo, error) {
|
|
var err error
|
|
JumperInfo := JumperInfo{}
|
|
if m[0].Data.Current.Firstname == "" || m[0].Data.Current.Lastname == "" {
|
|
err = fmt.Errorf("Name is empty")
|
|
return JumperInfo, err
|
|
}
|
|
JumperInfo.Name = fmt.Sprint(m[0].Data.Current.Firstname, " ", strings.ToUpper(m[0].Data.Current.Lastname))
|
|
JumperInfo.Bib = m[0].Data.Current.Bib
|
|
JumperInfo.Nation = strings.ToUpper(m[0].Data.Current.Nat)
|
|
JumperInfo.Image = m[0].Data.Current.Image2
|
|
|
|
return JumperInfo, err
|
|
}
|
|
|
|
func fillJumperScore(m BerkutschiJumpUpdateMessages) (JumperScore, error) {
|
|
var err error
|
|
JumperScore := JumperScore{}
|
|
if m[0].Data.Current.Firstname == "" || m[0].Data.Current.Lastname == "" {
|
|
err = fmt.Errorf("Name is empty")
|
|
return JumperScore, err
|
|
}
|
|
JumperScore.Name = fmt.Sprint(m[0].Data.Current.Firstname, " ", strings.ToUpper(m[0].Data.Current.Lastname))
|
|
JumperScore.Bib = m[0].Data.Current.Bib
|
|
JumperScore.Nation = strings.ToUpper(m[0].Data.Current.Nat)
|
|
JumperScore.Points = m[0].Data.Current.Points.Points
|
|
JumperScore.Rank = m[0].Data.Current.Points.Rank
|
|
JumperScore.Wind = m[0].Data.Current.Wind.Wind
|
|
JumperScore.Length = fmt.Sprintf("%vm", m[0].Data.Current.Length.Length)
|
|
for i, _ := range JumperScore.Judges {
|
|
switch i {
|
|
case 0:
|
|
JumperScore.Judges[i].Score = m[0].Data.Current.Judge.One.Rate
|
|
JumperScore.Judges[i].Nation = Judges[i].Nation
|
|
JumperScore.Judges[i].Discard = m[0].Data.Current.Judge.One.Discard
|
|
case 1:
|
|
JumperScore.Judges[i].Score = m[0].Data.Current.Judge.Two.Rate
|
|
JumperScore.Judges[i].Nation = Judges[i].Nation
|
|
JumperScore.Judges[i].Discard = m[0].Data.Current.Judge.Two.Discard
|
|
case 2:
|
|
JumperScore.Judges[i].Score = m[0].Data.Current.Judge.Three.Rate
|
|
JumperScore.Judges[i].Nation = Judges[i].Nation
|
|
JumperScore.Judges[i].Discard = m[0].Data.Current.Judge.Three.Discard
|
|
case 3:
|
|
JumperScore.Judges[i].Score = m[0].Data.Current.Judge.Four.Rate
|
|
JumperScore.Judges[i].Nation = Judges[i].Nation
|
|
JumperScore.Judges[i].Discard = m[0].Data.Current.Judge.Four.Discard
|
|
case 4:
|
|
JumperScore.Judges[i].Score = m[0].Data.Current.Judge.Five.Rate
|
|
JumperScore.Judges[i].Nation = Judges[i].Nation
|
|
JumperScore.Judges[i].Discard = m[0].Data.Current.Judge.Five.Discard
|
|
}
|
|
}
|
|
|
|
return JumperScore, err
|
|
}
|
|
|
|
func fillJudgeraceinfo(data berkutschi.PollData) {
|
|
for i, _ := range Judges {
|
|
switch i {
|
|
case 0:
|
|
Judges[i].Nation = strings.ToUpper(data.Data.Raceinfo.Judges.One.Nation)
|
|
case 1:
|
|
Judges[i].Nation = strings.ToUpper(data.Data.Raceinfo.Judges.Two.Nation)
|
|
case 2:
|
|
Judges[i].Nation = strings.ToUpper(data.Data.Raceinfo.Judges.Three.Nation)
|
|
case 3:
|
|
Judges[i].Nation = strings.ToUpper(data.Data.Raceinfo.Judges.Four.Nation)
|
|
case 4:
|
|
Judges[i].Nation = strings.ToUpper(data.Data.Raceinfo.Judges.Five.Nation)
|
|
}
|
|
}
|
|
}
|
|
|
|
func newJumperInfoData(oldData, newData JumperInfo) bool {
|
|
if oldData.Name != newData.Name || oldData.Bib != newData.Bib || oldData.Nation != newData.Nation || oldData.Image != newData.Image {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
func newJumperScoreData(oldData, newData JumperScore) bool {
|
|
if oldData.Name != newData.Name || oldData.Bib != newData.Bib || oldData.Nation != newData.Nation ||
|
|
oldData.Points != newData.Points || oldData.Rank != newData.Rank || oldData.Wind != newData.Wind || oldData.Length != newData.Length {
|
|
return true
|
|
}
|
|
for i, oldJudge := range oldData.Judges {
|
|
if oldJudge.Score != newData.Judges[i].Score || oldJudge.Nation != newData.Judges[i].Nation || oldJudge.Discard != newData.Judges[i].Discard {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|