-
Notifications
You must be signed in to change notification settings - Fork 8
/
theactualgame.py
139 lines (117 loc) · 4 KB
/
theactualgame.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
import time
import datetime
import MySQLdb
import os
import signal
import subprocess
import RPi.GPIO as GPIO
db = MySQLdb.connect(host="dlw-hackathon.westeurope.cloudapp.azure.com", user="hackathon",
passwd="Delaware.2011", db="hackathon")
proc = subprocess.Popen(
"python3 alive.py3",
stderr=subprocess.STDOUT, # Merge stdout and stderr
stdout=subprocess.PIPE,
shell=True,
preexec_fn=os.setsid)
#create a cursor for the select
cur = db.cursor()
target = [100000, 801.0, 1784.0, 833.0, 592.0, 244.0, 596.0, 237.0, 601.0, 239.0, 341.0]
result = [True] * 9
percentage = 0.30
diffList = [0.0] * 10
def compare(arr):
ok = True
for i in range(1,9):
arr[i] = (target[1] / arr[1]) * arr[i]
diffList[i] = abs(target[i] - arr[i])
result[i-1] = abs(target[i] - arr[i]) <= percentage * target[i]
if(not (abs(target[i] - arr[i]) <= percentage * target[i])):
ok = False
break
print((arr[1:9]))
print(diffList)
print(result)
print('\n')
return ok
power = 7
buzzer = 23
ir = 21
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(power,GPIO.OUT)
GPIO.output(power,GPIO.HIGH)
GPIO.setup(buzzer,GPIO.OUT)
GPIO.output(buzzer,GPIO.HIGH)
GPIO.setup(ir,GPIO.IN)
f = open('team2.csv', 'a')
f.write('\n')
lastread = 0
lasttime = time.time()
counter = 0
largenumberseen = False
counterWritten = 0
arr = [0] * 11
isShot = False
while(True):
newlastread = GPIO.input(ir)
if newlastread != lastread:
if counter > 50000:
largenumberseen = not largenumberseen
if largenumberseen and counterWritten < 11:
arr[counterWritten] = counter;
counterWritten = counterWritten + 1
#print(str(lastread) + " / " + str(counter))
#f.write(str(counter) + '\t')
if counterWritten == 11:
counterWritten = 0
isShot = compare(arr)
if isShot:
os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
print(proc)
proc = subprocess.Popen(
"python3 dead.py3",
stderr=subprocess.STDOUT, # Merge stdout and stderr
stdout=subprocess.PIPE,
shell=True,
preexec_fn=os.setsid)
try:
file = open('ReadIR/currentGame.txt','r')
newId = file.read()
print(newId)
file.close()
file = open('piid','r')
piid = file.read()
file.close()
ts = time.time()
timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
cur.execute("""INSERT INTO hackathon.scores (game, timestamp, idpi) VALUES (%s, %s, %s)""",
(newId, timestamp, piid))
db.commit()
except Exception as e:
print("error: ", str(e))
db.rollback()
print('I\'ve been shot! Inactive for 10 seconds!');
time.sleep(10);
print('I am active! Please, don\'t shoot me, I\'m only the Pi(ano player)!');
os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
proc = subprocess.Popen(
"python3 alive.py3",
stderr=subprocess.STDOUT, # Merge stdout and stderr
stdout=subprocess.PIPE,
shell=True,
preexec_fn=os.setsid)
print(isShot)
counter = 0
largenumberseen = False
counterWritten = 0
arr = [0] * 11
else:
counter = 0
lastread = newlastread
newlasttime = time.time()
diff = (newlasttime - lasttime)*1000
#if diff > 5.5 and diff < 6.5:
#print((newlasttime-lasttime)*1000)
lasttime = newlasttime
else:
counter = counter + 1