Skip to content

Commit

Permalink
adding new method for delete player from registration
Browse files Browse the repository at this point in the history
  • Loading branch information
MedovTimur committed Oct 24, 2024
1 parent 11c9311 commit 287219d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
44 changes: 43 additions & 1 deletion contracts/battle/app/src/services/game/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,49 @@ pub fn cancel_register(storage: &mut Storage) -> Result<Event, BattleError> {
battle.participants.remove(&msg_src);
storage.players_to_battle_id.remove(&msg_src);

Ok(Event::RegisterCanceled)
Ok(Event::RegisterCanceled { player_id: msg_src })
}

pub fn delete_player(storage: &mut Storage, player_id: ActorId) -> Result<Event, BattleError> {
let msg_src = msg::source();
let admin_id = storage
.players_to_battle_id
.get(&msg_src)
.ok_or(BattleError::NoSuchPlayer)?;

let battle = storage
.battles
.get_mut(admin_id)
.ok_or(BattleError::NoSuchGame)?;

if battle.admin != msg_src {
return Err(BattleError::AccessDenied);
}

if battle.state != State::Registration {
return Err(BattleError::WrongState);
}

if !battle.participants.contains_key(&player_id) {
return Err(BattleError::NoSuchPlayer);
}

let reservation_id = battle
.reservation
.get(&player_id)
.ok_or(BattleError::NoSuchReservation)?;

if battle.bid != 0 {
msg::send_with_gas(player_id, "", 0, battle.bid).expect("Error in sending the value");
}
reservation_id
.unreserve()
.expect("Unreservation across executions");
battle.reservation.remove(&player_id);
battle.participants.remove(&player_id);
storage.players_to_battle_id.remove(&player_id);

Ok(Event::RegisterCanceled { player_id })
}

pub fn cancel_tournament(storage: &mut Storage) -> Result<Event, BattleError> {
Expand Down
21 changes: 18 additions & 3 deletions contracts/battle/app/src/services/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ pub enum Event {
user_name: String,
bid: u128,
},
RegisterCanceled,
RegisterCanceled {
player_id: ActorId,
},
BattleCanceled {
game_id: ActorId,
},
Expand Down Expand Up @@ -154,6 +156,11 @@ impl BattleService {
let event = services::utils::panicking(|| funcs::cancel_register(storage));
self.notify_on(event.clone()).expect("Notification Error");
}
pub fn delete_player(&mut self, player_id: ActorId) {
let storage = self.get_mut();
let event = services::utils::panicking(|| funcs::delete_player(storage, player_id));
self.notify_on(event.clone()).expect("Notification Error");
}
pub fn cancel_tournament(&mut self) {
let storage = self.get_mut();
let event = services::utils::panicking(|| funcs::cancel_tournament(storage));
Expand Down Expand Up @@ -214,12 +221,20 @@ impl BattleService {

pub fn get_battle(&self, game_id: ActorId) -> Option<BattleState> {
let storage = self.get();
storage.battles.get(&game_id).cloned().map(|battle| battle.into())
storage
.battles
.get(&game_id)
.cloned()
.map(|battle| battle.into())
}
pub fn get_my_battle(&self) -> Option<BattleState> {
let storage = self.get();
if let Some(game_id) = storage.players_to_battle_id.get(&msg::source()) {
storage.battles.get(game_id).cloned().map(|battle| battle.into())
storage
.battles
.get(game_id)
.cloned()
.map(|battle| battle.into())
} else {
None
}
Expand Down
7 changes: 2 additions & 5 deletions contracts/battle/app/src/services/game/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,8 @@ impl Battle {
pub fn create_pairs(&mut self, round_start_time: u64) {
self.pairs = HashMap::new();
self.players_to_pairs = HashMap::new();
let mut participants_vec: Vec<(ActorId, Player)> = self
.participants
.clone()
.into_iter()
.collect();
let mut participants_vec: Vec<(ActorId, Player)> =
self.participants.clone().into_iter().collect();

while participants_vec.len() > 1 {
let range = participants_vec.len() as u8;
Expand Down

0 comments on commit 287219d

Please sign in to comment.