SubstrateChess / pallet-chess

The Unlicense
11 stars 6 forks source link

Match Timer via on_initialize hook #3

Closed bernardoaraujor closed 1 year ago

bernardoaraujor commented 1 year ago

With the current implementation, the NextMove player could simply decide not to make any more moves. The match will not progress, and the funds will be locked forever (potentially against the oppponent's will).

The ideal solution is to implement a Match Timer.

An enum called MatchStyle provides different options of time-to-move. E.g:

pub enum MatchStyle {
    Bullet,
    Blitz,
    Rapid,
    Daily,
}

This enum is added to Match, along with a last_move: T::BlockNumber parameter:

pub struct Match<T: Config> {
    pub challenger: T::AccountId,
    pub opponent: T::AccountId,
    pub board: Vec<u8>,
    pub state: MatchState,
    pub nonce: u128,
+   pub match_style: MatchStyle,
+   pub last_move: T::BlockNumber,
}

A on_initialize hook continuously checks the last_move of every ongoing Match on storage, and compares it to the BlockNumber of the current block. If the delta is bigger than the threshold established by MatchStyle, that means the player defined by NextMove ran out of time for their move, and they must lose the match.