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

Adding id caching and some proc fixes #58

Merged
merged 10 commits into from
Jun 28, 2024
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ Cargo.lock
Config.toml
ws_config.json
Player.toml
integration/

.DS_Store
1 change: 1 addition & 0 deletions cli/miden-client.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[rpc]
endpoint = { protocol = "http", host = "18.203.155.106", port = 57291 }
timeout_ms = 20000

[store]
database_filepath = "store.sqlite3"
16 changes: 12 additions & 4 deletions cli/src/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use miden_lib::AuthScheme;
use miden_objects::{
accounts::{AccountId, AuthSecretKey},
crypto::dsa::rpo_falcon512::{PublicKey, SecretKey},
notes::NoteType,
notes::{ NoteType, NoteTag, NoteExecutionHint },
Felt, FieldElement
};
use tokio::time::{ sleep, Duration };
Expand Down Expand Up @@ -132,14 +132,22 @@ pub async fn consume_game_notes(account_id: AccountId) {
let mut client: AzeClient = create_aze_client();
client.sync_state().await.unwrap();
let account = client.get_account(account_id).unwrap();
let expected_note_tag = NoteTag::from_account_id(account_id, NoteExecutionHint::Local).unwrap();
let consumable_notes = client.get_consumable_notes(Some(account_id)).unwrap();
println!("Consumable notes: {:?}", consumable_notes.len());
let filtered_notes: Vec<_> = consumable_notes
.into_iter()
.filter(
|consumable_note| consumable_note.note.metadata().unwrap().tag() == expected_note_tag
)
.collect();

println!("Consumable notes: {:?}", filtered_notes.len());

for consumable_note in consumable_notes {
for consumable_note in filtered_notes {
let tx_template = TransactionTemplate::ConsumeNotes(account_id, vec![consumable_note.note.id()]);
let tx_request = client.build_transaction_request(tx_template).unwrap();
execute_tx_and_sync(&mut client, tx_request).await;
sleep(Duration::from_secs(5)).await;
sleep(Duration::from_secs(2)).await;
}
}

Expand Down
66 changes: 51 additions & 15 deletions cli/src/action.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
use crate::actions;
use aze_lib::gamestate::Check_Action;
use aze_lib::utils::Ws_config;
use aze_lib::utils::{ Ws_config, Player };
use aze_lib::{
constants::{BUY_IN_AMOUNT, NO_OF_PLAYERS, SMALL_BLIND_AMOUNT},
constants::{BUY_IN_AMOUNT, NO_OF_PLAYERS, SMALL_BLIND_AMOUNT, PLAYER_FILE_PATH},
utils::validate_action,
};
use aze_types::actions::{ActionType, GameActionResponse};
use clap::{Parser, ValueEnum};
use dialoguer::{Input, Select};
use std::fs::File;
use std::io::Write;
use std::path::Path;

#[derive(Debug, Clone, Parser)]
pub struct ActionCmd {}

impl ActionCmd {
pub async fn execute(&self, ws_config_path: &std::path::PathBuf) -> Result<(), String> {
let playerid: u64 = Input::<String>::new()
.with_prompt("What is your player id?")
.interact()
.expect("Failed to get player id")
.parse()
.expect("Invalid player id");

let gameid: u64 = Input::<String>::new()
.with_prompt("What is the game id?")
.interact()
.expect("Failed to get game id")
.parse()
.expect("Invalid game id");
let (playerid, gameid) = get_or_prompt_ids();

let action_type = Select::new()
.with_prompt("What is your action type?")
Expand Down Expand Up @@ -87,6 +79,7 @@ async fn send_action(
},
ws_url,
player_id,
game_id.clone(),
)
.await.unwrap();
if result == false {
Expand All @@ -101,3 +94,46 @@ async fn send_action(
ActionType::Fold => actions::fold(player_id, game_id, ws_config_path).await,
}
}

fn get_or_prompt_ids() -> (u64, u64) {
let path = Path::new(PLAYER_FILE_PATH);
let mut player_id: u64 = 0;
let mut identifier: String = "".to_string();
if path.exists() {
let player: Player = toml::from_str(
&std::fs::read_to_string(path).expect("Failed to read Player.toml file"),
)
.expect("Failed to parse Player.toml file");

if let Some(game_id) = player.game_id() {
return (player.player_id(), game_id);
}
else {
player_id = player.player_id();
identifier = player.identifier();
}
}
else {
player_id = Input::<String>::new()
.with_prompt("What is your player id?")
.interact()
.expect("Failed to get player id")
.parse()
.expect("Invalid player id");
}

let game_id: u64 = Input::<String>::new()
.with_prompt("What is the game id?")
.interact()
.expect("Failed to get game id")
.parse()
.expect("Invalid game id");

let player = Player::new(player_id, identifier, Some(game_id));
let toml_string = toml::to_string(&player).expect("Failed to serialize player data");
let mut file = File::create(&path).expect("Failed to create Player.toml file");
file.write_all(toml_string.as_bytes())
.expect("Failed to write player data to Player.toml file");

(player_id, game_id)
}
Loading
Loading