Skip to content

Commit

Permalink
add randon + attack evasion impl (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzn committed Dec 24, 2023
1 parent a87acce commit 5153010
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 118 deletions.
6 changes: 5 additions & 1 deletion src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ mod systems {
mod skill_system;
}

mod utils {
mod random;
}

#[cfg(test)]
mod tests {
mod setup;
Expand All @@ -35,6 +39,6 @@ mod tests {
mod test_match_system;
mod test_move_system;
mod test_skill_system;
// mod test_turn_system;
// mod test_turn_system;
}

36 changes: 22 additions & 14 deletions src/systems/action_system.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mod action_system {
};
use starkane::systems::stadistics_system::stadistics_system;
use starkane::store::{Store, StoreTrait};
use starkane::utils::random::{Random, RandomImpl, RandomTrait};

use debug::PrintTrait;

Expand Down Expand Up @@ -83,8 +84,8 @@ mod action_system {
receiver_character,
receiver_character_state
);
check_and_update_game_state_winner(world,
ref store, match_id, player, receiver_character_id, receiver
check_and_update_game_state_winner(
world, ref store, match_id, player, receiver_character_id, receiver
);
},
SkillType::RangeAttack => {
Expand All @@ -96,8 +97,8 @@ mod action_system {
receiver_character,
receiver_character_state
);
check_and_update_game_state_winner(world,
ref store, match_id, player, receiver_character_id, receiver
check_and_update_game_state_winner(
world, ref store, match_id, player, receiver_character_id, receiver
);
},
SkillType::Fireball => {
Expand All @@ -110,8 +111,8 @@ mod action_system {
receiver_character_state
);

check_and_update_game_state_winner(world,
ref store, match_id, player, receiver_character_id, receiver
check_and_update_game_state_winner(
world, ref store, match_id, player, receiver_character_id, receiver
);
},
SkillType::Heal => heal(
Expand Down Expand Up @@ -178,14 +179,21 @@ mod action_system {
);
assert(distance_to <= skill.range, 'character cannot attk that far');

let mut receiver_state = receiver_state;
receiver_state
.remain_hp =
if receiver_state.remain_hp < (attacker.attack + skill.power) {
0
} else {
receiver_state.remain_hp - (attacker.attack + skill.power)
};
// Check if the receiver evades the attack
let mut randomizer = RandomImpl::new(world);
let receiver_evade_attack = randomizer.between::<u128>(0, 100) <= receiver.evasion;

if !receiver_evade_attack {
let mut receiver_state = receiver_state;
receiver_state
.remain_hp =
if receiver_state.remain_hp < (attacker.attack + skill.power) {
0
} else {
receiver_state.remain_hp - (attacker.attack + skill.power)
};
}

set!(world, (receiver_state));
}

Expand Down
14 changes: 2 additions & 12 deletions src/systems/character_system.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ use starkane::models::entities::character::CharacterType;
#[starknet::interface]
trait ICharacterSystem<TContractState> {
fn init(self: @TContractState);
fn mint(
self: @TContractState,
character_type: CharacterType,
owner: felt252,
skin_id: u32
);
fn mint(self: @TContractState, character_type: CharacterType, owner: felt252, skin_id: u32);
}

#[dojo::contract]
Expand All @@ -34,12 +29,7 @@ mod character_system {
store.set_character(CharacterTrait::new(CharacterType::Pig));
}

fn mint(
self: @ContractState,
character_type: CharacterType,
owner: felt252,
skin_id: u32
) {
fn mint(self: @ContractState, character_type: CharacterType, owner: felt252, skin_id: u32) {
// [Setup] Datastore
let world = self.world();
let mut store: Store = StoreTrait::new(world);
Expand Down
9 changes: 2 additions & 7 deletions src/systems/match_system.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ struct PlayerCharacter {

#[starknet::interface]
trait IMatchSystem<TContractState> {
fn init(
self: @TContractState, players_characters: Array<PlayerCharacter>
);
fn init(self: @TContractState, players_characters: Array<PlayerCharacter>);
}

#[dojo::contract]
Expand All @@ -33,10 +31,7 @@ mod match_system {

#[external(v0)]
impl MatchSystem of IMatchSystem<ContractState> {
fn init(
self: @ContractState,
players_characters: Array<PlayerCharacter>
) {
fn init(self: @ContractState, players_characters: Array<PlayerCharacter>) {
// [Setup] Datastore
let world = self.world();
let mut store: Store = StoreTrait::new(world);
Expand Down
24 changes: 4 additions & 20 deletions src/systems/skill_system.cairo
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
#[starknet::interface]
trait ISkillSystem<TContractState> {
fn init(self: @TContractState);
fn has_skill(
self: @TContractState,
character_id: u32,
player: felt252,
skill_id: u32
) -> bool;
fn has_skill(self: @TContractState, character_id: u32, player: felt252, skill_id: u32) -> bool;
fn can_use_skill(
self: @TContractState,
character_id: u32,
player: felt252,
skill_id: u32,
level: u32
self: @TContractState, character_id: u32, player: felt252, skill_id: u32, level: u32
) -> bool;
}

Expand Down Expand Up @@ -63,21 +54,14 @@ mod skill_system {
}

fn has_skill(
self: @ContractState,
character_id: u32,
player: felt252,
skill_id: u32
self: @ContractState, character_id: u32, player: felt252, skill_id: u32
) -> bool {
// TODO: Not implemented
true
}

fn can_use_skill(
self: @ContractState,
character_id: u32,
player: felt252,
skill_id: u32,
level: u32
self: @ContractState, character_id: u32, player: felt252, skill_id: u32, level: u32
) -> bool {
// TODO: Not implemented
true
Expand Down
58 changes: 29 additions & 29 deletions src/systems/stadistics_system.cairo
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
#[starknet::interface]
trait IStadisticsSystem<TContractState> {
// fn record_match_stadistics(self: @TContractState, match_id: u32);
trait IStadisticsSystem<TContractState> {// fn record_match_stadistics(self: @TContractState, match_id: u32);
}
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

#[dojo::contract]
mod stadistics_system {
use super::IStadisticsSystem;
use starkane::store::{Store, StoreTrait};
use starkane::models::states::match_state::{ MatchState, MatchPlayer };
use starkane::models::states::match_state::{MatchState, MatchPlayer};
use starkane::models::states::character_state::CharacterState;


#[storage]
struct Storage {}

#[external(v0)]
impl StadisticsSystem of IStadisticsSystem<ContractState> {
}
impl StadisticsSystem of IStadisticsSystem<ContractState> {}

fn record_match_stadistics(world: IWorldDispatcher, match_id: u32) {
// [Setup] Datastore
let mut store: Store = StoreTrait::new(world);

let match_state = store.get_match_state(match_id);
let match_players = store.get_match_players(match_id);
let winner_characters_states = store.get_match_player_characters_states(match_id, match_state.winner);

let mut i = 0;
loop {
if i == match_players.len() {
break;
}
let match_player: MatchPlayer = (*match_players[i]);
let mut player_stadistics = store.get_player_stadistics(match_player.player);
if match_player.player == match_state.winner {
player_stadistics.total_score += calculate_score(match_state, @winner_characters_states);
player_stadistics.matchs_won += 1;
} else {
// give 50 points to player for participate
player_stadistics.total_score += 50;
player_stadistics.matchs_lost += 1;
}
store.set_player_stadistics(player_stadistics);
i += 1;
// [Setup] Datastore
let mut store: Store = StoreTrait::new(world);

let match_state = store.get_match_state(match_id);
let match_players = store.get_match_players(match_id);
let winner_characters_states = store
.get_match_player_characters_states(match_id, match_state.winner);

let mut i = 0;
loop {
if i == match_players.len() {
break;
}
let match_player: MatchPlayer = (*match_players[i]);
let mut player_stadistics = store.get_player_stadistics(match_player.player);
if match_player.player == match_state.winner {
player_stadistics
.total_score += calculate_score(match_state, @winner_characters_states);
player_stadistics.matchs_won += 1;
} else {
// give 50 points to player for participate
player_stadistics.total_score += 50;
player_stadistics.matchs_lost += 1;
}
store.set_player_stadistics(player_stadistics);
i += 1;
}
}

fn calculate_score(match_state: MatchState, characters_states: @Array<CharacterState>) -> u128 {
let mut score = 0;
Expand Down
8 changes: 4 additions & 4 deletions src/tests/setup.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ mod setup {
match_system: IMatchSystemDispatcher,
action_system: IActionSystemDispatcher,
move_system: IMoveSystemDispatcher,
// turn_system: ITurnSystemDispatcher,
// turn_system: ITurnSystemDispatcher,
}

fn spawn_game() -> (IWorldDispatcher, Systems) {
Expand All @@ -56,7 +56,7 @@ mod setup {
character_player_progress::TEST_CLASS_HASH,
match_count::TEST_CLASS_HASH,
];

let world = spawn_test_world(models);

// [Setup] Systems
Expand Down Expand Up @@ -85,8 +85,8 @@ mod setup {
contract_address: world
.deploy_contract('paper', move_system::TEST_CLASS_HASH.try_into().unwrap())
},
};
};

// let character_system_address = deploy_contract(
// character_system::TEST_CLASS_HASH, array![].span()
// );
Expand Down
Loading

0 comments on commit 5153010

Please sign in to comment.