Skip to content
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

Add round number #661

Merged
merged 3 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions contracts/button/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub enum GameError {
InkEnvError(String),
/// Couldn't have retrieved own code hash
CantRetrieveOwnCodeHash,
/// Overflow error
Arithmethic,
}

impl From<PSP22Error> for GameError {
Expand Down
35 changes: 21 additions & 14 deletions contracts/button/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ mod button_game {
pub marketplace: AccountId,
/// scoring strategy
pub scoring: Scoring,
/// current round number
pub round: u64,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: change to tyle Round(u64)

}

impl AccessControlled for ButtonGame {
Expand All @@ -117,7 +119,7 @@ mod button_game {
let required_role = Role::Initializer(code_hash);
let access_control = AccountId::from(ACCESS_CONTROL_PUBKEY);

match ButtonGame::check_role(&access_control, &caller, required_role) {
match ButtonGame::check_role(access_control, caller, required_role) {
Ok(_) => Self::init(
ticket_token,
reward_token,
Expand All @@ -137,6 +139,12 @@ mod button_game {
self.last_press + self.button_lifetime
}

/// Returns the curent round number
#[ink(message)]
pub fn round(&self) -> u64 {
self.round
}

/// Returns the buttons status
#[ink(message)]
pub fn is_dead(&self) -> bool {
Expand Down Expand Up @@ -232,7 +240,7 @@ mod button_game {
pub fn reset(&mut self) -> ButtonResult<()> {
self.ensure_dead()?;
self.reward_pressiah()?;
self.reset_state();
self.reset_state()?;
self.transfer_tickets_to_marketplace()?;
self.reset_marketplace()
}
Expand All @@ -246,7 +254,7 @@ mod button_game {
let caller = self.env().caller();
let this = self.env().account_id();
let required_role = Role::Owner(this);
ButtonGame::check_role(&self.access_control, &caller, required_role)?;
ButtonGame::check_role(self.access_control, caller, required_role)?;
self.access_control = new_access_control;
Ok(())
}
Expand All @@ -259,7 +267,7 @@ mod button_game {
let caller = self.env().caller();
let this = self.env().account_id();
let required_role = Role::Owner(this);
ButtonGame::check_role(&self.access_control, &caller, required_role)?;
ButtonGame::check_role(self.access_control, caller, required_role)?;
self.env().terminate_contract(caller)
}

Expand All @@ -286,6 +294,7 @@ mod button_game {
last_presser: None,
presses: 0,
total_rewards: 0,
round: 0,
};

Self::emit_event(
Expand All @@ -301,15 +310,17 @@ mod button_game {
contract
}

fn reset_state(&mut self) {
fn reset_state(&mut self) -> ButtonResult<()> {
let now = self.env().block_number();

self.presses = 0;
self.last_presser = None;
self.last_press = now;
self.total_rewards = 0;
self.round.checked_add(1).ok_or(GameError::Arithmethic)?;

Self::emit_event(self.env(), Event::GameReset(GameReset { when: now }));
Ok(())
}

fn reward_pressiah(&self) -> ButtonResult<()> {
Expand All @@ -323,7 +334,7 @@ mod button_game {

fn ensure_dead(&self) -> ButtonResult<()> {
if !self.is_dead() {
return Err(GameError::BeforeDeadline);
Err(GameError::BeforeDeadline)
} else {
Ok(())
}
Expand Down Expand Up @@ -370,22 +381,18 @@ mod button_game {
Ok(())
}

fn check_role(
access_control: &AccountId,
account: &AccountId,
role: Role,
) -> ButtonResult<()>
fn check_role(access_control: AccountId, account: AccountId, role: Role) -> ButtonResult<()>
where
Self: AccessControlled,
{
<Self as AccessControlled>::check_role(
access_control.clone(),
account.clone(),
access_control,
account,
role,
|why: InkEnvError| {
GameError::InkEnvError(format!("Calling access control has failed: {:?}", why))
},
|role: Role| GameError::MissingRole(role),
GameError::MissingRole,
)
}

Expand Down