Skip to content

Commit cfc6c16

Browse files
committed
Adding upddates and first test of my job
1 parent 71cec25 commit cfc6c16

File tree

4 files changed

+101
-13
lines changed

4 files changed

+101
-13
lines changed

src/lib.cairo

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod systems {
55
mod spawn;
66
mod world_setup;
77
mod bag;
8+
mod tournament;
89
}
910

1011
mod models {
@@ -26,9 +27,11 @@ mod models {
2627
mod achievement_rarity;
2728
mod achievement_type;
2829
mod achievements;
30+
mod tournament;
2931
}
3032

3133
mod tests {
3234
mod test_battle;
3335
mod test_bag;
36+
mod test_tournament;
3437
}

src/models/tournament.cairo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub enum TournamentStatus {
88
}
99

1010

11-
#[derive(Copy, Drop, Serde)]
11+
#[derive(Drop, Serde)]
1212
#[dojo::model]
1313
pub struct Tournament {
1414
#[key]

src/systems/tournament.cairo

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,59 @@
11
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
2-
use bytebeasts::{
3-
models::{player::Player, tournament::Tournament, tournament::TournamentStatus},
4-
};
2+
use bytebeasts::{models::{player::Player, tournament::Tournament, tournament::TournamentStatus},};
53

64
#[dojo::interface]
75
trait ITournamentAction {
8-
fn create_tournament(ref world: IWorldDispatcher, tournament_id: u32, name: felt252,
6+
fn create_tournament(
7+
ref world: IWorldDispatcher,
8+
tournament_id: u32,
9+
name: felt252,
910
status: TournamentStatus,
1011
entry_fee: u32,
1112
max_participants: u32,
1213
current_participants: Array<Player>,
13-
prize_pool: u32) -> Tournament;
14-
fn register_player(ref world: IWorldDispatcher, tournament_id: u32, player_id: u32);
15-
fn start_tournament(ref world: IWorldDispatcher, tournament_id: u32, player_id: u32);
16-
fn complete_torunament(ref world: IWorldDispatcher, tournament_id: u32, player_id: u32);
17-
fn get_tournament(ref world: IWorldDispatcher, tournament_id: u32) -> Tournament;
14+
prize_pool: u32
15+
);
16+
// fn register_player(ref world: IWorldDispatcher, tournament_id: u32, player_id: u32);
17+
// fn start_tournament(ref world: IWorldDispatcher, tournament_id: u32, player_id: u32);
18+
// fn complete_torunament(ref world: IWorldDispatcher, tournament_id: u32, player_id: u32);
19+
fn get_tournament(world: @IWorldDispatcher, tournament_id: u32) -> Tournament;
1820
}
1921

2022

2123
#[dojo::contract]
22-
mod spawn_action {
24+
mod tournament_system {
2325
use super::ITournamentAction;
2426
use bytebeasts::{
25-
models::{player::Player, tournament::Tournament},
27+
models::{player::Player, tournament::Tournament, tournament::TournamentStatus},
2628
};
2729

2830
#[abi(embed_v0)]
2931
impl TournamentActionImpl of ITournamentAction<ContractState> {
32+
fn create_tournament(
33+
ref world: IWorldDispatcher,
34+
tournament_id: u32,
35+
name: felt252,
36+
status: TournamentStatus,
37+
entry_fee: u32,
38+
max_participants: u32,
39+
current_participants: Array<Player>,
40+
prize_pool: u32
41+
) {
42+
let tournament = Tournament {
43+
tournament_id: tournament_id,
44+
name: name,
45+
status: status,
46+
entry_fee: entry_fee,
47+
max_participants: max_participants,
48+
current_participants: current_participants,
49+
prize_pool: prize_pool
50+
};
51+
set!(world, (tournament))
52+
}
53+
3054
fn get_tournament(world: @IWorldDispatcher, tournament_id: u32) -> Tournament {
3155
let tournament_from_world = get!(world, tournament_id, (Tournament));
3256
tournament_from_world
3357
}
3458
}
35-
}
59+
}

src/tests/test_tournament.cairo

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use starknet::ContractAddress;
4+
5+
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
6+
use dojo::utils::test::{spawn_test_world, deploy_contract};
7+
8+
use bytebeasts::{
9+
systems::{
10+
tournament::{
11+
tournament_system, ITournamentActionDispatcher, ITournamentActionDispatcherTrait
12+
}
13+
},
14+
};
15+
16+
use bytebeasts::{
17+
models::tournament::{{Tournament, tournament}}, models::tournament::TournamentStatus,
18+
models::player::{{Player, player}},
19+
};
20+
21+
22+
// Helper function
23+
// This function create the world and define the required models
24+
#[test]
25+
fn setup_world() -> (IWorldDispatcher, ITournamentActionDispatcher) {
26+
let mut models = array![tournament::TEST_CLASS_HASH, player::TEST_CLASS_HASH];
27+
28+
let world = spawn_test_world("bytebeasts", models);
29+
30+
let contract_address = world
31+
.deploy_contract('salt', tournament_system::TEST_CLASS_HASH.try_into().unwrap());
32+
33+
let tournament_system = ITournamentActionDispatcher { contract_address };
34+
35+
world.grant_writer(dojo::utils::bytearray_hash(@"bytebeasts"), contract_address);
36+
37+
(world, tournament_system)
38+
}
39+
40+
#[test]
41+
fn test_create_tournament() {
42+
let (_, tournament_system) = setup_world();
43+
let mut players = ArrayTrait::new();
44+
45+
let player_ash = Player {
46+
player_id: 1,
47+
player_name: 'Ash',
48+
beast_1: 1, // Beast 1 assigned
49+
beast_2: 0, // No beast assigned
50+
beast_3: 0, // No beast assigned
51+
beast_4: 0, // No beast assigned
52+
potions: 1
53+
};
54+
players.append(player_ash);
55+
56+
tournament_system
57+
.create_tournament(1, 'tournament', TournamentStatus::Pending, 1, 2, players, 1);
58+
let tournament = tournament_system.get_tournament(1);
59+
assert!(tournament.name == 'tournament', "The tournament name is wrong!");
60+
}
61+
}

0 commit comments

Comments
 (0)