Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stat override message #564

Merged
merged 19 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
e9139ee
Add energy modifications to history
ncontinanza Apr 30, 2024
377f0ca
Add energy_regen message
ncontinanza Apr 30, 2024
5d8a25e
Update protos
ncontinanza May 2, 2024
66808d2
Fix battle.md preloads
ncontinanza May 2, 2024
fc70d12
Add StatOverride message to override a stat value
ncontinanza May 3, 2024
0497d81
Change inspects for logs
ncontinanza May 3, 2024
2b6094b
Refactor prepare_action/1 to use pattern matching instead of case
ncontinanza May 3, 2024
6cc7530
Fix docs to include all the needed preloads in the battle.md example
ncontinanza May 3, 2024
587fee0
Checkout changes from https://github.com/lambdaclass/mirra_backend/bl…
ncontinanza May 3, 2024
f211877
Merge branch 'main' of github.com:lambdaclass/game_backend into stat-…
ncontinanza May 7, 2024
58934ef
Merge branch 'stat-affected-energy' of github.com:lambdaclass/game_ba…
ncontinanza May 7, 2024
dd3ca4b
Merge branch 'main' of github.com:lambdaclass/game_backend into stat-…
ncontinanza May 7, 2024
2bf5234
Merge branch 'stat-override-message' of github.com:lambdaclass/game_b…
ncontinanza May 7, 2024
e3a7945
Merge branch 'main' of github.com:lambdaclass/game_backend into stat-…
ncontinanza May 7, 2024
b79c5ff
Merge branch 'main' of github.com:lambdaclass/game_backend into stat-…
ncontinanza May 8, 2024
1497e64
Send StatOverride when needing to cap a unit's health
ncontinanza May 8, 2024
bb11dd9
Merge branch 'main' of github.com:lambdaclass/game_backend into stat-…
ncontinanza May 9, 2024
14297b6
Optimize heal cap fn
ncontinanza May 9, 2024
bed7572
fix energy override
ncontinanza May 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions apps/champions/lib/champions/battle/simulator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ defmodule Champions.Battle.Simulator do
},
:skill_action
)
|> add_to_history(
%{
target_id: unit.id,
stat_affected: %{
stat: :ENERGY,
amount: -@ultimate_energy_cost
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be 0, otehrwise the whole concept of stat_override doesn't make sense

}
},
:stat_override
)

{new_state, new_history}

Expand Down Expand Up @@ -892,13 +902,32 @@ defmodule Champions.Battle.Simulator do

# Called at the end of step processing. Sets unit health to max_health if it's above it.
defp cap_units_health({state, history}) do
{Map.put(
state,
:units,
Enum.map(state.units, fn {unit_id, unit} ->
{unit_id, Map.put(unit, :health, min(unit.max_health, unit.health))}
end)
), history}
{new_history, units_state} =
Enum.reduce(state.units, {history, %{}}, fn {unit_id, unit}, {history_acc, state_acc} ->
units_state =
Map.put(state_acc, unit_id, Map.put(unit, :health, min(unit.max_health, unit.health)))

if unit.health > unit.max_health do
new_history =
add_to_history(
history_acc,
%{
target_id: unit.id,
stat_affected: %{
stat: :HEALTH,
amount: unit.max_health
}
},
:stat_override
)

{new_history, units_state}
else
{history_acc, units_state}
end
end)

{Map.put(state, :units, units_state), new_history}
end

# Used to create the initial unit maps to be used during simulation.
Expand Down
139 changes: 96 additions & 43 deletions apps/gateway/lib/gateway/champions_socket_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ defmodule Gateway.ChampionsSocketHandler do
SkillAction,
WebSocketResponse,
ExecutionReceived,
EnergyRegen
EnergyRegen,
StatOverride
}

alias Champions.{Battle, Campaigns, Gacha, Items, Users, Units}
Expand Down Expand Up @@ -235,50 +236,102 @@ defmodule Gateway.ChampionsSocketHandler do
end)
end

