1extern crate dcss_api;
2
3use dcss_api::Webtile;
4use dcss_api::{BlockingError, Error as APIError};
5use serde_json::Value;
6use std::process;
7
8fn main() {
9 let mut webtile =
11 Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
12
13 let gameid = webtile
15 .login_with_credentials("Username", "Password")
16 .expect("Failed to login");
17
18 while webtile.get_message().is_some() {}
20
21 webtile
23 .start_game_seeded(&gameid[0], "1", false, "b", "i", "b")
24 .expect("Failed to start game");
25
26 while let Some(message) = webtile.get_message() {
28 processor(&message);
29 }
30
31 write_key_bot(&mut webtile, "key_dir_n", "player").expect("Failed");
34 write_key_bot(&mut webtile, "key_dir_s", "player").expect("Failed");
35
36 webtile.quit_game().expect("Failed to quit");
38
39 webtile.disconnect().expect("Failed to disconnect");
41}
42
43fn write_key_bot(webtile: &mut Webtile, to_send: &str, to_receive: &str) -> Result<(), APIError> {
44 println!("SEND: {}", to_send);
45
46 webtile.write_key(to_send)?;
47
48 if let Err(e) = webtile.read_until(to_receive, None, None) {
50 match e {
51 APIError::Blocking(BlockingError::More) => webtile.write_key(" ")?,
52 APIError::Blocking(BlockingError::TextInput) => {
53 println!("ERROR: Likely level up choice");
54 }
55 APIError::Blocking(BlockingError::Pickup) => println!("ERROR: Pickup"),
56 APIError::Blocking(BlockingError::Acquirement(_)) => println!("ERROR: Acquirement"),
57 APIError::Blocking(BlockingError::Identify(_)) => println!("ERROR: Identify"),
58 APIError::Blocking(BlockingError::EnchantWeapon(_)) => println!("ERROR: EnchantWeapon"),
59 APIError::Blocking(BlockingError::EnchantItem(_)) => println!("ERROR: EnchantItem"),
60 APIError::Blocking(BlockingError::BrandWeapon(_)) => println!("ERROR: BrandWeapon"),
61 APIError::Blocking(BlockingError::Skill) => println!("ERROR: Skill"),
62 APIError::Blocking(BlockingError::Died) => {
63 println!("ERROR: Died");
64 process::exit(0);
65 }
66 _ => Err(e)?,
67 }
68 }
69
70 while let Some(message) = webtile.get_message() {
72 processor(&message);
73 }
74
75 Ok(())
76}
77
78fn processor(message: &Value) {
79 let msg = message["msg"].as_str().unwrap();
80
81 match msg {
82 "ping" => (),
84 "lobby_clear" => (),
85 "go_lobby" => (),
86 "html" => (),
87 "set_game_links" => (),
88 "game_client" => (),
89 "chat" => (),
90 "version" => (),
91 "options" => (),
92 "layout" => (),
93 "ui-state-sync" => (),
94 "text_cursor" => (),
95 "cursor" => (),
96 "ui_state" => (),
97 "flash" => (),
98 "ui-stack" => (),
99 "ui-state" => (),
100 "update_menu_items" => (),
101 "close_all_menus" => (),
102 "delay" => (),
103 "menu_scroll" => (),
104 "ui-scroller-scroll" => (),
105 "ui_cutoff" => (),
106 "lobby_complete" => (),
107 "login_success" => (),
108 "game_started" => (),
109
110 "input_mode" => println!("PROCESS: input_mode"),
112
113 "msgs" => println!("PROCESS: game log"),
115
116 "update_spectators" => println!("PROCESS: number of spectators"),
118
119 "player" => println!("PROCESS: player data"),
121
122 "map" => println!("PROCESS: map data"),
124
125 "menu" => println!("PROCESS: menu data"),
127 "update_menu" => println!("PROCESS: menu data"),
128 "close_menu" => println!("PROCESS: menu data"),
129 "ui-push" => println!("PROCESS: menu data"),
130 "ui-pop" => println!("PROCESS: menu data"),
131
132 _ => {
133 unreachable!();
134 }
135 };
136}