Skip to content

Commit

Permalink
Account for LangError
Browse files Browse the repository at this point in the history
  • Loading branch information
athei committed Jan 4, 2023
1 parent 007c218 commit 51926d6
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions game/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub use squink_splash::{Field, SquinkSplash, SquinkSplashRef};
pub use squink_splash::{
Field,
SquinkSplash,
SquinkSplashRef,
};

#[ink::contract]
mod squink_splash {
use core::ops::{Mul, RangeInclusive};
use core::ops::{
Mul,
RangeInclusive,
};
use ink::{
env::{
call::{build_call, Call, ExecutionInput, Selector},
CallFlags, DefaultEnvironment,
call::{
build_call,
Call,
ExecutionInput,
Selector,
},
debug_println,
CallFlags,
DefaultEnvironment,
},
prelude::{
collections::BTreeMap,
string::String,
vec::Vec,
},
prelude::{collections::BTreeMap, string::String, vec::Vec},
storage::{Lazy, Mapping},
storage::{
Lazy,
Mapping,
},
LangError,
};

/// The amount of players that are allowed to register for a single game.
Expand Down Expand Up @@ -90,10 +112,10 @@ mod squink_splash {
}

/// Describing either a single point in the field or its dimensions.
#[derive(scale::Decode, scale::Encode, Clone, Copy)]
#[derive(scale::Decode, scale::Encode, Clone, Copy, Debug)]
#[cfg_attr(
feature = "std",
derive(Debug, scale_info::TypeInfo, ink::storage::traits::StorageLayout)
derive(scale_info::TypeInfo, ink::storage::traits::StorageLayout)
)]
pub struct Field {
/// The width component.
Expand Down Expand Up @@ -359,16 +381,16 @@ mod squink_splash {
.call_type(Call::new().callee(player.id))
.exec_input(ExecutionInput::new(Selector::from([0x00; 4])))
.call_flags(CallFlags::default().set_allow_reentry(true))
.returns::<Field>();
.returns::<Result<Field, LangError>>();

let gas_before = self.env().gas_left();
let turn = call.fire();
player.gas_used += gas_before - self.env().gas_left();

// We don't bubble up the error cause we still want to record the gas usage
// and disallow another try. This should be enough punishment for a defunct contract.
let outcome = match turn.as_ref() {
Ok(&turn) => {
let outcome = match turn {
Ok(Ok(turn)) => {
if !self.is_valid_coord(&turn) {
TurnOutcome::OutOfBounds { turn }
} else {
Expand All @@ -380,8 +402,8 @@ mod squink_splash {
}
}
}
Err(err) => {
ink::env::debug_println!("Contract failed to make a turn: {:?}", err);
err => {
debug_println!("Contract failed to make a turn: {:?}", err);
TurnOutcome::BrokenPlayer
}
};
Expand Down

0 comments on commit 51926d6

Please sign in to comment.