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

Gateway tests #333

Merged
merged 90 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
253f42c
General fixes to Champions.Units
lotuuu Feb 23, 2024
24414fb
Add doc to unselect_unit.ex
lotuuu Feb 23, 2024
a6b2da1
Refactor units
lotuuu Feb 23, 2024
f112fc4
Create GameBackend.Transaction
lotuuu Feb 23, 2024
39b8e6d
Remove Jason.Encoder derive
lotuuu Feb 23, 2024
59714c5
Add CurrencyCost
lotuuu Feb 23, 2024
0cee293
Add Currency operations
lotuuu Feb 23, 2024
f3d7a41
Add game_id util
lotuuu Feb 23, 2024
406c7f7
Set slots range to 1..6
lotuuu Feb 23, 2024
6e0d89e
Return ok tuple in register user
lotuuu Feb 26, 2024
e9d2080
Update unit.ex
lotuuu Feb 26, 2024
a403f23
Add add_level to GameBackend.Units
lotuuu Feb 26, 2024
a3c85d8
Implement Champions unit level up
lotuuu Feb 26, 2024
782b15b
Run format after protos
lotuuu Feb 26, 2024
6b84d44
Add gateway commands for new functionalities
lotuuu Feb 26, 2024
49c4a1e
Format Champions.Units
lotuuu Feb 26, 2024
5805b10
Add doc
lotuuu Feb 26, 2024
7b3a5aa
Add ChampionsSocketHandler handlers
lotuuu Feb 26, 2024
4988803
Update SocketTester
lotuuu Feb 26, 2024
bb2b729
Fix rarity type
lotuuu Feb 26, 2024
062905c
Rename unit_level to level and level assoc to campaign_level
lotuuu Feb 26, 2024
2519f22
Revert "Rename unit_level to level and level assoc to campaign_level"
lotuuu Feb 26, 2024
0263347
Rename unit_level to level and level assoc to campaign_level
lotuuu Feb 26, 2024
2a12d4c
Rename update_selected
lotuuu Feb 26, 2024
8ccbec9
Fix selection
lotuuu Feb 26, 2024
9d76bae
Merge branch 'main' into unit-level-up
lotuuu Feb 26, 2024
d967e7e
Merge branch 'unit-level-up' into rename-unit-attributes
lotuuu Feb 26, 2024
79ce702
Format
lotuuu Feb 26, 2024
b576dc4
Merge branch 'unit-level-up' into unit-tier-up
lotuuu Feb 26, 2024
7201a8d
Add rank to unit
lotuuu Feb 26, 2024
efc1370
Add add_tier to GameBackend.Units
lotuuu Feb 26, 2024
cc2f97d
Add tier_up to Champions.Units
lotuuu Feb 26, 2024
28fac21
Fix item doc
lotuuu Feb 26, 2024
436071e
Fix units calculate_tier_up_cost
lotuuu Feb 26, 2024
9e90e1e
Fix doc
lotuuu Feb 26, 2024
db5daa2
Update substract_currencies doc
lotuuu Feb 26, 2024
b6d5a9f
Add example
lotuuu Feb 26, 2024
60d96c2
Update substract_currencies docs
lotuuu Feb 26, 2024
a92ea1b
Remove empty line
lotuuu Feb 26, 2024
fdd2493
Allow slot 6
lotuuu Feb 26, 2024
b6f0051
Merge branch 'unit-level-up' into rename-unit-attributes
lotuuu Feb 26, 2024
d373506
Merge branch 'rename-unit-attributes' into unit-tier-up
lotuuu Feb 26, 2024
ee5e782
Fix seeds
lotuuu Feb 26, 2024
df24dea
Add messages
lotuuu Feb 26, 2024
ac2da33
Add fusion
lotuuu Feb 26, 2024
3a78ea6
Move rank to the back of the cast list, do not require tier
lotuuu Feb 26, 2024
3bf3026
Fix SocketTester
lotuuu Feb 26, 2024
1a158c1
Add alias
lotuuu Feb 26, 2024
1744f98
Add messages
lotuuu Feb 26, 2024
2c2c454
Fix seeds
lotuuu Feb 26, 2024
3b5929c
Add get_units_by_ids
lotuuu Feb 26, 2024
69cc039
Default to 0 in get_amount_of_currency
lotuuu Feb 26, 2024
58f36a2
Add useful currencies functions
lotuuu Feb 26, 2024
a7aa5b4
Add tests
lotuuu Feb 26, 2024
1e2f0fc
Add doc
lotuuu Feb 26, 2024
840fbc5
Restore delete_units
lotuuu Feb 27, 2024
6c4b850
Update apps/game_backend/lib/game_backend/units.ex
lotuuu Feb 29, 2024
1c30247
Check that target unit is not in consumed list
lotuuu Feb 29, 2024
8c6908d
Add rank to add_sample_units
lotuuu Feb 29, 2024
097c0e6
Refactor get_amount_of_currency_by_name
lotuuu Feb 29, 2024
68028f1
Refactor validate_units
lotuuu Feb 29, 2024
f5ace52
Merge branch 'main' into unit-tier-up
lotuuu Feb 29, 2024
ab15a72
Merge branch 'unit-tier-up' into unit-fusion
lotuuu Feb 29, 2024
5b9fd57
Merge branch 'unit-fusion' into gateway-tests
lotuuu Feb 29, 2024
90770c9
Remove unnecesary nested function
lotuuu Mar 1, 2024
2bc875c
Merge branch 'main' into unit-tier-up
lotuuu Mar 1, 2024
f6d458a
Merge branch 'unit-tier-up' into unit-fusion
lotuuu Mar 1, 2024
dd7e636
Merge branch 'unit-fusion' into gateway-tests
lotuuu Mar 1, 2024
bb4b2b1
Changed inconclusive comment to TODO with issue number
lotuuu Mar 1, 2024
efa0056
Merge branch 'main' into unit-tier-up
lotuuu Mar 5, 2024
e7844b3
Merge branch 'unit-tier-up' into unit-fusion
lotuuu Mar 5, 2024
629865b
Merge branch 'unit-fusion' into gateway-tests
lotuuu Mar 5, 2024
dae82a0
Rename validate_consumed_units to meets_fuse_requirements?
lotuuu Mar 6, 2024
66c4dbe
Merge branch 'main' into unit-fusion
lotuuu Mar 7, 2024
4b4b59c
Merge branch 'unit-fusion' into gateway-tests
lotuuu Mar 7, 2024
01097e4
Merge branch 'main' into unit-fusion
lotuuu Mar 7, 2024
e3b28f4
Merge branch 'unit-fusion' into gateway-tests
lotuuu Mar 7, 2024
f0c2f23
Fix tests
lotuuu Mar 7, 2024
ebeeb54
Format tests
lotuuu Mar 7, 2024
fed37f2
Merge branch 'main' into gateway-tests
lotuuu Mar 7, 2024
b66b1fa
Check unselected_unit id
lotuuu Mar 8, 2024
fd1a454
Refactor create_units_to_consume
lotuuu Mar 8, 2024
17386b3
Add username validation
lotuuu Mar 8, 2024
d2cc913
Remove unnecesary calls and add an assertion
lotuuu Mar 8, 2024
1763781
Format
lotuuu Mar 8, 2024
77dc5c2
User username variable instead of string
lotuuu Mar 8, 2024
06e7a53
Add more assertions
lotuuu Mar 11, 2024
fa435f9
Comment unused code
lotuuu Mar 11, 2024
e0a22ec
Merge branch 'main' into gateway-tests
lotuuu Mar 11, 2024
1e895e1
Fix seeds
lotuuu Mar 11, 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
2 changes: 1 addition & 1 deletion apps/game_backend/lib/game_backend/units.ex
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ defmodule GameBackend.Units do
def delete_unit(id), do: Repo.get(Unit, id) |> delete_unit()

