-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JOPN-464: Integrate adventurer (#20)
* wip integrate adventurer * add mock erc721 * add interface * add interface * add use_adventurer * integrate aventurer * update state
- Loading branch information
Showing
13 changed files
with
2,990 additions
and
1,319 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; | ||
use starknet::ContractAddress; | ||
|
||
#[dojo::interface] | ||
trait IERC721System { | ||
fn owner_of(world: @IWorldDispatcher, token_id: u256) -> ContractAddress; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; | ||
#[derive(Drop, Copy, PartialEq, Serde)] | ||
struct Item { // 21 storage bits | ||
id: u8, // 7 bits | ||
xp: u16, // 9 bits | ||
} | ||
|
||
#[derive(Drop, Copy, Serde, PartialEq)] | ||
struct Equipment { // 128 bits | ||
weapon: Item, | ||
chest: Item, | ||
head: Item, | ||
waist: Item, // 16 bits per item | ||
foot: Item, | ||
hand: Item, | ||
neck: Item, | ||
ring: Item, | ||
} | ||
|
||
#[derive(Drop, Copy, Serde, PartialEq)] | ||
struct Stats { // 30 bits total | ||
strength: u8, | ||
dexterity: u8, | ||
vitality: u8, // 5 bits per stat | ||
intelligence: u8, | ||
wisdom: u8, | ||
charisma: u8, | ||
luck: u8 // dynamically generated, not stored. | ||
} | ||
|
||
#[derive(Drop, Copy, Serde)] | ||
struct Adventurer { | ||
health: u16, // 10 bits | ||
xp: u16, // 15 bits | ||
gold: u16, // 9 bits | ||
beast_health: u16, // 10 bits | ||
stat_upgrades_available: u8, // 4 bits | ||
stats: Stats, // 30 bits | ||
equipment: Equipment, // 128 bits | ||
battle_action_count: u8, // 8 bits | ||
mutated: bool, // not packed | ||
awaiting_item_specials: bool, // not packed | ||
} | ||
|
||
#[dojo::interface] | ||
trait ILootSurvivorSystem { | ||
fn get_adventurer(world: @IWorldDispatcher, adventurer_id: felt252) -> Adventurer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use starknet::ContractAddress; | ||
|
||
#[derive(Copy, Drop, IntrospectPacked, Serde)] | ||
#[dojo::model] | ||
struct AdventurerConsumed { | ||
#[key] | ||
adventurer_id: u32, | ||
owner: ContractAddress, | ||
consumed: bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use core::num::traits::{Sqrt}; | ||
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; | ||
|
||
use jokers_of_neon::models::status::game::game::Game; | ||
use jokers_of_neon::interfaces::erc721::{IERC721SystemDispatcher, IERC721SystemDispatcherTrait}; | ||
use jokers_of_neon::interfaces::loot_survivor::{ | ||
ILootSurvivorSystemDispatcher, ILootSurvivorSystemDispatcherTrait, Adventurer | ||
}; | ||
use jokers_of_neon::models::data::adventurer::{AdventurerConsumed, AdventurerConsumedStore}; | ||
use jokers_of_neon::utils::adventurer::{is_mainnet, ADVENTURER_ADDRESS_MAINNET}; | ||
use starknet::{ContractAddress, get_caller_address, get_tx_info}; | ||
|
||
mod errors { | ||
const NOT_TOKEN_OWNER: felt252 = 'Not token owner'; | ||
const ADVENTURER_CONSUMED: felt252 = 'Adventurer consumed'; | ||
} | ||
|
||
#[generate_trait] | ||
impl AdventurerImpl of AdventurerTrait { | ||
fn use_adventurer(world: IWorldDispatcher, adventurer_id: u32, ref game: Game) { | ||
if is_mainnet(get_tx_info().unbox().chain_id) { | ||
let erc721_dispatcher = IERC721SystemDispatcher { contract_address: ADVENTURER_ADDRESS_MAINNET() }; | ||
let owner = erc721_dispatcher.owner_of(adventurer_id.into()); | ||
assert(owner == get_caller_address(), errors::NOT_TOKEN_OWNER); | ||
|
||
let mut adventurer_consumed = AdventurerConsumedStore::get(world, adventurer_id); | ||
assert(!adventurer_consumed.consumed, errors::ADVENTURER_CONSUMED); | ||
|
||
let loot_survivor_dispatcher = ILootSurvivorSystemDispatcher { contract_address: ADVENTURER_ADDRESS_MAINNET() }; | ||
let adventurer = loot_survivor_dispatcher.get_adventurer(adventurer_id.into()); | ||
let level = get_level_from_xp(adventurer.xp); | ||
|
||
game.player_hp += (level + level / 2).into(); | ||
|
||
adventurer_consumed.consumed = true; | ||
AdventurerConsumedStore::set(@adventurer_consumed, world); | ||
} else { | ||
let mut adventurer_consumed = AdventurerConsumedStore::get(world, adventurer_id); | ||
assert(!adventurer_consumed.consumed, errors::ADVENTURER_CONSUMED); | ||
|
||
let level = 13; | ||
|
||
game.player_hp += level + level / 2; | ||
|
||
adventurer_consumed.consumed = true; | ||
AdventurerConsumedStore::set(@adventurer_consumed, world); | ||
} | ||
} | ||
} | ||
|
||
fn get_level_from_xp(xp: u16) -> u8 { | ||
if (xp == 0) { | ||
1 | ||
} else { | ||
xp.sqrt() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.