aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornsensfel <SpamShield0@noot-noot.org>2018-03-12 17:55:25 +0100
committernsensfel <SpamShield0@noot-noot.org>2018-03-12 17:55:25 +0100
commit68cfa3b73e6f7def9641b08c4acf2f0f96685843 (patch)
tree5d6bbe4b6f1e777235fcc1235060b5101ca46c4d
parent11a7a03a0088b2c4b8edc394469396d54190dc53 (diff)
downloadtacticians-server-68cfa3b73e6f7def9641b08c4acf2f0f96685843.zip
tacticians-server-68cfa3b73e6f7def9641b08c4acf2f0f96685843.tar.bz2
Stores and sends entire turn timelines.
-rw-r--r--src/battle/battle_turn.erl25
-rw-r--r--src/query/character_turn.erl26
-rw-r--r--src/query/load_state.erl3
-rw-r--r--src/reply/turn_results.erl27
-rw-r--r--src/shim/database_shim.erl2
-rw-r--r--src/struct/battle.erl23
-rw-r--r--src/struct/player.erl2
7 files changed, 82 insertions, 26 deletions
diff --git a/src/battle/battle_turn.erl b/src/battle/battle_turn.erl
index 6e8d226..638e8f9 100644
--- a/src/battle/battle_turn.erl
+++ b/src/battle/battle_turn.erl
@@ -10,7 +10,8 @@
-export
(
[
- handle_post_play/1
+ handle_post_play/1,
+ store_timeline/2
]
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -75,7 +76,7 @@ start_next_players_turn (Battle) ->
),
NextPlayer = array:get(NextPlayerIX, Players),
- UpdatedNextPlayer = player:reset_timeline(),
+ UpdatedNextPlayer = player:reset_timeline(NextPlayer),
{ActivatedCharacterInstanceIXs, UpdatedCharacterInstances} =
activate_relevant_character_instances
@@ -107,8 +108,24 @@ start_next_players_turn (Battle) ->
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
--spec handle_post_play (battle:struct()) ->
- {database_diff:struct(), battle:struct()}.
+-spec store_timeline
+ (
+ list(any()),
+ battle:struct()
+ )
+ -> battle:struct().
+store_timeline (TurnEffects, Battle) ->
+ PlayerTurn = battle:get_current_player_turn(Battle),
+ PlayerIX = player_turn:get_player_ix(PlayerTurn),
+ Player = battle:get_player(PlayerIX, Battle),
+
+ UpdatedPlayer = player:add_to_timeline(TurnEffects, Player),
+
+ battle:set_player(PlayerIX, UpdatedPlayer, Battle).
+
+
+-spec handle_post_play (battle:struct())
+ -> {database_diff:struct(), battle:struct()}.
handle_post_play (Battle) ->
CharacterInstances = battle:get_character_instances(Battle),
diff --git a/src/query/character_turn.erl b/src/query/character_turn.erl
index e796493..e2b6165 100644
--- a/src/query/character_turn.erl
+++ b/src/query/character_turn.erl
@@ -145,27 +145,18 @@ update_cache (Battle, Input) ->
Battle
).
--spec generate_reply
- (
- list(turn_result:struct())
- )
- -> binary().
-generate_reply (ClientUpdate) ->
+-spec generate_reply ( list(any())) -> binary().
+generate_reply (EncodedClientUpdate) ->
jiffy:encode
(
[
- {
- [
- {<<"msg">>, <<"turn_results">>},
- {<<"cnt">>, lists:map(fun turn_result:encode/1, ClientUpdate)}
- ]
- }
+ turn_results:generate(EncodedClientUpdate)
]
).
handle_actions (RData, Input) ->
Battle = RData#relevant_data.battle,
- CharacterInstance= RData#relevant_data.played_character_instance,
+ CharacterInstance = RData#relevant_data.played_character_instance,
CharacterInstanceIX = Input#input.character_instance_ix,
Actions = Input#input.actions,
@@ -236,10 +227,15 @@ handle (Req) ->
{ActionsDiffUpdate, ClientUpdate, UpdatedRData} =
handle_actions(RData, Input),
+ EncodedClientUpdate = lists:map(fun turn_result:encode/1, ClientUpdate),
+
UpdatedBattle = finalize_and_fuse_relevant_data(UpdatedRData, Input),
+ UpdatedBattle2 =
+ battle_turn:store_timeline(EncodedClientUpdate, UpdatedBattle),
+
{TurnDiffUpdate, FinalizedBattle} =
- battle_turn:handle_post_play(UpdatedBattle),
+ battle_turn:handle_post_play(UpdatedBattle2),
DiffUpdate = (TurnDiffUpdate ++ ActionsDiffUpdate),
@@ -251,7 +247,7 @@ handle (Req) ->
security:unlock_queries(PlayerID),
- generate_reply(ClientUpdate).
+ generate_reply(EncodedClientUpdate).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
diff --git a/src/query/load_state.erl b/src/query/load_state.erl
index cc8f28f..d92c091 100644
--- a/src/query/load_state.erl
+++ b/src/query/load_state.erl
@@ -74,6 +74,7 @@ generate_reply (QueryState, Input) ->
jiffy:encode
(
[
+ turn_results:generate(battle:get_encoded_last_turns_effects(Battle)),
set_map:generate(battle:get_battlemap(Battle))
|
array:sparse_to_list
@@ -87,8 +88,6 @@ generate_reply (QueryState, Input) ->
)
)
]
- ++
- battle:get_encoded_last_turns_effects(Battle)
).
-spec handle (binary()) -> binary().
diff --git a/src/reply/turn_results.erl b/src/reply/turn_results.erl
new file mode 100644
index 0000000..8402389
--- /dev/null
+++ b/src/reply/turn_results.erl
@@ -0,0 +1,27 @@
+-module(turn_results).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+-export([generate/1]).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+-spec generate (list(any())) -> {list(any())}.
+generate (EncodedClientUpdate) ->
+ io:format("~nSending timeline:~n~p~n", [EncodedClientUpdate]),
+ {
+ [
+ {<<"msg">>, <<"turn_results">>},
+ {<<"cnt">>, EncodedClientUpdate}
+ ]
+ }.
diff --git a/src/shim/database_shim.erl b/src/shim/database_shim.erl
index e5afd2b..a26087d 100644
--- a/src/shim/database_shim.erl
+++ b/src/shim/database_shim.erl
@@ -112,7 +112,7 @@ generate_db (Heir) ->
BattlemapHeight = roll:between(16, 64),
Battlemap = battlemap:random(0, BattlemapWidth, BattlemapHeight),
Characters = generate_random_characters(1, 8, 8, 0, []),
- PlayersAsList = [<<"0">>, <<"1">>],
+ PlayersAsList = [player:new(<<"0">>), player:new(<<"1">>)],
Battle =
battle:random
(
diff --git a/src/struct/battle.erl b/src/struct/battle.erl
index d668bbb..bf2b010 100644
--- a/src/struct/battle.erl
+++ b/src/struct/battle.erl
@@ -56,6 +56,18 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+get_all_timelines (Result, CurrentIndex, EndPoint, ArraySize, Players) ->
+ Player = array:get(CurrentIndex, Players),
+ Timeline = player:get_timeline(Player),
+ NextIndex = ((CurrentIndex + 1) rem ArraySize),
+ NextResult = (Result ++ Timeline),
+ case CurrentIndex of
+ EndPoint ->
+ NextResult;
+
+ _ ->
+ get_all_timelines(NextResult, NextIndex, EndPoint, ArraySize, Players)
+ end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -91,9 +103,14 @@ get_current_player_turn (Battle) ->
Battle#battle.current_player_turn.
-spec get_encoded_last_turns_effects (struct()) -> list(any()).
-get_encoded_last_turns_effects (_Battle) ->
- % TODO
- [].
+get_encoded_last_turns_effects (Battle) ->
+ CurrentPlayerTurn = Battle#battle.current_player_turn,
+ Players = Battle#battle.players,
+ CurrentPlayerIX = player_turn:get_player_ix(CurrentPlayerTurn),
+
+ PlayersCount = array:size(Players),
+ StartingPoint = ((CurrentPlayerIX + 1) rem PlayersCount),
+ get_all_timelines([], StartingPoint, CurrentPlayerIX, PlayersCount, Players).
-spec set_battlemap (battlemap:struct(), struct()) -> struct().
set_battlemap (Battlemap, Battle) ->
diff --git a/src/struct/player.erl b/src/struct/player.erl
index 824e474..857906e 100644
--- a/src/struct/player.erl
+++ b/src/struct/player.erl
@@ -3,7 +3,7 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
--type id() :: string().
+-type id() :: binary().
-record
(