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

Gh 1020 bots all in #1040

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 10 additions & 1 deletion apps/arena/lib/arena/game_socket_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ defmodule Arena.GameSocketHandler do
targetting_angle: mechanic[:angle],
targetting_range: mechanic[:range],
targetting_offset: mechanic[:offset] || mechanic[:projectile_offset],
is_combo: skill.is_combo?
is_combo: skill.is_combo?,
attack_type: cast_attack_type(skill.attack_type),
skill_type: cast_skill_type(skill.type)
}

{key, Map.merge(skill, extra_params)}
Expand Down Expand Up @@ -272,4 +274,11 @@ defmodule Arena.GameSocketHandler do
# This is to override jwt validation for human clients in loadtests.
defp maybe_override_jwt(_client_id, "true", req), do: :cowboy_req.binding(:client_id, req)
defp maybe_override_jwt(client_id, _override_jwt?, _req), do: client_id

defp cast_attack_type("melee"), do: :MELEE
defp cast_attack_type("ranged"), do: :RANGED

defp cast_skill_type("basic"), do: :BASIC
defp cast_skill_type("ultimate"), do: :ULTIMATE
defp cast_skill_type("dash"), do: :DASH
end
27 changes: 27 additions & 0 deletions apps/arena/lib/arena/serialization/messages.pb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ defmodule Arena.Serialization.GameMode do
field(:QUICK_GAME, 3)
end

defmodule Arena.Serialization.AttackType do
@moduledoc false

use Protobuf, enum: true, syntax: :proto3, protoc_gen_elixir_version: "0.13.0"

field(:MELEE, 0)
field(:RANGED, 1)
end

defmodule Arena.Serialization.SkillType do
@moduledoc false

use Protobuf, enum: true, syntax: :proto3, protoc_gen_elixir_version: "0.13.0"

field(:BASIC, 0)
field(:ULTIMATE, 1)
field(:DASH, 2)
end

defmodule Arena.Serialization.GameStatus do
@moduledoc false

Expand Down Expand Up @@ -319,6 +338,14 @@ defmodule Arena.Serialization.ConfigSkill do
field(:targetting_offset, 8, type: :float, json_name: "targettingOffset")
field(:mana_cost, 9, type: :uint64, json_name: "manaCost")
field(:is_combo, 10, type: :bool, json_name: "isCombo")

field(:attack_type, 11,
type: Arena.Serialization.AttackType,
json_name: "attackType",
enum: true
)

field(:skill_type, 12, type: Arena.Serialization.SkillType, json_name: "skillType", enum: true)
end

defmodule Arena.Serialization.GameState.PlayersEntry do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ defmodule ArenaLoadTest.Serialization.GameMode do
field(:QUICK_GAME, 3)
end

defmodule ArenaLoadTest.Serialization.AttackType do
@moduledoc false

use Protobuf, enum: true, syntax: :proto3, protoc_gen_elixir_version: "0.13.0"

field(:MELEE, 0)
field(:RANGED, 1)
end

defmodule ArenaLoadTest.Serialization.SkillType do
@moduledoc false

use Protobuf, enum: true, syntax: :proto3, protoc_gen_elixir_version: "0.13.0"

field(:BASIC, 0)
field(:ULTIMATE, 1)
field(:DASH, 2)
end

defmodule ArenaLoadTest.Serialization.GameStatus do
@moduledoc false

Expand Down Expand Up @@ -336,6 +355,18 @@ defmodule ArenaLoadTest.Serialization.ConfigSkill do
field(:targetting_offset, 8, type: :float, json_name: "targettingOffset")
field(:mana_cost, 9, type: :uint64, json_name: "manaCost")
field(:is_combo, 10, type: :bool, json_name: "isCombo")

field(:attack_type, 11,
type: ArenaLoadTest.Serialization.AttackType,
json_name: "attackType",
enum: true
)

field(:skill_type, 12,
type: ArenaLoadTest.Serialization.SkillType,
json_name: "skillType",
enum: true
)
end

