Skip to content

Commit

Permalink
🐛 Fix slot manamgent issue
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Apr 20, 2024
1 parent 4e92356 commit 8e0c127
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 118 deletions.
2 changes: 2 additions & 0 deletions examples/matchmaker/src/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const DEFAULT_RATING: u32 = 1000;
// Leagues

const LEAGUE_SIZE: u8 = 20;
const LEAGUE_COUNT: u8 = 17;
const LEAGUE_MIN_THRESHOLD: u32 = 1000;

// World

Expand Down
49 changes: 30 additions & 19 deletions examples/matchmaker/src/models/league.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use starknet::ContractAddress;

// Internal imports

use matchmaker::constants::LEAGUE_SIZE;
use matchmaker::constants::{LEAGUE_SIZE, LEAGUE_COUNT, LEAGUE_MIN_THRESHOLD};
use matchmaker::models::player::{Player, PlayerTrait, PlayerAssert};
use matchmaker::models::slot::{Slot, SlotTrait};

Expand Down Expand Up @@ -33,7 +33,14 @@ impl LeagueImpl of LeagueTrait {

#[inline(always)]
fn compute_id(rating: u32) -> u8 {
let id = rating / LEAGUE_SIZE.into();
if rating <= LEAGUE_MIN_THRESHOLD {
return 1;
}
let max_rating = LEAGUE_MIN_THRESHOLD + LEAGUE_SIZE.into() * LEAGUE_COUNT.into();
if rating >= max_rating {
return LEAGUE_COUNT;
}
let id = 1 + (rating - LEAGUE_MIN_THRESHOLD) / LEAGUE_SIZE.into();
if id > 251 {
251
} else if id < 1 {
Expand Down Expand Up @@ -90,37 +97,41 @@ mod tests {

// Local imports

use super::{League, LeagueTrait, Player, PlayerTrait, ContractAddress};
use super::{
League, LeagueTrait, Player, PlayerTrait, ContractAddress, LEAGUE_SIZE, LEAGUE_COUNT,
LEAGUE_MIN_THRESHOLD
};

// Constants

fn PLAYER() -> ContractAddress {
starknet::contract_address_const::<'PLAYER'>()
}

const REGISTER_ID: u32 = 1;
const PLAYER_NAME: felt252 = 'NAME';
const REGISTRY_ID: u32 = 1;
const LEAGUE_ID: u8 = 1;

#[test]
fn test_new() {
let league = LeagueTrait::new(REGISTER_ID, LEAGUE_ID);
assert_eq!(league.registry_id, REGISTER_ID);
let league = LeagueTrait::new(REGISTRY_ID, LEAGUE_ID);
assert_eq!(league.registry_id, REGISTRY_ID);
assert_eq!(league.id, LEAGUE_ID);
assert_eq!(league.size, 0);
}

#[test]
fn test_compute_id() {
let rating = 1000;
let rating = LEAGUE_MIN_THRESHOLD - 1;
let league_id = LeagueTrait::compute_id(rating);
assert_eq!(league_id, 50);
assert_eq!(league_id, 1);
}

#[test]
fn test_compute_id_overflow() {
let rating = 10000;
let league_id = LeagueTrait::compute_id(rating);
assert_eq!(league_id, 251);
let max_rating = LEAGUE_MIN_THRESHOLD + LEAGUE_SIZE.into() * LEAGUE_COUNT.into() + 1;
let league_id = LeagueTrait::compute_id(max_rating);
assert_eq!(league_id, LEAGUE_COUNT);
}

#[test]
Expand All @@ -132,8 +143,8 @@ mod tests {

#[test]
fn test_subscribe_once() {
let mut player = PlayerTrait::new(REGISTER_ID, PLAYER());
let mut league = LeagueTrait::new(REGISTER_ID, LEAGUE_ID);
let mut player = PlayerTrait::new(REGISTRY_ID, PLAYER(), PLAYER_NAME);
let mut league = LeagueTrait::new(REGISTRY_ID, LEAGUE_ID);
let slot = LeagueTrait::subscribe(ref league, ref player);
// [Assert] League
assert_eq!(league.size, 1);
Expand All @@ -147,16 +158,16 @@ mod tests {
#[test]
#[should_panic(expected: ('Player: not subscribable',))]
fn test_subscribe_twice() {
let mut player = PlayerTrait::new(REGISTER_ID, PLAYER());
let mut league = LeagueTrait::new(REGISTER_ID, LEAGUE_ID);
let mut player = PlayerTrait::new(REGISTRY_ID, PLAYER(), PLAYER_NAME);
let mut league = LeagueTrait::new(REGISTRY_ID, LEAGUE_ID);
LeagueTrait::subscribe(ref league, ref player);
LeagueTrait::subscribe(ref league, ref player);
}

#[test]
fn test_unsubscribe_once() {
let mut player = PlayerTrait::new(REGISTER_ID, PLAYER());
let mut league = LeagueTrait::new(REGISTER_ID, LEAGUE_ID);
let mut player = PlayerTrait::new(REGISTRY_ID, PLAYER(), PLAYER_NAME);
let mut league = LeagueTrait::new(REGISTRY_ID, LEAGUE_ID);
LeagueTrait::subscribe(ref league, ref player);
LeagueTrait::unsubscribe(ref league, ref player);
// [Assert] League
Expand All @@ -169,8 +180,8 @@ mod tests {
#[test]
#[should_panic(expected: ('League: player not subscribed',))]
fn test_unsubscribe_twice() {
let mut player = PlayerTrait::new(REGISTER_ID, PLAYER());
let mut league = LeagueTrait::new(REGISTER_ID, LEAGUE_ID);
let mut player = PlayerTrait::new(REGISTRY_ID, PLAYER(), PLAYER_NAME);
let mut league = LeagueTrait::new(REGISTRY_ID, LEAGUE_ID);
LeagueTrait::subscribe(ref league, ref player);
LeagueTrait::unsubscribe(ref league, ref player);
LeagueTrait::unsubscribe(ref league, ref player);
Expand Down
22 changes: 12 additions & 10 deletions examples/matchmaker/src/models/player.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct Player {
registry_id: u32,
#[key]
id: ContractAddress,
name: felt252,
league_id: u8,
index: u32,
rating: u32,
Expand All @@ -37,8 +38,8 @@ struct Player {
#[generate_trait]
impl PlayerImpl of PlayerTrait {
#[inline(always)]
fn new(registry_id: u32, id: ContractAddress) -> Player {
Player { registry_id, id, league_id: 0, index: 0, rating: DEFAULT_RATING, }
fn new(registry_id: u32, id: ContractAddress, name: felt252) -> Player {
Player { registry_id, id, name, league_id: 0, index: 0, rating: DEFAULT_RATING, }
}

#[inline(always)]
Expand Down Expand Up @@ -89,7 +90,7 @@ impl PlayerAssert of AssertTrait {
impl PlayerZeroable of Zeroable<Player> {
#[inline(always)]
fn zero() -> Player {
Player { registry_id: 0, id: ZERO(), league_id: 0, index: 0, rating: 0, }
Player { registry_id: 0, id: ZERO(), name: 0, league_id: 0, index: 0, rating: 0, }
}

#[inline(always)]
Expand Down Expand Up @@ -119,13 +120,14 @@ mod tests {
starknet::contract_address_const::<'PLAYER'>()
}

const REGISTER_ID: u32 = 1;
const PLAYER_NAME: felt252 = 'NAME';
const REGISTRY_ID: u32 = 1;

#[test]
fn test_new() {
let player_id = PLAYER();
let player = PlayerTrait::new(REGISTER_ID, player_id);
assert_eq!(player.registry_id, REGISTER_ID);
let player = PlayerTrait::new(REGISTRY_ID, player_id, PLAYER_NAME);
assert_eq!(player.registry_id, REGISTRY_ID);
assert_eq!(player.id, player_id);
assert_eq!(player.league_id, 0);
assert_eq!(player.index, 0);
Expand All @@ -135,23 +137,23 @@ mod tests {
#[test]
fn test_subscribable() {
let player_id = PLAYER();
let player = PlayerTrait::new(REGISTER_ID, player_id);
let player = PlayerTrait::new(REGISTRY_ID, player_id, PLAYER_NAME);
AssertTrait::assert_subscribable(player);
}

#[test]
#[should_panic(expected: ('Player: not subscribable',))]
fn test_subscribable_revert_not_subscribable() {
let player_id = PLAYER();
let mut player = PlayerTrait::new(REGISTER_ID, player_id);
let mut player = PlayerTrait::new(REGISTRY_ID, player_id, PLAYER_NAME);
player.league_id = 1;
AssertTrait::assert_subscribable(player);
}

#[test]
fn test_subscribed() {
let player_id = PLAYER();
let mut player = PlayerTrait::new(REGISTER_ID, player_id);
let mut player = PlayerTrait::new(REGISTRY_ID, player_id, PLAYER_NAME);
player.league_id = 1;
AssertTrait::assert_subscribed(player);
}
Expand All @@ -160,7 +162,7 @@ mod tests {
#[should_panic(expected: ('Player: not subscribed',))]
fn test_subscribed_revert_not_subscribed() {
let player_id = PLAYER();
let player = PlayerTrait::new(REGISTER_ID, player_id);
let player = PlayerTrait::new(REGISTRY_ID, player_id, PLAYER_NAME);
AssertTrait::assert_subscribed(player);
}
}
41 changes: 21 additions & 20 deletions examples/matchmaker/src/models/registry.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl RegistryImpl of RegistryTrait {
}

#[inline(always)]
fn search_league(mut self: Registry, mut league: League, mut player: Player) -> u8 {
fn search_league(ref self: Registry, ref league: League, ref player: Player) -> u8 {
// [Check] Player has subscribed
PlayerAssert::assert_subscribed(player);
// [Effect] Unsubcribe player from his league
Expand Down Expand Up @@ -110,6 +110,7 @@ mod tests {
starknet::contract_address_const::<'TARGET'>()
}

const PLAYER_NAME: felt252 = 'NAME';
const REGISTRY_ID: u32 = 1;
const LEAGUE_ID: u8 = 1;
const CLOSEST_LEAGUE_ID: u8 = 2;
Expand All @@ -127,7 +128,7 @@ mod tests {
#[test]
fn test_subscribe() {
let mut registry = RegistryTrait::new(REGISTRY_ID);
let mut player = PlayerTrait::new(REGISTRY_ID, PLAYER());
let mut player = PlayerTrait::new(REGISTRY_ID, PLAYER(), PLAYER_NAME);
let mut league = LeagueTrait::new(REGISTRY_ID, LEAGUE_ID);
registry.subscribe(ref league, ref player);
// [Assert] Registry
Expand All @@ -137,7 +138,7 @@ mod tests {
#[test]
fn test_unsubscribe() {
let mut registry = RegistryTrait::new(REGISTRY_ID);
let mut player = PlayerTrait::new(REGISTRY_ID, PLAYER());
let mut player = PlayerTrait::new(REGISTRY_ID, PLAYER(), PLAYER_NAME);
let mut league = LeagueTrait::new(REGISTRY_ID, LEAGUE_ID);
registry.subscribe(ref league, ref player);
registry.unsubscribe(ref league, ref player);
Expand All @@ -149,11 +150,11 @@ mod tests {
fn test_search_league_same() {
let mut registry = RegistryTrait::new(REGISTRY_ID);
let mut league = LeagueTrait::new(REGISTRY_ID, LEAGUE_ID);
let mut player = PlayerTrait::new(REGISTRY_ID, PLAYER());
let mut player = PlayerTrait::new(REGISTRY_ID, PLAYER(), PLAYER_NAME);
registry.subscribe(ref league, ref player);
let mut foe = PlayerTrait::new(REGISTRY_ID, TARGET());
let mut foe = PlayerTrait::new(REGISTRY_ID, TARGET(), PLAYER_NAME);
registry.subscribe(ref league, ref foe);
let league_id = registry.search_league(league, player);
let league_id = registry.search_league(ref league, ref player);
// [Assert] Registry
assert(league_id == LEAGUE_ID, 'Registry: wrong search league');
}
Expand All @@ -162,12 +163,12 @@ mod tests {
fn test_search_league_close() {
let mut registry = RegistryTrait::new(REGISTRY_ID);
let mut league = LeagueTrait::new(REGISTRY_ID, LEAGUE_ID);
let mut player = PlayerTrait::new(REGISTRY_ID, PLAYER());
let mut player = PlayerTrait::new(REGISTRY_ID, PLAYER(), PLAYER_NAME);
registry.subscribe(ref league, ref player);
let mut foe_league = LeagueTrait::new(REGISTRY_ID, CLOSEST_LEAGUE_ID);
let mut foe = PlayerTrait::new(REGISTRY_ID, TARGET());
let mut foe = PlayerTrait::new(REGISTRY_ID, TARGET(), PLAYER_NAME);
registry.subscribe(ref foe_league, ref foe);
let league_id = registry.search_league(league, player);
let league_id = registry.search_league(ref league, ref player);
// [Assert] Registry
assert(league_id == CLOSEST_LEAGUE_ID, 'Registry: wrong search league');
}
Expand All @@ -176,12 +177,12 @@ mod tests {
fn test_search_league_target() {
let mut registry = RegistryTrait::new(REGISTRY_ID);
let mut league = LeagueTrait::new(REGISTRY_ID, LEAGUE_ID);
let mut player = PlayerTrait::new(REGISTRY_ID, PLAYER());
let mut player = PlayerTrait::new(REGISTRY_ID, PLAYER(), PLAYER_NAME);
registry.subscribe(ref league, ref player);
let mut foe_league = LeagueTrait::new(REGISTRY_ID, TARGET_LEAGUE_ID);
let mut foe = PlayerTrait::new(REGISTRY_ID, TARGET());
let mut foe = PlayerTrait::new(REGISTRY_ID, TARGET(), PLAYER_NAME);
registry.subscribe(ref foe_league, ref foe);
let league_id = registry.search_league(league, player);
let league_id = registry.search_league(ref league, ref player);
// [Assert] Registry
assert(league_id == TARGET_LEAGUE_ID, 'Registry: wrong search league');
}
Expand All @@ -190,12 +191,12 @@ mod tests {
fn test_search_league_far_down_top() {
let mut registry = RegistryTrait::new(REGISTRY_ID);
let mut league = LeagueTrait::new(REGISTRY_ID, LEAGUE_ID);
let mut player = PlayerTrait::new(REGISTRY_ID, PLAYER());
let mut player = PlayerTrait::new(REGISTRY_ID, PLAYER(), PLAYER_NAME);
registry.subscribe(ref league, ref player);
let mut foe_league = LeagueTrait::new(REGISTRY_ID, FAREST_LEAGUE_ID);
let mut foe = PlayerTrait::new(REGISTRY_ID, TARGET());
let mut foe = PlayerTrait::new(REGISTRY_ID, TARGET(), PLAYER_NAME);
registry.subscribe(ref foe_league, ref foe);
let league_id = registry.search_league(league, player);
let league_id = registry.search_league(ref league, ref player);
// [Assert] Registry
assert(league_id == FAREST_LEAGUE_ID, 'Registry: wrong search league');
}
Expand All @@ -204,12 +205,12 @@ mod tests {
fn test_search_league_far_top_down() {
let mut registry = RegistryTrait::new(REGISTRY_ID);
let mut league = LeagueTrait::new(REGISTRY_ID, FAREST_LEAGUE_ID);
let mut player = PlayerTrait::new(REGISTRY_ID, PLAYER());
let mut player = PlayerTrait::new(REGISTRY_ID, PLAYER(), PLAYER_NAME);
registry.subscribe(ref league, ref player);
let mut foe_league = LeagueTrait::new(REGISTRY_ID, LEAGUE_ID);
let mut foe = PlayerTrait::new(REGISTRY_ID, TARGET());
let mut foe = PlayerTrait::new(REGISTRY_ID, TARGET(), PLAYER_NAME);
registry.subscribe(ref foe_league, ref foe);
let league_id = registry.search_league(league, player);
let league_id = registry.search_league(ref league, ref player);
// [Assert] Registry
assert(league_id == LEAGUE_ID, 'Registry: wrong search league');
}
Expand All @@ -219,8 +220,8 @@ mod tests {
fn test_search_league_revert_empty() {
let mut registry = RegistryTrait::new(REGISTRY_ID);
let mut league = LeagueTrait::new(REGISTRY_ID, LEAGUE_ID);
let mut player = PlayerTrait::new(REGISTRY_ID, PLAYER());
let mut player = PlayerTrait::new(REGISTRY_ID, PLAYER(), PLAYER_NAME);
registry.subscribe(ref league, ref player);
registry.search_league(league, player);
registry.search_league(ref league, ref player);
}
}
13 changes: 10 additions & 3 deletions examples/matchmaker/src/models/slot.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use starknet::ContractAddress;

// Internal imports

use matchmaker::constants::ZERO;
use matchmaker::models::player::{Player, PlayerTrait};

#[derive(Model, Copy, Drop, Serde)]
Expand All @@ -28,6 +29,11 @@ impl SlotImpl of SlotTrait {
player_id: player.id,
}
}

#[inline(always)]
fn nullify(ref self: Slot) {
self.player_id = ZERO();
}
}

#[cfg(test)]
Expand All @@ -46,18 +52,19 @@ mod tests {
starknet::contract_address_const::<'PLAYER'>()
}

const REGISTER_ID: u32 = 1;
const PLAYER_NAME: felt252 = 'NAME';
const REGISTRY_ID: u32 = 1;
const LEAGUE_ID: u8 = 2;
const INDEX: u32 = 3;

#[test]
fn test_new() {
let player_id = PLAYER();
let mut player = PlayerTrait::new(REGISTER_ID, player_id);
let mut player = PlayerTrait::new(REGISTRY_ID, player_id, PLAYER_NAME);
player.league_id = LEAGUE_ID;
player.index = INDEX;
let slot = SlotTrait::new(player);
assert_eq!(slot.registry_id, REGISTER_ID);
assert_eq!(slot.registry_id, REGISTRY_ID);
assert_eq!(slot.league_id, LEAGUE_ID);
assert_eq!(slot.index, INDEX);
assert_eq!(slot.player_id, player_id);
Expand Down
Loading

0 comments on commit 8e0c127

Please sign in to comment.