4_blocking_error/
4_blocking_error.rs

1extern crate dcss_api;
2
3use dcss_api::Webtile;
4use dcss_api::{BlockingError, Error};
5
6fn main() {
7    // Connect to DCSS Webtile
8    let mut webtile =
9        Webtile::connect("ws://localhost:8080/socket", 100, "0.32").expect("Failed to connect");
10
11    // Empty message queue;
12    while webtile.get_message().is_some() {}
13
14    // Log in (to a user called "Username", with a password "Password")
15    let _gameid = webtile
16        .login_with_credentials("Username", "Password")
17        .expect("Failed to login");
18
19    // Empty message queue;
20    while webtile.get_message().is_some() {}
21
22    // Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
23    webtile
24        .start_game("dcss-web-trunk", "b", "f", "b")
25        .expect("Failed to start game");
26
27    // Print the messages you get upon starting the game (should be processed)
28    while let Some(message) = webtile.get_message() {
29        println!("{:?}", message)
30    }
31
32    // Open inventory, drop everything
33    webtile.write_key("i").expect("");
34    webtile
35        .read_until("menu", None, None)
36        .expect("Failed to read");
37    webtile.write_key("a").expect("");
38    webtile
39        .read_until("ui-push", None, None)
40        .expect("Failed to read");
41    webtile.write_key("d").expect("");
42    webtile
43        .read_until("player", None, None)
44        .expect("Failed to read");
45    webtile.write_key("i").expect("");
46    webtile
47        .read_until("menu", None, None)
48        .expect("Failed to read");
49    webtile.write_key("b").expect("");
50    webtile
51        .read_until("ui-push", None, None)
52        .expect("Failed to read");
53    webtile.write_key("d").expect("");
54    webtile
55        .read_until("player", None, None)
56        .expect("Failed to read");
57
58    // Print the messages you get upon doing these actions (should be processed)
59    while let Some(message) = webtile.get_message() {
60        println!("{:?}", message)
61    }
62
63    // Try to pick up what was dropped.
64    webtile.write_key(",").expect("");
65
66    // Normally when picking up ONE item on the ground, you would read until
67    // DCSS Webtiles returns a "input_mode" of mode = 1 (ready for input),
68    // but since there are two items on the ground, a menu will pop up so you can
69    // select the item to pick up(can't be easily anticipated, so dealt with using
70    // a BlockingError).
71    match webtile.read_until("input_mode", Some("mode"), Some(1)) {
72        Ok(_) => (),
73        Err(e) => match *e {
74            Error::Blocking(BlockingError::Pickup) => {
75                println!("Pickup menu pop-up -- decide what to do");
76                webtile.write_key("key_esc").expect(""); // Esc to ignore it
77                webtile
78                    .read_until("msgs", None, None)
79                    .expect("Failed to read");
80            }
81            _ => panic!("Unexpected Error"),
82        },
83    };
84
85    // Print the messages you get upon picking up an item (should be processed)
86    while let Some(message) = webtile.get_message() {
87        println!("{:?}", message)
88    }
89
90    // Quit game (same as dying)
91    webtile.quit_game().expect("Failed to quit");
92
93    // Print the messages after you quit game
94    while let Some(message) = webtile.get_message() {
95        println!("{:?}", message)
96    }
97
98    // Disconnect from webtile
99    webtile.disconnect().expect("Failed to disconnect");
100}