-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
70 lines (59 loc) · 2.67 KB
/
main.py
File metadata and controls
70 lines (59 loc) · 2.67 KB
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
import time
from Tufts_ble import Sniff, Yell
from human import Human
import asyncio
RANGE = -60
carla = Human(infected = 0, team = 4)
async def run_human(human, sniffer):
'''Check for zombie tags and update human status.'''
while not human.infected:
latest = sniffer.last
curr_time_ms = time.time_ns() // 1_000_000 # Current time in milliseconds
if latest and latest[1:].isdigit():
message = int(latest[1:])
# If RSSI is in range and it's a different team
if sniffer.rssi > RANGE and message != human.team:
# If this is the first tag from this zombie
if human.current_time[message - 1] == 0:
human.current_time[message - 1] = curr_time_ms
human.tag[message - 1] = curr_time_ms
print(f'First tag from team {message}, time set: {human.current_time[message - 1]}')
else:
# Check if 3 seconds have passed since first hit
time_diff = curr_time_ms - human.tag[message - 1]
if time_diff >= 3000:
print(f'Tag confirmed from team {message}, time diff: {time_diff}')
human.tagged[message - 1] += 1
human.current_time[message - 1] = 0 # Reset the current time
human.tag[message - 1] = 0 # Reset first hit time
# Check if the human should become infected
human.status()
else:
# Update the last seen time if within range, but not a full 3 seconds
human.current_time[message - 1] = curr_time_ms
print(f'Tag in progress from team {message}, {time_diff}ms elapsed.')
# Reset the sniffer's last message flag
sniffer.last = ''
await asyncio.sleep(0.1)
async def run_zombie(human):
broadcaster = Yell()
while human.infected:
print(f'!{human.team}')
broadcaster.advertise(f'!{human.team}')
await asyncio.sleep(0.1)
broadcaster.stop_advertising()
async def main():
human = Human(infected=0, team=4) # Initialize a human instance
sniffer = Sniff('!', verbose=True)
sniffer.scan(0) # Start scanning for messages
# Start visual and buzz tasks for human feedback
asyncio.create_task(human.visual())
asyncio.create_task(human.buzz())
# Start human and zombie behavior loops
while True:
if not human.infected:
await run_human(human, sniffer)
else:
await run_zombie(human)
await asyncio.sleep(0.01)
asyncio.run(main())