Skip to content

Commit

Permalink
chore: add test logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tiankonglan committed Nov 22, 2023
1 parent 71cccc8 commit f847c9a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/config.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use starknet::{ContractAddress};
trait IConfig<TContractState> {
fn init_role(self: @TContractState);
fn init_skill(self: @TContractState);
fn init_config(self: @TContractState);
fn init_global(self: @TContractState);
}

// dojo decorator
#[dojo::contract]
mod config {
use mississippi_mini::models::{Role, Skill};
use mississippi_mini::models::{Role, Skill, Global};
use mississippi_mini::constants;
use super::IConfig;

Expand Down Expand Up @@ -65,6 +67,15 @@ mod config {
};
set!(world, (addSpeedSkill));
}

fn init_config(self: @ContractState) {

}

fn init_global(self: @ContractState) {
let world = self.world_dispatcher.read();
set!(world, (Global{id:constants::GlOBAL_CONFIG_KEY, battleId:0}));
}
}

}
3 changes: 3 additions & 0 deletions src/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ const ROLE_KNIGHT: u32 = 1;
const SKILL_COMBO_ATTACK: u32 = 0;
const SKILL_ADD_HP: u32 = 1;
const SKILL_ADD_SPEED: u32 = 2;

// Global Config Key
const GlOBAL_CONFIG_KEY: u32 = 100;
14 changes: 12 additions & 2 deletions src/game.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait IGame<TContractState> {
#[dojo::contract]
mod game {
use starknet::{ContractAddress, get_caller_address};
use mississippi_mini::models::{Player, BattleInfo, BattleResult, Skill, Role};
use mississippi_mini::models::{Player, BattleInfo, BattleResult, Skill, Role, Global};
use mississippi_mini::utils::next_position;
use mississippi_mini::constants;
// use mississippi_mini::utils::battle_property_settting;
Expand Down Expand Up @@ -77,7 +77,12 @@ mod game {
// retrive player
let world = self.world_dispatcher.read();
let player = get_caller_address();
let battleId = 1;


let mut globalParams = get!(world, constants::GlOBAL_CONFIG_KEY, (Global));
globalParams.battleId = globalParams.battleId + 1;
let battleId = globalParams.battleId;
set!(world, (globalParams));

// check target exist

Expand Down Expand Up @@ -161,6 +166,7 @@ mod game {
battleId : attacker.battleId,
winner : winner,
};
set!(world, (battleResult));

emit!(world, Win { battleId: attacker.battleId, winner: winner});
}
Expand Down Expand Up @@ -246,6 +252,10 @@ mod tests {
let x = 5;
x.print();

let battleReuslt = get!(world, 1, (BattleResult));
battleReuslt.print();
let battleInfo = get!(world, (1, false), (BattleInfo));




Expand Down
10 changes: 10 additions & 0 deletions src/models.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ struct Config {
battleId : u32,
}

#[derive(Model, Copy, Drop, Print, Serde)]
struct Global {
#[key] // primary key
id: u32,
battleId : u32,
}




#[derive(Model, Copy, Drop, Print, Serde)]
struct BattleInfo {
#[key] // primary key
Expand Down

0 comments on commit f847c9a

Please sign in to comment.