Skip to content

Commit

Permalink
Merge pull request #13 from RizeLabs/rg/add-play-check
Browse files Browse the repository at this point in the history
Feat: Adding proc fold, its endpoint and its unit test
  • Loading branch information
nlok5923 authored May 10, 2024
2 parents 11d0b90 + 4d75c70 commit 03a3481
Show file tree
Hide file tree
Showing 13 changed files with 3,827 additions and 3,416 deletions.
6,573 changes: 3,287 additions & 3,286 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ miden-tx = { git = "https://github.com/0xPolygonMiden/miden-base.git", branch =
# miden-objects = { git = "https://github.com/0xPolygonMiden/miden-base.git", branch = "main", version = "0.2", features = ["serde"] }
miden-client = { git = "https://github.com/0xPolygonMiden/miden-client.git", branch = "main", features= ["concurrent","testing"] }
miden-objects = { version = "0.2", default-features = false }
aze-types = { path = "../types" }

[dev-dependencies]

[build-dependencies]
Expand Down
98 changes: 98 additions & 0 deletions lib/contracts/core/game.masm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const.HIGHEST_BET_SLOT_INDEX=61 # highest bet amount which player will try to ma
const.CURRENT_TURN_INDEX=60 # index of the player whose turn it is currently
const.CURRENT_TURN_PLAYER_PUB_KEY_INDEX=60 # need to check it's storage initially small blind public key on slot 62
const.CURRENT_PHASE=62 # whole game is divided into 4 phases
const.PLAYER_STATS_SLOTS=13
const.CARDS_SLOTS=52

# game events
# TODO: fix some storage for the value corresponding to these events
Expand Down Expand Up @@ -240,6 +242,102 @@ export.play_raise
# []
end


export.play_fold
# make is_fold of player 1
# inc turn
push.CURRENT_TURN_INDEX exec.account::get_item
# => [0, 0, 0, current_turn_index]

drop drop drop
# => [current_turn_index]

push.9
# 9, current_turn_index
add
# => [current_turn_index + 9]
dup #for getting the get item
dup #for updating the turn
# => [current_turn_index + 9, current_turn_index + 9, current_turn_index + 9]

# getting the is_fold of player
exec.account::get_item
drop drop drop
# => [is_fold, current_turn_index + 9, current_turn_index + 9]

# making sure that is_fold is zero currently
assertz
# => [current_turn_index + 9, current_turn_index + 9]
push.1
# => [1, current_turn_index + 9, current_turn_index + 9]

swap padw drop movup.3
# => [current_turn_index + 9, 0, 0, 0, 1, current_turn_index + 9]
exec.account::set_item
dropw drop drop drop
# => [current_turn_index + 9]

# adding 4 will give the slot of next player
push.4 add
# => [next_turn_index]

# find last player's index
push.NO_OF_PLAYERS_INDEX exec.account::get_item
drop drop drop
# => [no_of_players, next_turn_index]

# Calculate last_player_index = 52 + 13 * no_of_players + 0
push.PLAYER_STATS_SLOTS mul
push.CARDS_SLOTS add
# => [last_player_index, next_turn_index]

mem_store.0
# => [next_turn_index]

push.1
# => [1, next_turn_index]

while.true

dup push.9 add
# => [next_turn_index + 9, next_turn_index]
exec.account::get_item
drop drop drop
# => [is_fold, next_turn_index]

if.true
# if player has folded
dup mem_load.0 lt
# => [0/1, next_turn_index]

if.true
push.PLAYER_STATS_SLOTS add
push.1
# => [1, next_turn_index + 13]
else
push.0
end
else
# checking is fold
# => [next_turn_index]

padw drop
# => [0, 0, 0, next_turn_index]

push.CURRENT_TURN_INDEX # slot of current turn
# => [CURRENT_TURN_INDEX, 0, 0, 0, next_turn_index]

exec.account::set_item
dropw dropw
# => [...]
push.0
end
end
dropw
# => [...]
end


# some basic account methods

export.basic_wallet::receive_asset
Expand Down
117 changes: 117 additions & 0 deletions lib/contracts/notes/game/fold.masm
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use.miden::account
use.miden::note
use.miden::contracts::wallets::basic->wallet

const.NO_OF_PLAYERS_INDEX=57
const.CURRENT_TURN_INDEX=60
const.PLAYER_STATS_SLOTS=13
const.CARDS_SLOTS=52

proc.play_fold
push.CURRENT_TURN_INDEX exec.account::get_item
# => [0, 0, 0, current_turn_index]

drop drop drop
# => [current_turn_index]

push.9
# 9, current_turn_index
add
# => [current_turn_index + 9]
dup #for getting the get item
dup #for updating the turn
# => [current_turn_index + 9, current_turn_index + 9, current_turn_index + 9]

# getting the is_fold of player
exec.account::get_item
drop drop drop
# => [is_fold, current_turn_index + 9, current_turn_index + 9]

# making sure that is_fold is zero currently
assertz
# => [current_turn_index + 9, current_turn_index + 9]
push.1
# => [1, current_turn_index + 9, current_turn_index + 9]

swap padw drop movup.3
# => [current_turn_index + 9, 0, 0, 0, 1, current_turn_index + 9]
exec.account::set_item
dropw drop drop drop
# => [current_turn_index + 9]

# adding 4 will give the slot of next player
push.4 add
# => [next_turn_index]

# find last player's index
push.NO_OF_PLAYERS_INDEX exec.account::get_item
drop drop drop
# => [no_of_players, next_turn_index]

# Calculate last_player_index = 52 + 13 * no_of_players + 0
push.PLAYER_STATS_SLOTS mul
push.CARDS_SLOTS add
# => [last_player_index, next_turn_index]

mem_store.0
# => [next_turn_index]

push.1
# => [1, next_turn_index]

while.true

dup push.9 add
# => [next_turn_index + 9, next_turn_index]
exec.account::get_item
drop drop drop
# => [is_fold, next_turn_index]

if.true
# if player has folded
dup mem_load.0 lt
# => [0/1, next_turn_index]

if.true
push.PLAYER_STATS_SLOTS add
push.1
# => [1, next_turn_index + 13]
else
push.0
end
else
# checking is fold
# => [next_turn_index]

padw drop
# => [0, 0, 0, next_turn_index]

push.CURRENT_TURN_INDEX # slot of current turn
# => [CURRENT_TURN_INDEX, 0, 0, 0, next_turn_index]

exec.account::set_item
dropw dropw
# => [...]
push.0
end
end
dropw
# => [...]
end

begin
dropw

call.play_fold
# => [...]

dropw
exec.note::get_assets drop mem_loadw
# => [ASSET, ...]

# load the asset and add it to the account
call.wallet::receive_asset
# => [...]

dropw
end
Empty file removed lib/contracts/poker/poker.masm
Empty file.
5 changes: 3 additions & 2 deletions lib/src/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use miden_objects::{

use miden_lib::{ transaction::TransactionKernel, AuthScheme };
use crate::storage::GameStorageSlotData;
use crate::constants::PLAYER_STATS_SLOTS;

fn construct_game_constructor_storage(
auth_scheme: AuthScheme,
Expand Down Expand Up @@ -173,10 +174,10 @@ fn construct_game_constructor_storage(
];
player_pub_keys.extend(player_slots);

slot_index += 8; // since the mid 9 elements would cover the player stats and initially all those values are zero
slot_index += PLAYER_STATS_SLOTS; // since the mid 13 elements would cover the player stats and initially all those values are zero
}

// merghe player_id with card_suit
// merge player_id with card_suit
game_info.push(auth_slot);
game_info.extend(cards);
game_info.extend(game_stats);
Expand Down
Loading

0 comments on commit 03a3481

Please sign in to comment.