add auto_state_update client & server

This commit is contained in:
Swrup 2023-01-07 23:15:03 +01:00 committed by Gitea
parent cdd46850bf
commit b074eac54f
4 changed files with 55 additions and 8 deletions

View file

@ -18,25 +18,30 @@ let handle_client request client =
Dream.send ~text_or_binary:`Text client (Network.marshal state_msg)
in
let rec loop state =
let rec loop () =
match%lwt Dream.receive client with
| None -> Dream.close_websocket client
| Some s ->
let state =
match User.get_state user_id with
| Error _e -> assert false
| Ok state -> state
in
let (Network.Action_msg action : Network.client_message) =
Network.unmarshal s
in
let res, state =
let res =
match State.check_action state action with
| Error _e as error -> (error, state)
| Error _e as error -> error
| Ok action' ->
(* update server state *)
let state = State.perform_action state action' in
User.set_state user_id state;
(Ok action', state)
Ok action'
in
let* () =
Dream.send client (Network.marshal (Network.Update_result res))
in
loop state
loop ()
in
loop state
loop ()