@@ -17,79 +17,70 @@ func (c *GameController) ProcessAutoRefRequests(id string, request refproto.Auto
17
17
log .Printf ("Received request from autoRef '%v': %v" , id , request )
18
18
19
19
details := GameEventDetailsFromProto (* request .GameEvent )
20
- event := Event { GameEvent : & GameEvent {Type : details .EventType (), Details : details } }
20
+ gameEvent := GameEvent {Type : details .EventType (), Details : details }
21
21
22
- c .Engine .applyGameEventFilters (event . GameEvent )
22
+ c .Engine .applyGameEventFilters (& gameEvent )
23
23
24
24
validUntil := c .Engine .TimeProvider ().Add (c .Config .Game .AutoRefProposalTimeout )
25
- proposal := GameEventProposal {GameEvent : * event .GameEvent , ProposerId : id , ValidUntil : validUntil }
26
-
27
- if matchingEvent := c .Engine .State .FindMatchingRecentGameEvent (event .GameEvent ); matchingEvent != nil {
28
- // there was already such a game event recently. Just add the proposer to the existing event.
29
- matchingEvent .Origins = append (matchingEvent .Origins , id )
30
- } else if c .Engine .applyMajority (& event ) { // look for a majority
25
+ proposal := GameEventProposal {GameEvent : gameEvent , ProposerId : id , ValidUntil : validUntil }
26
+ proposal .GameEvent .Origins = []string {id }
31
27
28
+ recentGameEvent := c .Engine .State .IsRecentGameEvent (& gameEvent )
29
+ if ! recentGameEvent && c .Engine .applyMajority (& gameEvent ) {
30
+ log .Printf ("Apply majority logic to autoRef proposal %v" , proposal )
32
31
if c .Engine .isNewProposal (& proposal ) {
33
32
// the autoRef has not submitted the same event recently, so add it to the proposals - waiting for majority
34
33
c .Engine .GcState .GameEventProposals = append (c .Engine .GcState .GameEventProposals , & proposal )
35
34
}
36
35
37
- numProposals := c .Engine .numberOfMatchingProposals (event .GameEvent )
36
+ matchingProposals := c .Engine .collectAllMatchingProposals (& gameEvent )
37
+ numProposals := len (matchingProposals )
38
38
majority := int (math .Floor (float64 (len (c .AutoRefServer .Clients )) / 2.0 ))
39
39
if numProposals > majority {
40
40
// there is a majority for a game event that has not yet been submitted to the GC
41
- // remove the matching proposals and submit the event to the GC
42
- c .OnNewEvent (event )
41
+ // remove the matching proposals and submit the events to the GC
42
+ log .Printf ("Majority (%v > %v) for %v" , numProposals , majority , gameEvent )
43
+ for _ , proposal := range matchingProposals {
44
+ c .OnNewEvent (Event {GameEvent : & proposal .GameEvent })
45
+ }
46
+ c .Engine .GcState .GameEventProposals = c .Engine .collectNonMatchingProposals (gameEvent .Type )
43
47
}
44
-
45
48
} else {
46
- // game event has not been submitted recently (by any autoRef) and no majority required.
47
- // Just submit this event
48
- event .GameEvent .Origins = []string {id }
49
- c .OnNewEvent (event )
49
+ // no majority required or event submitted recently. Submit event to engine
50
+ log .Printf ("Submit %v game event proposal to engine: %v" , recentGameEventString (recentGameEvent ), gameEvent )
51
+ c .OnNewEvent (Event {GameEvent : & gameEvent })
50
52
}
51
53
52
54
return nil
53
55
}
54
56
55
- func (e * Engine ) numberOfMatchingProposals (event * GameEvent ) (count int ) {
56
- count = 0
57
- for _ , proposal := range e .GcState .GameEventProposals {
58
- if proposal .GameEvent .Type == event .Type && e .proposalValid (proposal ) {
59
- count ++
60
- }
57
+ func recentGameEventString (isRecent bool ) string {
58
+ if isRecent {
59
+ return "recent"
61
60
}
62
- return
61
+ return "new"
63
62
}
64
63
65
64
func (e * Engine ) collectAllMatchingProposals (event * GameEvent ) []* GameEventProposal {
66
65
var proposals []* GameEventProposal
67
66
for _ , proposal := range e .GcState .GameEventProposals {
68
- if proposal .GameEvent .Type == event .Type {
67
+ if proposal .GameEvent .Type == event .Type && e . proposalValid ( proposal ) {
69
68
proposals = append (proposals , proposal )
70
69
}
71
70
}
72
71
return proposals
73
72
}
74
73
75
- func (e * Engine ) collectNonMatchingProposals (event * GameEvent ) []* GameEventProposal {
74
+ func (e * Engine ) collectNonMatchingProposals (eventType GameEventType ) []* GameEventProposal {
76
75
var proposals []* GameEventProposal
77
76
for _ , proposal := range e .GcState .GameEventProposals {
78
- if proposal .GameEvent .Type != event . Type {
77
+ if proposal .GameEvent .Type != eventType {
79
78
proposals = append (proposals , proposal )
80
79
}
81
80
}
82
81
return proposals
83
82
}
84
83
85
- func collectAllOrigins (proposals []* GameEventProposal ) []string {
86
- var origins []string
87
- for _ , proposal := range proposals {
88
- origins = append (origins , proposal .ProposerId )
89
- }
90
- return origins
91
- }
92
-
93
84
func (e * Engine ) proposalValid (proposal * GameEventProposal ) bool {
94
85
return proposal .ValidUntil .After (e .TimeProvider ())
95
86
}
@@ -105,6 +96,6 @@ func (e *Engine) isNewProposal(newProposal *GameEventProposal) bool {
105
96
return true
106
97
}
107
98
108
- func (e * Engine ) applyMajority (event * Event ) bool {
109
- return e .GcState .GameEventBehavior [event .GameEvent . Type ] == GameEventBehaviorMajority
99
+ func (e * Engine ) applyMajority (event * GameEvent ) bool {
100
+ return e .GcState .GameEventBehavior [event .Type ] == GameEventBehaviorMajority
110
101
}
0 commit comments