Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/dojo_examples/combat_game/.tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
dojo 1.1.1
dojo 1.2.1
scarb 2.9.2
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ pub impl AchievementImpl of AchievementTrait {
fn tasks(self: Achievement) -> Span<Task> {
match self {
Achievement::None => [].span(),
Achievement::FirstBlood => array![TaskTrait::new('First Blood', 1, "Win a game")].span(),
Achievement::FirstBlood => array![TaskTrait::new('First Blood', 1, "Win a game")]
.span(),
Achievement::Warrior => array![TaskTrait::new('Warrior', 5, "Win 5 games")].span(),
Achievement::Veteran => array![TaskTrait::new('Veteran', 15, "Win 15 games")].span(),
Achievement::Champion => array![TaskTrait::new('Champion', 30, "Win 30 games")].span(),
Expand Down Expand Up @@ -164,4 +165,4 @@ pub impl IntoU8Achievement of Into<u8, Achievement> {
_ => Achievement::None,
}
}
}
}
261 changes: 212 additions & 49 deletions backend/dojo_examples/combat_game/src/helpers/experience_utils.cairo

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion backend/dojo_examples/combat_game/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ pub mod achievements {
pub mod achievement;
}

pub mod tests {}
pub mod tests {
mod test_battle;
}
26 changes: 15 additions & 11 deletions backend/dojo_examples/combat_game/src/models/battle.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use starknet::{ContractAddress, get_block_timestamp};
use core::num::traits::zero::Zero;
use combat_game::{
helpers::{pseudo_random::PseudoRandom::generate_random_u8},
types::{
battle_status::BattleStatus,
},
types::{battle_status::BattleStatus},
};

#[derive(Copy, Drop, Serde, Debug, Introspect, PartialEq)]
Expand All @@ -19,12 +17,14 @@ pub struct Battle {
pub winner_id: ContractAddress,
pub battle_type: u8,
pub timestamp_start: u64,
pub timestamp_last_action: u64
pub timestamp_last_action: u64,
}

#[generate_trait]
pub impl BattleImpl of BattleTrait {
fn new(id: u256, player1: ContractAddress, player2: ContractAddress, battle_type: u8) -> Battle {
fn new(
id: u256, player1: ContractAddress, player2: ContractAddress, battle_type: u8,
) -> Battle {
let current_timestamp = get_block_timestamp();
let players = array![player1, player2];
Battle {
Expand All @@ -39,14 +39,14 @@ pub impl BattleImpl of BattleTrait {
.into(),
),
status: BattleStatus::Waiting,
winner_id: Zero::zero(),
winner_id: Zero::zero(),
battle_type: battle_type,
timestamp_start: current_timestamp,
timestamp_last_action: current_timestamp
timestamp_last_action: current_timestamp,
}
}

fn end(ref self: Battle, winner: ContractAddress ) {
fn end(ref self: Battle, winner: ContractAddress) {
self.status = BattleStatus::Finished.into();
self.winner_id = winner;
self.timestamp_last_action = get_block_timestamp();
Expand All @@ -65,9 +65,14 @@ pub impl BattleImpl of BattleTrait {
}

fn switch_turn(ref self: Battle) {
self.current_turn = if self.current_turn == self.player1 { self.player2 } else { self.player1 };
self
.current_turn =
if self.current_turn == self.player1 {
self.player2
} else {
self.player1
};
}

}

#[cfg(test)]
Expand Down Expand Up @@ -114,4 +119,3 @@ mod tests {
}
}


9 changes: 6 additions & 3 deletions backend/dojo_examples/combat_game/src/store.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use combat_game::{
constants::SECONDS_PER_DAY,
models::{
player::Player, beast::{Beast, BeastTrait}, skill, skill::{Skill, SkillTrait},
beast_skill::BeastSkill, beast_stats::{BeastStats, BeastStatsActionTrait}, battle::{Battle, BattleTrait},
beast_skill::BeastSkill, beast_stats::{BeastStats, BeastStatsActionTrait},
battle::{Battle, BattleTrait},
},
types::{
beast_type::BeastType, skill::SkillType, status_condition::StatusCondition,
Expand All @@ -20,7 +21,7 @@ struct Store {
}

#[generate_trait]
impl StoreImpl of StoreTrait {
pub impl StoreImpl of StoreTrait {
fn new(world: WorldStorage) -> Store {
Store { world: world }
}
Expand Down Expand Up @@ -242,7 +243,9 @@ impl StoreImpl of StoreTrait {
fn create_rematch(ref self: Store, battle_id: u256) -> Battle {
let battle = self.read_battle(battle_id);

let rematch = BattleTrait::new(battle_id, battle.player1, battle.player2, battle.battle_type);
let rematch = BattleTrait::new(
battle_id, battle.player1, battle.player2, battle.battle_type,
);
self.world.write_model(@rematch);
rematch
}
Expand Down
Loading
Loading