-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·329 lines (287 loc) · 11.7 KB
/
main.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
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
import sys
import yaml
import json
import random
from pprint import pprint
from os import walk
from Tkinter import *
print "Running"
UNIQUE_TEAMS = 6
colors = ['red','green','blue','yellow','orange','magenta']
# Globals for graph data
winHeight = 0
winWidth = 0
#########################################
# State of graphs for saving/workspaces #
#########################################
class State:
def __init__(self):
self.graphCount = 1
self.teams = [0]*UNIQUE_TEAMS
self.graphs = ["Tele High Goal"]*self.graphCount
def addGraph(self):
if (self.graphCount < 4):
self.graphCount += 1
self.graphs.append("Tele High Goal")
def removeGraph(self, index=-1):
if (index == -1):
index = self.graphCount - 1
if (index > 0 and index < 4):
print "Popping"
self.graphCount -= 1
self.graphs.pop(index)
def loop(self):
for i in range(self.graphCount):
self.graphs[i] = graphDropdown[i].get()
######################
# Configuration file #
######################
def loadConfigFile():
try:
configData = yaml.load(open("config.yaml"))
matchCount = configData['matchCount']
for i in range(len(configData['graphs'])):
if configData['graphs'][i]['xTickCount'] == "matchCount":
configData['graphs'][i]['xTickCount'] = matchCount
masterGraphData[configData['graphs'][i]['name']] = configData['graphs'][i]
graphList.append( configData['graphs'][i]['name'] )
pprint(masterGraphData)
except:
print "Error: Invalid config file"
sys.exit(1)
##############
# Parse JSON #
##############
def getFiles(path):
return next(walk(path))[2]
def recurse(d, array, configData):
for k, v in d.iteritems():
if isinstance(v, dict):
array = recurse(v, array, configData)
else:
for i, data in enumerate(configData):
if k == data:
array[i] = int(v)
break
return array
def getMatchDataFromJson():
arr = []
# Load config
configData = open("jsonConfig.txt").read().split('\n')
jsonPath = "jsonFiles/"
files = getFiles(jsonPath)
files.sort()
# Loop through all of the json files and convert them into a nice csv
for f in files:
js = json.load(open(jsonPath+f))
arr.append(recurse(js, [0]*len(configData), configData))
pprint(arr)
return arr
######################
# Load the team data #
######################
def loadMatchData():
# try:
# Get the array from the JSONs
data = getMatchDataFromJson()
# Create all the teamList stuff
for i in data:
if i[0] not in teamList:
teamList.append(i[0])
print "Data:"
pprint(data)
# Create the teamList elements for matchData
for i in teamList:
teamMatches = {}
for j in graphList:
teamMatches[j] = {}
teamMatches[j]['solid'] = []
teamMatches[j]['dashed'] = []
matchData[i] = teamMatches
print "Team list: ",teamList
# Looping through data/graphs
for i in data:
currentTeam = i[0]
print i
for j in graphList:
currentGraph = matchData[currentTeam][j]
# Check if we are adding data to a graph that uses the match count
if ( masterGraphData[j]['usesMatch'] == 1 ):
# Solid line
currentGraph['solid'].append( [ len(currentGraph['solid'])+1 , i[masterGraphData[j]['solidCol']] ] )
# Dashed line?
if ( masterGraphData[j]['dashCol'] != -1 ):
currentGraph['dashed'].append( [ len(currentGraph['dashed'])+1 , i[masterGraphData[j]['dashCol']] ] )
# Otherwise, do the math for match data (need to fix this for future games, but it works for now)
else:
# Check if we need to make the empyt array for the graphData
if ( len(currentGraph['solid']) == 0 ):
for z in range(len(masterGraphData[j]['xCols'])):
currentGraph['solid'].append([ z+1, 0 ])
# currentGraph['solid'] = [0]*len(masterGraphData[j]['xCols'])
# Loop through xCols
for k in range(len(currentGraph['solid'])):
currentGraph['solid'][k][1] += i[masterGraphData[j]['xCols'][k]]
matchData[currentTeam][j] = currentGraph
# except:
# print "Error: Invalid match file"
# sys.exit(2)
def drawTeam(canvas, graphName, minX, minY, maxX, maxY, teamNumber, yTicks, xTicks, color):
# Offset each of the teams a little bit so that they don't overlap
yOffset = 0
if color == 'red':
yOffset = -5
elif color == 'green':
yOffset = -3
elif color == 'blue':
yOffset = -1
elif color == 'yellow':
yOffset = 1
elif color == 'orange':
yOffset = 3
elif color == 'magenta':
yOffset = 5
h_dist = (maxX-minX)/xTicks # Distance to move on X diff
v_dist = (maxY-minY)/yTicks # Distance to move on Y diff
# Loop through the solid lines
lastPoint = (minX,maxY+yOffset)
for i in matchData[teamNumber][graphName]['solid']:
pprint(matchData[teamNumber][graphName]['solid'])
newPoint = ( minX+i[0]*h_dist, maxY-i[1]*v_dist+yOffset )
if (lastPoint != (minX, maxY+yOffset)):
canvas.create_line(lastPoint, newPoint, fill=color, width=3)
lastPoint = newPoint
# Loop through the dashed lines
lastPoint = (minX,maxY+yOffset)
for i in matchData[teamNumber][graphName]['dashed']:
newPoint = ( minX+i[0]*h_dist, maxY-i[1]*v_dist+yOffset )
canvas.create_line(lastPoint, newPoint, fill=color, width=3, dash=(4,4))
lastPoint = newPoint
def drawGraph(canvas, state):
# Draw the bounding boxes
canvas.create_rectangle(0, 0, largeGraphDimensions[0], largeGraphDimensions[1], fill='black', outline='white')
if state.graphCount > 1:
canvas.create_rectangle(0, 0, largeGraphDimensions[0]/2, largeGraphDimensions[1], outline='white')
if state.graphCount > 2:
canvas.create_rectangle(0, largeGraphDimensions[1]/2, largeGraphDimensions[0], largeGraphDimensions[1], outline='white')
# Better draw method
xCount = 1
if (state.graphCount != 1):
xCount = 2
yCount = (state.graphCount+1)/2
for z in range(state.graphCount):
j = z%2
k = z/2
currentGraph = state.graphs[j+k*2]
print "Current Graph: ",currentGraph
# Added graph selectors
if (k == 0):
graphSelection[z].place(x = j*largeGraphDimensions[0]/2, y= k*largeGraphDimensions[1]/2)
else:
graphSelection[z].place(x = j*largeGraphDimensions[0]/2, y= (k+1)*largeGraphDimensions[1]/2-30)
minX = j *largeGraphDimensions[0]/xCount+50
maxX = (j+1)*largeGraphDimensions[0]/xCount-50
minY = k *largeGraphDimensions[1]/yCount+50
maxY = (k+1)*largeGraphDimensions[1]/yCount-75
# Vertical line
canvas.create_line(minX, minY, minX, maxY, fill='white')
# Horizontal line
canvas.create_line(minX, maxY, maxX, maxY, fill='white')
# Horizontal ticks
h_dist = (largeGraphDimensions[0]/xCount-100)/masterGraphData[currentGraph]['xTickCount']
for i in range(masterGraphData[currentGraph]['xTickCount']):
canvas.create_line( minX+h_dist*(i+1), maxY+10, minX+h_dist*(i+1), maxY, fill='white')
# Vertical ticks
v_dist = (largeGraphDimensions[1]/yCount-125)/(masterGraphData[currentGraph]['yTickCount']-1)
for i in range(masterGraphData[currentGraph]['yTickCount']):
canvas.create_line(j*largeGraphDimensions[0]/xCount+50,k*largeGraphDimensions[1]/yCount+v_dist*i+50,j*largeGraphDimensions[0]/xCount+40,k*largeGraphDimensions[1]/yCount+v_dist*i+50, fill='white')
# Once the axis are drawn, graph the team
#def drawTeam(canvas, graphName, minX, minY, maxX, maxY, teamNumber, yTicks, xTicks, color):
for i in range(len(teamVarsForDropdown)):
drawTeam(canvas, currentGraph, minX, minY, maxX, maxY, int(teamSelection[i].get("1.0",'end-1c')), masterGraphData[currentGraph]['yTickCount'], masterGraphData[currentGraph]['xTickCount'], colors[i])
def drawComments(canvas, state):
for i in range(UNIQUE_TEAMS):
canvas.create_rectangle(windowDimensions[0]-commentsDimensions[0], commentsDimensions[1]*i, windowDimensions[0], commentsDimensions[1]*(i+1), fill=colors[i], outline='white')
teamSelection[i].place(x=1000,y=commentsDimensions[1]*i+50)
# Update the teams in the state for graphing
state.teams[i] = int(teamSelection[i].get("1.0",'end-1c'))
print "Teams: ",state.teams
def key(event):
if event.char == event.keysym:
print "Normal key press: %s" % event.char
if event.char == 'w':
print "Increasing"
states[currentState].addGraph()
elif event.char == 's':
print "Decreasing"
states[currentState].removeGraph()
elif event.char == 'q':
print "Exit!"
sys.exit(0)
for s in states:
s.loop()
drawGraph(canvas, states[currentState])
drawComments(canvas, states[currentState])
matchCount = 6
matchData = {}
masterGraphData = {}
states = [State()]*9
currentState = 0
teamList = [0]
graphList = []
loadConfigFile()
loadMatchData()
print "TEAMLIST:",teamList
windowDimensions = (1024,660)
commentsDimensions = (224,windowDimensions[1]/6)
largeGraphDimensions = (800,windowDimensions[1])
root = Tk()
root.geometry("%dx%d" % windowDimensions)
root.title("Hazy Scout")
canvas = Canvas(root, width=windowDimensions[0], height=windowDimensions[1])
canvas.pack()
# Team number textEntry
teamVarsForDropdown = [StringVar(root) for var in colors]
teamSelection = [Text(root, width=5, height=1) for var in teamVarsForDropdown]
for i in teamSelection:
i.insert(END,"0")
# Graph Selector
graphDropdown = [StringVar(root) for var in range(4)]
for i in range(4):
graphDropdown[i].set(i+1)
graphSelection = [OptionMenu(root, var, *graphList) for var in graphDropdown]
for i in graphDropdown:
i.set("Showed Up")
drawGraph(canvas,states[currentState])
drawComments(canvas,states[currentState])
root.bind_all('<Key>',key)
root.bind_class('Text', '<Return>', lambda e: None)
root.bind_class('Text', 'a', lambda e: None)
root.bind_class('Text', 'b', lambda e: None)
root.bind_class('Text', 'c', lambda e: None)
root.bind_class('Text', 'd', lambda e: None)
root.bind_class('Text', 'e', lambda e: None)
root.bind_class('Text', 'f', lambda e: None)
root.bind_class('Text', 'g', lambda e: None)
root.bind_class('Text', 'h', lambda e: None)
root.bind_class('Text', 'i', lambda e: None)
root.bind_class('Text', 'j', lambda e: None)
root.bind_class('Text', 'k', lambda e: None)
root.bind_class('Text', 'l', lambda e: None)
root.bind_class('Text', 'm', lambda e: None)
root.bind_class('Text', 'n', lambda e: None)
root.bind_class('Text', 'o', lambda e: None)
root.bind_class('Text', 'p', lambda e: None)
root.bind_class('Text', 'q', lambda e: None)
root.bind_class('Text', 'r', lambda e: None)
root.bind_class('Text', 's', lambda e: None)
root.bind_class('Text', 't', lambda e: None)
root.bind_class('Text', 'u', lambda e: None)
root.bind_class('Text', 'v', lambda e: None)
root.bind_class('Text', 'w', lambda e: None)
root.bind_class('Text', 'x', lambda e: None)
root.bind_class('Text', 'y', lambda e: None)
root.bind_class('Text', 'z', lambda e: None)
root.bind("<Return>", lambda e: "break")
root.mainloop()
sys.exit(0)