diff --git a/src/island_client.ml b/src/island_client.ml index 37aaccd..5e430e0 100644 --- a/src/island_client.ml +++ b/src/island_client.ml @@ -105,7 +105,9 @@ let kb_handler ev = in 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; let new_state = (* TODO repesct order of action *) @@ -116,7 +118,16 @@ let rec game_loop state _timestamp = (* send input action to server *) Queue.iter (send_action state) 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 G.request_animation_frame (game_loop new_state) diff --git a/src/pellest.ml b/src/pellest.ml index 92b30e6..7356070 100644 --- a/src/pellest.ml +++ b/src/pellest.ml @@ -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 logger = if App.log then Dream.logger else Fun.id in Dream.run ~port:App.port @@ logger @@ Dream.memory_sessions diff --git a/src/state.ml b/src/state.ml index 69f86f6..4b3564d 100644 --- a/src/state.ml +++ b/src/state.ml @@ -31,3 +31,12 @@ let perform_action state = function | Add_mana n -> { state with mana = state.mana + n } | Set_player_position player_pos -> { state with player_pos } | 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 *) diff --git a/src/ws.ml b/src/ws.ml index 78f7a29..b94646c 100644 --- a/src/ws.ml +++ b/src/ws.ml @@ -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 ()