From 01e0fa64363bb22c5e322ce2977637f483aea5c6 Mon Sep 17 00:00:00 2001 From: Lucas Fryzek Date: Mon, 31 Mar 2025 21:47:45 +0100 Subject: Add more packets and get json lib working with server data --- main.rhm | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) (limited to 'main.rhm') 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) -- cgit v1.2.3