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-431] Add currency cost to levels #617

Merged
merged 14 commits into from
May 21, 2024
Merged

Conversation

ncontinanza
Copy link
Contributor

@ncontinanza ncontinanza commented May 14, 2024

Closes: https://github.com/lambdaclass/champions_of_mirra/issues/431

Associated PR: https://github.com/lambdaclass/champions_of_mirra/issues/463

Motivation

We need to be able to charge the user with a currency cost in order to get access to fight some of the levels. For example, the Dungeon levels will have a cost in Shovels.

Summary of changes

  • Add an embedded array of CurrencyCost to the Level schema.
  • Before starting a battle, check that the user can afford it.
  • Substract the level's cost in currency when fighting the battle.

How to test it?

Run the backend and start the client in the main branch. For starters, everything should be working as usual, when none of the existing levels have a CurrencyCost associated.

Add this line to the map used for level creation in seeds.ex: currency_costs: [%CurrencyCost{currency_id: gold_currency.id, amount: 13}],
Also, you'll need to add this alias to that file: alias GameBackend.Users.Currencies.CurrencyCost

You should end up with something like this:

levels =
  Enum.flat_map(Enum.with_index(rules, 1), fn {campaign_rules, campaign_index} ->
    {_, campaign} =
      Campaigns.insert_campaign(
        %{
          game_id: champions_of_mirra_id,
          super_campaign_id: super_campaign.id,
          campaign_number: campaign_index
        },
        returning: true
      )

    Enum.map(1..campaign_rules.length, fn level_index ->
      %{
        game_id: champions_of_mirra_id,
        campaign_id: campaign.id,
        level_number: level_index,
        experience_reward: 100 * level_index,
        attempt_cost: [%CurrencyCost{currency_id: gold_currency.id, amount: 13}],
        inserted_at: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second),
        updated_at: NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
      }
    end)
  end)

Now all the levels cost 13 Gold. Before fighting a new level, check your current amount of gold. If you fight the level and lose the battle, your amount of gold should have decreased by 13. If you won, your gold amount should now be previous amount + level reward - level cost.

Checklist

  • Tested the changes locally.
  • Reviewed the changes on GitHub, line by line.
  • This change requires new documentation.
    • Documentation has been added/updated.

@ncontinanza ncontinanza added the autobattler AFK Gacha Autobattler game label May 14, 2024
@ncontinanza ncontinanza marked this pull request as ready for review May 16, 2024 20:54
apps/champions/lib/champions/battle.ex Outdated Show resolved Hide resolved
priv/repo/seeds.exs Outdated Show resolved Hide resolved
priv/repo/seeds.exs Outdated Show resolved Hide resolved
apps/serialization/gateway.proto Outdated Show resolved Hide resolved
lotuuu
lotuuu previously approved these changes May 21, 2024
Copy link
Contributor

@lotuuu lotuuu left a comment

Choose a reason for hiding this comment

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

Just as a nitpicky comment, I like the uncountable version of cost better for the cost of playing a level. Instead of attempt_costs, i'd go for attempt_cost.

Also, the description is now outdated as it uses the previous "currency_costs" attribute.

Lastly, is there an issue already in the client for implementing handling of "cant afford" errors? Because of course right now this breaks in the client

@lotuuu lotuuu merged commit c78cff2 into main May 21, 2024
1 check passed
@lotuuu lotuuu deleted the gh-431-add-currency-cost-to-level branch May 21, 2024 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autobattler AFK Gacha Autobattler game
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DUNGEON] Add CurrencyCost to Level
3 participants