pellest/src/state.ml

43 lines
1.1 KiB
OCaml

type t =
{ map : Map.t
; mana : int
; player_pos : Map.position
}
let init () = { map = Map.init (); mana = 0; player_pos = (20, 3, Down) }
type action =
| Meditate
(* TODO some action do not needs to be checked by server *)
| Move of Map.dir
| Do_nothing
(* type for result of action send to the client by the server *)
type action' =
| Add_mana of int
| Set_player_position of Map.position
| Look_at_the_sky
let check_action state = function
| Meditate ->
if state.mana < 99 then Ok (Add_mana 1) else Error "maximum mana"
| Move dir -> (
match Map.check_move state.map state.player_pos dir with
| Error _e as error -> error
| Ok pos -> Ok (Set_player_position pos) )
| Do_nothing -> Ok Look_at_the_sky
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 *)