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

Feat: Adding proc fold, its endpoint and its unit test #13

Merged
merged 14 commits into from
May 10, 2024
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
97 changes: 97 additions & 0 deletions lib/contracts/core/game.masm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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

# game events
# TODO: fix some storage for the value corresponding to these events
Expand Down Expand Up @@ -240,6 +241,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.13 mul
push.52 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
116 changes: 116 additions & 0 deletions lib/contracts/notes/game/fold.masm
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
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

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.13 mul
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since 52 and 13 would always remain fixed so can you create constants for storing these values. Or use them if you have already created one.

push.52 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
Loading