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-258] Send unit and level rewards to client #698

Closed
Show file tree
Hide file tree
Changes from 6 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
1 change: 0 additions & 1 deletion apps/champions/lib/champions/campaigns.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ defmodule Champions.Campaigns do
%{
user_id: user_id,
template_id: item_reward.item_template_id,
level: 1,
inserted_at: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second),
updated_at: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
}
Expand Down
2 changes: 1 addition & 1 deletion apps/champions/lib/champions/users.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ defmodule Champions.Users do
end

defp add_sample_items(user) do
Items.get_item_templates()
Items.get_item_templates(Utils.get_game_id(:champions_of_mirra))
|> Enum.each(fn template ->
Items.insert_item(%{user_id: user.id, template_id: template.id, level: Enum.random(1..5)})
end)
Expand Down
11 changes: 10 additions & 1 deletion apps/game_backend/lib/game_backend/campaigns.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ defmodule GameBackend.Campaigns do
do:
from(l in Level,
order_by: [asc: l.level_number],
preload: [currency_rewards: :currency, units: [:items, :character], attempt_cost: :currency]
preload: [
currency_rewards: :currency,
units: [:items, :character],
attempt_cost: :currency,
item_rewards: :item_template,
unit_rewards: :character
]
)

@doc """
Expand Down Expand Up @@ -125,6 +131,9 @@ defmodule GameBackend.Campaigns do
|> Repo.preload([
:currency_rewards,
campaign: :super_campaign,
attempt_cost: :currency,
item_rewards: :item_template,
unit_rewards: :character,
units: [
:items,
character: [basic_skill: [mechanics: :apply_effects_to], ultimate_skill: [mechanics: :apply_effects_to]]
Expand Down
6 changes: 3 additions & 3 deletions apps/game_backend/lib/game_backend/items.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ defmodule GameBackend.Items do
end

@doc """
Get all item templates.
Get all item templates from a game.

## Examples
iex> get_item_templates()
iex> get_item_templates(game_id)
[%ItemTemplate{}]
"""
def get_item_templates(), do: Repo.all(ItemTemplate)
def get_item_templates(game_id), do: Repo.all(from(it in ItemTemplate, where: it.game_id == ^game_id))

@doc """
Get an item template by id.
Expand Down
7 changes: 4 additions & 3 deletions apps/game_backend/lib/game_backend/units/characters.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ defmodule GameBackend.Units.Characters do
def get_character(id), do: Repo.get(Character, id) |> Repo.preload([:basic_skill, :ultimate_skill])

@doc """
Get all Characters.
Get all Characters from a game.

## Examples
iex> get_characters()
iex> get_characters(1)
[%Character{}]
"""
def get_characters(), do: Repo.all(Character) |> Repo.preload([:basic_skill, :ultimate_skill])
def get_characters(game_id),
do: Repo.all(from(c in Character, where: c.game_id == ^game_id)) |> Repo.preload([:basic_skill, :ultimate_skill])

