add topbar with mana, fix bug where newly created state was not stored

in the hashtbl 😠, clean code
This commit is contained in:
pena 2023-01-08 04:10:15 +01:00 committed by Swrup
parent 15d42e5038
commit 84129826b5
11 changed files with 177 additions and 89 deletions

View file

@ -1,40 +1,41 @@
open Lwt.Syntax
open Shared
let get_state_unsafe user_id =
match User.get_state user_id with
| Error _e -> assert false
| Ok state -> state
let handle_client request client =
match Dream.session "user_id" request with
| None -> Dream.log "User does not exists" |> Lwt.return
| Some user_id ->
(* TODO catch marshal failure *)
let state =
match User.get_state user_id with
| Error _e -> assert false
| Ok state -> state
in
let state_msg = Network.Full_state state in
(* send user island state *)
(* send user island state for the first time *)
let state = get_state_unsafe user_id in
let* () =
Dream.send ~text_or_binary:`Text client (Network.marshal state_msg)
Dream.send ~text_or_binary:`Text client
(Network.marshal (Network.Full_state state))
in
let rec loop () =
match%lwt Dream.receive client with
| None -> Dream.close_websocket client
| None ->
(* TODO: backup everything to database *)
Dream.close_websocket client
| Some s ->
let state =
match User.get_state user_id with
| Error _e -> assert false
| Ok state -> state
in
let state = get_state_unsafe user_id in
let (Network.Action_msg action : Network.client_message) =
Network.unmarshal s
in
Dream.log "checking action %a" State.pp_action action;
Dream.log "current state %a" State.pp state;
let res =
match State.check_action state action with
| Error _e as error -> error
| Error msg as e ->
Dream.log "check_action error: %s" msg;
e
| Ok action' ->
(* update server state *)
Dream.log "check_action ok: %a" State.pp_action' action';
let state = State.perform_action state action' in
User.set_state user_id state;
Ok action'