-
Notifications
You must be signed in to change notification settings - Fork 1
/
gamestats.go
335 lines (294 loc) · 9.62 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package dctycoon
import (
"fmt"
"reflect"
"time"
"github.com/nzin/dctycoon/supplier"
log "github.com/sirupsen/logrus"
)
type ReputationStat struct {
reputation float64
date time.Time
}
func (self *ReputationStat) Save() string {
str := "{"
str += fmt.Sprintf("\"date\": \"%d-%d-%d\",", self.date.Year(), self.date.Month(), self.date.Day())
str += fmt.Sprintf("\"reputation\": %f", self.reputation)
return str + "}"
}
func NewReputationStat(v map[string]interface{}) *ReputationStat {
var year, month, day int
fmt.Sscanf(v["date"].(string), "%d-%d-%d", &year, &month, &day)
rs := &ReputationStat{
reputation: v["reputation"].(float64),
date: time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC),
}
return rs
}
type PowerStat struct {
consumption float64
generation float64
provided float64
cooler float64
date time.Time
}
func (self *PowerStat) Save() string {
str := "{"
str += fmt.Sprintf("\"date\": \"%d-%d-%d\",", self.date.Year(), self.date.Month(), self.date.Day())
str += fmt.Sprintf("\"consumption\": %f,", self.consumption)
str += fmt.Sprintf("\"generation\": %f,", self.generation)
str += fmt.Sprintf("\"provided\": %f,", self.provided)
str += fmt.Sprintf("\"cooler\": %f", self.cooler)
return str + "}"
}
func NewPowerStat(v map[string]interface{}) *PowerStat {
var year, month, day int
fmt.Sscanf(v["date"].(string), "%d-%d-%d", &year, &month, &day)
ps := &PowerStat{
consumption: v["consumption"].(float64),
generation: v["generation"].(float64),
provided: v["provided"].(float64),
cooler: v["cooler"].(float64),
date: time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC),
}
return ps
}
type ServerDemandStat struct {
ramsize int32
nbcores int32
disksize int32
nb int32
}
type DemandStat struct {
serverdemands []*ServerDemandStat
date time.Time
price float64
buyer string
}
func (self *DemandStat) Save() string {
str := "{"
str += fmt.Sprintf("\"date\": \"%d-%d-%d\",", self.date.Year(), self.date.Month(), self.date.Day())
str += fmt.Sprintf("\"price\": %f,", self.price)
str += fmt.Sprintf("\"buyer\": \"%s\",", self.buyer)
str += "\"servers\": ["
for i, s := range self.serverdemands {
if i != 0 {
str += ","
}
str += fmt.Sprintf(`{"ramsize": %d, "nbcores": %d, "disksize": %d, "nb":%d}`, s.ramsize, s.nbcores, s.disksize, s.nb)
}
return str + "]}"
}
func NewDemandStat(v map[string]interface{}) *DemandStat {
var year, month, day int
fmt.Sscanf(v["date"].(string), "%d-%d-%d", &year, &month, &day)
ds := &DemandStat{
price: v["price"].(float64),
date: time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC),
buyer: v["buyer"].(string),
serverdemands: make([]*ServerDemandStat, 0, 0),
}
servers := v["servers"].([]interface{})
for _, s := range servers {
server := s.(map[string]interface{})
demand := &ServerDemandStat{
ramsize: int32(server["ramsize"].(float64)),
nbcores: int32(server["nbcores"].(float64)),
disksize: int32(server["disksize"].(float64)),
nb: int32(server["nb"].(float64)),
}
ds.serverdemands = append(ds.serverdemands, demand)
}
return ds
}
// DemandStatSubscriber is mainly used by the MainStatsWidget
type DemandStatSubscriber interface {
NewDemandStat(*DemandStat)
}
// PowerStatSubscriber is mainly used by the MainStatsWidget
type PowerStatSubscriber interface {
NewPowerStat(*PowerStat)
}
// ReputationStatSubscriber is mainly used by the MainStatsWidget
type ReputationStatSubscriber interface {
NewReputationStat(*ReputationStat)
}
type GameStats struct {
demandstatsubscribers []DemandStatSubscriber
demandsstats []*DemandStat
powerstatssubscribers []PowerStatSubscriber
powerstats []*PowerStat
reputationstatssubscribers []ReputationStatSubscriber
reputationstats []*ReputationStat
}
func (self *GameStats) AddDemandStatSubscriber(subscriber DemandStatSubscriber) {
for _, s := range self.demandstatsubscribers {
if s == subscriber {
return
}
}
self.demandstatsubscribers = append(self.demandstatsubscribers, subscriber)
}
func (self *GameStats) RemoveDemandStatSubscriber(subscriber DemandStatSubscriber) {
for i, s := range self.demandstatsubscribers {
if s == subscriber {
self.demandstatsubscribers = append(self.demandstatsubscribers[:i], self.demandstatsubscribers[i+1:]...)
break
}
}
}
func (self *GameStats) TriggerDemandStat(t time.Time, demand *supplier.DemandInstance, actor supplier.Actor, serverbundle *supplier.ServerBundle) {
log.Debug("GameStats::TriggerDemandStat(", t, ",", demand, ",", serverbundle, ")")
stat := &DemandStat{
date: t,
serverdemands: make([]*ServerDemandStat, 0, 0),
}
if serverbundle != nil {
stat.buyer = actor.GetName()
for _, sc := range serverbundle.Contracts {
stat.price += sc.Price
}
}
for templatename, templatevalue := range demand.Template.Specs {
sds := &ServerDemandStat{
nb: demand.Nb[templatename],
}
for _, filter := range templatevalue.Filters {
if reflect.TypeOf(filter) == reflect.TypeOf((**supplier.CriteriaFilterDisk)(nil)).Elem() {
sds.disksize = (filter.(*supplier.CriteriaFilterDisk)).Disksize
}
if reflect.TypeOf(filter) == reflect.TypeOf((**supplier.CriteriaFilterRam)(nil)).Elem() {
sds.ramsize = (filter.(*supplier.CriteriaFilterRam)).Ramsize
}
if reflect.TypeOf(filter) == reflect.TypeOf((**supplier.CriteriaFilterNbcores)(nil)).Elem() {
sds.disksize = (filter.(*supplier.CriteriaFilterNbcores)).Nbcores
}
}
stat.serverdemands = append(stat.serverdemands, sds)
}
self.demandsstats = append(self.demandsstats, stat)
for _, s := range self.demandstatsubscribers {
s.NewDemandStat(stat)
}
}
func (self *GameStats) AddPowerStatSubscriber(subscriber PowerStatSubscriber) {
for _, s := range self.powerstatssubscribers {
if s == subscriber {
return
}
}
self.powerstatssubscribers = append(self.powerstatssubscribers, subscriber)
}
func (self *GameStats) RemovePowerStatSubscriber(subscriber PowerStatSubscriber) {
for i, s := range self.powerstatssubscribers {
if s == subscriber {
self.powerstatssubscribers = append(self.powerstatssubscribers[:i], self.powerstatssubscribers[i+1:]...)
break
}
}
}
func (self *GameStats) PowerChange(t time.Time, consumption, generation, provided, cooler float64) {
stat := &PowerStat{
date: t,
consumption: consumption,
generation: generation,
provided: provided,
cooler: cooler,
}
self.powerstats = append(self.powerstats, stat)
for _, s := range self.powerstatssubscribers {
s.NewPowerStat(stat)
}
}
func (self *GameStats) AddReputationStatSubscriber(subscriber ReputationStatSubscriber) {
for _, s := range self.reputationstatssubscribers {
if s == subscriber {
return
}
}
self.reputationstatssubscribers = append(self.reputationstatssubscribers, subscriber)
}
func (self *GameStats) RemoveReputationStatSubscriber(subscriber ReputationStatSubscriber) {
for i, s := range self.reputationstatssubscribers {
if s == subscriber {
self.reputationstatssubscribers = append(self.reputationstatssubscribers[:i], self.reputationstatssubscribers[i+1:]...)
break
}
}
}
func (self *GameStats) NewReputationScore(date time.Time, score float64) {
stat := &ReputationStat{
reputation: score,
date: date,
}
self.reputationstats = append(self.reputationstats, stat)
for _, s := range self.reputationstatssubscribers {
s.NewReputationStat(stat)
}
}
func NewGameStats() *GameStats {
gs := &GameStats{
demandsstats: make([]*DemandStat, 0, 0),
demandstatsubscribers: make([]DemandStatSubscriber, 0, 0),
powerstats: make([]*PowerStat, 0, 0),
powerstatssubscribers: make([]PowerStatSubscriber, 0, 0),
reputationstats: make([]*ReputationStat, 0, 0),
reputationstatssubscribers: make([]ReputationStatSubscriber, 0, 0),
}
return gs
}
func (self *GameStats) InitGame(inventory *supplier.Inventory, reputation *supplier.Reputation) {
self.demandsstats = make([]*DemandStat, 0, 0)
self.powerstats = make([]*PowerStat, 0, 0)
self.reputationstats = make([]*ReputationStat, 0, 0)
inventory.AddPowerStatSubscriber(self)
reputation.AddReputationSubscriber(self)
}
func (self *GameStats) LoadGame(inventory *supplier.Inventory, reputation *supplier.Reputation, stats map[string]interface{}) {
log.Debug("GameStats::LoadGame(", stats, ")")
self.demandsstats = make([]*DemandStat, 0, 0)
inventory.AddPowerStatSubscriber(self)
demandsstats := stats["demandsstats"].([]interface{})
for _, d := range demandsstats {
self.demandsstats = append(self.demandsstats, NewDemandStat(d.(map[string]interface{})))
}
self.powerstats = make([]*PowerStat, 0, 0)
powerstats := stats["powerstats"].([]interface{})
for _, d := range powerstats {
self.powerstats = append(self.powerstats, NewPowerStat(d.(map[string]interface{})))
}
self.reputationstats = make([]*ReputationStat, 0, 0)
reputationstats := stats["reputationstats"].([]interface{})
for _, d := range reputationstats {
self.reputationstats = append(self.reputationstats, NewReputationStat(d.(map[string]interface{})))
}
reputation.AddReputationSubscriber(self)
}
func (self *GameStats) Save() string {
log.Debug("GameStats::Save()")
str := "{\n"
str += "\"demandsstats\": ["
for i, d := range self.demandsstats {
if i != 0 {
str += ",\n"
}
str += d.Save()
}
str += "],\n"
str += "\"reputationstats\": ["
for i, d := range self.reputationstats {
if i != 0 {
str += ",\n"
}
str += d.Save()
}
str += "],\n"
str += "\"powerstats\": ["
for i, ps := range self.powerstats {
if i != 0 {
str += ",\n"
}
str += ps.Save()
}
return str + "]}"
}