@doc """
Get a Character by name.
Expand Down
31 changes: 30 additions & 1 deletion apps/gateway/lib/gateway/champions_socket_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,36 @@ defmodule Gateway.ChampionsSocketHandler do
id: campaign.id,
super_campaign_name: campaign.super_campaign.name,
campaign_number: campaign.campaign_number,
levels: campaign.levels
levels: Enum.map(campaign.levels, &prepare_level/1)
}
end

defp prepare_level(level) do
%{
id: level.id,
campaign_id: level.campaign_id,
level_number: level.level_number,
units: level.units,
currency_rewards: level.currency_rewards,
item_rewards:
Enum.map(
level.item_rewards,
&%{
item_template_name: &1.item_template.name,
amount: &1.amount
}
),
unit_rewards:
Enum.map(
level.unit_rewards,
&%{
character_name: &1.character.name,
rank: &1.rank
}
),
experience_reward: level.experience_reward,
attempt_cost: level.attempt_cost,
max_units: level.max_units
}
end

Expand Down
34 changes: 32 additions & 2 deletions apps/gateway/lib/gateway/serialization/gateway.pb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -654,9 +654,21 @@ defmodule Gateway.Serialization.Level do
json_name: "currencyRewards"
)

field(:experience_reward, 6, type: :uint32, json_name: "experienceReward")
field(:item_rewards, 6,
repeated: true,
type: Gateway.Serialization.ItemReward,
json_name: "itemRewards"
)

field(:unit_rewards, 7,
repeated: true,
type: Gateway.Serialization.UnitReward,
json_name: "unitRewards"
)

field(:attempt_cost, 7,
field(:experience_reward, 9, type: :uint32, json_name: "experienceReward")

field(:attempt_cost, 10,
repeated: true,
type: Gateway.Serialization.CurrencyCost,
json_name: "attemptCost"
Expand All @@ -674,6 +686,24 @@ defmodule Gateway.Serialization.CurrencyReward do
field(:amount, 3, type: :uint64)
end

defmodule Gateway.Serialization.ItemReward do
@moduledoc false

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

field(:item_template_name, 1, type: :string, json_name: "itemTemplateName")
field(:level, 2, type: :uint32)
end

defmodule Gateway.Serialization.UnitReward do
@moduledoc false

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

field(:character_name, 1, type: :string, json_name: "characterName")
field(:rank, 4, type: :uint32)
end

defmodule Gateway.Serialization.AfkRewards do
@moduledoc false

Expand Down
16 changes: 14 additions & 2 deletions apps/serialization/gateway.proto
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,10 @@ syntax = "proto3";
uint32 level_number = 3;
repeated Unit units = 4;
repeated CurrencyReward currency_rewards = 5;
uint32 experience_reward = 6;
repeated CurrencyCost attempt_cost = 7;
repeated ItemReward item_rewards = 6;
repeated UnitReward unit_rewards = 7;
uint32 experience_reward = 9;
repeated CurrencyCost attempt_cost = 10;
uint32 max_units = 8;
}

Expand All @@ -314,6 +316,16 @@ syntax = "proto3";
uint64 amount = 3;
}

message ItemReward {
string item_template_name = 1;
uint32 level = 2;
}

message UnitReward {
string character_name = 1;
uint32 rank = 4;
Copy link
Contributor

Choose a reason for hiding this comment

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

why 4?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

:P

}

message AfkRewards {
repeated AfkReward afk_rewards = 1;
}
Expand Down
33 changes: 31 additions & 2 deletions priv/repo/seeds.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
alias GameBackend.{Gacha, Repo, Users, Utils}
alias GameBackend.Campaigns.Rewards.AfkRewardRate
alias GameBackend.Campaigns.Rewards.{AfkRewardRate, ItemReward, UnitReward}
alias GameBackend.Campaigns
alias GameBackend.CurseOfMirra.Config
alias GameBackend.Items
alias GameBackend.Users.{KalineTreeLevel, Upgrade}
alias GameBackend.Units.Characters
alias GameBackend.CurseOfMirra.Config

curse_of_mirra_id = Utils.get_game_id(:curse_of_mirra)
champions_of_mirra_id = Utils.get_game_id(:champions_of_mirra)
Expand Down Expand Up @@ -224,6 +226,33 @@ Champions.Config.import_dungeon_settlement_levels_config()

Champions.Config.import_dungeon_levels_config()

# Add sample unit/item rewards to first level of main campaign
# Should be removed when we implement unit/item rewards on campaign config import [CHoM-#494]
main_super_campaign =
Campaigns.get_super_campaign_by_name_and_game("Main Campaign", champions_of_mirra_id)

main_campaign_1 =
Enum.find(main_super_campaign.campaigns, &(&1.campaign_number == 1)) |> Repo.preload(:levels)

level_1 = Enum.find(main_campaign_1.levels, &(&1.level_number == 1))

%UnitReward{}
|> UnitReward.changeset(%{
character_id: Characters.get_characters(champions_of_mirra_id) |> hd() |> Map.get(:id),
level_id: level_1.id,
amount: 1,
rank: 5
})
|> Repo.insert!()

%ItemReward{}
|> ItemReward.changeset(%{
item_template_id: Items.get_item_templates(champions_of_mirra_id) |> hd() |> Map.get(:id),
level_id: level_1.id,
amount: 1
})
|> Repo.insert!()

##################### CURSE OF MIRRA #####################
# Insert characters
Config.get_characters_config()
Expand Down
Loading