From 114dc835cb90a82bde298367df7848499a73dc1c Mon Sep 17 00:00:00 2001 From: nsensfel Date: Sat, 24 Feb 2018 16:23:01 +0100 Subject: Fixes parries, starts work on load_state. --- src/query/character_turn.erl | 6 +- .../handle_character_instance_attacking_2.erl | 204 +++++++++------------ src/query/load_state.erl | 23 ++- 3 files changed, 101 insertions(+), 132 deletions(-) diff --git a/src/query/character_turn.erl b/src/query/character_turn.erl index 39154eb..7ea12ad 100644 --- a/src/query/character_turn.erl +++ b/src/query/character_turn.erl @@ -352,8 +352,8 @@ generate_reply (QueryResult, TurnType, Input) -> handle (Req) -> Input = parse_input(Req), - security:assert_identity(Req#input.player_id, Req#input.session_token), - security:lock_queries(Req#input.player_id), + security:assert_identity(Input#input.player_id, Input#input.session_token), + security:lock_queries(Input#input.player_id), QueryState = fetch_data(Input), assert_character_instance_can_be_played(Input, QueryState), TurnType = get_type_of_turn(Input), @@ -369,7 +369,7 @@ handle (Req) -> ), send_to_database(QueryResult, TurnType, Input), update_cache(QueryResult, TurnType, Input), - security:unlock_queries(Req#input.player_id), + security:unlock_queries(Input#input.player_id), generate_reply(QueryResult, TurnType, Input). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 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 ecb2a95..52c3f3d 100644 --- a/src/query/character_turn/handle_character_instance_attacking_2.erl +++ b/src/query/character_turn/handle_character_instance_attacking_2.erl @@ -1,14 +1,7 @@ roll_hits (AttackerStatistics, DefenderStatistics) -> - MissChance = - max - ( - 0, - ( - statistics:get_dodges(DefenderStatistics) - - - statistics:get_accuracy(AttackerStatistics) - ) - ), + DefenderDodges = statistics:get_dodges(DefenderStatistics), + AttackerAccuracy = statistics:get_accuracy(AttackerStatistics), + MissChance = max(0, (DefenderDodges - AttackerAccuracy)), case roll:percentage() of X when (X =< MissChance) -> misses; X when (X =< (MissChance * 2)) -> grazes; @@ -21,119 +14,122 @@ roll_damage (AttackerStatistics, _DefenderStatistics) -> BaseDamage = MinimumDamage + (rand:uniform(MaximumRoll) - 1), CriticalHitChance = statistics:get_critical_hits(AttackerStatistics), case roll:percentage() of - X when (X =< CriticalHitChance) -> - {critical, (BaseDamage * 2)}; - _ -> - {basic, BaseDamage} + X when (X =< CriticalHitChance) -> {critical, (BaseDamage * 2)}; + _ -> {basic, BaseDamage} end. handle_attack (AttackerStatistics, DefenderStatistics) -> Hits = roll_hits(AttackerStatistics, DefenderStatistics), {Critical, Damage} = roll_damage(AttackerStatistics, DefenderStatistics), case Hits of - misses -> - {Hits, Critical, 0}; - - grazes -> - { - Hits, - Critical, - trunc(Damage / 2) - }; - - _ -> - { - Hits, - Critical, - Damage - } + misses -> {Hits, Critical, 0}; + grazes -> {Hits, Critical, trunc(Damage / 2)}; + hits -> {Hits, Critical, Damage} end. -handle_parry (AttackerStatistics, DefenderStatistics) -> - ParryChance = statistics:get_parries(DefenderStatistics), - case roll:percentage() of - X when (X =< ParryChance) -> - [{parry, handle_attack(DefenderStatistics, AttackerStatistics)}]; - _ -> - [] - end. %% FIXME: parry not working as intended handle_attacks ([], _AttackerStatistics, _DefenderStatistics, Results) -> Results; handle_attacks ( - [nothing|Next], + [first|Next], AttackerStatistics, DefenderStatistics, Results ) -> + AttackResult = handle_attack(AttackerStatistics, DefenderStatistics), handle_attacks ( Next, AttackerStatistics, DefenderStatistics, - Results + [{first, AttackResult} | Results] ); handle_attacks ( - [first|Next], + [second|Next], AttackerStatistics, DefenderStatistics, Results ) -> - handle_attacks - ( - Next, - AttackerStatistics, - DefenderStatistics, - [ - { - first, - handle_attack(AttackerStatistics, DefenderStatistics) - } - | - Results - ] - ); + SecondHitChance = statistics:get_second_hits(AttackerStatistics), + UpdatedResults = + case roll:percentage() of + X when (X =< SecondHitChance) -> + [ + {second, handle_attack(AttackerStatistics, DefenderStatistics)} + | + Results + ]; + + _ -> + Results + end, + handle_attacks(Next, AttackerStatistics, DefenderStatistics, UpdatedResults); handle_attacks ( - [second|Next], + [{first, parry}|Next], AttackerStatistics, DefenderStatistics, Results ) -> + ParryChance = statistics:get_parries(DefenderStatistics), + AttackResult = + case roll:percentage() of + X when (X =< ParryChance) -> + { + {first, parry}, + handle_attack(DefenderStatistics, AttackerStatistics) + }; + + _ -> + {first, handle_attack(AttackerStatistics, DefenderStatistics)} + end, handle_attacks ( Next, AttackerStatistics, DefenderStatistics, - [ - { - second, - handle_attack(AttackerStatistics, DefenderStatistics) - } - | - Results - ] + [AttackResult|Results] ); handle_attacks ( - [parry|Next], + [{second, parry}|Next], AttackerStatistics, DefenderStatistics, Results ) -> + SecondHitChance = statistics:get_second_hits(AttackerStatistics), + ParryChance = statistics:get_parries(DefenderStatistics), + AttackResult = + case roll:percentage() of + X when (X =< SecondHitChance) -> + case roll:percentage() of + Y when (Y =< ParryChance) -> + { + {second, parry}, + handle_attack(DefenderStatistics, AttackerStatistics) + }; + + _ -> + { + second, + handle_attack(AttackerStatistics, DefenderStatistics) + } + end; + + _ -> nothing + end, handle_attacks ( Next, AttackerStatistics, DefenderStatistics, - ( - handle_parry(AttackerStatistics, DefenderStatistics) - ++ - Results - ) + case AttackResult of + nothing -> Results; + _ -> [AttackResult|Results] + end ); handle_attacks ( @@ -148,10 +144,7 @@ handle_attacks AttackerStatistics, DefenderStatistics, [ - { - counter, - handle_attack(DefenderStatistics, AttackerStatistics) - } + {counter, handle_attack(DefenderStatistics, AttackerStatistics)} | Results ] @@ -161,31 +154,12 @@ apply_attacks_to_healths ([], AttackerHealth, DefenderHealth, ValidEffects) -> {ValidEffects, AttackerHealth, DefenderHealth}; apply_attacks_to_healths ( - [{first, Effect}|Next], - AttackerHealth, - DefenderHealth, - ValidEffects -) -> - {_Hit, _Critical, Damage} = Effect, - case (AttackerHealth > 0) of - true -> - apply_attacks_to_healths - ( - Next, - AttackerHealth, - max(0, (DefenderHealth - Damage)), - [{first, Effect}|ValidEffects] - ); - false -> - ValidEffects - end; -apply_attacks_to_healths -( - [{second, Effect}|Next], + [{Action, Effect}|Next], AttackerHealth, DefenderHealth, ValidEffects -) -> +) +when ((Action == first) or (Action == second)) -> {_Hit, _Critical, Damage} = Effect, case (AttackerHealth > 0) of true -> @@ -194,37 +168,24 @@ apply_attacks_to_healths Next, AttackerHealth, max(0, (DefenderHealth - Damage)), - [{second, Effect}|ValidEffects] + [{Action, Effect}|ValidEffects] ); + false -> ValidEffects end; apply_attacks_to_healths ( - [{counter, Effect}|Next], + [{Action, Effect}|Next], AttackerHealth, DefenderHealth, ValidEffects -) -> - {_Hit, _Critical, Damage} = Effect, - case (DefenderHealth > 0) of - true -> - apply_attacks_to_healths - ( - Next, - max(0, (AttackerHealth - Damage)), - DefenderHealth, - [{counter, Effect}|ValidEffects] - ); - false -> - ValidEffects - end; -apply_attacks_to_healths +) +when ( - [{parry, Effect}|Next], - AttackerHealth, - DefenderHealth, - ValidEffects + (Action == counter) + or (Action == {first, parry}) + or (Action == {second, parry}) ) -> {_Hit, _Critical, Damage} = Effect, case (DefenderHealth > 0) of @@ -234,8 +195,9 @@ apply_attacks_to_healths Next, max(0, (AttackerHealth - Damage)), DefenderHealth, - [{parry, Effect}|ValidEffects] + [{Action, Effect}|ValidEffects] ); + false -> ValidEffects end. @@ -324,11 +286,11 @@ handle_character_instance_attacking (QueryState, Input) -> Actions = case {CanDefend, CanParry} of {true, true} -> - [{second, parry}, counter, {first, parry}]; + [{attack, parry}, counter, {attack, parry}]; {true, false} -> - [{second, no_parry}, counter, {fist, no_parry}]; + [second, counter, no_parry]; {false, _} -> - [{second, no_parry}, {first, no_parry}] + [second, first] end, Effects = handle_attacks diff --git a/src/query/load_state.erl b/src/query/load_state.erl index c738a4c..9c88782 100644 --- a/src/query/load_state.erl +++ b/src/query/load_state.erl @@ -9,13 +9,19 @@ ( input, { - session_token, player_id, - battlemap_id, - instance_id + session_token, + battlemap_instance_id } ). +-record +( + query_state, + { + battlemap_instance + } +). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -28,12 +34,13 @@ parse_input (Req) -> JSONReqMap = jiffy:decode(Req, [return_maps]), PlayerID = maps:get(<<"player_id">>, JSONReqMap), SessionToken = maps:get(<<"session_token">>, JSONReqMap), - database_shim:assert_session_is_valid(PlayerID, SessionToken), + BattlemapInstanceID = maps:get(<<"battlemap_id">>, JSONReqMap), + #input { player_id = PlayerID, - battlemap_id = maps:get(<<"battlemap_id">>, JSONReqMap), - instance_id = maps:get(<<"instance_id">>, JSONReqMap) + session_token = SessionToken, + battlemap_instance_id = BattlemapInstanceID }. generate_reply (Battlemap, BattlemapInstance, Characters, PlayerID) -> @@ -67,9 +74,9 @@ generate_reply (Battlemap, BattlemapInstance, Characters, PlayerID) -> ). handle (Req) -> - %%%% Parse Input = parse_input(Req), - %%%% Fetch + security:assert_identity(Input#input.player_id, Input#input.session_token), + security:lock_queries(Input#input.player_id), Battlemap = timed_cache:fetch ( -- cgit v1.2.3-70-g09d2