-
Notifications
You must be signed in to change notification settings - Fork 0
/
pflag.py
202 lines (174 loc) · 11.1 KB
/
pflag.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
#!/usr/bin/env python
import urllib2, time, json, ConfigParser, signal
import os
import sys
import unicornhat
from decimal import *
FLAG_COLOUR_NONE = 0, # Not used for actual flags, only for some query functions
def showflagno():
pixels = [[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)],
[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)],
[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)],
[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)],
[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)],
[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)],
[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)],
[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)]]
unicornhat.set_pixels(pixels)
unicornhat.brightness(0.1)
unicornhat.show()
FLAG_COLOUR_GREEN = 1, # End of danger zone, or race started
def showflaggreen():
pixels = [[(0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0)],
[(0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0)],
[(0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0)],
[(0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0)],
[(0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0)],
[(0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0)],
[(0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0)],
[(0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0), (0, 255, 0)]]
unicornhat.set_pixels(pixels)
unicornhat.brightness(0.1)
unicornhat.show()
FLAG_COLOUR_BLUE = 2, # Faster car wants to overtake the participant
def showflagblue():
pixels = [[(132, 132, 0), (132, 132, 0), (0, 0, 132), (0, 0, 132), (0, 0, 132), (0, 0, 132), (0, 0, 132), (0, 0, 132)],
[(132, 132, 0), (132, 132, 0), (132, 132, 0), (0, 0, 132), (0, 0, 132), (0, 0, 132), (0, 0, 132), (0, 0, 132)],
[(0, 0, 132), (132, 132, 0), (132, 132, 0), (132, 132, 0), (0, 0, 132), (0, 0, 132), (0, 0, 132), (0, 0, 132)],
[(0, 0, 132), (0, 0, 132), (132, 132, 0), (132, 132, 0), (132, 132, 0), (0, 0, 132), (0, 0, 132), (0, 0, 132)],
[(0, 0, 132), (0, 0, 132), (0, 0, 132), (132, 132, 0), (132, 132, 0), (132, 132, 0), (0, 0, 132), (0, 0, 132)],
[(0, 0, 132), (0, 0, 132), (0, 0, 132), (0, 0, 132), (132, 132, 0), (132, 132, 0), (132, 132, 0), (0, 0, 132)],
[(0, 0, 132), (0, 0, 132), (0, 0, 132), (0, 0, 132), (0, 0, 132), (132, 132, 0), (132, 132, 0), (132, 132, 0)],
[(0, 0, 132), (0, 0, 132), (0, 0, 132), (0, 0, 132), (0, 0, 132), (0, 0, 132), (132, 132, 0), (132, 132, 0)]]
unicornhat.set_pixels(pixels)
unicornhat.brightness(0.1)
unicornhat.show()
FLAG_COLOUR_WHITE = 3, # Approaching a slow car
def showflagwhite():
pixels = [[(255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255)],
[(255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255)],
[(255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255)],
[(255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255)],
[(255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255)],
[(255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255)],
[(255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255)],
[(255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255)]]
unicornhat.set_pixels(pixels)
unicornhat.brightness(0.1)
unicornhat.show()
FLAG_COLOUR_YELLOW = 4, # Danger on the racing surface itself
def showflagyellow():
pixels = [[(255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0)],
[(255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0)],
[(255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0)],
[(255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0)],
[(255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0)],
[(255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0)],
[(255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0)],
[(255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0)]]
unicornhat.set_pixels(pixels)
unicornhat.brightness(0.1)
unicornhat.show()
FLAG_COLOUR_DOUBLE_YELLOW = 5, # Danger that wholly or partly blocks the racing surface
def showflagdoubleyellow():
pixels = [[(255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0)],
[(255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0)],
[(255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0)],
[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)],
[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)],
[(255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0)],
[(255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0)],
[(255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0), (255, 255, 0)]]
unicornhat.set_pixels(pixels)
unicornhat.brightness(0.1)
unicornhat.show()
FLAG_COLOUR_BLACK = 6, # Participant disqualified
def showflagblack():
pixels = [[(255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255)],
[(255, 255, 255), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (255, 255, 255)],
[(0, 0, 0), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (0, 0, 0)],
[(0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0)],
[(0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0)],
[(0, 0, 0), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), (0, 0, 0)],
[(255, 255, 255), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (255, 255, 255)],
[(255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255)]]
unicornhat.set_pixels(pixels)
unicornhat.brightness(0.1)
unicornhat.show()
FLAG_COLOUR_CHEQUERED = 7, # Chequered flag
def showflagfinish():
pixels = [[(255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0)],
[(255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0)],
[(0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255)],
[(0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255)],
[(255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0)],
[(255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0)],
[(0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255)],
[(0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255), (0, 0, 0), (0, 0, 0), (255, 255, 255), (255, 255, 255)]]
unicornhat.set_pixels(pixels)
unicornhat.brightness(0.1)
unicornhat.show()
#
# Parses the configuration file
#
def loadConfig():
print("Reading configuration...")
configini = ConfigParser.ConfigParser()
configini.read('./apiconfig.ini')
config = {}
for section in configini.sections():
for option in configini.options(section):
config[section.lower()+':'+option.lower()] = configini.get(section, option)
config['logging:verbose'] = config['logging:verbose'].lower() in ('yes', 'y', 'true', 't', '1')
if(config['logging:verbose']):
print("Loaded configuration:")
for option in config:
print("%s = %s") % (option,config[option])
return config
#
# Gets the current flag from the API
#
def getFlag():
global config, crest_api_url
flags_url=crest_api_url+"?flags=true"
if (config['logging:verbose']):
print("Requesting "+flags_url)
response = urllib2.urlopen(flags_url)
flag_json_raw = response.read()
flag_json = json.loads(flag_json_raw)
if (config['logging:verbose']):
print flag_json
return flag_json['flags']['mHighestFlagColour']
# Load the config
config = loadConfig()
# Assemble the URL
crest_api_url = "%s://%s:%s%s" % (config['api:proto'], config['api:host'], config['api:port'], config['api:path'])
# Convert miliseconds to seconds
config['general:delay'] = float(config['general:delay'])/1000
while True:
flag = getFlag()
if flag == 0:
print "No Flag"
showflagno()
elif flag == 1:
print "End of danger zone, or race started"
showflaggreen()
elif flag == 2:
print "Faster car wants to overtake the participant";
showflagblue()
elif flag == 3:
print "Approaching a slow car"
showflagwhite()
elif flag == 4:
print "Danger on the racing surface itself"
showflagyellow()
elif flag == 5:
print "Danger that wholly or partly blocks the racing surface"
showflagdoubleyellow()
elif flag == 6:
print "Participant disqualified"
showflagblack()
else:
print "Race Finished"
showflagfinish()
time.sleep(config['general:delay'])