/
vis.go
140 lines (120 loc) · 3.06 KB
/
vis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package visual
import (
"context"
"fmt"
"github.com/kkoralsky/gosprints/core"
pb "github.com/kkoralsky/gosprints/proto"
"image/color"
"math"
"time"
)
type VisInterface interface {
pb.VisualServer
Run()
ResetConfiguration() bool
IsConfigured() bool
GetVisCfg() *pb.VisConfiguration
Clear()
updRacingData(playerNum, distance uint32) (realDistance, velocity float32)
}
type RacingData struct {
dist uint32
ts time.Time
realDist float32
velo float32
}
type BaseVis struct {
VisInterface
visCfg *pb.VisConfiguration
configured bool
colors []color.RGBA
playerCount uint32
playerNames []string
destValue uint32
mode pb.Tournament_TournamentMode
modeUnit string
racingData map[uint32]RacingData
}
func (b *BaseVis) ResetConfiguration() bool {
b.configured = !b.configured
return b.configured
}
func (b *BaseVis) IsConfigured() bool {
return b.configured
}
func (b *BaseVis) GetVisCfg() *pb.VisConfiguration {
return b.visCfg
}
func (b *BaseVis) updRacingData(playerNum, dist uint32) (realDist, velo float32) {
var (
now = time.Now()
racingData, ok = b.racingData[playerNum]
km = float32(100 * 1000)
m = float32(100)
)
if ok {
realDist = float32(b.visCfg.DistFactor*dist) / m
velo = float32(dist-racingData.dist) / float32(now.Sub(racingData.ts).Hours())
velo *= float32(b.visCfg.DistFactor) / km
}
if b.racingData == nil {
b.racingData = make(map[uint32]RacingData, b.playerCount)
}
b.racingData[playerNum] = RacingData{
dist: dist,
ts: now,
realDist: realDist,
velo: velo,
}
return
}
func (b *BaseVis) getResult(result float32) float32 {
switch b.mode {
case pb.Tournament_DISTANCE:
return result * float32(math.Pow10(-9)) // decode from nanoseconds to seconds
case pb.Tournament_TIME:
return result
}
return 0.0
}
func selectVis(visName string) (VisInterface, error) {
switch visName {
case "bar":
return NewBarVis(), nil
case "clock":
return NewClockVis(), nil
case "clock2":
return NewClock2Vis(), nil
case "game":
return NewGameVis(), nil
default:
err := fmt.Errorf("'%s' visualization not found; falling back to 'bar'", visName)
core.ErrorLogger.Println(err.Error())
return &barVis{}, nil
}
}
func Reconfigure(b VisInterface) VisInterface {
if !b.IsConfigured() {
vis, _ := selectVis(b.GetVisCfg().VisName)
vis.ConfigureVis(context.Background(), b.GetVisCfg())
return vis
}
return nil
}
func SetupVis(hostName string, visName string, fullscreen bool, resolutionWidth uint,
resolutionHeight uint, movingUnit uint, distFactor uint) (VisInterface, error) {
vis, err := selectVis(visName)
if vis != nil {
vis.ConfigureVis(context.Background(), &pb.VisConfiguration{
HostName: hostName,
VisName: visName,
Fullscreen: fullscreen,
ResolutionWidth: uint32(resolutionWidth),
ResolutionHeight: uint32(resolutionHeight),
MovingUnit: uint32(movingUnit),
DistFactor: uint32(distFactor),
})
vis.NewTournament(context.Background(), &core.DefultTournament)
}
return vis, err
}