-
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 all 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,70 @@ | ||
use super::player::Player; | ||
|
||
#[derive(Serde, Copy, Drop, Introspect, PartialEq, Debug)] | ||
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, | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use bytebeasts::{ | ||
models::{tournament::Tournament, tournament::TournamentStatus, player::Player} | ||
}; | ||
|
||
|
||
#[test] | ||
fn test_tournament_initialization() { | ||
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); | ||
|
||
let tournament = Tournament { | ||
tournament_id: 1, | ||
name: 'gersonwashere', | ||
status: TournamentStatus::Pending, | ||
entry_fee: 1, | ||
max_participants: 2, | ||
current_participants: players, | ||
prize_pool: 1, | ||
}; | ||
|
||
assert_eq!(tournament.tournament_id, 1, "Tournament ID should be 1"); | ||
assert_eq!(tournament.name, 'gersonwashere', "Tournament name should be gersonwashere"); | ||
assert_eq!( | ||
tournament.status, TournamentStatus::Pending, "Tournament status should be pending" | ||
); | ||
assert_eq!(tournament.entry_fee, 1, "Tournament entry fee should be 1"); | ||
assert_eq!(tournament.max_participants, 2, "Tournament max participants should be 2"); | ||
assert_eq!( | ||
tournament.current_participants.len(), 1, "Tournament current participants should be 1" | ||
); | ||
assert_eq!(tournament.prize_pool, 1, "Tournament prize pool should be 1"); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,89 @@ | ||||||||||||||||||||
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, new_player: Player); | ||||||||||||||||||||
fn start_tournament(ref world: IWorldDispatcher, tournament_id: u32); | ||||||||||||||||||||
// fn complete_tournament(ref world: IWorldDispatcher, tournament_id: u32, player_id: u32); | ||||||||||||||||||||
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 register_player(ref world: IWorldDispatcher, tournament_id: u32, new_player: Player) { | ||||||||||||||||||||
let mut tournament = get!(world, tournament_id, (Tournament)); | ||||||||||||||||||||
|
||||||||||||||||||||
assert!(tournament.status == TournamentStatus::Pending, "Tournament not open for registration"); | ||||||||||||||||||||
|
||||||||||||||||||||
assert!( | ||||||||||||||||||||
tournament.current_participants.len() < tournament.max_participants.try_into().unwrap(), | ||||||||||||||||||||
"Tournament is full" | ||||||||||||||||||||
); | ||||||||||||||||||||
Comment on lines
+59
to
+62
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. Fix potential integer overflow in length comparison. The current comparison between Consider this safer approach: - assert!(
- tournament.current_participants.len() < tournament.max_participants.try_into().unwrap(),
- "Tournament is full"
- );
+ let current_count: u32 = tournament.current_participants.len().try_into().unwrap();
+ assert!(current_count < tournament.max_participants, "Tournament is full"); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||
|
||||||||||||||||||||
tournament.current_participants.append(new_player); | ||||||||||||||||||||
|
||||||||||||||||||||
set!(world, (tournament)); | ||||||||||||||||||||
} | ||||||||||||||||||||
Comment on lines
+54
to
+67
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 player validation and entry fee handling. The
Consider adding these validations: fn register_player(ref world: IWorldDispatcher, tournament_id: u32, new_player: Player) {
let mut tournament = get!(world, tournament_id, (Tournament));
assert!(tournament.status == TournamentStatus::Pending, "Tournament not open for registration");
assert!(
tournament.current_participants.len() < tournament.max_participants.try_into().unwrap(),
"Tournament is full"
);
+ // Check for duplicate players
+ let mut i = 0;
+ loop {
+ if i >= tournament.current_participants.len() {
+ break;
+ }
+ assert!(
+ tournament.current_participants[i].player_id != new_player.player_id,
+ "Player already registered"
+ );
+ i += 1;
+ };
+
+ // TODO: Handle entry fee payment
+ // This would require integration with a payment system
+ // assert!(payment_received == tournament.entry_fee, "Entry fee not paid");
tournament.current_participants.append(new_player);
|
||||||||||||||||||||
|
||||||||||||||||||||
fn start_tournament(ref world: IWorldDispatcher, tournament_id: u32) { | ||||||||||||||||||||
let mut tournament = get!(world, tournament_id, (Tournament)); | ||||||||||||||||||||
|
||||||||||||||||||||
assert!(tournament.status == TournamentStatus::Pending, "Tournament not pending"); | ||||||||||||||||||||
|
||||||||||||||||||||
assert!( | ||||||||||||||||||||
tournament.current_participants.len() >= 2, | ||||||||||||||||||||
"Not enough participants to start" | ||||||||||||||||||||
); | ||||||||||||||||||||
|
||||||||||||||||||||
tournament.status = TournamentStatus::Ongoing; | ||||||||||||||||||||
|
||||||||||||||||||||
set!(world, (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
|
||||||||||||||||||||
} | ||||||||||||||||||||
} |
This comment was marked as spam.
Sorry, something went wrong.