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

@ -105,7 +105,9 @@ let kb_handler ev =
in in
Queue.add act input_queue Queue.add act input_queue
let rec game_loop state _timestamp = let last_auto_state_update = ref 0.
let rec game_loop state timestamp =
draw state; draw state;
let new_state = let new_state =
(* TODO repesct order of action *) (* TODO repesct order of action *)
@ -116,7 +118,16 @@ let rec game_loop state _timestamp =
(* send input action to server *) (* send input action to server *)
Queue.iter (send_action state) input_queue; Queue.iter (send_action state) input_queue;
Queue.clear input_queue; Queue.clear input_queue;
state
(* auto_update *)
if
timestamp -. !last_auto_state_update
>= float_of_int @@ (State.auto_state_update_rate * 1000)
then (
Format.printf "MANA: %d@." state.mana;
last_auto_state_update := timestamp;
State.auto_update state )
else state
in in
G.request_animation_frame (game_loop new_state) G.request_animation_frame (game_loop new_state)

View file

@ -1,3 +1,25 @@
let regularly_call_fun f v =
let () = Sys.set_signal Sys.sigalrm (Sys.Signal_handle (fun _ -> f ())) in
let (_ : Unix.interval_timer_status) =
Unix.setitimer Unix.ITIMER_REAL { Unix.it_interval = v; Unix.it_value = v }
in
()
let update_offline_user_state () =
(* TODO *)
()
let update_online_user_state () =
Hashtbl.filter_map_inplace
(fun _user_id state -> Some (Shared.State.auto_update state))
User.state_ht
let () =
regularly_call_fun update_online_user_state
(float_of_int Shared.State.auto_state_update_rate);
regularly_call_fun update_offline_user_state
(float_of_int Shared.State.auto_state_update_rate)
let () = let () =
let logger = if App.log then Dream.logger else Fun.id in let logger = if App.log then Dream.logger else Fun.id in
Dream.run ~port:App.port @@ logger @@ Dream.memory_sessions Dream.run ~port:App.port @@ logger @@ Dream.memory_sessions

View file

@ -31,3 +31,12 @@ let perform_action state = function
| Add_mana n -> { state with mana = state.mana + n } | Add_mana n -> { state with mana = state.mana + n }
| Set_player_position player_pos -> { state with player_pos } | Set_player_position player_pos -> { state with player_pos }
| Look_at_the_sky -> state | Look_at_the_sky -> state
let auto_update state =
match check_action state Meditate with
| Error _e -> state
| Ok action' ->
let state = perform_action state action' in
state
let auto_state_update_rate = 5 (* in secs *)

View file

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