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
|
#lang rhombus/static
import:
lib("web-server/http.rkt") as http
lib("web-server/servlet-env.rkt") as servlet_env
"thread/thread.rhm" open
"src/world.rhm"
"json/main.rhm" as json
fun loop():
println("Hello World!")
sleep(1)
loop()
fun sim_world():
let wrld = world.World()
wrld.init()
wrld.simulate()
println("Creating thread")
def thr: thread(sim_world)
fun start(req):
let type = http.#{request-method}(req)
let body = http.#{request-post-data/raw}(req)
cond
| type is_now #"POST":
let req_json :: Map = json.from_bytes(body)
cond
| req_json["cmd"] is_now "Login":
thread_send(thr, world.MessageLogin("test", current_thread()))
let response :: world.MessageLoginResponse = thread_receive()
http.#{response/full}(200, #"OK",
system.seconds(),
http.#{APPLICATION/JSON-MIME-TYPE},
PairList(),
PairList(json.to_bytes(response.to_map())))
| req_json["cmd"] is_now "GetChunk":
let user_id = math.exact(req_json["user_id"])
thread_send(thr, world.MessageGetChunks(user_id, current_thread()))
let response :: world.MessageGetChunksResponse = thread_receive()
http.#{response/full}(200, #"OK",
system.seconds(),
http.#{APPLICATION/JSON-MIME-TYPE},
PairList(),
PairList(json.to_bytes(response.to_map())))
| ~else:
http.#{response/full}(400, #"INVALID COMMAND",
system.seconds(),
http.#{APPLICATION/JSON-MIME-TYPE},
PairList(),
PairList())
// Create webserver
servlet_env.#{serve/servlet}(start, #{#:launch-browser?}: #false, #{#:servlet-path}: "/game")
// Wait on world simulation thread so we don't kill the world before it has cleanly exited
thread_wait(thr)
|