diff options
author | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2025-03-31 21:47:45 +0100 |
---|---|---|
committer | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2025-03-31 21:47:45 +0100 |
commit | 01e0fa64363bb22c5e322ce2977637f483aea5c6 (patch) | |
tree | 2e8e280934c64760e75a4cb2409eb2242d60f4c6 /main.rhm | |
parent | 3ba85e55e3bd5f28ce81aa1aa93f18a741f78ca2 (diff) |
Add more packets and get json lib working with server data
Diffstat (limited to 'main.rhm')
-rw-r--r-- | main.rhm | 48 |
1 files changed, 38 insertions, 10 deletions
@@ -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) |