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

Account for LangError #31

Merged
merged 1 commit into from
Jan 4, 2023
Merged
Changes from all commits
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
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