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

Modified game and player account creation endpoints for frontend integration #21

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
34 changes: 11 additions & 23 deletions node/src/api/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ use aze_lib::storage::GameStorageSlotData;

Copy link
Member

Choose a reason for hiding this comment

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

Please don't raise pr from develop. We will only raise pr from develop once the feature is quite well tested.

use aze_types::accounts::{
AccountCreationError,
AccountCreationRequest,
AccountCreationResponse,
PlayerAccountCreationRequest,
PlayerAccountCreationResponse,
};
use aze_lib::utils::log_account_status;
use miden_lib::AuthScheme;
use miden_objects::{
accounts:: AccountId,
assets::TokenSymbol,
assets::{ Asset, FungibleAsset },
crypto::dsa::rpo_falcon512::{ PublicKey, SecretKey },
Expand All @@ -31,28 +34,17 @@ use miden_client::client::{
transactions::transaction_request::TransactionTemplate,
};

use actix_web::{ get, web::Json };
use actix_web::{ get, post, web::Json };

// TODO: pass account id of the players as request object in this game
#[get("/v1/game/create-account")]
pub async fn create_aze_game_account() -> Result<
#[post("/v1/game/create-account")]
pub async fn create_aze_game_account(request_object: Json<AccountCreationRequest>) -> Result<
Json<AccountCreationResponse>,
AccountCreationError
> {
let mut client: AzeClient = create_aze_client();
let slot_data = GameStorageSlotData::new(0, 0, 0, 0, 0, 0);

// TODO: creating player just for testing purposes
let (player_account, _) = client
.new_game_account(
AzeAccountTemplate::PlayerAccount {
mutable_code: false,
storage_mode: AccountStorageMode::Local, // for now
},
None
)
.unwrap();

let (faucet_account, _) = client
.new_account(AccountTemplate::FungibleFaucet {
token_symbol: TokenSymbol::new("MATIC").unwrap(),
Expand All @@ -66,7 +58,7 @@ pub async fn create_aze_game_account() -> Result<
let fungible_asset = FungibleAsset::new(faucet_account_id, BUY_IN_AMOUNT).unwrap();

// TODO: get the player account ids from the request object
let player_account_ids = vec![player_account.id()];
let player_account_ids = request_object.game_player_ids.clone();

let (game_account, _) = client
.new_game_account(
Expand Down Expand Up @@ -102,11 +94,9 @@ pub async fn create_aze_game_account() -> Result<

println!("Start sending cards to players");
for (i, _) in player_account_ids.iter().enumerate() {
let target_account_id = player_account_ids[i];
let target_account_id = AccountId::try_from(player_account_ids[i]).unwrap();
println!("Target account id {:?}", target_account_id);

log_account_status(&client, target_account_id).await;

let input_cards = [cards[i], cards[i + 1]];
let sendcard_txn_data = SendCardTransactionData::new(
Asset::Fungible(fungible_asset),
Expand All @@ -128,15 +118,14 @@ pub async fn create_aze_game_account() -> Result<
execute_tx_and_sync(&mut client, tx_request).await;

println!("Executed and synced with node");
log_account_status(&client, target_account_id).await;
}

// TODO: define appropriate response types
Ok(Json(AccountCreationResponse { is_created: true }))
Ok(Json(AccountCreationResponse { game_id: game_account_id.into() }))
}

#[get("/v1/player/create-account")]
pub async fn create_aze_player_account() -> Result<
#[post("/v1/player/create-account")]
pub async fn create_aze_player_account(request_object: Json<PlayerAccountCreationRequest>) -> Result<
Json<PlayerAccountCreationResponse>,
AccountCreationError
> {
Expand All @@ -160,7 +149,6 @@ pub async fn create_aze_player_account() -> Result<

Ok(
Json(PlayerAccountCreationResponse {
is_created: true,
account_id: game_account.id().into(),
})
)
Expand Down
16 changes: 14 additions & 2 deletions types/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,26 @@ use actix_web::{
use derive_more::Display;
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
pub struct AccountCreationRequest {
pub game_player_ids: Vec<u64>,
pub big_blind: u8,
pub small_blind: u8,
pub buy_in: u8,
}

#[derive(Deserialize, Serialize)]
pub struct AccountCreationResponse {
pub is_created: bool,
pub game_id: u64,
}

#[derive(Deserialize, Serialize)]
pub struct PlayerAccountCreationRequest {
pub username: String,
}

#[derive(Deserialize, Serialize)]
pub struct PlayerAccountCreationResponse {
pub is_created: bool,
pub account_id: u64,
}

Expand Down
Loading