defp prepare_action(%{action_type: {type, action}}) do
defp prepare_action(%{action_type: {:skill_action, action}}) do
%{
action_type:
case type do
:skill_action ->
{type,
Kernel.struct(
SkillAction,
action
)}

:modifier_received ->
{type,
Kernel.struct(
ModifierReceived,
update_in(action, [:stat_affected], &Kernel.struct(StatAffected, &1))
)}

:tag_received ->
{type, Kernel.struct(TagReceived, action)}

:tag_expired ->
{type, Kernel.struct(TagExpired, action)}

:modifier_expired ->
{type,
Kernel.struct(
ModifierExpired,
update_in(action, [:stat_affected], &Kernel.struct(StatAffected, &1))
)}

:death ->
{type, Kernel.struct(Death, action)}

:execution_received ->
{type,
Kernel.struct(
ExecutionReceived,
update_in(action, [:stat_affected], &Kernel.struct(StatAffected, &1))
)}

:energy_regen ->
{type, Kernel.struct(EnergyRegen, action)}
end
{:skill_action,
Kernel.struct(
SkillAction,
action
)}
}
end

defp prepare_action(%{action_type: {:modifier_received, action}}) do
%{
action_type:
{:modifier_received,
Kernel.struct(
ModifierReceived,
update_in(action, [:stat_affected], &Kernel.struct(StatAffected, &1))
)}
}
end

defp prepare_action(%{action_type: {:tag_received, action}}) do
%{
action_type:
{:tag_received,
Kernel.struct(
TagReceived,
action
)}
}
end

defp prepare_action(%{action_type: {:tag_expired, action}}) do
%{
action_type:
{:tag_expired,
Kernel.struct(
TagExpired,
action
)}
}
end

defp prepare_action(%{action_type: {:modifier_expired, action}}) do
%{
action_type:
{:modifier_expired,
Kernel.struct(
ModifierExpired,
update_in(action, [:stat_affected], &Kernel.struct(StatAffected, &1))
)}
}
end

defp prepare_action(%{action_type: {:death, action}}) do
%{
action_type:
{:death,
Kernel.struct(
Death,
action
)}
}
end

defp prepare_action(%{action_type: {:execution_received, action}}) do
%{
action_type:
{:execution_received,
Kernel.struct(
ExecutionReceived,
update_in(action, [:stat_affected], &Kernel.struct(StatAffected, &1))
)}
}
end

defp prepare_action(%{action_type: {:energy_regen, action}}) do
%{
action_type:
{:energy_regen,
Kernel.struct(
EnergyRegen,
action
)}
}
end

defp prepare_action(%{action_type: {:stat_override, action}}) do
%{
action_type:
{:stat_override,
Kernel.struct(
StatOverride,
update_in(action, [:stat_affected], &Kernel.struct(StatAffected, &1))
)}
}
end

Expand Down
15 changes: 15 additions & 0 deletions apps/gateway/lib/gateway/serialization/gateway.pb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,12 @@ defmodule Gateway.Serialization.Action do
json_name: "energyRegen",
oneof: 0
)

field(:stat_override, 9,
type: Gateway.Serialization.StatOverride,
json_name: "statOverride",
oneof: 0
)
end

defmodule Gateway.Serialization.StatAffected do
Expand Down Expand Up @@ -853,3 +859,12 @@ defmodule Gateway.Serialization.EnergyRegen do
field(:skill_id, 2, type: :string, json_name: "skillId")
field(:amount, 3, type: :float)
end

defmodule Gateway.Serialization.StatOverride do
@moduledoc false

use Protobuf, syntax: :proto3, protoc_gen_elixir_version: "0.12.0"

field(:target_id, 1, type: :string, json_name: "targetId")
field(:stat_affected, 2, type: Gateway.Serialization.StatAffected, json_name: "statAffected")
end
6 changes: 6 additions & 0 deletions apps/serialization/gateway.proto
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ syntax = "proto3";
Death death = 6;
ExecutionReceived execution_received = 7;
EnergyRegen energy_regen = 8;
StatOverride stat_override = 9;
}
}

Expand Down Expand Up @@ -431,3 +432,8 @@ syntax = "proto3";
string skill_id = 2;
float amount = 3;
}

message StatOverride {
string target_id = 1;
StatAffected stat_affected = 2;
}
Loading