@doc """
Deletes all units in a given list.
Deletes all units in a list by ids.
"""
def delete_units(unit_ids), do: Repo.delete_all(from(u in Unit, where: u.id in ^unit_ids))

Expand Down
266 changes: 266 additions & 0 deletions apps/gateway/test/champions_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
defmodule Gateway.Test.Champions do
@moduledoc """
Test for Champions of Mirra messages.
"""
use ExUnit.Case

alias Champions.{Units, Users, Utils}
alias GameBackend.Repo
alias GameBackend.Users.Currencies.CurrencyCost
alias GameBackend.Users.Currencies

alias Gateway.Serialization.{
Currency,
Error,
Unit,
UnitAndCurrencies,
User,
UserCurrency,
WebSocketResponse
}

alias Gateway.SocketTester

setup_all do
# Start Phoenix endpoint
{:ok, _} =
Plug.Cowboy.http(Gateway.Endpoint, [],
ip: {127, 0, 0, 1},
port: 4001,
dispatch: [
_: [{"/2", Gateway.ChampionsSocketHandler, []}]
]
)

:ok
end

setup do
{:ok, socket_tester} = SocketTester.start_link()

{:ok, %{socket_tester: socket_tester}}
end

describe "users" do
test "users", %{socket_tester: socket_tester} do
username = "Username"

