-
Notifications
You must be signed in to change notification settings - Fork 1
/
game_controller.py
162 lines (119 loc) · 4.92 KB
/
game_controller.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
import multiprocessing
import lights
import buttons
import time
import boto3
import os
import pygame
from decimal import Decimal
from dotenv import load_dotenv
load_dotenv()
aws_access_key_id = os.getenv('AWS_ACCESS_KEY')
aws_secret_access_key = os.getenv('AWS_SECRET_ACCESS_KEY')
aws_region = os.getenv('REGION')
dynamodb_table = os.getenv('TABLE_NAME')
dynamodb_table_id = 'dance-til-you-pi-id'
def light_process(queue):
result = lights.main()
queue.put(result)
def button_process(queue):
result = buttons.main()
queue.put(result)
def multiplier(buttonTS, lightTS, nextLightTS):
reaction = buttonTS - lightTS
lightTime = nextLightTS - lightTS
# return 0.8 + 0.2 * (lightTime - reaction) / lightTime
return 1 - abs((lightTS + nextLightTS) / 2 - buttonTS)
def calculateScore(light_result, button_result):
# light_result = [{'ts': 1, 'color': 'RED'}, {'ts': 2, 'color': 'GREEN'}, {'ts': 3, 'color': 'YELLOW'}, {'ts': 4, 'color': 'GREEN'}, {'ts': 5, 'color': 'BLUE'}, {'ts': 7, 'color': 'PURPLE'}]
# button_result = [{'ts': 1.5, 'color': 'RED'}, {'ts': 1.8, 'color': 'GREEN'}, {'ts': 3.5, 'color': 'YELLOW'}, {'ts': 4.5, 'color': 'GREEN'}, {'ts': 5.5, 'color': 'BLUE'}]
total = 0
button_index = 0
for light_index in range(len(light_result) - 1):
# print("light index:", light_index)
lightTS = light_result[light_index]["ts"]
lightColor = light_result[light_index]["color"]
nextLightTS = light_result[light_index + 1]["ts"]
if button_index >= len(button_result):
break
buttonTS = button_result[button_index]["ts"]
buttonPressed = False
while button_index < len(button_result):
buttonTS = button_result[button_index]["ts"]
if buttonTS >= lightTS and buttonTS < nextLightTS: # in the light window
if not buttonPressed:
# print("in not buttonPresssed")
buttonColor = button_result[button_index]["color"]
if buttonColor == lightColor:
total += multiplier(buttonTS, lightTS, nextLightTS)
buttonPressed = True
# print("current light ts: " + str(lightTS))
# print("used button ts: " + str(buttonTS))
# print("next light ts: " + str(nextLightTS))
if buttonTS >= nextLightTS:
break
button_index += 1
# print("button index:", button_index)
# print("button index: " + str(button_index))
# print("while loop")
# print("for loop")
return total / (len(light_result) - 1)
def get_next_id():
id_table = dynamodb.Table(dynamodb_table_id)
response = id_table.update_item(
Key={'table_name': 'dance-til-you-pi-scores'},
UpdateExpression='SET id = id + :val',
ExpressionAttributeValues={':val': 1},
ReturnValues='UPDATED_NEW'
)
return response['Attributes']['id']
if __name__ == "__main__":
# Initialize DynamoDB client
dynamodb = boto3.resource('dynamodb', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, region_name=aws_region)
table = dynamodb.Table(dynamodb_table)
pygame.mixer.init()
pygame.mixer.music.load("pimusic.mp3")
name = input("Enter player name: ")
print()
print("Welcome,", name + "!")
print("Press the buttons to the beat of the song!")
print()
time.sleep(1)
pygame.mixer.music.play()
print("Game starting in 3 seconds...")
time.sleep(1)
print("Game starting in 2 seconds...")
time.sleep(1)
print("Game starting in 1 seconds...")
time.sleep(1)
print("GO!")
result_queue = multiprocessing.Queue()
light_proc = multiprocessing.Process(target=light_process, args=(result_queue,))
button_proc = multiprocessing.Process(target=button_process, args=(result_queue,))
time.sleep(8)
light_proc.start()
button_proc.start()
light_proc.join() # Wait for light process to finish
button_proc.join() # Wait for button process to finish
# Retrieve results from the queue
light_result = result_queue.get()
button_result = result_queue.get()
# print("Light result size:", len(light_result))
# print("Button result size:", len(button_result))
print("==========")
score = round(calculateScore(light_result, button_result), 5) * 100
print("score:", score)
# do database st*ff here
next_id = get_next_id()
score_to_add = {
'id': next_id,
'name': name,
'accuracy' : Decimal("{:.5f}".format(score))
}
# print("light:", light_result)
# print("button:", button_result)
try:
response = table.put_item(Item=score_to_add)
print("Your score has been added to the database. Check it out!")
except Exception as e:
print("Error adding item:", str(e))