Skip to content

Commit

Permalink
JOPN-464: Integrate adventurer (#20)
Browse files Browse the repository at this point in the history
* wip integrate adventurer

* add mock erc721

* add interface

* add interface

* add use_adventurer

* integrate aventurer

* update state
  • Loading branch information
dpinones authored Nov 3, 2024
1 parent 491fbca commit 72459ab
Show file tree
Hide file tree
Showing 13 changed files with 2,990 additions and 1,319 deletions.
3,992 changes: 2,687 additions & 1,305 deletions manifests/dev/deployment/manifest.json

Large diffs are not rendered by default.

82 changes: 76 additions & 6 deletions manifests/dev/deployment/manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ manifest_name = "dojo-base"
[[contracts]]
kind = "DojoContract"
address = "0x6a41badee85305fa1aac33488860360c66b60d7f3b204d6e2cd84071dc3c394"
class_hash = "0x352fd9eeb1842a703803b0b06071e21caf0e093be86e0bdeb3b7bad13f11be5"
original_class_hash = "0x352fd9eeb1842a703803b0b06071e21caf0e093be86e0bdeb3b7bad13f11be5"
class_hash = "0x6064cd42481d56780719611f314fd168f59da3e319e4fdaf3606c3483563db0"
original_class_hash = "0x6064cd42481d56780719611f314fd168f59da3e319e4fdaf3606c3483563db0"
base_class_hash = "0x2427dd10a58850ac9a5ca6ce04b7771b05330fd18f2e481831ad903b969e6b2"
abi = "manifests/dev/deployment/abis/contracts/jokers_of_neon-game_system-7a205bbc.json"
reads = []
Expand All @@ -37,7 +37,10 @@ systems = [
"play",
"discard",
"end_turn",
"create_reward",
"select_reward",
"select_deck",
"use_adventurer",
"select_special_cards",
"select_modifier_cards",
"discard_effect_card",
Expand All @@ -62,8 +65,8 @@ manifest_name = "jokers_of_neon-poker_hand_system-25bae3ed"
[[contracts]]
kind = "DojoContract"
address = "0x75602558fd679c49be1a6ccb73e3d6d842f0558e1f41c4013ce109d05a5790f"
class_hash = "0x5f1bddfab781d4c4b67d7814361d1a38549f876f4e2574a576903291c3f536d"
original_class_hash = "0x5f1bddfab781d4c4b67d7814361d1a38549f876f4e2574a576903291c3f536d"
class_hash = "0x4da1956c768854cdfddd16b65b34b7ce24c2c44a2acf60b1f2592475b503fa2"
original_class_hash = "0x4da1956c768854cdfddd16b65b34b7ce24c2c44a2acf60b1f2592475b503fa2"
base_class_hash = "0x2427dd10a58850ac9a5ca6ce04b7771b05330fd18f2e481831ad903b969e6b2"
abi = "manifests/dev/deployment/abis/contracts/jokers_of_neon-rage_system-601fc835.json"
reads = []
Expand All @@ -73,6 +76,30 @@ tag = "jokers_of_neon-rage_system"
systems = ["calculate"]
manifest_name = "jokers_of_neon-rage_system-601fc835"

[[models]]
kind = "DojoModel"
class_hash = "0x79045f9b971b4009a1822c5471f5b54365f86045fbf1ca4745a441195ec8c40"
original_class_hash = "0x79045f9b971b4009a1822c5471f5b54365f86045fbf1ca4745a441195ec8c40"
abi = "manifests/dev/deployment/abis/models/jokers_of_neon-AdventurerConsumed-5d6c7696.json"
tag = "jokers_of_neon-AdventurerConsumed"
qualified_path = "jokers_of_neon::models::data::adventurer::adventurer_consumed"
manifest_name = "jokers_of_neon-AdventurerConsumed-5d6c7696"

[[models.members]]
name = "adventurer_id"
type = "u32"
key = true

[[models.members]]
name = "owner"
type = "ContractAddress"
key = false

[[models.members]]
name = "consumed"
type = "bool"
key = false

[[models]]
kind = "DojoModel"
class_hash = "0x138b1cf753d19f46b39fc7eebc67b9cdea0ce30ef41c961bf150392837796bf"
Expand Down Expand Up @@ -637,8 +664,8 @@ key = false

[[models]]
kind = "DojoModel"
class_hash = "0x3ffc052edb32b194e03a69e0b624c9ee4d508d4ce3de3de43a0f5503832b6c2"
original_class_hash = "0x3ffc052edb32b194e03a69e0b624c9ee4d508d4ce3de3de43a0f5503832b6c2"
class_hash = "0x25c9eb7813071767dcf7069b422a5b8a3895c51346932209f389a63ba62ecae"
original_class_hash = "0x25c9eb7813071767dcf7069b422a5b8a3895c51346932209f389a63ba62ecae"
abi = "manifests/dev/deployment/abis/models/jokers_of_neon-Game-162de85b.json"
tag = "jokers_of_neon-Game"
qualified_path = "jokers_of_neon::models::status::game::game::game"
Expand Down Expand Up @@ -1008,6 +1035,30 @@ name = "energy"
type = "u8"
key = false

[[models]]
kind = "DojoModel"
class_hash = "0xc08b898bbf24211a37fbdccc3bc3bf615954fe0c821a9de3e6e8151eaa3871"
original_class_hash = "0xc08b898bbf24211a37fbdccc3bc3bf615954fe0c821a9de3e6e8151eaa3871"
abi = "manifests/dev/deployment/abis/models/jokers_of_neon-PlayerHealed-3cdab4ab.json"
tag = "jokers_of_neon-PlayerHealed"
qualified_path = "jokers_of_neon::models::data::events::player_healed"
manifest_name = "jokers_of_neon-PlayerHealed-3cdab4ab"

[[models.members]]
name = "game_id"
type = "u32"
key = true

[[models.members]]
name = "potion_heal"
type = "u32"
key = false

[[models.members]]
name = "current_hp"
type = "u32"
key = false

[[models]]
kind = "DojoModel"
class_hash = "0x34c07816aa25e5daadc2370bd4793af7dbd786de2c1e99d74e7347680ffa281"
Expand Down Expand Up @@ -1071,6 +1122,25 @@ name = "last_active_level"
type = "u8"
key = false

[[models]]
kind = "DojoModel"
class_hash = "0x315761eb593391eaf1293fa2f5b43903a93c31ded0a925f9d31077fbd8ccc5a"
original_class_hash = "0x315761eb593391eaf1293fa2f5b43903a93c31ded0a925f9d31077fbd8ccc5a"
abi = "manifests/dev/deployment/abis/models/jokers_of_neon-Reward-1ffe01c2.json"
tag = "jokers_of_neon-Reward"
qualified_path = "jokers_of_neon::models::data::reward::reward"
manifest_name = "jokers_of_neon-Reward-1ffe01c2"

[[models.members]]
name = "game_id"
type = "u32"
key = true

[[models.members]]
name = "rewards_ids"
type = "Span<u32>"
key = false

[[models]]
kind = "DojoModel"
class_hash = "0x58fe22073d9dc0c2cf86d7a19fa3d32e90e1eac8b3b057c20a9477c88565134"
Expand Down
5 changes: 5 additions & 0 deletions scripts/enviroment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ sozo execute game_system create_game -c 1 --wait --world 0x37578f01e123327fb366f
sleep 3
echo -e "\n[INFO] Game created successfully. Waiting 3 seconds before selecting deck..."

echo -e "\n[INFO] Selecting aventurer for the game..."
sozo execute game_system use_adventurer -c 1,1 --wait --world 0x37578f01e123327fb366fc6e2224f4be4e44234d682855e1326363b57444b88
sleep 3
echo -e "\n[INFO] Aventurer selected successfully. Waiting 3 seconds before selecting deck..."

echo -e "\n[INFO] Selecting deck for the game..."
sozo execute game_system select_deck -c 1,0 --wait --world 0x37578f01e123327fb366fc6e2224f4be4e44234d682855e1326363b57444b88
sleep 3
Expand Down
7 changes: 7 additions & 0 deletions src/interfaces/erc721.cairo
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;
}
48 changes: 48 additions & 0 deletions src/interfaces/loot_survivor.cairo
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;
}
8 changes: 8 additions & 0 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod constants {

mod models {
mod data {
mod adventurer;
mod beast;
mod blister_pack;
mod card;
Expand All @@ -34,6 +35,7 @@ mod models {
mod rage;
}
mod round {
mod adventurer;
mod beast;
mod challenge;
mod current_hand_card;
Expand Down Expand Up @@ -66,6 +68,7 @@ mod tests {
}

mod utils {
mod adventurer;
mod calculate_hand;
mod constants;
mod game;
Expand All @@ -82,3 +85,8 @@ mod configs {
mod rage;
mod slot_special_cards;
}

mod interfaces {
mod erc721;
mod loot_survivor;
}
10 changes: 10 additions & 0 deletions src/models/data/adventurer.cairo
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
}
4 changes: 3 additions & 1 deletion src/models/status/game/game.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ enum GameSubState {
REWARD_CARDS_PACK,
DRAFT_MODIFIERS,
DRAFT_SPECIALS,
DRAFT_DECK
DRAFT_DECK,
DRAFT_ADVENTURER,
DRAFT_ADVENTURER_CARDS
}

#[derive(Serde, Copy, Drop, IntrospectPacked, PartialEq)]
Expand Down
57 changes: 57 additions & 0 deletions src/models/status/round/adventurer.cairo
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()
}
}
3 changes: 1 addition & 2 deletions src/models/status/round/beast.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ impl BeastImpl of BeastTrait {
player_beast.energy -= game_mode_beast.cost_play;
PlayerBeastStore::set(@player_beast, world);


if beast.current_health.is_zero() {
let play_win_game_event = PlayWinGameEvent {
player: get_caller_address(), game_id, level: game.level, player_score: 0
Expand All @@ -112,7 +111,7 @@ impl BeastImpl of BeastTrait {
_ => Option::None
}.unwrap();
IRageSystemDispatcher { contract_address: rage_system_address.try_into().unwrap() }.calculate(game.id);
// create_level(world, ref store, game); TODO:
// create_level(world, ref store, game); TODO:
} else if player_beast.energy.is_zero() {
_attack_beast(world, ref store, ref game, ref player_beast, ref beast, ref game_mode_beast);
} else {
Expand Down
Loading

0 comments on commit 72459ab

Please sign in to comment.