# Create our user
:ok = SocketTester.create_user(socket_tester, username)
fetch_last_message(socket_tester)
%WebSocketResponse{response_type: {:user, %User{} = user}} = get_last_message()

assert user.username == username

# Creating another user with the same name fails
:ok = SocketTester.create_user(socket_tester, username)
fetch_last_message(socket_tester)
assert_receive %WebSocketResponse{response_type: {:error, %Error{reason: "username_taken"}}}

# Get user by name
:ok = SocketTester.get_user_by_username(socket_tester, username)
fetch_last_message(socket_tester)
assert_receive %WebSocketResponse{response_type: {:user, ^user}}
agustinesco marked this conversation as resolved.
Show resolved Hide resolved
end
end

describe "units" do
test "unit selection", %{socket_tester: socket_tester} do
{:ok, user} = Users.register("SelectUser")

[unit_to_unselect | _] = user.units
slot = unit_to_unselect.slot

# Unit is selected by default (this will change when we remove sample data in user creation)
assert unit_to_unselect.selected

# Unselect the unit
:ok = SocketTester.unselect_unit(socket_tester, user.id, unit_to_unselect.id)
fetch_last_message(socket_tester)
%WebSocketResponse{response_type: {:unit, %Unit{} = unselected_unit}} = get_last_message()

assert not unselected_unit.selected
# Protobuf doesn't support nil values, returns zero instead
assert unselected_unit.slot == 0
assert unselected_unit.id == unit_to_unselect.id

:ok = SocketTester.select_unit(socket_tester, user.id, unselected_unit.id, slot)
fetch_last_message(socket_tester)
%WebSocketResponse{response_type: {:unit, %Unit{} = selected_unit}} = get_last_message()

assert selected_unit.selected
assert selected_unit.slot == slot
end

test "unit progression", %{socket_tester: socket_tester} do
muflus = GameBackend.Units.Characters.get_character_by_name("Muflus")
{:ok, user} = Users.register("LevelUpUser")
Currencies.add_currency_by_name!(user.id, "Gold", 9999)

{:ok, unit} =
GameBackend.Units.insert_unit(%{
user_id: user.id,
level: 19,
tier: 1,
rank: 2,
selected: false,
character_id: muflus.id
})

gold = Currencies.get_amount_of_currency_by_name(user.id, "Gold")
level = unit.level

#### LevelUpUnit
[%CurrencyCost{currency_id: _gold_id, amount: level_up_cost}] = Units.calculate_level_up_cost(unit)

:ok = SocketTester.level_up_unit(socket_tester, user.id, unit.id)
fetch_last_message(socket_tester)

%WebSocketResponse{
response_type: {:unit_and_currencies, %UnitAndCurrencies{unit: unit, user_currency: [user_currency]}}
} = get_last_message()

assert unit.level == level + 1
assert user_currency.currency.name == "Gold"
assert user_currency.amount == gold - level_up_cost

# Cannot level up because unit is level 20 with tier 1
assert unit.level == 20
assert unit.tier == 1
:ok = SocketTester.level_up_unit(socket_tester, user.id, unit.id)
fetch_last_message(socket_tester)
assert_receive %WebSocketResponse{response_type: {:error, %Error{reason: "cant_level_up"}}}

#### TierUpUnit

user_gold = Currencies.get_amount_of_currency_by_name(user.id, "Gold")
user_gems = Currencies.get_amount_of_currency_by_name(user.id, "Gems")

[%CurrencyCost{currency_id: _gold_id, amount: gold_cost}, %CurrencyCost{currency_id: _gems_id, amount: gems_cost}] =
Units.calculate_tier_up_cost(unit)
agustinesco marked this conversation as resolved.
Show resolved Hide resolved

:ok = SocketTester.tier_up_unit(socket_tester, user.id, unit.id)
fetch_last_message(socket_tester)

%WebSocketResponse{
response_type: {:unit_and_currencies, %UnitAndCurrencies{unit: unit, user_currency: user_currencies}}
} = get_last_message()

