About Social Code
summaryrefslogtreecommitdiff
path: root/main.rhm
diff options
context:
space:
mode:
Diffstat (limited to 'main.rhm')
-rw-r--r--main.rhm48
1 files changed, 38 insertions, 10 deletions
diff --git a/main.rhm b/main.rhm
index da4701d..bc41348 100644
--- a/main.rhm
+++ b/main.rhm
@@ -1,17 +1,11 @@
#lang rhombus/static
import:
- lib("web-server/http.rkt") open
- lib("web-server/servlet.rkt") open
- lib("web-server/servlet-env.rkt") open
+ lib("web-server/http.rkt") as http
+ lib("web-server/servlet-env.rkt") as servlet_env
"thread/thread.rhm" open
"src/world.rhm"
-
-fun start(req):
- println(#{request-method}(req))
- println(#{request-post-data/raw}(req))
- #{response/xexpr}("Hello world!")
-
+ "json/main.rhm" as json
fun loop():
println("Hello World!")
@@ -20,14 +14,48 @@ fun 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
-#{serve/servlet}(start, #{#:launch-browser?}: #false, #{#:servlet-path}: "/game")
+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)