-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding the model and system tournament blueprint #96
Changes from 3 commits
71cec25
32f38c3
3fac51d
7a8bae0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use super::player::Player; | ||
|
||
#[derive(Serde, Copy, Drop, Introspect, PartialEq)] | ||
pub enum TournamentStatus { | ||
Pending, | ||
Ongoing, | ||
Completed, | ||
} | ||
|
||
|
||
#[derive(Drop, Serde)] | ||
#[dojo::model] | ||
pub struct Tournament { | ||
#[key] | ||
pub tournament_id: u32, | ||
pub name: felt252, | ||
pub status: TournamentStatus, | ||
pub entry_fee: u32, | ||
pub max_participants: u32, | ||
pub current_participants: Array<Player>, | ||
pub prize_pool: u32, | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,59 @@ | ||||||||||||||||||||
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; | ||||||||||||||||||||
use bytebeasts::{models::{player::Player, tournament::Tournament, tournament::TournamentStatus},}; | ||||||||||||||||||||
|
||||||||||||||||||||
#[dojo::interface] | ||||||||||||||||||||
trait ITournamentAction { | ||||||||||||||||||||
fn create_tournament( | ||||||||||||||||||||
ref world: IWorldDispatcher, | ||||||||||||||||||||
tournament_id: u32, | ||||||||||||||||||||
name: felt252, | ||||||||||||||||||||
status: TournamentStatus, | ||||||||||||||||||||
entry_fee: u32, | ||||||||||||||||||||
max_participants: u32, | ||||||||||||||||||||
current_participants: Array<Player>, | ||||||||||||||||||||
prize_pool: u32 | ||||||||||||||||||||
); | ||||||||||||||||||||
// fn register_player(ref world: IWorldDispatcher, tournament_id: u32, player_id: u32); | ||||||||||||||||||||
// fn start_tournament(ref world: IWorldDispatcher, tournament_id: u32, player_id: u32); | ||||||||||||||||||||
// fn complete_torunament(ref world: IWorldDispatcher, tournament_id: u32, player_id: u32); | ||||||||||||||||||||
This comment was marked as spam.
Sorry, something went wrong. |
||||||||||||||||||||
fn get_tournament(world: @IWorldDispatcher, tournament_id: u32) -> Tournament; | ||||||||||||||||||||
This comment was marked as spam.
Sorry, something went wrong. |
||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
|
||||||||||||||||||||
#[dojo::contract] | ||||||||||||||||||||
mod tournament_system { | ||||||||||||||||||||
use super::ITournamentAction; | ||||||||||||||||||||
use bytebeasts::{ | ||||||||||||||||||||
models::{player::Player, tournament::Tournament, tournament::TournamentStatus}, | ||||||||||||||||||||
}; | ||||||||||||||||||||
|
||||||||||||||||||||
#[abi(embed_v0)] | ||||||||||||||||||||
impl TournamentActionImpl of ITournamentAction<ContractState> { | ||||||||||||||||||||
fn create_tournament( | ||||||||||||||||||||
ref world: IWorldDispatcher, | ||||||||||||||||||||
tournament_id: u32, | ||||||||||||||||||||
name: felt252, | ||||||||||||||||||||
status: TournamentStatus, | ||||||||||||||||||||
entry_fee: u32, | ||||||||||||||||||||
max_participants: u32, | ||||||||||||||||||||
current_participants: Array<Player>, | ||||||||||||||||||||
prize_pool: u32 | ||||||||||||||||||||
) { | ||||||||||||||||||||
let tournament = Tournament { | ||||||||||||||||||||
tournament_id: tournament_id, | ||||||||||||||||||||
name: name, | ||||||||||||||||||||
status: status, | ||||||||||||||||||||
entry_fee: entry_fee, | ||||||||||||||||||||
max_participants: max_participants, | ||||||||||||||||||||
current_participants: current_participants, | ||||||||||||||||||||
prize_pool: prize_pool | ||||||||||||||||||||
}; | ||||||||||||||||||||
set!(world, (tournament)) | ||||||||||||||||||||
} | ||||||||||||||||||||
Comment on lines
+32
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add validation for tournament ID uniqueness. The Add this validation before creating the tournament: prize_pool: u32
) {
+ let existing = get!(world, tournament_id, (Tournament));
+ assert!(existing.tournament_id == 0, "Tournament ID already exists");
let tournament = Tournament {
|
||||||||||||||||||||
|
||||||||||||||||||||
fn get_tournament(world: @IWorldDispatcher, tournament_id: u32) -> Tournament { | ||||||||||||||||||||
This comment was marked as spam.
Sorry, something went wrong. |
||||||||||||||||||||
let tournament_from_world = get!(world, tournament_id, (Tournament)); | ||||||||||||||||||||
tournament_from_world | ||||||||||||||||||||
} | ||||||||||||||||||||
Comment on lines
+84
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling for non-existent tournament. The Add error handling: fn get_tournament(world: @IWorldDispatcher, tournament_id: u32) -> Tournament {
let tournament_from_world = get!(world, tournament_id, (Tournament));
+ assert!(tournament_from_world.tournament_id != 0, "Tournament not found");
tournament_from_world
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||
} | ||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use starknet::ContractAddress; | ||
|
||
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; | ||
use dojo::utils::test::{spawn_test_world, deploy_contract}; | ||
|
||
use bytebeasts::{ | ||
systems::{ | ||
tournament::{ | ||
tournament_system, ITournamentActionDispatcher, ITournamentActionDispatcherTrait | ||
} | ||
}, | ||
}; | ||
|
||
use bytebeasts::{ | ||
models::tournament::{{Tournament, tournament}}, models::tournament::TournamentStatus, | ||
models::player::{{Player, player}}, | ||
}; | ||
|
||
|
||
// Helper function | ||
// This function create the world and define the required models | ||
#[test] | ||
fn setup_world() -> (IWorldDispatcher, ITournamentActionDispatcher) { | ||
let mut models = array![tournament::TEST_CLASS_HASH]; | ||
|
||
let world = spawn_test_world("bytebeasts", models); | ||
|
||
let contract_address = world | ||
.deploy_contract('salt', tournament_system::TEST_CLASS_HASH.try_into().unwrap()); | ||
|
||
let tournament_system = ITournamentActionDispatcher { contract_address }; | ||
|
||
world.grant_writer(dojo::utils::bytearray_hash(@"bytebeasts"), contract_address); | ||
|
||
(world, tournament_system) | ||
} | ||
|
||
#[test] | ||
fn test_create_tournament() { | ||
let (_, tournament_system) = setup_world(); | ||
let mut players = ArrayTrait::new(); | ||
|
||
let player_ash = Player { | ||
player_id: 1, | ||
player_name: 'Ash', | ||
beast_1: 1, // Beast 1 assigned | ||
beast_2: 0, // No beast assigned | ||
beast_3: 0, // No beast assigned | ||
beast_4: 0, // No beast assigned | ||
potions: 1 | ||
}; | ||
players.append(player_ash); | ||
|
||
tournament_system | ||
.create_tournament(1, 'tournament', TournamentStatus::Pending, 1, 2, players, 1); | ||
let tournament = tournament_system.get_tournament(1); | ||
assert!(tournament.name == 'tournament', "The tournament name is wrong!"); | ||
} | ||
} |
This comment was marked as spam.
Sorry, something went wrong.