user_currencies =
Enum.into(user_currencies, %{}, fn %UserCurrency{
currency: %Currency{name: name},
amount: amount
} ->
{name, amount}
end)

assert unit.level == 20
assert unit.tier == 2
assert user_currencies["Gold"] == user_gold - gold_cost
assert user_currencies["Gems"] == user_gems - gems_cost

# Check that we can now LevelUpUnit
:ok = SocketTester.level_up_unit(socket_tester, user.id, unit.id)
fetch_last_message(socket_tester)

%WebSocketResponse{
response_type: {:unit_and_currencies, %UnitAndCurrencies{unit: unit}}
} = get_last_message()

assert unit.level == level + 2

#### Rank up (FuseUnit)

{:ok, unit} =
GameBackend.Units.insert_unit(%{
user_id: user.id,
level: 100,
tier: 5,
rank: Units.get_rank(:star5),
selected: false,
character_id: muflus.id
})

unit = Repo.preload(unit, [:character])

rank = unit.rank
user_units_count = user.id |> GameBackend.Units.get_units() |> Enum.count()

# Add to-be-consumed units

{:ok, same_faction_character} =
GameBackend.Units.Characters.insert_character(%{
game_id: Utils.game_id(),
active: true,
name: "SameFactionUnit",
faction: muflus.faction,
quality: Champions.Units.get_quality(:rare)
})

# We will need three 5* Muflus and two i2 of the same faction
# For the same faction, we will do one of each unit.
units_to_consume = create_units_to_consume(user, muflus, same_faction_character)

# Characters need a certain quality to rank up
assert unit.character.quality == Units.get_quality(:epic)

# Check that we cant TierUpUnit again without ranking up
:ok = SocketTester.tier_up_unit(socket_tester, user.id, unit.id)
fetch_last_message(socket_tester)

%WebSocketResponse{
response_type: {:error, %Error{reason: "cant_tier_up"}}
} = get_last_message()

# FuseUnit
:ok = SocketTester.fuse_unit(socket_tester, user.id, unit.id, units_to_consume)
fetch_last_message(socket_tester)

%WebSocketResponse{
response_type: {:unit, %Unit{} = unit}
} = get_last_message()

assert unit.rank == rank + 1
assert user_units_count == user.id |> GameBackend.Units.get_units() |> Enum.count()
end
end

defp get_last_message() do
receive do
message ->
message
after
5000 ->
raise "No message"
end
end

defp fetch_last_message(socket_tester) do
:timer.sleep(50)
send(socket_tester, {:last_message, self()})
end

defp create_units_to_consume(user, same_character, same_faction) do
params = %{
user_id: user.id,
level: 100,
tier: 5,
rank: Units.get_rank(:star5),
selected: false
}

same_character_ids =
Enum.map(1..3, fn _ ->
{:ok, unit} = params |> Map.put(:character_id, same_character.id) |> GameBackend.Units.insert_unit()
unit.id
end)

same_faction_ids =
Enum.map(1..2, fn _ ->
{:ok, unit} = params |> Map.put(:character_id, same_faction.id) |> GameBackend.Units.insert_unit()
unit.id
end)

same_character_ids ++ same_faction_ids
end
end
12 changes: 0 additions & 12 deletions apps/gateway/test/gateway_web/controllers/error_json_test.exs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule SocketTester do
defmodule Gateway.SocketTester do
@moduledoc """
Module for manually testing the CoM websocket.
Logs received messages and keeps the last one received in the state.
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule MirraBackend.MixProject do
setup: ["deps.get", "ecto.setup"],
"ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
"ecto.reset": ["ecto.drop", "ecto.setup"],
test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"]
test: ["ecto.reset --quiet", "ecto.migrate --quiet", "run priv/repo/seeds.exs", "test"]
]
end

Expand Down
2 changes: 1 addition & 1 deletion priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Items.insert_item_template(%{

{:ok, gold_currency} = Users.Currencies.insert_currency(%{game_id: champions_of_mirra_id, name: "Gold"})
{:ok, _gems_currency} = Users.Currencies.insert_currency(%{game_id: champions_of_mirra_id, name: "Gems"})
{:ok, scrolls_currency} = Users.Currencies.insert_currency(%{game_id: champions_of_mirra_id, name: "Summon Scrolls"})
{:ok, _scrolls_currency} = Users.Currencies.insert_currency(%{game_id: champions_of_mirra_id, name: "Summon Scrolls"})

######################
# Campaigns creation #
Expand Down
Loading