defmodule ArenaLoadTest.Serialization.GameState.PlayersEntry do
Expand Down
94 changes: 60 additions & 34 deletions apps/bot_manager/lib/bot_state_machine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ defmodule BotManager.BotStateMachine do
@skill_1_key "1"
@skill_2_key "2"
@dash_skill_key "3"
@vision_range 1200

@run_away_vision 400
@ranged_vision 1200
@melee_vision 300
@min_distance_to_switch 10

def decide_action(%{bots_enabled?: false, bot_state_machine: bot_state_machine}) do
Expand All @@ -25,7 +28,8 @@ defmodule BotManager.BotStateMachine do
game_state: game_state,
bot_player: bot_player,
bot_state_machine: bot_state_machine,
attack_blocked: attack_blocked
attack_blocked: attack_blocked,
config: config
}) do
bot_state_machine =
if is_nil(bot_state_machine.previous_position) do
Expand Down Expand Up @@ -55,7 +59,8 @@ defmodule BotManager.BotStateMachine do
bot_player: bot_player,
bot_state_machine: bot_state_machine,
game_state: game_state,
attack_blocked: attack_blocked
attack_blocked: attack_blocked,
config: config
})

:running_away ->
Expand All @@ -76,44 +81,65 @@ defmodule BotManager.BotStateMachine do
def use_skill(%{
game_state: game_state,
bot_player: bot_player,
bot_state_machine: bot_state_machine
bot_state_machine: bot_state_machine,
config: config
}) do
players_with_distances = map_directions_to_players(game_state, bot_player, @vision_range)

if Enum.empty?(players_with_distances) do
move(bot_player, bot_state_machine)
else
cond do
bot_state_machine.progress_for_ultimate_skill >= bot_state_machine.cap_for_ultimate_skill ->
bot_state_machine =
Map.put(
bot_state_machine,
:progress_for_ultimate_skill,
bot_state_machine.progress_for_ultimate_skill - bot_state_machine.cap_for_ultimate_skill
)
|> Map.put(:state, :attacking)

{:player, aditional_info} = bot_player.aditional_info

skills = BotManager.Utils.list_character_skills_from_config(aditional_info.character_name, config.characters)

cond do
bot_state_machine.progress_for_ultimate_skill >= bot_state_machine.cap_for_ultimate_skill ->
bot_state_machine =
Map.put(
bot_state_machine,
:progress_for_ultimate_skill,
bot_state_machine.progress_for_ultimate_skill - bot_state_machine.cap_for_ultimate_skill
)
|> Map.put(:state, :attacking)

players_with_distances =
map_directions_to_players(
game_state,
bot_player,
if(skills.ultimate.attack_type == :MELEE, do: @melee_vision, else: @ranged_vision)
)

if Enum.empty?(players_with_distances) do
move(bot_player, bot_state_machine)
else
direction = maybe_aim_to_a_player(bot_player, players_with_distances)

%{action: {:use_skill, @skill_2_key, direction}, bot_state_machine: bot_state_machine}

bot_state_machine.progress_for_basic_skill >= bot_state_machine.cap_for_basic_skill ->
bot_state_machine =
Map.put(
bot_state_machine,
:progress_for_basic_skill,
bot_state_machine.progress_for_basic_skill - bot_state_machine.cap_for_basic_skill
)
|> Map.put(:progress_for_ultimate_skill, bot_state_machine.progress_for_ultimate_skill + 1)
|> Map.put(:state, :attacking)

end

bot_state_machine.progress_for_basic_skill >= bot_state_machine.cap_for_basic_skill ->
bot_state_machine =
Map.put(
bot_state_machine,
:progress_for_basic_skill,
bot_state_machine.progress_for_basic_skill - bot_state_machine.cap_for_basic_skill
)
|> Map.put(:progress_for_ultimate_skill, bot_state_machine.progress_for_ultimate_skill + 1)
|> Map.put(:state, :attacking)

players_with_distances =
map_directions_to_players(
game_state,
bot_player,
if(skills.basic.attack_type == :MELEE, do: @melee_vision, else: @ranged_vision)
)

