5_bot_core/
5_bot_core.rs

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    // Connect to DCSS Webtile
10    let mut webtile =
11        Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
12
13    // Log in (to a user called "Username", with a password "Password")
14    let gameid = webtile
15        .login_with_credentials("Username", "Password")
16        .expect("Failed to login");
17
18    // Empty message queue;
19    while webtile.get_message().is_some() {}
20
21    // Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
22    webtile
23        .start_game_seeded(&gameid[0], "1", false, "b", "i", "b")
24        .expect("Failed to start game");
25
26    // Process the messages
27    while let Some(message) = webtile.get_message() {
28        processor(&message);
29    }
30
31    // Depending on what is found in the "map" data, a move up may make sense (up to the
32    // bot to decide this) -- note this may if a north wall exists (no bot intelligence here).
33    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    // Quit game (same as dying)
37    webtile.quit_game().expect("Failed to quit");
38
39    // Disconnect from webtile
40    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    // Make sure you verify for blocking errors;
49    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    // Process the data based on what was done (e.g. new map revealed, health of player...)
71    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        // Ignore
83        "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 & blocking
111        "input_mode" => println!("PROCESS: input_mode"),
112
113        // Messages
114        "msgs" => println!("PROCESS: game log"),
115
116        // Lobby
117        "update_spectators" => println!("PROCESS: number of spectators"),
118
119        // Player
120        "player" => println!("PROCESS: player data"),
121
122        // Dungeon
123        "map" => println!("PROCESS: map data"),
124
125        // Menu
126        "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}