-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.go
232 lines (179 loc) · 5.19 KB
/
game.go
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
package main
import (
"fmt"
"log"
"strconv"
"time"
)
// --- EVENT HANDLERS FOR USER INTERACTION ---
// handleButtonPress will be called on every button press
// - i.e. start, stop, restart - and should be passed the gender
// that the button is allocated to (male, female)
func handleButtonPress(gender Gender) {
log.Println("button pressed:", gender)
currentState := getState()
// start or stop the game
if currentState == IS_STOPPED {
go startGame(gender)
return
}
// send a message into our finish channel
// if the game is running
select {
case finishChannel <- STOPPED:
default:
}
}
// handleStartContact will make sure, that the user is starting from the
// beginning of the wire
func handleStartContact(s interface{}) {
// send a signal down the start channel
select {
case startChannel <- true:
default:
}
}
// handleWireContact will be called whenever the user
// touches the wire
func handleWireContact(s interface{}) {
debug("wire touched")
currentState := getState()
if currentState != IS_RUNNING {
debug("the timer is currently not running")
return
}
// send a contact event to our communication channel
select {
case contactChannelDebounced <- true:
default:
}
}
// handleFinishContact will be called whenever the user
// is touching the finish platform
func handleFinishContact(s interface{}) {
debug("game finished")
currentState := getState()
if currentState != IS_RUNNING {
debug("the timer is currently not running")
return
}
// send a message into our finish channel
select {
case finishChannel <- FINISHED:
default:
}
}
// --- ACTUAL GAME ACTIONS ---
// startGame will start a new round
func startGame(gender Gender) {
// clear the console output
fmt.Print("\033[H\033[2J")
// generate a new study id
studyID := generateNewStudyID()
// signal the webserver that we are about to start the game
signal("game::countdown::" + string(gender) + "::" + studyID)
debug("game countdown started")
// initialize the start time and touch counter
touchCounter := 0
// set the game state
setState(IS_RUNNING)
// initialize a channel to end the game-round
done := make(chan struct{})
// define the led event to use (male or female)
var ledEvent string
if gender == FEMALE {
ledEvent = "enableLedWoman"
} else {
ledEvent = "enableLedMan"
}
// enable the led light
select {
case GameEvents <- ledEvent:
default:
}
// wait three seconds for the counter to finish
startTime := time.Now().Add(3 * time.Second)
// create a separate go-routine for our ticker
go func(startTime time.Time, studyID string, done <-chan struct{}) {
debug("game started")
for {
select {
// create a ticker to check the time in regular timespans
case <-time.After(200 * time.Millisecond):
timeElapsed := time.Now().Sub(startTime)
// fmt.Print("\rTICK: ", timeElapsed.Seconds())
if timeElapsed >= Timeout {
finishChannel <- TIMEOUT
}
case <-done:
return
}
}
}(startTime, studyID, done)
// increase the touch counter on every touch and react to finish events
go func(startTime time.Time, counter int, gender Gender, studyID string, done chan struct{}) {
// check if the user touched the start of the wire
var startTouched bool
for {
select {
case <-startChannel:
// ignore touching the start channel if the game is already started
if startTouched == true {
break
}
if time.Now().Before(startTime) {
debug("wait for timer to finish")
break
}
// start region was touched after the timer finished
debug("start region touched")
startTouched = true
// signal the server, that the game should start
signalChannel <- "game::start::" + string(gender)
case <-contactChannel:
debug("register contact")
// check if the start region has been touched before
if startTouched == false {
break
}
// increase the touch counter
counter++
// signal the webserver that the wire was touched
signalChannel <- "game::contact::" + strconv.Itoa(counter)
// sound the buzzer
select {
case GameEvents <- "soundBuzzer":
default:
}
case reason := <-finishChannel:
// only react to events if the run was correctly started
if startTouched == false && reason == FINISHED {
break
}
// user never started the game and timeout occured
if startTouched == false && reason == TIMEOUT {
reason = STOPPED
}
timeElapsed := time.Now().Sub(startTime)
timeElapsedInSeconds := strconv.FormatFloat(timeElapsed.Seconds(), 'f', 3, 64)
// signal the webserver that the game was finished
signalChannel <- "game::finished::" + string(reason) + "::" + timeElapsedInSeconds
// set the state of the game to stopped
setState(IS_STOPPED)
// print the results
fmt.Printf(resultLog, gender, reason, timeElapsedInSeconds, counter)
// save the results to the output file
saveResultToDisk(studyID, string(gender), timeElapsedInSeconds, strconv.Itoa(counter), string(reason))
// close our waiting channel
close(done)
case <-done:
debug("game was finished..")
select {
case GameEvents <- "ledOff":
default:
}
return
}
}
}(startTime, touchCounter, gender, studyID, done)
}