From 755ab8046277b7c5acd0be5a78861263ca3b28de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Pi=C3=B1ones?= <30808181+dpinones@users.noreply.github.com> Date: Tue, 5 Nov 2024 15:46:46 -0300 Subject: [PATCH] add safe mint (#34) --- src/interfaces/erc721.cairo | 5 +++++ src/models/data/beast.cairo | 7 +++++++ src/models/status/round/beast.cairo | 30 +++++++++++++++++------------ src/utils/adventurer.cairo | 4 ++++ 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/interfaces/erc721.cairo b/src/interfaces/erc721.cairo index ae931fa..8657c81 100644 --- a/src/interfaces/erc721.cairo +++ b/src/interfaces/erc721.cairo @@ -1,7 +1,12 @@ use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; use starknet::ContractAddress; +use jokers_of_neon::models::data::beast::BeastStats; #[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 get_owner(world: @IWorldDispatcher, beast_stats: BeastStats) -> ContractAddress; } diff --git a/src/models/data/beast.cairo b/src/models/data/beast.cairo index d3c210e..bde91b9 100644 --- a/src/models/data/beast.cairo +++ b/src/models/data/beast.cairo @@ -37,3 +37,10 @@ struct PlayerBeast { game_id: u32, energy: u8 } + +#[derive(Copy, Drop, Serde)] +struct BeastStats { + tier: u8, + level: u8, + beast_id: u8 +} diff --git a/src/models/status/round/beast.cairo b/src/models/status/round/beast.cairo index d48157b..e8b4b48 100644 --- a/src/models/status/round/beast.cairo +++ b/src/models/status/round/beast.cairo @@ -4,7 +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::models::data::beast::{ - GameModeBeast, GameModeBeastStore, Beast, BeastStore, PlayerBeast, PlayerBeastStore, TypeBeast + GameModeBeast, GameModeBeastStore, Beast, BeastStore, PlayerBeast, PlayerBeastStore, TypeBeast, BeastStats }; use jokers_of_neon::models::data::events::{PlayWinGameEvent, PlayGameOverEvent, BeastAttack, PlayerAttack}; use jokers_of_neon::models::data::game_deck::{GameDeckImpl, GameDeck, GameDeckStore}; @@ -13,15 +13,17 @@ 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::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; use jokers_of_neon::utils::rage::is_rage_card_active; use jokers_of_neon::utils::random::{Random, RandomImpl, RandomTrait}; -use starknet::{ContractAddress, get_caller_address, ClassHash}; +use starknet::{ContractAddress, get_caller_address, ClassHash, get_tx_info}; mod errors { const GAME_NOT_FOUND: felt252 = 'Game: game not found'; @@ -76,8 +78,6 @@ impl BeastImpl of BeastTrait { assert(player_beast.energy >= game_mode_beast.cost_play, errors::PLAYER_WITHOUT_ENERGY); - let rage_round = RageRoundStore::get(world, game_id); - let attack = play(world, ref game, @cards_index, @modifiers_index); emit!(world, (PlayerAttack { player: get_caller_address(), attack })); @@ -105,15 +105,21 @@ impl BeastImpl of BeastTrait { game.substate = GameSubState::CREATE_REWARD; - if is_rage_card_active(@rage_round, RAGE_CARD_DIMINISHED_HOLD) { - // return the cards to the deck - game.len_hand += 2; + + 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() + }; + let erc721_dispatcher = IERC721SystemDispatcher { contract_address: NFT_ADDRESS_MAINNET() }; + let owner = erc721_dispatcher.get_owner(beast_stats); + if owner.is_zero() { + erc721_dispatcher.safe_mint(get_caller_address(), beast_stats); + } + } } - let (_, rage_system_address) = match world.resource(selector_from_tag!("jokers_of_neon-rage_system")) { - Contract((class_hash, contract_address)) => Option::Some((class_hash, contract_address)), - _ => Option::None - }.unwrap(); - IRageSystemDispatcher { contract_address: rage_system_address.try_into().unwrap() }.calculate(game.id); } else { let mut cards = array![]; let mut idx = 0; diff --git a/src/utils/adventurer.cairo b/src/utils/adventurer.cairo index ab44f57..1674891 100644 --- a/src/utils/adventurer.cairo +++ b/src/utils/adventurer.cairo @@ -12,3 +12,7 @@ fn is_mainnet(chain_id: felt252) -> bool { fn ADVENTURER_ADDRESS_MAINNET() -> ContractAddress { contract_address_const::<0x018108b32cea514a78ef1b0e4a0753e855cdf620bc0565202c02456f618c4dc4>() } + +fn NFT_ADDRESS_MAINNET() -> ContractAddress { + contract_address_const::<0x07268fcf96383f8691b91ba758cc8fefe0844146f0557909345b841fb1de042f>() +}