From 5235345620c0d4a6669ccc6badc387902ea8c92a Mon Sep 17 00:00:00 2001 From: nsensfel Date: Wed, 28 Feb 2018 13:59:39 +0100 Subject: Adds more types specifications. --- src/io/security.erl | 4 + src/io/timed_cache.erl | 16 ++-- src/query/character_turn.erl | 95 ++++++++++++++++++---- .../handle_character_instance_attacking_2.erl | 64 ++++++++++++++- src/query/load_state.erl | 16 +++- src/reply/add_char.erl | 12 +++ src/reply/set_map.erl | 2 + src/shim/database_shim.erl | 20 ++++- src/struct/battlemap.erl | 10 ++- src/struct/character.erl | 2 +- 10 files changed, 208 insertions(+), 33 deletions(-) diff --git a/src/io/security.erl b/src/io/security.erl index 34b6df2..60f6661 100644 --- a/src/io/security.erl +++ b/src/io/security.erl @@ -23,7 +23,11 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec assert_identity (any(), any()) -> 'unimplemented'. assert_identity (_PlayerID, _SessionToken) -> unimplemented. +-spec lock_queries (any()) -> 'unimplemented'. lock_queries (_PlayerID) -> unimplemented. + +-spec unlock_queries (any()) -> 'unimplemented'. unlock_queries (_PlayerID) -> unimplemented. diff --git a/src/io/timed_cache.erl b/src/io/timed_cache.erl index d76a4f0..4c82ee8 100644 --- a/src/io/timed_cache.erl +++ b/src/io/timed_cache.erl @@ -35,15 +35,18 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec add_to_cache (atom(), any(), any()) -> any(). add_to_cache (DB, Owner, ObjectID) -> {ok, TimerPID} = gen_server:start(?MODULE, {DB, {Owner, ObjectID}}, []), {ok, Data} = database_shim:fetch(DB, ObjectID), ets:insert(DB, {{Owner, ObjectID}, TimerPID, Data}), Data. +-spec add_update_to_cache (atom(), any(), any(), any()) -> 'ok'. add_update_to_cache (DB, Owner, ObjectID, Data) -> {ok, TimerPID} = gen_server:start(?MODULE, {DB, {Owner, ObjectID}}, []), - ets:insert(DB, {{Owner, ObjectID}, TimerPID, Data}). + ets:insert(DB, {{Owner, ObjectID}, TimerPID, Data}), + ok. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -82,6 +85,7 @@ handle_info(_, {DB, ObjectID}) -> {noreply, {DB, ObjectID}, timed_caches_manager:get_timeout()}. %%%% Interface Functions +-spec fetch (atom(), any(), any()) -> any(). fetch (DB, Owner, ObjectID) -> io:format("~nfetch from cache: ~p.~n", [{DB, {Owner, ObjectID}}]), case ets:lookup(DB, {Owner, ObjectID}) of @@ -92,6 +96,7 @@ fetch (DB, Owner, ObjectID) -> Data end. +-spec update (atom(), any(), any(), any()) -> 'ok'. update (DB, Owner, ObjectID, Data) -> io:format("~nUpdating cache: ~p.~n", [{DB, {Owner, ObjectID}}]), case ets:lookup(DB, {Owner, ObjectID}) of @@ -102,7 +107,7 @@ update (DB, Owner, ObjectID, Data) -> end, add_update_to_cache(DB, Owner, ObjectID, Data). - +-spec invalidate (atom(), any(), any()) -> 'ok'. invalidate (DB, Owner, ObjectID) -> case ets:lookup(DB, {Owner, ObjectID}) of [] -> @@ -119,9 +124,6 @@ invalidate (DB, Owner, ObjectID) -> "~nInvalidation request on stored entry: ~p.~n", [{DB, Owner, ObjectID}] ), - gen_server:stop(TimerPID) + gen_server:stop(TimerPID), + ok end. - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% Notes %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff --git a/src/query/character_turn.erl b/src/query/character_turn.erl index d8fe694..c920745 100644 --- a/src/query/character_turn.erl +++ b/src/query/character_turn.erl @@ -9,12 +9,12 @@ ( input, { - player_id, - session_token, - battlemap_instance_id, - character_instance_ix, - path, - target_ix + player_id :: player:id(), + session_token :: binary(), + battlemap_instance_id :: binary(), + character_instance_ix :: non_neg_integer(), + path :: list(binary()), + target_ix :: (-1 | non_neg_integer()) } ). @@ -22,8 +22,8 @@ ( query_state, { - battlemap_instance, - character_instance + battlemap_instance :: battlemap_instance:struct(), + character_instance :: character_instance:struct() } ). @@ -31,12 +31,15 @@ ( query_result, { - is_new_turn, - updated_character_instance_ixs, - updated_battlemap_instance + is_new_turn :: boolean(), + updated_character_instance_ixs :: list(non_neg_integer()), + updated_battlemap_instance :: battlemap_instance:struct() } ). +-type input() :: #input{}. +-type query_state() :: #query_state{}. +-type query_result() :: #query_result{}. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -45,6 +48,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec parse_input (binary()) -> input(). parse_input (Req) -> JSONReqMap = jiffy:decode(Req, [return_maps]), CharacterInstanceIX = binary_to_integer(maps:get(<<"cix">>, JSONReqMap)), @@ -59,6 +63,7 @@ parse_input (Req) -> target_ix = TargetIX }. +-spec fetch_data (input()) -> query_state(). fetch_data (Input) -> PlayerID = Input#input.player_id, BattlemapInstanceID = Input#input.battlemap_instance_id, @@ -84,12 +89,17 @@ fetch_data (Input) -> character_instance = CharacterInstance }. +-spec assert_character_instance_can_be_played + ( + query_state(), + input() + ) + -> 'ok'. assert_character_instance_can_be_played (QueryState, Input) -> - %%% Var BattlemapInstance = QueryState#query_state.battlemap_instance, PlayerID = Input#input.player_id, ControlledCharacterInstance = QueryState#query_state.character_instance, - %%% Asserts + PlayerID = array:get ( @@ -104,8 +114,16 @@ assert_character_instance_can_be_played (QueryState, Input) -> ( character_instance:get_character(ControlledCharacterInstance) ), - true = character_instance:get_is_active(ControlledCharacterInstance). + true = character_instance:get_is_active(ControlledCharacterInstance), + ok. + +-spec handle_character_instance_moving + ( + query_state(), + input() + ) + -> {list(any()), query_state()}. handle_character_instance_moving (QueryState, Input) -> BattlemapInstance = QueryState#query_state.battlemap_instance, ControlledCharacterInstance = QueryState#query_state.character_instance, @@ -165,6 +183,12 @@ handle_character_instance_moving (QueryState, Input) -> UpdatedQueryState }. +-spec handle_character_instance_switching_weapons + ( + query_state(), + input() + ) + -> {list(any()), query_state()}. handle_character_instance_switching_weapons (QueryState, Input) -> ControlledCharacterInstance = QueryState#query_state.character_instance, ControlledCharacter = @@ -211,6 +235,7 @@ handle_character_instance_switching_weapons (QueryState, Input) -> -include("character_turn/handle_character_instance_attacking_2.erl"). +-spec get_type_of_turn (input()) -> list(atom()). get_type_of_turn (Input) -> case {Input#input.path, Input#input.target_ix} of {[], -1} -> [nothing, nothing]; @@ -221,14 +246,22 @@ get_type_of_turn (Input) -> {_, _} -> [move, attack] end. +-spec finalize_character_instance + ( + query_state(), + input() + ) + -> query_state(). finalize_character_instance (QueryState, Input) -> BattlemapInstance = QueryState#query_state.battlemap_instance, + FinalizedCharacterInstance = character_instance:set_is_active ( false, QueryState#query_state.character_instance ), + QueryState#query_state { battlemap_instance = @@ -245,6 +278,14 @@ finalize_character_instance (QueryState, Input) -> character_instance = FinalizedCharacterInstance }. +-spec activate_relevant_character_instances + ( + list(non_neg_integer()), + array:array(character_instance:struct()), + player:id(), + (-1 | non_neg_integer()) + ) + -> {list(non_neg_integer()), array:array(character_instance:struct())}. activate_relevant_character_instances (IXs, CharacterInstances, _Owner, -1) -> {IXs, CharacterInstances}; activate_relevant_character_instances (IXs, CharacterInstances, Owner, IX) -> @@ -275,6 +316,11 @@ activate_relevant_character_instances (IXs, CharacterInstances, Owner, IX) -> ) end. +-spec start_next_players_turn + ( + query_state() + ) + -> {list(non_neg_integer()), battlemap_instance:struct()}. start_next_players_turn (QueryState) -> BattlemapInstance = QueryState#query_state.battlemap_instance, PlayerIDs = battlemap_instance:get_player_ids(BattlemapInstance), @@ -315,6 +361,7 @@ start_next_players_turn (QueryState) -> ), {ActivatedCharacterInstanceIXs, UpdatedBattlemapInstance}. +-spec finalize_character_turn (query_state()) -> query_result(). finalize_character_turn (QueryState) -> BattlemapInstance = QueryState#query_state.battlemap_instance, CharacterInstances = @@ -351,6 +398,14 @@ finalize_character_turn (QueryState) -> } end. +-spec play + ( + list(any()), + query_state(), + list(atom()), + input() + ) + -> {list(any()), query_state()}. play (DiffUpdate, QueryState, [], _Input) -> {DiffUpdate, QueryState}; play (DiffUpdate, QueryState, [nothing|Next], Input) -> @@ -375,7 +430,6 @@ play (DiffUpdate, QueryState, [switch|Next], Input) -> Next, Input ); - play (DiffUpdate, QueryState, [attack|Next], Input) -> {AddedDiffContent, NewQueryState} = handle_character_instance_attacking(QueryState, Input), @@ -387,6 +441,7 @@ play (DiffUpdate, QueryState, [attack|Next], Input) -> Input ). +-spec send_to_database (query_result(), any(), input()) -> 'ok'. send_to_database (QueryResult, _TurnType, Input) -> PlayerID = Input#input.player_id, BattlemapInstanceID = Input#input.battlemap_instance_id, @@ -401,6 +456,7 @@ send_to_database (QueryResult, _TurnType, Input) -> BattlemapInstance ). +-spec update_cache (query_result(), input()) -> 'ok'. update_cache (QueryResult, Input) -> PlayerID = Input#input.player_id, BattlemapInstanceID = Input#input.battlemap_instance_id, @@ -414,6 +470,14 @@ update_cache (QueryResult, Input) -> BattlemapInstance ). +-spec generate_reply + ( + query_result(), + list(any()), + any(), + input() + ) + -> binary(). generate_reply (_QueryResult, DiffUpdate, _TurnType, _Input) -> %% TODO jiffy:encode @@ -426,6 +490,7 @@ generate_reply (_QueryResult, DiffUpdate, _TurnType, _Input) -> ] ). +-spec handle (binary()) -> binary(). handle (Req) -> Input = parse_input(Req), security:assert_identity(Input#input.player_id, Input#input.session_token), diff --git a/src/query/character_turn/handle_character_instance_attacking_2.erl b/src/query/character_turn/handle_character_instance_attacking_2.erl index 5f34d05..6995c4c 100644 --- a/src/query/character_turn/handle_character_instance_attacking_2.erl +++ b/src/query/character_turn/handle_character_instance_attacking_2.erl @@ -1,4 +1,23 @@ -%% FIXME: parry not working as intended +% TODO: put all of that into separate modules. It's kind of a mess here. +-type hits() :: ('misses' | 'grazes' | 'hits'). +-type critical() :: ('critical' | 'basic'). +-type attack_category() :: + ( + 'first' + | 'second' + | 'counter' + | {'first', 'parry'} + | {'second', 'parry'} + ). +-type attack_effect() :: {hits(), critical(), non_neg_integer()}. +-type attack_desc() :: {attack_category(), attack_effect()}. + +-spec roll_hits + ( + statistics:struct(), + statistics:struct() + ) + -> hits(). roll_hits (AttackerStatistics, DefenderStatistics) -> DefenderDodges = statistics:get_dodges(DefenderStatistics), AttackerAccuracy = statistics:get_accuracy(AttackerStatistics), @@ -9,6 +28,12 @@ roll_hits (AttackerStatistics, DefenderStatistics) -> _ -> hits end. +-spec roll_damage + ( + statistics:struct(), + statistics:struct() + ) + -> {critical(), non_neg_integer()}. roll_damage (AttackerStatistics, _DefenderStatistics) -> {MinimumDamage, MaximumDamage} = statistics:get_damages(AttackerStatistics), MaximumRoll = max(1, MaximumDamage - MinimumDamage), @@ -19,6 +44,12 @@ roll_damage (AttackerStatistics, _DefenderStatistics) -> _ -> {basic, BaseDamage} end. +-spec handle_attack + ( + statistics:struct(), + statistics:struct() + ) + -> {hits(), critical(), non_neg_integer()}. handle_attack (AttackerStatistics, DefenderStatistics) -> Hits = roll_hits(AttackerStatistics, DefenderStatistics), {Critical, Damage} = roll_damage(AttackerStatistics, DefenderStatistics), @@ -28,7 +59,14 @@ handle_attack (AttackerStatistics, DefenderStatistics) -> hits -> {Hits, Critical, Damage} end. - +-spec handle_attacks + ( + list(attack_category()), + statistics:struct(), + statistics:struct(), + list(attack_desc()) + ) + -> list(attack_desc()). handle_attacks ([], _AttackerStatistics, _DefenderStatistics, Results) -> Results; handle_attacks @@ -150,6 +188,14 @@ handle_attacks ] ). +-spec apply_attacks_to_healths + ( + list(attack_desc()), + non_neg_integer(), + non_neg_integer(), + list(attack_desc()) + ) + -> {list(attack_desc()), non_neg_integer(), non_neg_integer()}. apply_attacks_to_healths ([], AttackerHealth, DefenderHealth, ValidEffects) -> {ValidEffects, AttackerHealth, DefenderHealth}; apply_attacks_to_healths @@ -202,6 +248,14 @@ when {ValidEffects, AttackerHealth, DefenderHealth} end. +-spec set_new_healths_in_query_state + ( + non_neg_integer(), + non_neg_integer(), + query_state(), + input() + ) + -> query_state(). set_new_healths_in_query_state ( RemainingAttackerHealth, @@ -246,6 +300,12 @@ set_new_healths_in_query_state ) }. +-spec handle_character_instance_attacking + ( + query_state(), + input() + ) + -> {list(attack_desc()), query_state()}. handle_character_instance_attacking (QueryState, Input) -> BattlemapInstance = QueryState#query_state.battlemap_instance, ControlledCharacterInstance = QueryState#query_state.character_instance, diff --git a/src/query/load_state.erl b/src/query/load_state.erl index 4ad1d45..f63a4b1 100644 --- a/src/query/load_state.erl +++ b/src/query/load_state.erl @@ -9,9 +9,9 @@ ( input, { - player_id, - session_token, - battlemap_instance_id + player_id :: player:id(), + session_token :: binary(), + battlemap_instance_id :: binary() } ). @@ -19,9 +19,13 @@ ( query_state, { - battlemap_instance + battlemap_instance :: battlemap_instance:struct() } ). + +-type input() :: #input{}. +-type query_state() :: #query_state{}. + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -30,6 +34,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec parse_input (binary()) -> input(). parse_input (Req) -> JSONReqMap = jiffy:decode(Req, [return_maps]), PlayerID = maps:get(<<"pid">>, JSONReqMap), @@ -43,6 +48,7 @@ parse_input (Req) -> battlemap_instance_id = BattlemapInstanceID }. +-spec fetch_data (input()) -> query_state(). fetch_data (Input) -> PlayerID = Input#input.player_id, BattlemapInstanceID = Input#input.battlemap_instance_id, @@ -60,6 +66,7 @@ fetch_data (Input) -> battlemap_instance = BattlemapInstance }. +-spec generate_reply(query_state()) -> binary(). generate_reply (QueryState) -> BattlemapInstance = QueryState#query_state.battlemap_instance, jiffy:encode @@ -78,6 +85,7 @@ generate_reply (QueryState) -> ] ). +-spec handle (binary()) -> binary(). handle (Req) -> Input = parse_input(Req), security:assert_identity(Input#input.player_id, Input#input.session_token), diff --git a/src/reply/add_char.erl b/src/reply/add_char.erl index 7d30bac..42c3359 100644 --- a/src/reply/add_char.erl +++ b/src/reply/add_char.erl @@ -12,6 +12,11 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec attributes_as_json + ( + attributes:struct() + ) -> + {list({binary(), non_neg_integer()})}. attributes_as_json (Attributes) -> { [ @@ -24,6 +29,7 @@ attributes_as_json (Attributes) -> ] }. +-spec encode (non_neg_integer(), character_instance:struct()) -> binary(). encode (IX, CharacterInstance) -> Character = character_instance:get_character(CharacterInstance), {X, Y} = character_instance:get_location(CharacterInstance), @@ -56,5 +62,11 @@ encode (IX, CharacterInstance) -> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec generate + ( + non_neg_integer(), + character_instance:struct() + ) + -> list(binary()). generate (IX, CharacterInstance) -> [<<"add_char">>, encode(IX, CharacterInstance)]. diff --git a/src/reply/set_map.erl b/src/reply/set_map.erl index 5a2cf55..0941239 100644 --- a/src/reply/set_map.erl +++ b/src/reply/set_map.erl @@ -12,6 +12,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec encode (battlemap:struct()) -> binary(). encode (Battlemap) -> jiffy:encode ( @@ -27,5 +28,6 @@ encode (Battlemap) -> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec generate (battlemap:struct()) -> list(binary()). generate (Battlemap) -> [<<"set_map">>, encode(Battlemap)]. diff --git a/src/shim/database_shim.erl b/src/shim/database_shim.erl index af49ca0..68b6ca8 100644 --- a/src/shim/database_shim.erl +++ b/src/shim/database_shim.erl @@ -19,6 +19,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec create_db (pid()) -> 'ok'. create_db (_Heir) -> ets:new ( @@ -31,12 +32,24 @@ create_db (_Heir) -> {read_concurrency, true} ] ), - io:format("~ndb_shim ets created.~n"). + io:format("~ndb_shim ets created.~n"), + ok. +-spec add_to_db (any(), any()) -> 'ok'. add_to_db (ID, Val) -> io:format("~nadd to db_shim: ~p.~n", [{ID, Val}]), - ets:insert(db_shim, {ID, Val}). + ets:insert(db_shim, {ID, Val}), + ok. +-spec generate_random_characters + ( + non_neg_integer(), + non_neg_integer(), + non_neg_integer(), + non_neg_integer(), + list(character:struct()) + ) + -> list(character:struct()). generate_random_characters ( 0, @@ -88,6 +101,7 @@ generate_random_characters %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +-spec generate_db (pid()) -> 'ok'. generate_db (Heir) -> Pid = self(), spawn(fun () -> create_db(Heir), Pid ! ok, receive ok -> ok end end), @@ -110,6 +124,7 @@ generate_db (Heir) -> add_to_db({battlemap_instance_db, <<"0">>}, BattlemapInstance). +-spec fetch (atom(), any()) -> ({'ok', any()} | 'nothing'). fetch (DB, ObjectID) -> io:format("~ndb_shim lookup: ~p.~n", [{DB, ObjectID}]), case ets:lookup(db_shim, {DB, ObjectID}) of @@ -117,5 +132,6 @@ fetch (DB, ObjectID) -> [] -> nothing end. +-spec commit (atom(), any(), any(), any()) -> 'ok'. commit (DB, _Owner, ObjectID, Value) -> add_to_db({DB, ObjectID}, Value). diff --git a/src/struct/battlemap.erl b/src/struct/battlemap.erl index 76f8fd4..8f85bbb 100644 --- a/src/struct/battlemap.erl +++ b/src/struct/battlemap.erl @@ -81,14 +81,20 @@ get_height (Battlemap) -> Battlemap#battlemap.height. -spec get_tile_ids (struct()) -> array:array(tile:id()). get_tile_ids (Battlemap) -> Battlemap#battlemap.tile_ids. --spec random (id(), non_neg_integer(), non_neg_integer()) -> struct(). +-spec random + ( + non_neg_integer(), + non_neg_integer(), + non_neg_integer() + ) + -> struct(). random (ID, Width, Height) -> InitialTile = tile:random_id(), TileIDs = generate_random_tile_ids(InitialTile, [], Width, Height, Width), #battlemap { - id = ID, + id = list_to_binary(integer_to_list(ID)), width = Width, height = Height, tile_ids = array:from_list(TileIDs) diff --git a/src/struct/character.erl b/src/struct/character.erl index bf6e046..96660ee 100644 --- a/src/struct/character.erl +++ b/src/struct/character.erl @@ -109,7 +109,7 @@ set_statistics (Stats, Char) -> -spec random ( - id(), + non_neg_integer(), player:id() ) -> struct(). -- cgit v1.2.3-70-g09d2