Skip to content

Commit

Permalink
add safe mint (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpinones authored Nov 5, 2024
1 parent 7a8ff69 commit 755ab80
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/interfaces/erc721.cairo
Original file line number Diff line number Diff line change
@@ -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;
}
7 changes: 7 additions & 0 deletions src/models/data/beast.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,10 @@ struct PlayerBeast {
game_id: u32,
energy: u8
}

#[derive(Copy, Drop, Serde)]
struct BeastStats {
tier: u8,
level: u8,
beast_id: u8
}
30 changes: 18 additions & 12 deletions src/models/status/round/beast.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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';
Expand Down Expand Up @@ -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 }));

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/utils/adventurer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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>()
}

0 comments on commit 755ab80

Please sign in to comment.