forked from zapashcanon/pellest
add auto_state_update client & server
This commit is contained in:
parent
caffcbb527
commit
0c5d6856a3
@ -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)
|
||||
|
||||
|
@ -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
|
||||
|
@ -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 *)
|
||||
|
17
src/ws.ml
17
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 ()
|
||||
|
Loading…
x
Reference in New Issue
Block a user