Skip to content

Commit fbf4f50

Browse files
Moves single game calculations out to separate method
Signed-off-by: Matt Bernhardt <[email protected]>
1 parent 4679298 commit fbf4f50

File tree

1 file changed

+71
-39
lines changed

1 file changed

+71
-39
lines changed

trapp/compile_teammate.py

Lines changed: 71 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import time
44
from trapp.combo import Combo
55
from trapp.compiler import Compiler
6+
from trapp.game import Game
67
from trapp.gameminute import GameMinute
78
from trapp.season import Season
89

@@ -23,6 +24,53 @@ def assembleCombos(self, players):
2324

2425
return combos
2526

27+
def calculateGame(self, p1, p2, duration):
28+
# This takes a dictionary of entrance and exit times, and parses
29+
# playing time into one of four buckets: one, two, both, neither
30+
31+
self.log.message('Comparing ' + str(p1) + ' with ' + str(p2))
32+
33+
record = {}
34+
record['one'] = 0
35+
record['two'] = 0
36+
record['both'] = 0
37+
record['neither'] = 0
38+
39+
# First we determine if the players overlapped
40+
if (p1['off'] > p2['on'] and p2['off'] > p1['on']):
41+
self.log.message('Overlap Yes')
42+
43+
# Who entered first?
44+
if (p1['on'] < p2['on']):
45+
record['one'] = p2['on'] - p1['on']
46+
elif (p1['on'] > p2['on']):
47+
record['two'] = p1['on'] - p2['on']
48+
# Who left first?
49+
if (p1['off'] < p2['off']):
50+
record['two'] += p2['off'] - p1['off']
51+
elif (p1['off'] > p2['off']):
52+
record['one'] += p1['off'] - p2['off']
53+
54+
record['both'] = min(p1['off'],p2['off']) - max(p1['on'],p2['on'])
55+
record['neither'] = min(p1['on'],p2['on']) + (duration - max(p1['off'],p2['off']))
56+
57+
else:
58+
self.log.message('Overlap No')
59+
record['one'] = p1['off'] - p1['on']
60+
record['two'] = p2['off'] - p2['on']
61+
record['both'] = 0
62+
record['neither'] = duration - (record['one'] + record['two'])
63+
64+
# Debug
65+
self.log.message(str(record))
66+
if (duration == record['one'] + record['two'] + record['both'] + record['neither']):
67+
self.log.message('Consistent')
68+
else:
69+
self.log.message('Consistency check failed!')
70+
self.log.message(str(record['one'] + record['two'] + record['both'] + record['neither']))
71+
72+
return record
73+
2674
def calculateTeammates(self, combo, season, games):
2775
# This calculates teammate statistics for a given combo in a given
2876
# season
@@ -43,23 +91,23 @@ def calculateTeammates(self, combo, season, games):
4391

4492
self.log.message('Comparing game records')
4593

46-
gm = GameMinute()
47-
gm.connectDB()
48-
49-
# local initialization
50-
thisOne = 0
51-
thisTwo = 0
52-
thisBoth = 0
53-
thisNeither = 0
94+
# Lookup game duration
95+
g = Game()
96+
g.connectDB()
97+
duration = g.lookupDuration(game[0], self.log)
98+
g.disconnectDB()
5499

55100
# Retrieve appearance data for this game for these players
101+
gm = GameMinute()
102+
gm.connectDB()
56103
needle = {}
57104
needle['GameID'] = game[0]
58105
needle['TeamID'] = season['TeamID']
59106
needle['PlayerID'] = combo['player1']
60107
p1 = gm.loadRecord(needle, self.log)
61108
needle['PlayerID'] = combo['player2']
62109
p2 = gm.loadRecord(needle, self.log)
110+
gm.disconnectDB()
63111

64112
self.log.message('Appearances:')
65113
self.log.message(str(p1))
@@ -71,53 +119,37 @@ def calculateTeammates(self, combo, season, games):
71119
p1 = p1[0]
72120
else:
73121
p1 = (0, 0, 0)
122+
player1 = {}
123+
player1['on'] = p1[0]
124+
player1['off'] = p1[1]
125+
74126
if (len(p2) == 1):
75127
p2 = p2[0]
76128
else:
77129
p2 = (0, 0, 0)
130+
player2 = {}
131+
player2['on'] = p2[0]
132+
player2['off'] = p2[1]
78133

79134
# Break down minutes played into one of four categories:
80135
# one - only p1 on field
81136
# two - only p2 on field
82137
# both - both p1 and p2 on field
83138
# neither - neither p1 nor p2 on field
84-
85-
self.log.message('Comparing ' + str(p1[0]) + ' with ' + str(p2[0]))
86-
if (p1[0] == p2[0]):
87-
# on together
88-
self.log.message('On together')
89-
thisNeither = p1[0]
90-
elif (p1[0] < p2[0]):
91-
# p1 on first
92-
self.log.message('p1 on first')
93-
thisNeither = p1[0]
94-
elif (p1[0] > p2[0]):
95-
# p2 on first
96-
self.log.message('p2 on first')
97-
thisNeither = p2[0]
98-
else:
99-
# error
100-
raise RuntimeError('Error comparing timeOn values')
101-
102-
self.log.message(
103-
str(thisOne) + ' _ ' +
104-
str(thisTwo) + ' _ ' +
105-
str(thisBoth) + ' _ ' +
106-
str(thisNeither)
107-
)
139+
pairing = self.calculateGame(player1, player2, duration)
108140

109141
# add this game's counts to totals
110-
one += thisOne
111-
two += thisTwo
112-
both += thisBoth
113-
neither += thisNeither
142+
one += pairing['one']
143+
two += pairing['two']
144+
both += pairing['both']
145+
neither += pairing['neither']
114146

115147
self.log.message('')
116148

117149
self.log.message(
118-
str(one) + ' _ ' +
119-
str(two) + ' _ ' +
120-
str(both) + ' _ ' +
150+
str(one) + ' _ ' +
151+
str(two) + ' _ ' +
152+
str(both) + ' _ ' +
121153
str(neither)
122154
)
123155

0 commit comments

Comments
 (0)