forked from ebemunk/pgnstats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameStats.go
177 lines (148 loc) · 4.29 KB
/
GameStats.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"encoding/json"
"sort"
)
type PosMap map[string]int
//PlyMap is a map[int]float64
type PlyMap map[int]float64
//MarshalJSON marshals to json
func (m PlyMap) MarshalJSON() ([]byte, error) {
if len(m) < 1 {
return json.Marshal([]int{})
}
keys := make([]int, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Ints(keys)
max := keys[len(keys)-1]
sorted := make([]float64, 0, max)
for i := 0; i <= max; i++ {
sorted = append(sorted, m[i])
}
return json.Marshal(sorted)
}
//Heatmaps are all the Heatmaps we return
type Heatmaps struct {
SquareUtilization Heatmap
MoveSquares Heatmap
CaptureSquares Heatmap
CheckSquares Heatmap
FirstBlood Heatmap
PromotionSquares Heatmap
EnPassantSquares Heatmap
MateSquares Heatmap
MateDeliverySquares Heatmap
StalemateSquares Heatmap
}
//GameStats is the statistics for games
type GameStats struct {
Total uint64
GameLengths PlyMap
BranchingFactor PlyMap
MaterialCount PlyMap
MaterialDiff PlyMap
GameEndMaterialCount PlyMap
GameEndMaterialDiff PlyMap
Years map[string]int
Ratings map[string]int
Heatmaps Heatmaps
Openings *OpeningMove
Trax PieceTracker
Positions PosMap
TotalPositions int
UniquePositions int
}
//NewGameStats creates new GameStats
func NewGameStats() *GameStats {
return &GameStats{
Total: 0,
GameLengths: make(map[int]float64),
BranchingFactor: make(map[int]float64),
MaterialCount: make(map[int]float64),
MaterialDiff: make(map[int]float64),
GameEndMaterialCount: make(map[int]float64),
GameEndMaterialDiff: make(map[int]float64),
Years: make(map[string]int),
Ratings: make(map[string]int),
Heatmaps: Heatmaps{
SquareUtilization: *NewHeatmap(),
MoveSquares: *NewHeatmap(),
CaptureSquares: *NewHeatmap(),
CheckSquares: *NewHeatmap(),
FirstBlood: *NewHeatmap(),
PromotionSquares: *NewHeatmap(),
EnPassantSquares: *NewHeatmap(),
MateSquares: *NewHeatmap(),
MateDeliverySquares: *NewHeatmap(),
StalemateSquares: *NewHeatmap(),
},
Trax: *NewPieceTracker(),
Positions: make(PosMap),
TotalPositions: 0,
UniquePositions: 0,
}
}
//Add adds GameStats together
func (gs *GameStats) Add(ad *GameStats) {
for k, v := range ad.GameLengths {
gs.GameLengths[k] += v
}
for k, v := range ad.BranchingFactor {
gs.BranchingFactor[k] += v
}
for k, v := range ad.MaterialCount {
gs.MaterialCount[k] += v
}
for k, v := range ad.MaterialDiff {
gs.MaterialDiff[k] += v
}
for k, v := range ad.GameEndMaterialCount {
gs.GameEndMaterialCount[k] += v
}
for k, v := range ad.GameEndMaterialDiff {
gs.GameEndMaterialDiff[k] += v
}
for k, v := range ad.Years {
gs.Years[k] += v
}
for k, v := range ad.Ratings {
gs.Ratings[k] += v
}
for k, v := range ad.Positions {
gs.Positions[k] += v
gs.TotalPositions += v
}
gs.Heatmaps.SquareUtilization.Add(&ad.Heatmaps.SquareUtilization)
gs.Heatmaps.MoveSquares.Add(&ad.Heatmaps.MoveSquares)
gs.Heatmaps.CaptureSquares.Add(&ad.Heatmaps.CaptureSquares)
gs.Heatmaps.CheckSquares.Add(&ad.Heatmaps.CheckSquares)
gs.Heatmaps.FirstBlood.Add(&ad.Heatmaps.FirstBlood)
gs.Heatmaps.PromotionSquares.Add(&ad.Heatmaps.PromotionSquares)
gs.Heatmaps.EnPassantSquares.Add(&ad.Heatmaps.EnPassantSquares)
gs.Heatmaps.MateSquares.Add(&ad.Heatmaps.MateSquares)
gs.Heatmaps.MateDeliverySquares.Add(&ad.Heatmaps.MateDeliverySquares)
gs.Heatmaps.StalemateSquares.Add(&ad.Heatmaps.StalemateSquares)
gs.Trax.Add(&ad.Trax)
gs.Total++
}
//Average averages statistics by total games
func (gs *GameStats) Average() {
for k, v := range gs.BranchingFactor {
gs.BranchingFactor[k] = v / float64(gs.Total)
}
for k, v := range gs.MaterialCount {
gs.MaterialCount[k] = v / float64(gs.Total)
}
for k, v := range gs.MaterialDiff {
gs.MaterialDiff[k] = v / float64(gs.Total)
}
for k, v := range gs.GameEndMaterialCount {
gs.GameEndMaterialCount[k] = v / gs.GameLengths[k]
}
for k, v := range gs.GameEndMaterialDiff {
gs.GameEndMaterialDiff[k] = v / gs.GameLengths[k]
}
gs.UniquePositions = len(gs.Positions)
}