-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.röd
394 lines (357 loc) · 10.2 KB
/
server.röd
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
{
http := require("http_server")
case := require("case").case
fileutil := require("fileutil")
mv := fileutil.mv
rm := fileutil.rm
logExec := fileutil.logExec
sessions := new map<<Session>>
session_ids := new list<<integer>>
/* map from role to party */
roles := new map<<string>>
localImport "day_state.röd"
localImport "detective_state.röd"
localImport "doctor_state.röd"
localImport "killer_state.röd"
localImport "mafia_state.röd"
}
record Session {
id : integer = {
id := abs(randomInteger())%10000
id = abs(randomInteger())%10000 while [ id in session_ids ]
session_ids += id
return id
}()
players : map<<Player>> = new map<<Player>>
state : string = "LOBBY"
substate : string = ""
cycle : list<<State>> = new list<<State>>([
new MafiaState(self),
new KillerState(self),
new DetectiveState(self),
new DoctorState(self),
new DayState(self)
])
cycle_position : integer = 0
broadcasts : list<<string>> = new list<<string>>
actions : list<<function>> = new list<<function>>
victims : list<<Player>> = new list<<Player>>
kill_list : list<<Player>> = new list<<Player>>
heal_list : list<<Player>> = new list<<Player>>
function pushPlayers {
keys(self.players) | [self.players[_]]
}
function cycleState {
return self.cycle[self.cycle_position%#self.cycle]
}
}
record Player(name) {
id : integer = abs(randomInteger())
name : string = name
state : string = "LOBBY"
role : string = ""
info : string = ""
prev_render : string = ""
function party {
return roles[self.role]
}
}
record State(session) {
session : Session = session
name : string
gmText : function
killTexts : function
onIntro : function
onKill : function = { return FALSE }
onOK : function = { return FALSE }
onOutro : function
}
record CommandController : http.Controller {
/* Game master */
@http.handle "/cmd/new"
function handleNewGame request {
session := new Session
sessions[session.id] = session
request.send "200 OK", `{"id":${session.id}}`, mime="text/json"
}
@http.handle "/cmd/broadcast/\\d+"
function handleBroadcast request {
(match("/cmd/broadcast/(\\d+)", request.path)[1:]) | pull sid
session := sessions[sid]
if [ #session.actions > 0 ] do
action for action in session.actions
del session.actions[:]
done
if [ session.state = "CYCLE" ] do
case session.substate | pull matches, noMatch
if matches "INTRO" do
(session.cycleState().onIntro)
done
if matches "OUTRO" do
(session.cycleState().onOutro)
done
done
if [ #session.broadcasts > 0 ] do
bc := session.broadcasts&" "
del session.broadcasts[:]
id := abs(randomInteger())
logExec "espeak", "-s", "130", "-w", `bc$id.wav`, bc
logExec "oggenc", "-o", `tmp/bc$id.ogg`, `bc$id.wav`
rm `bc$id.wav`
push `/tmp/bc$id.ogg`
else
push ""
done | pull file
case session.state | pull matches, noMatch
try do
if matches "LOBBY" do
n := #session.players
push `Game id: $sid Players: $n`
push([`{"text":"Start game","cmd":"/cmd/start/$sid"}`]) if [ n >= 4 ]
push([]) if [ n < 4 ]
done
if matches "INTRO" do
push `Please follow instructions.`, []
done
if matches "OUTRO" do
push `The game ended.`, []
done
if matches "NIGHT" do
push `It's night.`, []
session.state = "CYCLE"
session.substate = "INTRO"
done
if matches "CYCLE" do
push session.cycleState().gmText(), []
if [ session.state = "CYCLE" and session.substate = "END" ] do
session.cycle_position ++
session.substate = "INTRO"
done
if { [ session.state = "CYCLE" ]; session.pushPlayers | [ _.state = "DEAD" ] } do
session.state = "OUTRO"
done
done
catch e
errprint e.message, "\n"
done | pull title, buttons
request.send "200 OK", `{"title":"$title","file":"$file","buttons":[${buttons&","}]}`, mime="text/json"
}
@http.handle "/cmd/start/\\d+"
function handleStartGame request {
(match("/cmd/start/(\\d+)", request.path)[1:]) | pull sid
session := sessions[sid]
/* shuffle players */
keys session.players |
[[randomInteger(), session.players[_]]] |
sort |
[_[1]] |
sum fst=[] |
pull players
n := #players
players[:n//3] | _.role = "MAFIA"
players[n//3].role = "KILLER"
players[n//3+1].role = "DETECTIVE"
players[n//3+2].role = "DOCTOR"
players[n//3+3:] | _.role = "TOWN"
player.state = "INTRO" for player in players
session.state = "INTRO"
request.send "200 OK", `{}`, mime="text/json"
}
/* Player */
@http.handle "/cmd/canjoin/\\d+"
function handleCanJoinGame request {
(match("/cmd/canjoin/(\\d+)", request.path)[1:]) | pull sid
result := "true"
if [ sessions[sid]? ] do
result = "false" unless [ sessions[sid].state = "LOBBY" ]
else
result = "false"
done
request.send "200 OK", `{"result":$result}`, mime="text/json"
}
@http.handle "/cmd/join"
function handleJoinGame request {
if [ request.command != "POST" ] do
request.send403
return
done
session := sessions[request.form["sid"]]
request.send403 unless [ session.state = "LOBBY" ]
player := new Player(request.form["name"])
player.name ~= "[^a-zåäöA-ZÅÄÖ\\- ]", ""
session.players[player.id] = player
request.send "200 OK", `{"id":${player.id}}`, mime="text/json"
}
@http.handle "/cmd/render/\\d+/\\d+"
function handleRender request {
(match("/cmd/render/(\\d+)/(\\d+)", request.path)[1:]) | pull sid, pid
session := sessions[sid]
player := session.players[pid]
case player.state | pull matches, noMatch
{
if matches "LOBBY" do
push "Please wait.", "Please wait for the game to begin.", []
done
if matches "INTRO" do
push "The game begins.", "Please press the button below to see your role.",
[`{"text":"Ready","cmd":"/cmd/ready/$sid/$pid"}`]
done
if matches "SHOW ROLE" do
push "Your role is", `${player.role}.<br/><br/>Press the button below when you are ready.`,
[`{"text":"Ready","cmd":"/cmd/ready/$sid/$pid"}`]
done
if matches "SLEEP" do
push "It's night.", "Please close your eyes and wait for instructions.", []
done
if matches "WAIT" do
push "Please wait.", "Please wait for instructions.", []
done
if matches "INFO" do
push "Info.", player.info, [`{"text":"OK","cmd":"/cmd/ok/$sid/$pid"}`]
done
if matches "KILL" do
(session.cycleState().killTexts)
push([
session.pushPlayers() |
push(`{"text":"${p.name}","cmd":"/cmd/kill/$sid/$pid/${p.id}"}`) for p if [ p.state != "DEAD" ]
])
done
if matches "DEAD" do
push "Game over.", "You are dead.", []
done
if matches "VICTORY" do
push "Victory.", "You are a member of the winning party.", []
done
if matches "DEFEAT" do
push "Defeat.", "You are a member of the losing party.", []
done
if noMatch do
push "Error.", "An error happened. Please wait. If the problem stays, the game is probably corrupt.", []
done
} | pull title, text, buttons
content := `"title":"$title","text":"$text","buttons":[${buttons&","}]`
change := push("true") if [ player.prev_render != content ] else push("false")
player.prev_render = content
request.send "200 OK", `{"change":$change,$content}`, mime="text/json"
}
@http.handle "/cmd/ready/\\d+/\\d+"
function handleReady request {
(match("/cmd/ready/(\\d+)/(\\d+)", request.path)[1:]) | pull sid, pid
session := sessions[sid]
player := session.players[pid]
if [ session.state != "INTRO" ] do
request.send403
return
done
case player.state | pull matches, noMatch
if matches "INTRO" do
player.state = "SHOW ROLE"
done
if matches "SHOW ROLE" do
player.state = "SLEEP"
if session.pushPlayers | [ _.state = "SLEEP" ] do
session.state = "NIGHT"
session.actions += {
session.broadcasts += "Everybody sleeps."
}
done
done
request.send "200 OK", `{}`, mime="text/json"
}
@http.handle "/cmd/kill/\\d+/\\d+/\\d+"
function handleKill request {
(match("/cmd/kill/(\\d+)/(\\d+)/(\\d+)", request.path)[1:]) | pull sid, pid, tid
session := sessions[sid]
player := session.players[pid]
target := session.players[tid]
if [ session.state != "CYCLE" ] do
request.send403
return
done
if (session.cycleState().onKill) player, target do
request.send "200 OK", `{}`, mime="text/json"
else
request.send403
done
}
@http.handle "/cmd/ok/\\d+/\\d+"
function handleOK request {
(match("/cmd/ok/(\\d+)/(\\d+)", request.path)[1:]) | pull sid, pid
session := sessions[sid]
player := session.players[pid]
if [ session.state != "CYCLE" ] do
request.send403
return
done
if (session.cycleState().onOK) player do
request.send "200 OK", `{}`, mime="text/json"
else
request.send403
done
}
function handle request {
request.send403
}
}
record GameController : http.Controller {
@http.handle "/game/new"
function handleNew request {
request.sendFile mimeType("views/gm.html"), "views/gm.html"
}
@http.handle "/game/player\\?sid=\\d+"
function handleJoin request {
request.sendFile mimeType("views/player.html"), "views/player.html"
}
@http.handle "/game/index"
function handleIndex request {
request.sendFile mimeType("views/index.html"), "views/index.html"
}
function handle request {
request.send403
}
}
record StaticController : http.Controller {
@http.handle "/static/([^/]+)"
function handleStatic request {
matches := match("/static/([^/]+)", request.path)
file := "static/"..matches[1]
if fileExists(file) do
request.sendFile mimeType(file), file
return
done
request.send404
}
@http.handle "/tmp/([^/]+)"
function handleTmp request {
matches := match("/tmp/([^/]+)", request.path)
file := "tmp/"..matches[1]
if fileExists(file) do
request.sendFile mimeType(file), file
rm file
return
done
request.send404
}
function handle request {
request.send403
}
}
dateTime {
{} | bufferedExec "date", "+%d.%m.%Y/%H:%M" | head
}
main port {
hs := new http.HttpServer(parseInteger(port))
hs.controllers["/"] = http.controller({ |request|
request.redirect "/game/index"
})
static_and_tmp := new StaticController
hs.controllers["/static"] = static_and_tmp
hs.controllers["/tmp"] = static_and_tmp
hs.controllers["/game"] = new GameController
hs.controllers["/cmd"] = new CommandController
date = "["..dateTime().."]"
print date.." Started."
while true; do
hs.update
done
}