Skip to content

Commit

Permalink
Upgrade event leaderboard (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
MedovTimur authored Apr 6, 2024
1 parent 127af85 commit 6dadad4
Show file tree
Hide file tree
Showing 13 changed files with 545 additions and 331 deletions.
4 changes: 2 additions & 2 deletions contracts/auto-changed-nft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ unsafe extern fn handle() {
let gas_available = exec::gas_available();
gstd::debug!("Update. gas_available: {}", gas_available);
if gas_available <= GAS_FOR_UPDATE {
let reservations = unsafe { &mut RESERVATION };
let reservations: &mut Vec<ReservationId> = unsafe { RESERVATION.as_mut() };
let reservation_id = reservations.pop().expect("Need more gas");
send_delayed_from_reservation(
reservation_id,
Expand Down Expand Up @@ -337,7 +337,7 @@ impl AutoChangedNft {
}
}
fn reserve_gas(&self) {
let reservations = unsafe { &mut RESERVATION };
let reservations: &mut Vec<ReservationId> = unsafe { RESERVATION.as_mut() };
let reservation_id =
ReservationId::reserve(RESERVATION_AMOUNT, 600).expect("reservation across executions");
reservations.push(reservation_id);
Expand Down
57 changes: 40 additions & 17 deletions contracts/battleship/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct BattleshipMetadata;

impl Metadata for BattleshipMetadata {
type Init = In<BattleshipInit>;
type Handle = InOut<BattleshipAction, BattleshipReply>;
type Handle = InOut<BattleshipAction, Result<BattleshipReply, BattleshipError>>;
type Others = ();
type Reply = ();
type Signal = ();
Expand Down Expand Up @@ -130,12 +130,36 @@ pub struct Config {
}
#[derive(Debug, Clone, Encode, Decode, TypeInfo)]
pub enum BattleshipReply {
GameFinished(BattleshipParticipants),
MessageSentToBot,
EndGame(BattleshipParticipants),
BotChanged(ActorId),
SessionCreated,
SessionDeleted,
ConfigUpdated,
StateCleared,
GameDeleted,
}

#[derive(Debug, Clone, Encode, Decode, TypeInfo)]
pub enum BattleshipError {
GameIsAlreadyStarted,
GameIsNotStarted,
IncorrectLocationShips,
OutOfBounds,
GameIsAlreadyOver,
ThisCellAlreadyKnown,
BotDidNotInitializeBoard,
NotYourTurn,
NotAdmin,
WrongLength,
AccessDenied,
AlreadyHaveActiveSession,
NoMessagesForApprovalWerePassed,
DurationIsSmall,
HasNotValidSession,
SessionHasAlreadyExpired,
MessageIsNotAllowed,
NotApproved,
}

#[derive(Debug, Clone, Encode, Decode, TypeInfo)]
Expand Down Expand Up @@ -169,7 +193,7 @@ pub struct Game {

impl Game {
pub fn start_bot(&mut self, mut ships: Ships) {
let bot_board = ships.get_field();
let bot_board = ships.get_field().unwrap();
self.bot_board = bot_board;
ships.sort_by_length();
self.bot_ships = ships;
Expand Down Expand Up @@ -332,20 +356,19 @@ impl Ships {
let has_non_empty = !vectors.iter().any(|vector: &&Vec<u8>| !vector.is_empty());
has_non_empty
}
pub fn get_field(&self) -> Vec<Entity> {
pub fn get_field(&self) -> Result<Vec<Entity>, BattleshipError> {
let mut board = vec![Entity::Empty; 25];
for position in self.iter() {
assert!(
board[*position as usize] != Entity::Ship,
"Incorrect location of ships"
);
if board[*position as usize] == Entity::Ship {
return Err(BattleshipError::IncorrectLocationShips);
}
board[*position as usize] = Entity::Ship;
}
board
Ok(board)
}
pub fn check_correct_location(&self) -> bool {
pub fn check_correct_location(&self) -> Result<(), BattleshipError> {
if self.iter().any(|&position| position > 24) {
return false;
return Err(BattleshipError::OutOfBounds);
}
// ship size check
let mut vec_len = vec![
Expand All @@ -356,10 +379,10 @@ impl Ships {
];
vec_len.sort();
if vec_len != vec![1, 2, 2, 3] {
return false;
return Err(BattleshipError::WrongLength);
}
let mut field = self.get_field();
let mut ships = vec![
let mut field = self.get_field()?;
let mut ships = [
self.ship_1.clone(),
self.ship_2.clone(),
self.ship_3.clone(),
Expand All @@ -372,13 +395,13 @@ impl Ships {
match (ship.len(), distance) {
(1, 0) | (2, 1) | (2, 5) => (),
(3, 2) | (3, 10) if (ship[2] + ship[0]) % ship[1] == 0 => (),
_ => return false,
_ => return Err(BattleshipError::IncorrectLocationShips),
}
// checking the distance between ships
let mut occupy_cells = vec![];
for position in ship {
if field[*position as usize] == Entity::Occupied {
return false;
return Err(BattleshipError::IncorrectLocationShips);
}
let cells = match *position {
0 => vec![1, 5, 6],
Expand All @@ -403,7 +426,7 @@ impl Ships {
}
}

true
Ok(())
}

pub fn count_alive_ships(&self) -> Vec<(u8, u8)> {
Expand Down
Loading

0 comments on commit 6dadad4

Please sign in to comment.