Skip to content

Commit

Permalink
JON-569: Random tier beast (#35)
Browse files Browse the repository at this point in the history
* random tear + scarb fmt

* add function for rarity

* replace tier order
  • Loading branch information
dubzn authored Nov 6, 2024
1 parent d98fcd2 commit e9b95d1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
6 changes: 2 additions & 4 deletions src/interfaces/erc721.cairo
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
use starknet::ContractAddress;
use jokers_of_neon::models::data::beast::BeastStats;
use starknet::ContractAddress;

#[dojo::interface]
trait IERC721System {
fn owner_of(world: @IWorldDispatcher, token_id: u256) -> ContractAddress;
fn safe_mint(
ref world: IWorldDispatcher, recipient: ContractAddress, beast_stats: BeastStats
);
fn safe_mint(ref world: IWorldDispatcher, recipient: ContractAddress, beast_stats: BeastStats);
fn get_owner(world: @IWorldDispatcher, beast_stats: BeastStats) -> ContractAddress;
}
44 changes: 31 additions & 13 deletions src/models/status/round/beast.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
use jokers_of_neon::constants::beast::{all_beast, beast_loot_survivor, is_loot_survivor_beast};
use jokers_of_neon::constants::card::INVALID_CARD;
use jokers_of_neon::constants::reward::{REWARD_HP_POTION, REWARD_SPECIAL_CARDS};
use jokers_of_neon::interfaces::erc721::{IERC721SystemDispatcher, IERC721SystemDispatcherTrait};
use jokers_of_neon::models::data::beast::{
GameModeBeast, GameModeBeastStore, Beast, BeastStore, PlayerBeast, PlayerBeastStore, TypeBeast, BeastStats
};
Expand All @@ -15,12 +16,11 @@ use jokers_of_neon::models::status::game::rage::{RageRound, RageRoundStore};
use jokers_of_neon::models::status::round::current_hand_card::{CurrentHandCard, CurrentHandCardTrait};
use jokers_of_neon::store::{Store, StoreTrait};
use jokers_of_neon::systems::rage_system::{IRageSystemDispatcher, IRageSystemDispatcherTrait};
use jokers_of_neon::interfaces::erc721::{IERC721SystemDispatcher, IERC721SystemDispatcherTrait};
use jokers_of_neon::utils::adventurer::{is_mainnet, NFT_ADDRESS_MAINNET};
use jokers_of_neon::utils::constants::{
RAGE_CARD_DIMINISHED_HOLD, RAGE_CARD_SILENT_JOKERS, RAGE_CARD_SILENT_HEARTS, RAGE_CARD_SILENT_CLUBS,
RAGE_CARD_SILENT_DIAMONDS, RAGE_CARD_SILENT_SPADES, RAGE_CARD_ZERO_WASTE, is_neon_card, is_modifier_card
};
use jokers_of_neon::utils::adventurer::{is_mainnet, NFT_ADDRESS_MAINNET};

use jokers_of_neon::utils::game::play;
use jokers_of_neon::utils::level::create_level;
Expand Down Expand Up @@ -109,13 +109,10 @@ impl BeastImpl of BeastTrait {
game.substate = GameSubState::CREATE_REWARD;
RewardTrait::beast(world, game_id);


if is_mainnet(get_tx_info().unbox().chain_id) {
if !is_loot_survivor_beast(beast.beast_id) {
let beast_stats = BeastStats {
tier: beast.tier,
level: beast.level,
beast_id: beast.beast_id.try_into().unwrap()
tier: beast.tier, level: beast.level, beast_id: beast.beast_id.try_into().unwrap()
};
let erc721_dispatcher = IERC721SystemDispatcher { contract_address: NFT_ADDRESS_MAINNET() };
let owner = erc721_dispatcher.get_owner(beast_stats);
Expand Down Expand Up @@ -288,7 +285,7 @@ fn _create_beast(world: IWorldDispatcher, game_id: u32, level: u8) {
} else {
*all_beast()[randomizer.between::<u32>(0, all_beast().len() - 1)]
};
let (tier, health, attack) = _generate_stats(level, beast_id);
let (tier, health, attack) = _generate_stats(level, beast_id, ref randomizer);
let type_beast = if is_loot_survivor_beast(beast_id) {
TypeBeast::LOOT_SURVIVOR
} else {
Expand All @@ -300,18 +297,19 @@ fn _create_beast(world: IWorldDispatcher, game_id: u32, level: u8) {
}

// tier, health, attack
fn _generate_stats(level: u8, beast_id: u32) -> (u8, u32, u32) {
fn _generate_stats(level: u8, beast_id: u32, ref randomizer: Random) -> (u8, u32, u32) {
let random_tier = _obtain_random_tier(ref randomizer);
let mut stats = (0, 0, 0);
if level <= 4 {
stats = (5, _calculate_beast_hp(level), 10);
stats = (random_tier, _calculate_beast_hp(level), 10);
} else if level <= 8 {
stats = (4, _calculate_beast_hp(level), 20);
stats = (random_tier, _calculate_beast_hp(level), 20);
} else if level <= 12 {
stats = (3, _calculate_beast_hp(level), 30);
stats = (random_tier, _calculate_beast_hp(level), 30);
} else if level <= 16 {
stats = (2, _calculate_beast_hp(level), 40);
stats = (random_tier, _calculate_beast_hp(level), 40);
} else {
stats = (1, _calculate_beast_hp(level), 50);
stats = (random_tier, _calculate_beast_hp(level), 50);
}

if beast_id >= 101 && beast_id <= 108 {
Expand All @@ -322,6 +320,26 @@ fn _generate_stats(level: u8, beast_id: u32) -> (u8, u32, u32) {
}
}

// Tier 1: 00 - 40
// Tier 2: 41 - 70
// Tier 3: 71 - 85
// Tier 4: 86 - 95
// Tier 5: 96 - 100
fn _obtain_random_tier(ref randomizer: Random) -> u8 {
let random = randomizer.between::<u32>(1, 100);
if random <= 40 {
5
} else if random <= 70 {
4
} else if random <= 85 {
3
} else if random <= 95 {
2
} else {
1
}
}

fn _calculate_beast_hp(level: u8) -> u32 {
if level <= 2 {
300 * level.into()
Expand Down
5 changes: 1 addition & 4 deletions src/systems/game_system.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,7 @@ mod game_system {
let mut game = GameStore::get(world, game_id);
assert(game.owner.is_non_zero(), errors::GAME_NOT_FOUND);
assert(game.owner == get_caller_address(), errors::CALLER_NOT_OWNER);
assert(
game.substate == GameSubState::UNPASSED_OBSTACLE,
errors::WRONG_SUBSTATE_UNPASSED_OBSTABLE
);
assert(game.substate == GameSubState::UNPASSED_OBSTACLE, errors::WRONG_SUBSTATE_UNPASSED_OBSTABLE);
let mut store = StoreTrait::new(world);
game.substate = GameSubState::CREATE_LEVEL;
store.set_game(game);
Expand Down

0 comments on commit e9b95d1

Please sign in to comment.