-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.go
525 lines (463 loc) · 14.4 KB
/
model.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
package xfeed
import (
"time"
"github.com/golang/protobuf/ptypes"
"github.com/pkg/errors"
pb "github.com/x-feed/x-feed-sdk-golang/pkg/xfeed_proto"
)
type (
// EntitiesStreamStatus type represents current xfeed entities stream state
EntitiesStreamStatus int
// ConnectionStatus aggregates statuses of StreamEvents and StreamSettlements grpc endpoints
ConnectionStatus struct {
EventsStreamStatus EntitiesStreamStatus
SettlementsStream EntitiesStreamStatus
}
)
const (
// StatusGreen means connection is Ok bets can be accepted
StatusGreen EntitiesStreamStatus = iota + 1
// StatusYellow means that recovery is in a progress we can't accept bets
StatusYellow
// StatusRed means something is wrong we can't accept bets
StatusRed
)
type (
// SportDescription is sent for each sport which x-feed supports.
// It contains list of Periods and MarketTypes which are used for specific sport
SportDescription struct {
ID int32
Language string
Name string
Periods []*Period
MarketTypes []*MarketType
}
// Period represents Sport specific timespan of the game, e. g. full time, first half, set, etc.
Period struct {
ID int32
Name string
}
// MarketType contains template for the Market name
// Variables:
// "{%participant}" - get participant name by number in market_param "team" (1, 2)
// "{$participantN}" - participant by predefined number.
// "{+$handicap}", "{-$handicap}" - market_param "handicap"
// "{$total}" - market_param "total"
MarketType struct {
ID int32
// event.participants = ["Dinamo", "Shakhtar"]
// market_params.team = 1
// Ex.: "{%participant} Total" -> "Dinamo Total"
NameTemplate string
OutcomeTypes []*OutcomeType
}
// OutcomeType contains template for the Outcome name
OutcomeType struct {
ID int32
// event.participants = ["Dinamo", "Shakhtar"]
// market_params.handicap = 1.5
// Ex.: "{$participant2} ({-$handicap})" -> "Shakhtar (-1.5)"
NameTemplate string
}
// EventPoints represents live game resulting or final game statistics
EventPoints struct {
PointGroups []*PointsGroup
}
// PointsGroup represents statistics unit of specific type for specific game period
PointsGroup struct {
PointType PointType
GroupPeriodID int32
State []*StateEntry
}
// PointType represents type of statistics unit: Score, Red cards, Corners, etc.
PointType int32
// StateEntry represents statistics entry for specific participant
StateEntry struct {
ParticipantIndex int32
Value int32
}
)
const (
// PointTypeUnknown shall not be used as type of points, if x-feed sends this, it means something went wrong
PointTypeUnknown PointType = 0
// PointTypeScore represents game score
PointTypeScore PointType = 1
// PointTypeRedСards represents Red Cards
PointTypeRedСards PointType = 2
// PointTypeYellowСards represents yellow cards
PointTypeYellowСards PointType = 3
// PointTypePenalties represents penalties
PointTypePenalties PointType = 4
// PointTypeCorners represents corners
PointTypeCorners PointType = 5
)
type (
// Action defines operation which shall be done on entity: InsertAction, UpdateAction, DeleteAction
Action int32
// EventEnvelope represents state update of specific Event
EventEnvelope struct {
EventDiff *Event
GeneratedAt *time.Time
Action Action
}
// Event represents Sport event (game) within specific sport/category/league
Event struct {
ID string
SportID int32
Category string
League string
Status EventStatus
Start *time.Time
Participants []string
Timer *EventTimer
Statistics *EventPoints
}
// EventStatus represents state of event, prematch or live
EventStatus int32
// EventTimer represents game timer. For some Sports time direction is counterclockwise (for example for basketball)
EventTimer struct {
Changed *time.Time
Time *time.Duration
State TimerState
}
// TimerState represents state of the timer: paused, forward, Backward
TimerState int32
)
const (
// UnknownAction action shall not be received from x-feed, in case of receiving this value
UnknownAction Action = 0
// InsertAction action indicates that brand new entity is received
InsertAction Action = 1
// DeleteAction action indicates that x-feed is not going to keep sending updates for specific entity
DeleteAction Action = 2
// UpdateAction action indicates that update for existent entity was received
UpdateAction Action = 3
)
const (
// EventStatusUnknown shall not be received
EventStatusUnknown EventStatus = 0
// EventStatusPrematch indicates that match is in prematch
EventStatusPrematch EventStatus = 1
// EventStatusLive indicates that match is in live
EventStatusLive EventStatus = 2
)
const (
// TimerStateUnknown shall not be received, it indicates that something is wrong on x-feed side
TimerStateUnknown TimerState = 0
// TimerStateForward indicates that timer need to increase it's value
TimerStateForward TimerState = 1
// TimerStateBackward indicates that timer need to decrease it's value
TimerStateBackward TimerState = 2
// TimerStatePause indicates that timer is paused
TimerStatePause TimerState = 3
)
// market DTOs
type (
// MarketEnvelope represents state update of specific Market
MarketEnvelope struct {
EventID string
MarketDiff *Market
GeneratedAt *time.Time
Action Action
}
// Market represents market instance
Market struct {
ID string
MarketTypeID int32
MarketParams []*MarketParam
Outcomes []*Outcome
}
// MarketParam is key value for specific market parameter (for parametrized markets)
MarketParam struct {
Type MarketParamType
Value string
}
// MarketParamType represents type of parameter of parametrised market
MarketParamType int32
// Outcome represents specific instance of outcome
Outcome struct {
ID string
Type int32
Value string
Suspended bool
}
)
const (
// MarketParamTypeUnknown shall not be used
MarketParamTypeUnknown MarketParamType = 0
// MarketParamTypePeriod represents market parameter which reflects period of the game
MarketParamTypePeriod MarketParamType = 1
// MarketParamTypeTotal represents market parameter which reflects total count
MarketParamTypeTotal MarketParamType = 2
// MarketParamTypeHandicap represents market parameter which reflects fora for teams
MarketParamTypeHandicap MarketParamType = 3
// MarketParamTypeTeam epresents market parameter which reflects competitor team order number
MarketParamTypeTeam MarketParamType = 4
)
type (
// EventSettlementEnvelope represents state update of specific EventSettlement
EventSettlementEnvelope struct {
EventSettlement *EventSettlement
GeneratedAt *time.Time
}
// EventSettlement contains settlements for Outcomes for specific event
EventSettlement struct {
EventID string
Resulting *EventPoints
Outcomes map[string]OutcomeSettlementStatus
}
// OutcomeSettlementStatus represents result status of outcome: unsettled, win, lose, return
OutcomeSettlementStatus int32
)
const (
// OutcomeSettlementUnknown shall not be used
OutcomeSettlementUnknown OutcomeSettlementStatus = 0
OutcomeSettlementUnsettled OutcomeSettlementStatus = 1
OutcomeSettlementWin OutcomeSettlementStatus = 2
OutcomeSettlementLose OutcomeSettlementStatus = 3
OutcomeSettlementReturn OutcomeSettlementStatus = 4
)
func newSportDescription(sportDescription *pb.SportDescription, language string) *SportDescription {
result := &SportDescription{
ID: sportDescription.GetSportId(),
Name: sportDescription.GetSportName(),
Language: language,
}
result.Periods = make([]*Period, 0, len(sportDescription.GetPeriods()))
for _, period := range sportDescription.GetPeriods() {
if period == nil {
continue
}
result.Periods = append(result.Periods, &Period{
ID: period.GetPeriodId(),
Name: period.GetPeriodName(),
})
}
result.MarketTypes = make([]*MarketType, 0, len(sportDescription.GetMarketTypes()))
for _, marketType := range sportDescription.GetMarketTypes() {
if marketType == nil {
continue
}
mt := &MarketType{
ID: marketType.GetMarketTypeId(),
NameTemplate: marketType.GetMarketNameTemplate(),
}
mt.OutcomeTypes = make([]*OutcomeType, 0, len(marketType.GetOutcomeTypes()))
for _, outcomeType := range marketType.GetOutcomeTypes() {
if outcomeType == nil {
continue
}
mt.OutcomeTypes = append(mt.OutcomeTypes, &OutcomeType{
ID: outcomeType.GetOutcomeTypeId(),
NameTemplate: outcomeType.GetOutcomeNameTemplate(),
})
}
result.MarketTypes = append(result.MarketTypes, mt)
}
return result
}
func newEvent(feedEvent *pb.FeedEvent) (*Event, error) {
var err error
var startTs time.Time
if feedEvent.GetStartTs() != nil {
var startTsValidationError error
startTs, startTsValidationError = ptypes.Timestamp(feedEvent.GetStartTs())
if startTsValidationError != nil {
err = errors.Wrap(startTsValidationError, "can't parse Event StartTs")
}
}
var timer *EventTimer
if feedEvent.GetTimer() != nil {
var timerParseError error
timer, timerParseError = newEventTimer(feedEvent.GetTimer())
if timerParseError != nil {
err = errors.Wrap(timerParseError, "can't parse Event Timer")
}
}
var start *time.Time
if !startTs.IsZero() {
start = &startTs
}
return &Event{
ID: feedEvent.GetEventId(),
SportID: feedEvent.GetSportId(),
Category: feedEvent.GetCategory(),
League: feedEvent.GetLeague(),
Status: newEventStatus(feedEvent.GetStatus()),
Start: start,
Participants: feedEvent.GetParticipants(),
Timer: timer,
}, err
}
func newEventStatus(eventStatus pb.FeedEvent_EventStatus) EventStatus {
switch eventStatus {
case pb.FeedEvent_LIVE:
return EventStatusLive
case pb.FeedEvent_PREMATCH:
return EventStatusPrematch
default:
return EventStatusUnknown
}
}
func newEventTimer(eventTimer *pb.EventTimer) (*EventTimer, error) {
changedTs, err := ptypes.Timestamp(eventTimer.GetChangedTs())
if err != nil {
return nil, errors.Wrap(err, "can't parse EventTimer ChangedTs")
}
eventTime, err := ptypes.Duration(eventTimer.GetTime())
if err != nil {
return nil, errors.Wrap(err, "can't parse EventTimer Time")
}
state := newTimerState(eventTimer.GetState())
return &EventTimer{
Changed: &changedTs,
Time: &eventTime,
State: state,
}, nil
}
func newTimerState(timerState pb.EventTimer_TimerState) TimerState {
switch timerState {
case pb.EventTimer_FORWARD:
return TimerStateForward
case pb.EventTimer_BACKWARD:
return TimerStateBackward
case pb.EventTimer_PAUSE:
return TimerStatePause
default:
return TimerStateUnknown
}
}
func newMarket(feedMarket *pb.FeedMarket) *Market {
market := &Market{
ID: feedMarket.GetMarketId(),
MarketTypeID: feedMarket.GetMarketType(),
}
market.MarketParams = make([]*MarketParam, 0, len(feedMarket.GetMarketParams()))
for _, marketParam := range feedMarket.GetMarketParams() {
if marketParam == nil {
continue
}
market.MarketParams = append(market.MarketParams, &MarketParam{
Type: newMarketParamType(marketParam.GetType()),
Value: marketParam.GetValue(),
})
}
market.Outcomes = make([]*Outcome, 0, len(feedMarket.GetOutcomes()))
for _, outcome := range feedMarket.GetOutcomes() {
if outcome == nil {
continue
}
market.Outcomes = append(market.Outcomes, &Outcome{
ID: outcome.GetOutcomeId(),
Type: outcome.GetOutcomeType(),
Value: outcome.GetValue(),
Suspended: outcome.GetSuspended(),
})
}
return market
}
func newMarketParamType(marketParamType pb.FeedMarketParam_MarketParamType) MarketParamType {
switch marketParamType {
case pb.FeedMarketParam_TEAM:
return MarketParamTypeTeam
case pb.FeedMarketParam_HANDICAP:
return MarketParamTypeHandicap
case pb.FeedMarketParam_TOTAL:
return MarketParamTypeTotal
case pb.FeedMarketParam_PERIOD:
return MarketParamTypePeriod
default:
return MarketParamTypeUnknown
}
}
func newEventSettlement(settlement *pb.EventSettlement) *EventSettlement {
eventSettlement := &EventSettlement{
EventID: settlement.GetEventId(),
Resulting: newEventPoints(settlement.GetResulting()),
Outcomes: make(map[string]OutcomeSettlementStatus),
}
for outcomeID, settlementStatus := range settlement.GetOutcomes() {
if settlementStatus == nil {
eventSettlement.Outcomes[outcomeID] = OutcomeSettlementUnknown
continue
}
status := settlementStatus.GetSettlement()
eventSettlement.Outcomes[outcomeID] = newOutcomeSettlementStatus(status)
}
return eventSettlement
}
func newEventPoints(eventPoints *pb.EventPoints) *EventPoints {
if eventPoints == nil {
return nil
}
points := &EventPoints{
PointGroups: make([]*PointsGroup, 0, len(eventPoints.GetPointGroups())),
}
for _, pointGroup := range eventPoints.GetPointGroups() {
var periodID int32
var states []*StateEntry
if pointGroup.GetGroupParams() != nil {
periodID = pointGroup.GetGroupParams().Period
}
for _, state := range pointGroup.GetState() {
if state == nil {
continue
}
s := &StateEntry{
Value: state.GetValue(),
}
if state.GetStateParams() != nil {
s.ParticipantIndex = state.GetStateParams().GetParticipant()
}
states = append(states, s)
}
points.PointGroups = append(points.PointGroups, &PointsGroup{
PointType: newPointType(pointGroup.GetPointType()),
GroupPeriodID: periodID,
State: states,
})
}
return points
}
func newPointType(pointType pb.PointsGroup_PointType) PointType {
switch pointType {
case pb.PointsGroup_SCORE:
return PointTypeScore
case pb.PointsGroup_RED_CARDS:
return PointTypeRedСards
case pb.PointsGroup_YELLOW_CARDS:
return PointTypeYellowСards
case pb.PointsGroup_PENALTIES:
return PointTypePenalties
case pb.PointsGroup_CORNERS:
return PointTypeCorners
default:
return PointTypeUnknown
}
}
func newOutcomeSettlementStatus(status pb.OutcomeSettlement_SettlementType) OutcomeSettlementStatus {
switch status {
case pb.OutcomeSettlement_RETURN:
return OutcomeSettlementReturn
case pb.OutcomeSettlement_LOSE:
return OutcomeSettlementLose
case pb.OutcomeSettlement_WIN:
return OutcomeSettlementWin
case pb.OutcomeSettlement_UNSETTLED:
return OutcomeSettlementUnsettled
default:
return OutcomeSettlementUnknown
}
}
func newFeedAction(action pb.DiffType) Action {
switch action {
case pb.DiffType_UPDATE:
return UpdateAction
case pb.DiffType_DELETE:
return DeleteAction
case pb.DiffType_INSERT:
return InsertAction
default:
return UnknownAction
}
}