This repository has been archived by the owner on Nov 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathts.py
415 lines (368 loc) · 10.2 KB
/
ts.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# CSH Touchscreen Software
# Author: Angelo DiNardi ([email protected])
# Beginnings: Nov 21, 2008
import pygame
from pygame.locals import *
import drinkAPI
import math
import time
import sys
from threading import Thread
import signal
pygame.init()
pygame.mouse.set_visible(False)
font = pygame.font.Font(None, 36)
BUTTONS = {}
MACHINES = {}
IMG_SURFACES = {}
IBUTTONREADER = None
INFOSYS = None
if (sys.argv[1] == 'ld'):
MACHINES = {'Little Drink': 'ld'}
else:
MACHINES = {'Big Drink': 'd', 'Snack': 's'}
def endapp(s1, s2):
global IBUTTONREADER, INFOSYS
try:
IBUTTONREADER.end = True
except:
c = 1
try:
INFOSYS.end = True
except:
c = 1
signal.signal(signal.SIGINT, endapp)
def main():
global BUTTONS, IBUTTONREADER, INFOSYS
clock = pygame.time.Clock()
ibuttonreader = checkibutton()
IBUTTONREADER = ibuttonreader
ibuttonreader.start()
infosys = readinfosys()
INFOSYS = infosys
infosys.start()
# State
# 1 = Unauthenicated
# 2 = Select Machine
# 3 = Select Drink
# 4 = Dropping
drink_state = 1
# Machine we're using
current_machine = None
last_ibutton = ''
current_ibutton = ''
# Initialise screen
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Basic Pygame program')
# Fill background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))
# Display some text
#text = font.render("Hello There", 1, (10, 10, 10))
#textpos = text.get_rect()
#textpos.centerx = background.get_rect().centerx
#background.blit(text, textpos)
# Blit everything to the screen
screen.blit(background, (0, 0))
pygame.display.flip()
CHANGE = 1
PROCESSING = 0
csh_net = drinkAPI.Network('csh.rit.edu')
user = None
auth_ibutton = None
ready_to_auth = 0
infosys_message = ''
# Event loop
while 1:
if ibuttonreader.ibutton != last_ibutton and ibuttonreader.ibutton != '':
if len(MACHINES) == 1:
drink_state = 2.5
for i in MACHINES:
current_machine = MACHINES[i]
else:
drink_state = 2
CHANGE = 1
PROCESSING = 1
auth_ibutton = ibuttonreader.ibutton
ibuttonreader.ibutton = ''
last_ibutton = auth_ibutton
current_ibutton = last_ibutton
if infosys.message != '':
infosys_message = infosys.message
CHANGE = 1
for event in pygame.event.get():
if event.type == QUIT:
return
if event.type == MOUSEBUTTONDOWN:
#print 'buttons: ' + str(BUTTONS)
testrect = pygame.Rect(event.pos, (0, 0))
#print 'test rect: ' + str(testrect)
clicked_button = testrect.collidedict(BUTTONS)
#print 'clicked button: ' + str(clicked_button)
if clicked_button is None:
continue
elif clicked_button[1]['type'] == 'logout':
# Reset everything!
drink_state = 1
CHANGE = 1
PROCESSING = 0
auth_ibutton = ''
current_ibutton = ''
last_ibutton = ''
elif clicked_button[1]['type'] == 'switch':
drink_state = 2
CHANGE = 1
PROCESSING = 1
# Set us up to get authenticated again
auth_ibutton = current_ibutton
elif drink_state == 1:
if clicked_button[1]['type'] == 'login':
drink_state = 2
CHANGE = 1
PROCESSING = 1
elif drink_state == 2:
if clicked_button[1]['type'] == 'machine':
current_machine = clicked_button[1]['item']
CHANGE = 1
drink_state = 2.5
PROCESSING = 1
elif drink_state == 3:
clicked_key = clicked_button[1]
print "checking clicked key: " + str(clicked_key)
if clicked_key['type'] == 'item':
print "got item click: " + str(clicked_key['item'])
drink_state = 4
CHANGE = 1
PROCESSING = 1
item_to_drop = clicked_key['item']
if CHANGE == 1:
CHANGE = 0
background.fill((255, 255, 255))
# Redrawing the interface, so kill the stashed buttons
BUTTONS = {}
if drink_state == 1:
#login_btn = pygame.Surface((200, 50))
#login_btn_rect = login_btn.get_rect()
#login_btn_rect.left = 20
#login_btn_rect.top = 550
#text = font.render("Login", 1, (255, 255, 255))
#login_btn.blit(text, (10, 10))
# background.blit(login_btn, login_btn_rect)
#BUTTONS[tuple(login_btn_rect)] = {'type': 'login'}
render_message(background, "Touch iButton to continue...\n\n" + infosys_message)
elif drink_state == 2:
c = 1
render_machine_choices(background)
render_logout_btn(background)
elif drink_state == 2.5:
c = 1
render_message(background, "Authenticating...")
elif drink_state == 3:
render_switch_btn(background)
render_user_info(background, user)
render_logout_btn(background)
inv = system.inventory
render_drink_choices(background, inv, user)
elif drink_state == 4:
c = 1
render_message(background, "Dropping...")
screen.blit(background, (0, 0))
pygame.display.flip()
if PROCESSING == 1:
PROCESSING = 0
if drink_state == 2.5 and auth_ibutton is not None:
# user = drinkAPI.User('', '3b00000e4bbC9301')
user = drinkAPI.User('', auth_ibutton)
auth_ibutton = None
last_ibutton = ''
system = drinkAPI.System('drink', current_machine)
authed = user.connect_to_system(system, csh_net)
if authed:
drink_state = 3
CHANGE = 1
PROCESSING = 0
elif drink_state == 4:
print 'dropping ' + str(item_to_drop)
user.purchase(item_to_drop, 0)
time.sleep(5)
PROCESSING = 0
CHANGE = 1
drink_state = 1
clock.tick(20)
def render_message(bg, message):
fonting = pygame.font.Font(None, 30)
msg = pygame.Surface((800, 600))
msg.fill((255, 255, 255))
rect = msg.get_rect()
rect.left = 20
rect.top = 50
lines = message.splitlines()
c = 0
for line in lines:
text = fonting.render(line, 1, (0, 0, 0))
msg.blit(text, (0, c * 20))
c = c + 1
bg.blit(msg, rect)
return rect
def render_switch_btn(bg):
switch_btn = pygame.Surface((200, 50))
rect = switch_btn.get_rect()
rect.left = 20
rect.top = 550
BUTTONS[tuple(rect)] = {'type': 'switch'}
text = font.render("Switch", 1, (255, 255, 255))
tr = text.get_rect()
tr.centerx = switch_btn.get_rect().centerx
tr.centery = switch_btn.get_rect().centery
switch_btn.blit(text, tr)
bg.blit(switch_btn, rect)
return rect
def render_logout_btn(bg):
logout = pygame.Surface((200, 50))
rect = logout.get_rect()
rect.left = 580
rect.top = 550
BUTTONS[tuple(rect)] = {'type': 'logout'}
text = font.render("Logout", 1, (255, 255, 255))
tr = text.get_rect()
tr.centerx = logout.get_rect().centerx
tr.centery = logout.get_rect().centery
logout.blit(text, tr)
bg.blit(logout, rect)
return rect
def render_user_info(bg, user):
user_info = pygame.Surface((200, 50))
user_info.fill((255, 255, 255))
rect = user_info.get_rect()
#rect.left = 220
rect.centerx = bg.get_rect().centerx
rect.top = 550
font = pygame.font.Font(None, 25)
text = font.render("User: " + user.name, 1, (0, 0, 0))
user_info.blit(text, (10, 0))
text2 = font.render("Credits: " + str(user.get_credits_balance()), 1, (0, 0, 0))
user_info.blit(text2, (10, 20))
bg.blit(user_info, rect)
return rect
def render_drink_choices(bg, choices, user):
global IMG_SURFACES
#inv = pygame.Surface((800, 500))
#inv.fill((255, 255, 255))
#rect = inv.get_rect()
#rect.left = 0
#rect.top = 50
font = pygame.font.Font(None, 20)
item_count = len(choices)
width = (770 / math.ceil(item_count / 4.0))
cur_item_on_row = 0
items_per_row = math.ceil(770 / width) - 1
cur_row = 0
user_credits = user.get_credits_balance()
for item in choices:
enabled = 1
text_color = (255, 255, 255)
bg_color = (102, 0, 102)
if (item.quantity < 1 or item.price > user_credits):
enabled = 0
text_color = (187, 187, 187)
bg_color = (51, 51, 51)
s = pygame.Surface((width - 2, 98))
s.fill(bg_color)
NO_IMG = False
img_name = item.name.lower().replace(' ', '').replace('\'', '')
if (img_name in IMG_SURFACES):
img = IMG_SURFACES[img_name]
else:
try:
img = pygame.image.load("images/" + img_name + ".png")
img = pygame.transform.smoothscale(img, (80, 80))
except:
try:
img = pygame.image.load("images/unknown.png")
img = pygame.transform.smoothscale(img, (80, 80))
except:
NO_IMG = True
IMG_SURFACES[img_name] = img
if (NO_IMG == False):
s.blit(img, (10, 10))
text = font.render(item.name, 1, text_color)
s.blit(text, (100, 10))
text = font.render(str(item.price), 1, text_color)
s.blit(text, (100, 25))
if (cur_item_on_row > items_per_row):
# reset us to the next row, pos 1
cur_item_on_row = 0
cur_row = cur_row + 1
sr = s.get_rect()
sr.left = (cur_item_on_row * width) + 10
sr.top = (cur_row * 100) + 50
cur_item_on_row = cur_item_on_row + 1
#inv.blit(s, sr)
if enabled:
BUTTONS[tuple(sr)] = {'type': 'item', 'item': item}
bg.blit(s, sr)
#bg.blit(inv, rect)
def render_machine_choices(bg):
cur_machine = 0
for m in MACHINES:
ms = pygame.Surface((200, 100))
ms.fill((102, 0, 102))
text = font.render(m, 1, (255, 255, 255))
tr = text.get_rect()
tr.centerx = ms.get_rect().centerx
tr.centery = ms.get_rect().centery
ms.blit(text, tr)
rect = ms.get_rect()
rect.centerx = bg.get_rect().centerx
rect.top = 100 + (200*cur_machine)
bg.blit(ms, rect)
cur_machine = cur_machine + 1
BUTTONS[tuple(rect)] = {'type': 'machine', 'item': MACHINES[m]}
class readinfosys(Thread):
def __init__ (self):
Thread.__init__(self)
self.message = ''
self.end = False
def run(self):
while(self.end == False):
time.sleep(60)
try:
iffile = open('/tmp/infosysdata', 'r')
print 'checking infosys file'
if iffile is not None:
data = ''
line = None
line = iffile.readline()
while(line):
data = data + line
line = iffile.readline()
iffile.close()
self.message = data
except:
c = 1
class checkibutton(Thread):
def __init__ (self):
Thread.__init__(self)
self.ibutton = ''
self.end = False
def run(self):
while(self.end == False):
time.sleep(1)
try:
ibfile = open('/tmp/ibutton', "r")
print 'checking file'
if ibfile is not None:
line = ibfile.readline()
print 'got: "' + line + '"'
nibutton = line
if nibutton != '':
self.ibutton = nibutton
ibfile.close()
if (self.ibutton != ''):
ibfile = open('/tmp/ibutton', "w")
ibfile.write("")
ibfile.close()
finally:
c = 1
if __name__ == '__main__': main()