@@ -17,79 +17,70 @@ func (c *GameController) ProcessAutoRefRequests(id string, request refproto.Auto
1717 log .Printf ("Received request from autoRef '%v': %v" , id , request )
1818
1919 details := GameEventDetailsFromProto (* request .GameEvent )
20- event := Event { GameEvent : & GameEvent {Type : details .EventType (), Details : details } }
20+ gameEvent := GameEvent {Type : details .EventType (), Details : details }
2121
22- c .Engine .applyGameEventFilters (event . GameEvent )
22+ c .Engine .applyGameEventFilters (& gameEvent )
2323
2424 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 }
3127
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 )
3231 if c .Engine .isNewProposal (& proposal ) {
3332 // the autoRef has not submitted the same event recently, so add it to the proposals - waiting for majority
3433 c .Engine .GcState .GameEventProposals = append (c .Engine .GcState .GameEventProposals , & proposal )
3534 }
3635
37- numProposals := c .Engine .numberOfMatchingProposals (event .GameEvent )
36+ matchingProposals := c .Engine .collectAllMatchingProposals (& gameEvent )
37+ numProposals := len (matchingProposals )
3838 majority := int (math .Floor (float64 (len (c .AutoRefServer .Clients )) / 2.0 ))
3939 if numProposals > majority {
4040 // 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 )
4347 }
44-
4548 } 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 })
5052 }
5153
5254 return nil
5355}
5456
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"
6160 }
62- return
61+ return "new"
6362}
6463
6564func (e * Engine ) collectAllMatchingProposals (event * GameEvent ) []* GameEventProposal {
6665 var proposals []* GameEventProposal
6766 for _ , proposal := range e .GcState .GameEventProposals {
68- if proposal .GameEvent .Type == event .Type {
67+ if proposal .GameEvent .Type == event .Type && e . proposalValid ( proposal ) {
6968 proposals = append (proposals , proposal )
7069 }
7170 }
7271 return proposals
7372}
7473
75- func (e * Engine ) collectNonMatchingProposals (event * GameEvent ) []* GameEventProposal {
74+ func (e * Engine ) collectNonMatchingProposals (eventType GameEventType ) []* GameEventProposal {
7675 var proposals []* GameEventProposal
7776 for _ , proposal := range e .GcState .GameEventProposals {
78- if proposal .GameEvent .Type != event . Type {
77+ if proposal .GameEvent .Type != eventType {
7978 proposals = append (proposals , proposal )
8079 }
8180 }
8281 return proposals
8382}
8483
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-
9384func (e * Engine ) proposalValid (proposal * GameEventProposal ) bool {
9485 return proposal .ValidUntil .After (e .TimeProvider ())
9586}
@@ -105,6 +96,6 @@ func (e *Engine) isNewProposal(newProposal *GameEventProposal) bool {
10596 return true
10697}
10798
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
110101}
0 commit comments