Skip to content

Commit d088b3b

Browse files
authored
Merge pull request #87 from KevinMB0220/feat-documentation
feat-documentation
2 parents 84db185 + 36e15c7 commit d088b3b

File tree

3 files changed

+153
-71
lines changed

3 files changed

+153
-71
lines changed

src/models/battle.cairo

+19
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
1+
// Defines the `Battle` Model, which represents a battle between two players.
2+
// Includes various fields to track the state and progress of the battle.
13
#[derive(Copy, Drop, Serde)]
24
#[dojo::model]
35
pub struct Battle {
6+
// Unique identifier for the battle.
47
#[key]
58
pub battle_id: u32,
9+
10+
// ID of the player involved in the battle.
611
pub player_id: u32,
12+
13+
// ID of the opponent involved in the battle.
714
pub opponent_id: u32,
15+
16+
// ID of the active beast for the player.
817
pub active_beast_player: u32,
18+
19+
// ID of the active beast for the opponent.
920
pub active_beast_opponent: u32,
21+
22+
// Flag to indicate if the battle is currently active (1 for active, 0 for inactive).
1023
pub battle_active: u32,
24+
25+
// Current turn number in the battle.
1126
pub turn: u32,
1227
}
1328

29+
1430
#[cfg(test)]
1531
mod tests {
32+
1633
use bytebeasts::{models::{battle::Battle},};
1734

35+
36+
1837
#[test]
1938
fn test_battle_initialization() {
2039
let battle = Battle {

src/systems/battle.cairo

+77-41
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,63 @@
1+
// Required modules and models for the battle system.
12
use bytebeasts::{
23
models::{beast::Beast, mt::Mt, player::Player, battle::Battle, potion::Potion},
34
};
45

6+
// Interface defining battle-related actions.
57
#[dojo::interface]
68
trait IBattleActions {
7-
fn init_battle(ref world: IWorldDispatcher, player_id: u32, opponent_id: u32) -> u32;
8-
fn check_flee_success(player_beast: Beast, opponent_beast: Beast) -> bool;
9-
fn calculate_damage(mt: Mt, attacker: Beast, defender: Beast) -> u32;
10-
fn opponent_turn(ref world: IWorldDispatcher, battle_id: u32);
11-
fn attack(ref world: IWorldDispatcher, battle_id: u32, mt_id: u32);
12-
fn use_potion(ref world: IWorldDispatcher, battle_id: u32, potion_id: u32);
9+
// Initializes a battle
10+
fn init_battle(ref world: IWorldDispatcher, player_id: u32, opponent_id: u32) -> u32;
11+
// Checks flee success
12+
fn check_flee_success(player_beast: Beast, opponent_beast: Beast) -> bool;
13+
// Calculates damage
14+
fn calculate_damage(mt: Mt, attacker: Beast, defender: Beast) -> u32;
15+
// Handles opponent's turn
16+
fn opponent_turn(ref world: IWorldDispatcher, battle_id: u32);
17+
// Executes an attack
18+
fn attack(ref world: IWorldDispatcher, battle_id: u32, mt_id: u32);
19+
// Uses a potion
20+
fn use_potion(ref world: IWorldDispatcher, battle_id: u32, potion_id: u32);
21+
// Attempts to flee
1322
fn flee(ref world: IWorldDispatcher, battle_id: u32);
1423
}
1524

25+
// Contract implementing battle actions.
1626
#[dojo::contract]
1727
mod battle_system {
1828
use super::{IBattleActions};
1929
use bytebeasts::{
2030
models::{beast::Beast, mt::Mt, player::Player, battle::Battle, potion::Potion},
2131
};
2232

33+
// Event emitted for battle status updates.
2334
#[derive(Copy, Drop, Serde)]
2435
#[dojo::model]
2536
#[dojo::event]
2637
struct StatusBattle {
2738
#[key]
28-
battle_id: u32,
29-
message: felt252,
39+
battle_id: u32, // Battle identifier
40+
message: felt252, // Event message
3041
}
3142

43+
// Event emitted for player status updates.
3244
#[derive(Copy, Drop, Serde)]
3345
#[dojo::model]
3446
#[dojo::event]
3547
struct Status {
3648
#[key]
37-
player_id: u32,
38-
message: felt252,
49+
player_id: u32, // Player identifier
50+
message: felt252, // Event message
3951
}
4052

4153
#[abi(embed_v0)]
4254
impl BattleActionsImpl of IBattleActions<ContractState> {
55+
// Initializes a new battle.
4356
fn init_battle(ref world: IWorldDispatcher, player_id: u32, opponent_id: u32) -> u32 {
44-
let player = get!(world, player_id, (Player));
45-
let opponent = get!(world, opponent_id, (Player));
46-
let active_beast_player = get!(world, player.beast_1, (Beast));
47-
let active_beast_opponent = get!(world, opponent.beast_1, (Beast));
57+
let player = get!(world, player_id, (Player)); // Fetch player data
58+
let opponent = get!(world, opponent_id, (Player)); // Fetch opponent data
59+
let active_beast_player = get!(world, player.beast_1, (Beast)); // Player's beast
60+
let active_beast_opponent = get!(world, opponent.beast_1, (Beast)); // Opponent's beast
4861

4962
let battle_created_id = 1; // Hardcoded for now
5063
set!(
@@ -61,17 +74,19 @@ mod battle_system {
6174
);
6275

6376
let message = 'Battle started';
64-
emit!(world, (Status { player_id: player_id, message: message }));
77+
emit!(world, (Status { player_id: player_id, message: message })); // Emit player status
6578

66-
emit!(world, (StatusBattle { battle_id: battle_created_id, message: message }));
79+
emit!(world, (StatusBattle { battle_id: battle_created_id, message: message })); // Emit battle status
6780

68-
return battle_created_id;
81+
return battle_created_id; // Return battle ID
6982
}
7083

84+
// Checks if the player can flee based on beast levels.
7185
fn check_flee_success(player_beast: Beast, opponent_beast: Beast) -> bool {
7286
player_beast.level > opponent_beast.level
7387
}
7488

89+
// Calculates the damage dealt by an attacker to a defender.
7590
fn calculate_damage(mt: Mt, attacker: Beast, defender: Beast) -> u32 {
7691
let base_damage = mt.mt_power * attacker.attack / defender.defense;
7792

@@ -83,98 +98,119 @@ mod battle_system {
8398

8499
let hit_chance = 80_u32; // Hardcoded for now
85100
if hit_chance > mt.mt_accuracy {
86-
return 0_u32;
101+
return 0_u32; // Misses if hit chance is greater than accuracy
87102
}
88103

89104
effective_damage
90105
}
91106

107+
// Executes the opponent's turn in the battle.
92108
fn opponent_turn(ref world: IWorldDispatcher, battle_id: u32) {
93-
let mut battle = get!(world, battle_id, (Battle));
109+
let mut battle = get!(world, battle_id, (Battle)); // Retrieve battle details
94110

95111
let mut player_beast = get!(world, battle.active_beast_player, (Beast));
96112
let mut opponent_beast = get!(world, battle.active_beast_opponent, (Beast));
97-
let opponent_attack = get!(world, opponent_beast.mt1, (Mt));
113+
let opponent_attack = get!(world, opponent_beast.mt1, (Mt)); // Opponent's chosen move
98114

115+
// Calculate damage dealt by the opponent to the player
99116
let damage = self.calculate_damage(opponent_attack, opponent_beast, player_beast);
100117
if damage >= player_beast.current_hp {
101-
player_beast.current_hp = 0;
118+
player_beast.current_hp = 0; // Knock out player’s beast if damage exceeds current HP
102119
} else {
103-
player_beast.current_hp -= damage;
120+
player_beast.current_hp -= damage; // Subtract damage from player's beast HP
104121
}
105-
set!(world, (player_beast));
122+
set!(world, (player_beast)); // Update player's beast in the world
106123

124+
// Check if the player's beast is knocked out
107125
if player_beast.current_hp <= 0_u32 {
108126
let message = 'Player Beast Knocked Out!';
109-
emit!(world, (StatusBattle { battle_id, message }));
110-
battle.battle_active = 0;
111-
set!(world, (battle));
127+
emit!(world, (StatusBattle { battle_id, message })); // Emit battle status update
128+
battle.battle_active = 0; // Emit battle status update
129+
set!(world, (battle)); // Update battle status in the world
112130
}
113131
}
114132

133+
// Executes the player's attack in the battle.
115134
fn attack(ref world: IWorldDispatcher, battle_id: u32, mt_id: u32) {
116-
let mut battle = get!(world, battle_id, (Battle));
135+
let mut battle = get!(world, battle_id, (Battle)); // Retrieve battle details
117136

137+
// Fetch the player's and opponent's active beasts and the chosen move (mt) of the player
118138
let player_beast = get!(world, battle.active_beast_player, (Beast));
119139
let mut opponent_beast = get!(world, battle.active_beast_opponent, (Beast));
120140
let mt = get!(world, mt_id, (Mt));
121141

142+
// Calculate damage dealt by the player to the opponent
122143
let damage = self.calculate_damage(mt, player_beast, opponent_beast);
123144

145+
// Apply damage to opponent's beast HP
124146
if damage >= opponent_beast.current_hp {
125-
opponent_beast.current_hp = 0;
147+
opponent_beast.current_hp = 0; // Knock out opponent’s beast if damage exceeds current HP
126148
} else {
127-
opponent_beast.current_hp -= damage;
149+
opponent_beast.current_hp -= damage; // Subtract damage from opponent's beast HP
128150
}
129-
set!(world, (opponent_beast));
151+
set!(world, (opponent_beast)); // Update opponent's beast in the world
130152

153+
// Check if the opponent's beast is knocked out
131154
if opponent_beast.current_hp <= 0_u32 {
132155
let message = 'Opponent Beast Knocked Out!';
133-
emit!(world, (StatusBattle { battle_id, message }));
134-
battle.battle_active = 0;
135-
set!(world, (battle));
156+
emit!(world, (StatusBattle { battle_id, message })); // Emit battle status update
157+
battle.battle_active = 0; // End the battle
158+
set!(world, (battle)); // Update battle status in the world
136159
} else {
137160
let message = 'Attack Performed!';
138-
emit!(world, (StatusBattle { battle_id, message }));
161+
emit!(world, (StatusBattle { battle_id, message })); // Emit message indicating the attack was performed
139162
}
163+
140164
}
141165

166+
// Allows the player to use a potion on their beast to restore health.
142167
fn use_potion(ref world: IWorldDispatcher, battle_id: u32, potion_id: u32) {
143-
let mut battle = get!(world, battle_id, (Battle));
168+
let mut battle = get!(world, battle_id, (Battle)); // Retrieve battle details
144169

170+
// Fetch the player's active beast and the potion being used
145171
let mut player_beast = get!(world, battle.active_beast_player, (Beast));
146172
let potion = get!(world, potion_id, (Potion));
147173

174+
// Restore health points based on potion's effect without exceeding max HP
148175
if potion.potion_effect <= player_beast.current_hp {
149-
player_beast.current_hp += potion.potion_effect;
176+
player_beast.current_hp += potion.potion_effect; // Add potion effect to current HP
150177
}
151178
else {
152-
player_beast.current_hp = player_beast.hp;
179+
player_beast.current_hp = player_beast.hp; // Cap HP to the beast's max health
153180
}
154181

155-
set!(world, (player_beast));
182+
set!(world, (player_beast)); // Update the player's beast in the world
156183
player_beast = get!(world, battle.active_beast_player, (Beast));
157184

185+
// Emit an event to notify that the potion has been used
158186
let message = 'Item Used!';
159187
emit!(world, (StatusBattle { battle_id, message }));
160188
}
161189

190+
// Attempts to flee the battle. Success is determined by the relative levels of the player and opponent beasts.
162191
fn flee(ref world: IWorldDispatcher, battle_id: u32) {
163-
let mut battle = get!(world, battle_id, (Battle));
192+
let mut battle = get!(world, battle_id, (Battle)); // Retrieve battle details
164193

194+
// Fetch both player's and opponent's active beasts
165195
let player_beast = get!(world, battle.active_beast_player, (Beast));
166196
let opponent_beast = get!(world, battle.active_beast_opponent, (Beast));
167197

198+
// Determine if flee attempt is successful based on beast levels
168199
let flee_success = self.check_flee_success(player_beast, opponent_beast);
169200
if flee_success {
170-
battle.battle_active = 0;
171-
set!(world, (battle));
201+
battle.battle_active = 0; // Set battle as inactive upon successful flee
202+
set!(world, (battle)); // Update battle status in the world
203+
204+
// Emit event indicating the player successfully fled the battle
172205
let message = 'Player Fled!';
173206
emit!(world, (StatusBattle { battle_id, message }));
174207
} else {
208+
// Emit event indicating flee attempt failed
175209
let message = 'Flee failed!';
176210
emit!(world, (StatusBattle { battle_id, message }));
177211
}
212+
178213
}
179214
}
180215
}
216+

0 commit comments

Comments
 (0)