Skip to content

Commit

Permalink
Merge branch 'gear-foundation:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Madib036 authored Feb 10, 2024
2 parents 5f51c20 + f94687e commit e32b543
Show file tree
Hide file tree
Showing 8 changed files with 853 additions and 60 deletions.
2 changes: 1 addition & 1 deletion contracts/battleship/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition.workspace = true
publish.workspace = true

[dependencies]
gstd.workspace = true
gstd = { workspace = true, features = ["debug"] }
battleship-io.workspace = true

[dev-dependencies]
Expand Down
18 changes: 16 additions & 2 deletions contracts/battleship/bot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,25 @@ extern fn handle() {
match action {
BotBattleshipAction::Start => {
let ships = generate_field();
msg::reply(BattleshipAction::StartGame { ships }, 0).expect("Error in sending a reply");
msg::reply(
BattleshipAction::StartGame {
ships,
session_for_account: None,
},
0,
)
.expect("Error in sending a reply");
}
BotBattleshipAction::Turn(board) => {
let step = move_analysis(board);
msg::reply(BattleshipAction::Turn { step }, 0).expect("Error in sending a reply");
msg::reply(
BattleshipAction::Turn {
step,
session_for_account: None,
},
0,
)
.expect("Error in sending a reply");
}
}
}
Expand Down
79 changes: 73 additions & 6 deletions contracts/battleship/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use gstd::{
ActorId,
};

// Minimum duration of session: 3 mins = 180_000 ms = 60 blocks
pub const MINIMUM_SESSION_SURATION_MS: u64 = 180_000;

pub struct BattleshipMetadata;

impl Metadata for BattleshipMetadata {
Expand All @@ -25,15 +28,17 @@ pub enum StateQuery {
All,
Game(ActorId),
BotContractId,
SessionForTheAccount(ActorId),
}

#[derive(Encode, Decode, TypeInfo)]
#[derive(Encode, Decode, TypeInfo, Debug)]
#[codec(crate = gstd::codec)]
#[scale_info(crate = gstd::scale_info)]
pub enum StateReply {
All(BattleshipState),
Game(Option<GameState>),
BotContractId(ActorId),
SessionForTheAccount(Option<Session>),
}

#[derive(Debug, Clone, Encode, Decode, TypeInfo)]
Expand All @@ -45,6 +50,28 @@ pub struct BattleshipState {
pub admin: ActorId,
}

// This structure is for creating a gaming session, which allows players to predefine certain actions for an account that will play the game on their behalf for a certain period of time.
// Sessions can be used to send transactions from a dApp on behalf of a user without requiring their confirmation with a wallet.
// The user is guaranteed that the dApp can only execute transactions that comply with the allowed_actions of the session until the session expires.
#[derive(Debug, Clone, Encode, Decode, TypeInfo, PartialEq, Eq)]
#[codec(crate = gstd::codec)]
#[scale_info(crate = gstd::scale_info)]
pub struct Session {
// the address of the player who will play on behalf of the user
pub key: ActorId,
// until what time the session is valid
pub expires: u64,
// what messages are allowed to be sent by the account (key)
pub allowed_actions: Vec<ActionsForSession>,
}

#[derive(Debug, Clone, Encode, Decode, TypeInfo, PartialEq, Eq)]
#[codec(crate = gstd::codec)]
#[scale_info(crate = gstd::scale_info)]
pub enum ActionsForSession {
StartGame,
Turn,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode, TypeInfo)]
#[codec(crate = gstd::codec)]
#[scale_info(crate = gstd::scale_info)]
Expand All @@ -68,20 +95,59 @@ impl Entity {
#[codec(crate = gstd::codec)]
#[scale_info(crate = gstd::scale_info)]
pub enum BattleshipAction {
StartGame { ships: Ships },
Turn { step: u8 },
ChangeBot { bot: ActorId },
ClearState { leave_active_games: bool },
DeleteGame { player_address: ActorId },
StartGame {
ships: Ships,
session_for_account: Option<ActorId>,
},
Turn {
step: u8,
session_for_account: Option<ActorId>,
},
ChangeBot {
bot: ActorId,
},
ClearState {
leave_active_games: bool,
},
DeleteGame {
player_address: ActorId,
},
CreateSession {
key: ActorId,
duration: u64,
allowed_actions: Vec<ActionsForSession>,
},
DeleteSessionFromProgram {
account: ActorId,
},
DeleteSessionFromAccount,
UpdateConfig {
gas_for_start: Option<u64>,
gas_for_move: Option<u64>,
gas_to_delete_session: Option<u64>,
block_duration_ms: Option<u64>,
},
}

#[derive(Debug, Clone, Default, Encode, Decode, TypeInfo)]
#[codec(crate = gstd::codec)]
#[scale_info(crate = gstd::scale_info)]
pub struct Config {
pub gas_for_start: u64,
pub gas_for_move: u64,
pub gas_to_delete_session: u64,
pub block_duration_ms: u64,
}
#[derive(Debug, Clone, Encode, Decode, TypeInfo)]
#[codec(crate = gstd::codec)]
#[scale_info(crate = gstd::scale_info)]
pub enum BattleshipReply {
MessageSentToBot,
EndGame(BattleshipParticipants),
BotChanged(ActorId),
SessionCreated,
SessionDeleted,
ConfigUpdated,
}

#[derive(Debug, Clone, Encode, Decode, TypeInfo)]
Expand All @@ -98,6 +164,7 @@ pub enum Step {
#[scale_info(crate = gstd::scale_info)]
pub struct BattleshipInit {
pub bot_address: ActorId,
pub config: Config,
}

#[derive(Debug, Clone, Encode, Decode, TypeInfo, Default)]
Expand Down
Loading

0 comments on commit e32b543

Please sign in to comment.