extern crate dcss_api;
use dcss_api::Webtile;
use dcss_api::{BlockingError, Error as APIError};
use serde_json::Value;
use std::process;
fn main() {
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
let gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
while webtile.get_message().is_some() {}
webtile
.start_game_seeded(&gameid[0], "1", false, "b", "i", "b")
.expect("Failed to start game");
while let Some(message) = webtile.get_message() {
processor(&message);
}
write_key_bot(&mut webtile, "key_dir_n", "player").expect("Failed");
write_key_bot(&mut webtile, "key_dir_s", "player").expect("Failed");
webtile.quit_game().expect("Failed to quit");
webtile.disconnect().expect("Failed to disconnect");
}
fn write_key_bot(webtile: &mut Webtile, to_send: &str, to_receive: &str) -> Result<(), APIError> {
println!("SEND: {}", to_send);
webtile.write_key(to_send)?;
if let Err(e) = webtile.read_until(to_receive, None, None) {
match e {
APIError::Blocking(BlockingError::More) => webtile.write_key(" ")?,
APIError::Blocking(BlockingError::TextInput) => {
println!("ERROR: Likely level up choice");
}
APIError::Blocking(BlockingError::Pickup) => println!("ERROR: Pickup"),
APIError::Blocking(BlockingError::Acquirement) => println!("ERROR: Acquirement"),
APIError::Blocking(BlockingError::Identify) => println!("ERROR: Identify"),
APIError::Blocking(BlockingError::EnchantWeapon) => println!("ERROR: EnchantWeapon"),
APIError::Blocking(BlockingError::EnchantItem) => println!("ERROR: EnchantItem"),
APIError::Blocking(BlockingError::BrandWeapon) => println!("ERROR: BrandWeapon"),
APIError::Blocking(BlockingError::Died) => {
println!("ERROR: Died");
process::exit(0);
}
_ => Err(e)?,
}
}
while let Some(message) = webtile.get_message() {
processor(&message);
}
Ok(())
}
fn processor(message: &Value) {
let msg = message["msg"].as_str().unwrap();
match msg {
"ping" => (),
"lobby_clear" => (),
"go_lobby" => (),
"html" => (),
"set_game_links" => (),
"game_client" => (),
"chat" => (),
"version" => (),
"options" => (),
"layout" => (),
"ui-state-sync" => (),
"text_cursor" => (),
"cursor" => (),
"ui_state" => (),
"flash" => (),
"ui-stack" => (),
"ui-state" => (),
"update_menu_items" => (),
"close_all_menus" => (),
"delay" => (),
"menu_scroll" => (),
"ui-scroller-scroll" => (),
"ui_cutoff" => (),
"lobby_complete" => (),
"login_success" => (),
"game_started" => (),
"input_mode" => println!("PROCESS: input_mode"),
"msgs" => println!("PROCESS: game log"),
"update_spectators" => println!("PROCESS: number of spectators"),
"player" => println!("PROCESS: player data"),
"map" => println!("PROCESS: map data"),
"menu" => println!("PROCESS: menu data"),
"update_menu" => println!("PROCESS: menu data"),
"close_menu" => println!("PROCESS: menu data"),
"ui-push" => println!("PROCESS: menu data"),
"ui-pop" => println!("PROCESS: menu data"),
_ => {
unreachable!();
}
};
}