if Enum.empty?(players_with_distances) do
move(bot_player, bot_state_machine)
else
direction = maybe_aim_to_a_player(bot_player, players_with_distances)

%{action: {:use_skill, @skill_1_key, direction}, bot_state_machine: bot_state_machine}
end

true ->
move(bot_player, bot_state_machine)
end
true ->
move(bot_player, bot_state_machine)
end
end

Expand Down Expand Up @@ -191,7 +217,7 @@ defmodule BotManager.BotStateMachine do
end

defp run_away(bot_player, game_state, bot_state_machine) do
players_with_distances = map_directions_to_players(game_state, bot_player, @vision_range)
players_with_distances = map_directions_to_players(game_state, bot_player, @run_away_vision)

if Enum.empty?(players_with_distances) do
move(bot_player, bot_state_machine)
Expand Down
27 changes: 27 additions & 0 deletions apps/bot_manager/lib/protobuf/messages.pb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ defmodule BotManager.Protobuf.GameMode do
field(:QUICK_GAME, 3)
end

defmodule BotManager.Protobuf.AttackType do
@moduledoc false

use Protobuf, enum: true, syntax: :proto3, protoc_gen_elixir_version: "0.13.0"

field(:MELEE, 0)
field(:RANGED, 1)
end

defmodule BotManager.Protobuf.SkillType do
@moduledoc false

use Protobuf, enum: true, syntax: :proto3, protoc_gen_elixir_version: "0.13.0"

field(:BASIC, 0)
field(:ULTIMATE, 1)
field(:DASH, 2)
end

defmodule BotManager.Protobuf.GameStatus do
@moduledoc false

Expand Down Expand Up @@ -319,6 +338,14 @@ defmodule BotManager.Protobuf.ConfigSkill do
field(:targetting_offset, 8, type: :float, json_name: "targettingOffset")
field(:mana_cost, 9, type: :uint64, json_name: "manaCost")
field(:is_combo, 10, type: :bool, json_name: "isCombo")

field(:attack_type, 11,
type: BotManager.Protobuf.AttackType,
json_name: "attackType",
enum: true
)

field(:skill_type, 12, type: BotManager.Protobuf.SkillType, json_name: "skillType", enum: true)
end

defmodule BotManager.Protobuf.GameState.PlayersEntry do
Expand Down
13 changes: 13 additions & 0 deletions apps/bot_manager/lib/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,17 @@ defmodule BotManager.Utils do
def player_alive?(%{aditional_info: {:player, %{health: health}}}), do: health > 0

def player_alive?(_), do: :not_a_player

def list_character_skills_from_config(character_name, characters) do
character = Enum.find(characters, fn character -> character.name == character_name end)
{_id, basic} = Enum.find(character.skills, fn {_id, skill} -> skill.skill_type == :BASIC end)
{_id, ultimate} = Enum.find(character.skills, fn {_id, skill} -> skill.skill_type == :ULTIMATE end)
{_id, dash} = Enum.find(character.skills, fn {_id, skill} -> skill.skill_type == :DASH end)

%{
basic: basic,
ultimate: ultimate,
dash: dash
}
end
end
4 changes: 3 additions & 1 deletion apps/game_backend/lib/game_backend/units/skills/skill.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ defmodule GameBackend.Units.Skills.Skill do
field(:stamina_cost, :integer)
field(:mana_cost, :integer)
field(:type, Ecto.Enum, values: [:basic, :dash, :ultimate])
field(:attack_type, Ecto.Enum, values: [:melee, :ranged])

belongs_to(:buff, Buff)
belongs_to(:next_skill, __MODULE__)
Expand Down Expand Up @@ -62,7 +63,8 @@ defmodule GameBackend.Units.Skills.Skill do
:stamina_cost,
:mana_cost,
:type,
:version_id
:version_id,
:attack_type
]

@doc false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule GameBackend.Repo.Migrations.AddAttackTypeToSkill do
use Ecto.Migration

def change do
alter table(:skills) do
add :attack_type, :string
end
end
end
Loading
Loading