-
Notifications
You must be signed in to change notification settings - Fork 0
/
matches.py
251 lines (233 loc) · 12 KB
/
matches.py
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
import sqlite3
import os
from teams import Teams, Team
def getTeamRole(match):
ferried = match.stonesDeliveredTele
stacking = match.stonesPlaced
role = ""
if (ferried == 0) and (stacking > 0):
role = "Stacking"
elif (ferried > 0) and (stacking == 0):
role = "Ferrying"
elif (ferried == 0) and (stacking == 0):
role = "None"
elif abs(ferried - stacking) <= 1:
role = "Both"
elif abs(ferried - stacking) >= 3:
if ferried >= stacking:
role = "Ferrying"
else:
role = "Stacking"
else:
role = f"Inconclusive"
return role
class Match():
def __init__(self, teamNum, matchNum, alliance, skystoneBonus, stonesDelivered, autoStonesPlaced, waffle, autoPark, stonesDeliveredTele, stonesPlaced, height, repositioning, capstone, parking, notes, penalties, broken, matchId, submitedByNum, knocked):
self.teamNum = teamNum
self.matchNum = matchNum
self.alliance = alliance
self.skystoneBonus = skystoneBonus
self.stonesDelivered = stonesDelivered
self.autoStonesPlaced = autoStonesPlaced
self.waffle = True if (waffle=="true") else False
self.autoPark = True if (autoPark=="true") else False
self.stonesDeliveredTele = stonesDeliveredTele
self.stonesPlaced = stonesPlaced
self.height = height
self.repositioning = True if (repositioning=="true") else False
self.capstone = capstone
self.parking = True if (parking=="true") else False
self.notes = notes
self.penalties = True if (penalties=="true") else False
self.broken = True if (broken=="true") else False
self.matchId = matchId
self.submitedByNum = submitedByNum
self.autoScore = (8 * skystoneBonus) + (2 * stonesDelivered) + (4 * autoStonesPlaced) + (10 if self.waffle else 0) + (5 if self.autoPark else 0)
self.teleOpScore = (stonesDeliveredTele) + (stonesPlaced) + (2 * height)
self.endGameScore = (10 if self.repositioning else 0) + ((capstone) + 5 if capstone != -1 else 0) + (5 if self.parking else 0)
self.score = self.autoScore + self.teleOpScore + self.endGameScore
self.blnAuto = True if self.autoScore > 0 else False
self.role = getTeamRole(self)
self.knocked = True if knocked == "true" else False
class Matches():
def __init__(self):
self.teams = Teams()
def createTable(self, dbConnection):
dbConnection.execute("""
CREATE TABLE Matches(
id INTEGER PRIMARY KEY,
teamid INTEGER NOT NULL,
matchNum INTEGER NOT NULL,
alliance BOOLEAN NOT NULL,
skystoneBonus INTEGER NOT NULL,
stonesDelivered INTEGER NOT NULL,
autoStonesPlaced INTEGER NOT NULL,
waffle BOOLEAN NOT NULL,
autoPark BOOLEAN NOT NULL,
stonesDeliveredTele INTEGER NOT NULL,
stonesPlaced INTEGER NOT NULL,
height INTEGER NOT NULL,
repositioning BOOLEAN NOT NULL,
capstone INTEGER NOT NULL,
parking BOOLEAN NOT NULL,
knocked BOOLEAN NOT NULL,
notes TEXT,
penalties BOOLEAN NOT NULL,
broken BOOLEAN NOT NULL,
submitedByNum INTEGER NOT NULL
)""")
def addMatch(self, dbConnection, teamid, matchNum, redAlliance, skystoneBonus, stonesDelivered, autoStonesPlaced, waffle, autoPark, stonesDeliveredTele, stonesPlaced, height, repositioning, capstone, parking, notes, knocked, penalties, broken, submitedByNum):
dbConnection.execute("""
INSERT INTO Matches (teamid, matchNum, alliance, skystoneBonus, stonesDelivered, autoStonesPlaced, waffle, autoPark, stonesDeliveredTele, stonesPlaced, height, repositioning, capstone, parking, notes, penalties, broken, submitedByNum, knocked) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
""", (teamid, matchNum, redAlliance, skystoneBonus, stonesDelivered, autoStonesPlaced, waffle, autoPark, stonesDeliveredTele, stonesPlaced, height, repositioning, capstone, parking, notes, penalties, broken, submitedByNum, knocked))
def getMatches(self, dbConnection, text, params=""):
matchList = []
if params:
for row in dbConnection.execute(text, params):
matchList.append(Match(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8],row[9],row[10],row[11],row[12],row[13], row[14], row[15], row[16], row[17], row[18], row[19]))
else:
for row in dbConnection.execute(text):
matchList.append(Match(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8],row[9],row[10],row[11],row[12],row[13], row[14], row[15], row[16], row[17], row[18], row[19]))
return matchList
def getAllMatches(self, dbConnection):
text = """
SELECT Teams.number, matchNum, alliance, skystoneBonus, stonesDelivered, autoStonesPlaced, waffle,
autoPark, stonesDeliveredTele, stonesPlaced, height, repositioning, capstone, parking, notes, penalties, broken, Matches.id, submitedByNum, knocked
FROM Matches INNER JOIN Teams ON Matches.teamid = Teams.id ORDER BY Teams.number"""
return self.getMatches(dbConnection, text)
def getSelectedMatches(self, dbConnection, where, orderBy):
matchList = []
text = f"""
SELECT Teams.number, matchNum, alliance, skystoneBonus,
stonesDelivered, autoStonesPlaced, waffle, autoPark, stonesDeliveredTele, stonesPlaced, height, repositioning, capstone, parking, notes, penalties, broken, Matches.id, submitedByNum, knocked
FROM Matches INNER JOIN Teams ON Matches.teamid = Teams.id WHERE {where} ORDER BY {orderBy}"""
return self.getMatches(dbConnection, text)
def isTeamBroken(self, dbConnection, teamid):
broken = ""
findBrokenText = f"SELECT broken FROM matches WHERE teamid={teamid} ORDER BY matchNum"
for row in dbConnection.execute(findBrokenText):
broken = row[0]
return (True if broken == "true" else False) if (broken) else False
def hasTeamBeenPenalized(self, dbConnection, teamid):
penatlies = False
findPenaltyText = f"SELECT penalties FROM matches WHERE teamid={teamid} ORDER BY matchNum"
for row in dbConnection.execute(findPenaltyText):
if row[0] == "true":
penatlies = True
return penatlies
def getTeamInfo(self, dbConnection, teamid):
infoDict = {
"avgAuto":0,
"avgTele":0,
"avgEnd":0,
"percentBroken":0,
"matchScores":[],
"avgTeleBlocks":0,
"avgBlocksDeliverdFerryMatches":0,
"avgHeight":0,
"avgHeightStackingMatches":0,
"avgPlaced":0,
"avgPlacedStackingMatches":0,
"wafflePercent":0,
"avgSkystoneBonus":0,
"avgStonesDelivered":0,
"avgStonesPlaced":0,
"autoParkedPercent":0,
"parkedPercent":0,
"repositioningPercent":0,
"avgCapstone":0,
"knockedPercent":0,
"avgBlocksDelBoth":0,
"avgBlocksPlaceBoth":0,
"avgHeightBoth":0,
"roles":[]
}
autoTot = 0
teleTot = 0
endTot = 0
brokenTot = 0
teleBlockFerrying = 0
teleHeightStacking = 0
telePlacedStacking = 0
totalMatches = 0
TeleBlocks = 0
height = 0
placed = 0
wafflePercent = 0
skystoneBonus = 0
stonesDelivered = 0
stonesPlaced = 0
autoParkedPercent = 0
parkedPercent = 0
repositioningPercent = 0
capstone = 0
knockedPercent = 0
teleBlockBoth = 0
teleHeightBoth = 0
telePlacedBoth = 0
matchScores = []
roles = []
matchNums = []
matchList = self.getSelectedMatches(dbConnection, f"teamid={teamid}", "matchNum")
for match in matchList:
if match.matchNum not in matchNums:
totalMatches += 1
autoTot += match.autoScore
teleTot += match.teleOpScore
endTot += match.endGameScore
matchScores.append(match.autoScore + match.teleOpScore + match.endGameScore)
brokenTot += 1 if match.broken else 0
TeleBlocks += match.stonesDeliveredTele
height += match.height
placed += match.stonesPlaced
wafflePercent += match.waffle
skystoneBonus += match.skystoneBonus
stonesDelivered += match.stonesDelivered
stonesPlaced += match.autoStonesPlaced
autoParkedPercent += match.autoPark
parkedPercent += match.parking
repositioningPercent += match.repositioning
capstone += match.capstone
knockedPercent += match.knocked
roles.append(match.role)
teleBlockFerrying += match.stonesDeliveredTele if match.role == "Ferrying" else 0
teleHeightStacking += match.height if match.role == "Stacking" else 0
telePlacedStacking += match.stonesPlaced if match.role == "Stacking" else 0
teleBlockBoth += match.stonesDeliveredTele if match.role == "Ferrying" else 0
teleHeightBoth += match.height if match.role == "Stacking" else 0
telePlacedBoth += match.stonesPlaced if match.role == "Stacking" else 0
matchNums.append(match.matchNum)
if totalMatches > 0:
infoDict["avgAuto"] = round(autoTot / totalMatches, 1)
infoDict["avgTele"] = round(teleTot / totalMatches, 1)
infoDict["avgEnd"] = round(endTot / totalMatches, 1)
infoDict["percentBroken"] = round(brokenTot / totalMatches, 1)
infoDict["matchScores"] = matchScores
infoDict["avgTeleBlocks"] = round(TeleBlocks / totalMatches, 1)
infoDict["avgHeight"] = round(height / totalMatches, 1)
infoDict["avgPlaced"] = round(placed / totalMatches, 1)
infoDict["wafflePercent"] = round(wafflePercent / totalMatches, 1)
infoDict["avgSkystoneBonus"] = round(skystoneBonus / totalMatches, 1)
infoDict["avgStonesDelivered"] = round(stonesDelivered / totalMatches, 1)
infoDict["avgStonesPlaced"] = round(stonesPlaced / totalMatches, 1)
infoDict["autoParkedPercent"] = round(autoParkedPercent / totalMatches, 1)
infoDict["parkedPercent"] = round(parkedPercent / totalMatches, 1)
infoDict["repositioningPercent"] = round(repositioningPercent / totalMatches, 1)
infoDict["avgCapstone"] = round(capstone / totalMatches, 1)
infoDict["knockedPercent"] = round(knockedPercent / totalMatches, 1)
infoDict["avgBlocksDeliverdFerryMatches"] = round(teleBlockFerrying / roles.count("Ferrying"), 1) if roles.count("Ferrying") > 0 else 0
infoDict["avgHeightStackingMatches"] = round(teleHeightStacking / roles.count("Stacking"), 1) if roles.count("Stacking") > 0 else 0
infoDict["avgPlacedStackingMatches"] = round(telePlacedStacking / roles.count("Stacking"),1) if roles.count("Stacking") > 0 else 0
infoDict["avgBlocksDelBoth"] = round(teleBlockBoth / roles.count("Both"),1) if roles.count("Both") > 0 else 0
infoDict["avgBlocksPlaceBoth"] = round(telePlacedBoth / roles.count("Both"),1) if roles.count("Both") > 0 else 0
infoDict["avgHeightBoth"] = round(teleHeightBoth / roles.count("Both"),1) if roles.count("Both") > 0 else 0
return infoDict
if __name__ == "__main__":
DB_STRING = os.path.join(os.path.dirname(__file__), 'data/testdatabase.sqlite3')
matches = Matches()
with sqlite3.connect(DB_STRING) as connection:
matches.createTable(connection)
matches.addMatch(connection, 1,1,1,0,0,0,0,0,0,0,0,"",0,0)
matchList = matches.getAllMatches(connection)
for mathch in matchList:
print(mathch)