pub struct Webtile { /* private fields */ }
Expand description
Webtile connection, using websocket (tungstenite) and a Deflate decoder (flate2).
Implementations§
source§impl Webtile
impl Webtile
sourcepub fn login_with_credentials(
&mut self,
username: &str,
password: &str
) -> Result<Vec<String>, Error>
pub fn login_with_credentials( &mut self, username: &str, password: &str ) -> Result<Vec<String>, Error>
Login to the game, using a username and password. It returns a vector of all playable game IDs.
Arguments
username
- A string slice of the user’s username.password
- A string slice of the user’s password.
Example
// Login under the user "Username", with a password of "Password"
webtile.login_with_credentials("Username", "Password")?;
Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let _gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Write RC File
webtile
.set_rc_file("seeded-web-trunk", "show_more = false\nrest_delay = -1")
.expect("Failed to set RC file.");
// Empty message queue;
while webtile.get_message().is_some() {}
// Read RC File
let rc_file = webtile
.get_rc_file("seeded-web-trunk")
.expect("Failed to get RC file.");
print!("RC FILE: \n\n {}\n\n", rc_file);
// Empty message queue;
while webtile.get_message().is_some() {}
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
More examples
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Log in (to a user called "Username", with a password "Password")
let gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
webtile
.start_game_seeded(&gameid[0], "1", false, "b", "i", "b")
.expect("Failed to start game");
// Process the messages
while let Some(message) = webtile.get_message() {
processor(&message);
}
// Depending on what is found in the "map" data, a move up may make sense (up to the
// bot to decide this) -- note this may if a north wall exists (no bot intelligence here).
write_key_bot(&mut webtile, "key_dir_n", "player").expect("Failed");
write_key_bot(&mut webtile, "key_dir_s", "player").expect("Failed");
// Quit game (same as dying)
webtile.quit_game().expect("Failed to quit");
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let _gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Get cookie from the game
let cookie = webtile.request_cookie().unwrap();
println!("{}", cookie);
// Empty message queue;
while webtile.get_message().is_some() {}
// Disconnect from DCSS Webtile
webtile.disconnect().expect("Failed to disconnect.");
// Connect (again) to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Login with cookie
let _gameid = webtile
.login_with_cookie(cookie.as_str())
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Disconnect from DCSS Webtile
webtile.disconnect().expect("Failed to disconnect.");
}
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Print the game id's that can be started
println!("{:?}", gameid);
// Empty message queue;
while webtile.get_message().is_some() {}
// Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
webtile
.start_game(&gameid[0], "b", "i", "b")
.expect("Failed to start game");
// Print the messages you get upon starting the game (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Move up and back
webtile.write_key("key_dir_n").expect("Failed to write key");
webtile.write_key("key_dir_s").expect("Failed to write key");
// Print the messages you while moving (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Quit game (same as dying)
webtile.quit_game().expect("Failed to quit");
// Print the messages after you quit game
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let _gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
webtile
.start_game("dcss-web-trunk", "b", "i", "b")
.expect("Failed to start game");
// Print the messages you get upon starting the game (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Open inventory, drop everything
webtile.write_key("i").expect("");
webtile
.read_until("menu", None, None)
.expect("Failed to read");
webtile.write_key("a").expect("");
webtile
.read_until("ui-push", None, None)
.expect("Failed to read");
webtile.write_key("d").expect("");
webtile
.read_until("player", None, None)
.expect("Failed to read");
webtile.write_key("i").expect("");
webtile
.read_until("menu", None, None)
.expect("Failed to read");
webtile.write_key("b").expect("");
webtile
.read_until("ui-push", None, None)
.expect("Failed to read");
webtile.write_key("d").expect("");
webtile
.read_until("player", None, None)
.expect("Failed to read");
// Print the messages you get upon doing these actions (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Try to pick up what was dropped.
webtile.write_key(",").expect("");
// Normally when picking up ONE item on the ground, you would read until
// DCSS Webtiles returns a "input_mode" of mode = 1 (ready for input),
// but since there are two items on the ground, a menu will pop up so you can
// select the item to pick up(can't be easily anticipated, so dealt with using
// a BlockingError).
match webtile.read_until("input_mode", Some("mode"), Some(1)) {
Ok(_) => (),
Err(e) => match e {
Error::Blocking(BlockingError::Pickup) => {
println!("Pickup menu pop-up -- decide what to do");
webtile.write_key("key_esc").expect(""); // Esc to ignore it
webtile
.read_until("msgs", None, None)
.expect("Failed to read");
}
_ => panic!("Unexpected Error"),
},
};
// Print the messages you get upon picking up an item (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Quit game (same as dying)
webtile.quit_game().expect("Failed to quit");
// Print the messages after you quit game
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
Login to the game, using a cookie. It returns a vector of all playable game IDs.
Arguments
cookie
- A string slice of the user’s cookie (received previously).
Example
// Login under the user "Username", with a cookie
webtile.login_with_cookie("Username%123456789123456789123456789")?;
Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let _gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Get cookie from the game
let cookie = webtile.request_cookie().unwrap();
println!("{}", cookie);
// Empty message queue;
while webtile.get_message().is_some() {}
// Disconnect from DCSS Webtile
webtile.disconnect().expect("Failed to disconnect.");
// Connect (again) to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Login with cookie
let _gameid = webtile
.login_with_cookie(cookie.as_str())
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Disconnect from DCSS Webtile
webtile.disconnect().expect("Failed to disconnect.");
}
Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let _gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Get cookie from the game
let cookie = webtile.request_cookie().unwrap();
println!("{}", cookie);
// Empty message queue;
while webtile.get_message().is_some() {}
// Disconnect from DCSS Webtile
webtile.disconnect().expect("Failed to disconnect.");
// Connect (again) to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Login with cookie
let _gameid = webtile
.login_with_cookie(cookie.as_str())
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Disconnect from DCSS Webtile
webtile.disconnect().expect("Failed to disconnect.");
}
sourcepub fn get_rc_file(&mut self, game_id: &str) -> Result<String, Error>
pub fn get_rc_file(&mut self, game_id: &str) -> Result<String, Error>
Get the RC file content for a specific game ID.
Arguments
game_id
- A string slice of the game’s ID.
Example
webtile.get_rc_file("dcss-web-trunk")?;
Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let _gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Write RC File
webtile
.set_rc_file("seeded-web-trunk", "show_more = false\nrest_delay = -1")
.expect("Failed to set RC file.");
// Empty message queue;
while webtile.get_message().is_some() {}
// Read RC File
let rc_file = webtile
.get_rc_file("seeded-web-trunk")
.expect("Failed to get RC file.");
print!("RC FILE: \n\n {}\n\n", rc_file);
// Empty message queue;
while webtile.get_message().is_some() {}
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
sourcepub fn set_rc_file(&mut self, game_id: &str, content: &str) -> Result<(), Error>
pub fn set_rc_file(&mut self, game_id: &str, content: &str) -> Result<(), Error>
Set the RC file content of a specific game ID.
Arguments
game_id
- A string slice of the game’s ID.content
- A string slice of the content to write to the RC file.
Example
webtile.set_rc_file("dcss-web-trunk", "show_more = false\nrest_delay = -1")?;
Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let _gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Write RC File
webtile
.set_rc_file("seeded-web-trunk", "show_more = false\nrest_delay = -1")
.expect("Failed to set RC file.");
// Empty message queue;
while webtile.get_message().is_some() {}
// Read RC File
let rc_file = webtile
.get_rc_file("seeded-web-trunk")
.expect("Failed to get RC file.");
print!("RC FILE: \n\n {}\n\n", rc_file);
// Empty message queue;
while webtile.get_message().is_some() {}
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
source§impl Webtile
impl Webtile
sourcepub fn start_game(
&mut self,
game_id: &str,
species: &str,
background: &str,
weapon: &str
) -> Result<(), Error>
pub fn start_game( &mut self, game_id: &str, species: &str, background: &str, weapon: &str ) -> Result<(), Error>
Start an unseeded game by selecting the game_id and the character’s specifications.
Arguments
game_id
- A string slice of the game’s ID.species
- A string slice for the character’s species.background
- A string slice for the character’s background.weapon
- A string slice for the character’s weapon.
Example
// Start a game on "dcss-web-trunk", for a Minotaur (b), Berserker (i), with a mace (b)
webtile.start_game("dcss-web-trunk", "b", "i", "b")?;
Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Print the game id's that can be started
println!("{:?}", gameid);
// Empty message queue;
while webtile.get_message().is_some() {}
// Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
webtile
.start_game(&gameid[0], "b", "i", "b")
.expect("Failed to start game");
// Print the messages you get upon starting the game (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Move up and back
webtile.write_key("key_dir_n").expect("Failed to write key");
webtile.write_key("key_dir_s").expect("Failed to write key");
// Print the messages you while moving (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Quit game (same as dying)
webtile.quit_game().expect("Failed to quit");
// Print the messages after you quit game
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
More examples
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let _gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
webtile
.start_game("dcss-web-trunk", "b", "i", "b")
.expect("Failed to start game");
// Print the messages you get upon starting the game (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Open inventory, drop everything
webtile.write_key("i").expect("");
webtile
.read_until("menu", None, None)
.expect("Failed to read");
webtile.write_key("a").expect("");
webtile
.read_until("ui-push", None, None)
.expect("Failed to read");
webtile.write_key("d").expect("");
webtile
.read_until("player", None, None)
.expect("Failed to read");
webtile.write_key("i").expect("");
webtile
.read_until("menu", None, None)
.expect("Failed to read");
webtile.write_key("b").expect("");
webtile
.read_until("ui-push", None, None)
.expect("Failed to read");
webtile.write_key("d").expect("");
webtile
.read_until("player", None, None)
.expect("Failed to read");
// Print the messages you get upon doing these actions (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Try to pick up what was dropped.
webtile.write_key(",").expect("");
// Normally when picking up ONE item on the ground, you would read until
// DCSS Webtiles returns a "input_mode" of mode = 1 (ready for input),
// but since there are two items on the ground, a menu will pop up so you can
// select the item to pick up(can't be easily anticipated, so dealt with using
// a BlockingError).
match webtile.read_until("input_mode", Some("mode"), Some(1)) {
Ok(_) => (),
Err(e) => match e {
Error::Blocking(BlockingError::Pickup) => {
println!("Pickup menu pop-up -- decide what to do");
webtile.write_key("key_esc").expect(""); // Esc to ignore it
webtile
.read_until("msgs", None, None)
.expect("Failed to read");
}
_ => panic!("Unexpected Error"),
},
};
// Print the messages you get upon picking up an item (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Quit game (same as dying)
webtile.quit_game().expect("Failed to quit");
// Print the messages after you quit game
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
sourcepub fn start_game_seeded(
&mut self,
game_id: &str,
seed: &str,
pregenerate: bool,
species: &str,
background: &str,
weapon: &str
) -> Result<(), Error>
pub fn start_game_seeded( &mut self, game_id: &str, seed: &str, pregenerate: bool, species: &str, background: &str, weapon: &str ) -> Result<(), Error>
Start an seeded game by selecting the game_id, the seed and the character’s specifications.
Arguments
game_id
- A string slice of the game’s ID.seed
- A string slice of the game’s seed.pregenerate
- A bool on if the pregeneration option should be selected.species
- A string slice for the character’s species.background
- A string slice for the character’s background.weapon
- A string slice for the character’s weapon.
Example
// Start a game on "dcss-web-trunk", for the "123" seed (pregenerated) for a
// Minotaur (b), Berserker (i), with a mace (b)
webtile.start_game_seeded("dcss-web-trunk", "123", true, "b", "i", "b")?;
Examples found in repository?
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Log in (to a user called "Username", with a password "Password")
let gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
webtile
.start_game_seeded(&gameid[0], "1", false, "b", "i", "b")
.expect("Failed to start game");
// Process the messages
while let Some(message) = webtile.get_message() {
processor(&message);
}
// Depending on what is found in the "map" data, a move up may make sense (up to the
// bot to decide this) -- note this may if a north wall exists (no bot intelligence here).
write_key_bot(&mut webtile, "key_dir_n", "player").expect("Failed");
write_key_bot(&mut webtile, "key_dir_s", "player").expect("Failed");
// Quit game (same as dying)
webtile.quit_game().expect("Failed to quit");
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
sourcepub fn quit_game(&mut self) -> Result<(), Error>
pub fn quit_game(&mut self) -> Result<(), Error>
Quit the game (same result as dying), by sending a CTRL + Q
and
answering yes
.
Example
webtile.quit_game()?;
Examples found in repository?
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Log in (to a user called "Username", with a password "Password")
let gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
webtile
.start_game_seeded(&gameid[0], "1", false, "b", "i", "b")
.expect("Failed to start game");
// Process the messages
while let Some(message) = webtile.get_message() {
processor(&message);
}
// Depending on what is found in the "map" data, a move up may make sense (up to the
// bot to decide this) -- note this may if a north wall exists (no bot intelligence here).
write_key_bot(&mut webtile, "key_dir_n", "player").expect("Failed");
write_key_bot(&mut webtile, "key_dir_s", "player").expect("Failed");
// Quit game (same as dying)
webtile.quit_game().expect("Failed to quit");
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
More examples
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Print the game id's that can be started
println!("{:?}", gameid);
// Empty message queue;
while webtile.get_message().is_some() {}
// Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
webtile
.start_game(&gameid[0], "b", "i", "b")
.expect("Failed to start game");
// Print the messages you get upon starting the game (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Move up and back
webtile.write_key("key_dir_n").expect("Failed to write key");
webtile.write_key("key_dir_s").expect("Failed to write key");
// Print the messages you while moving (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Quit game (same as dying)
webtile.quit_game().expect("Failed to quit");
// Print the messages after you quit game
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let _gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
webtile
.start_game("dcss-web-trunk", "b", "i", "b")
.expect("Failed to start game");
// Print the messages you get upon starting the game (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Open inventory, drop everything
webtile.write_key("i").expect("");
webtile
.read_until("menu", None, None)
.expect("Failed to read");
webtile.write_key("a").expect("");
webtile
.read_until("ui-push", None, None)
.expect("Failed to read");
webtile.write_key("d").expect("");
webtile
.read_until("player", None, None)
.expect("Failed to read");
webtile.write_key("i").expect("");
webtile
.read_until("menu", None, None)
.expect("Failed to read");
webtile.write_key("b").expect("");
webtile
.read_until("ui-push", None, None)
.expect("Failed to read");
webtile.write_key("d").expect("");
webtile
.read_until("player", None, None)
.expect("Failed to read");
// Print the messages you get upon doing these actions (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Try to pick up what was dropped.
webtile.write_key(",").expect("");
// Normally when picking up ONE item on the ground, you would read until
// DCSS Webtiles returns a "input_mode" of mode = 1 (ready for input),
// but since there are two items on the ground, a menu will pop up so you can
// select the item to pick up(can't be easily anticipated, so dealt with using
// a BlockingError).
match webtile.read_until("input_mode", Some("mode"), Some(1)) {
Ok(_) => (),
Err(e) => match e {
Error::Blocking(BlockingError::Pickup) => {
println!("Pickup menu pop-up -- decide what to do");
webtile.write_key("key_esc").expect(""); // Esc to ignore it
webtile
.read_until("msgs", None, None)
.expect("Failed to read");
}
_ => panic!("Unexpected Error"),
},
};
// Print the messages you get upon picking up an item (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Quit game (same as dying)
webtile.quit_game().expect("Failed to quit");
// Print the messages after you quit game
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
source§impl Webtile
impl Webtile
sourcepub fn connect(
url: &str,
speed_ms: usize,
_version: &str
) -> Result<Self, Error>
pub fn connect( url: &str, speed_ms: usize, _version: &str ) -> Result<Self, Error>
Connects to a websocket URL, prepares the decompressor (using flate2::Decompress) and returns a Webtile connection object.
Arguments
url
- A &str that holds thews://
orwss://
URLspeed_ms
- A usize that depicts the speed limit in milliseconds between each command sent to DCSS Webtiles._version
- Currently a placeholder for the version number of DCSS, in case the API changes in the future.
Example
let mut webtile = Webtile::connect("ws://localhost:8080/socket", 100, "0.29")?;
Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let _gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Write RC File
webtile
.set_rc_file("seeded-web-trunk", "show_more = false\nrest_delay = -1")
.expect("Failed to set RC file.");
// Empty message queue;
while webtile.get_message().is_some() {}
// Read RC File
let rc_file = webtile
.get_rc_file("seeded-web-trunk")
.expect("Failed to get RC file.");
print!("RC FILE: \n\n {}\n\n", rc_file);
// Empty message queue;
while webtile.get_message().is_some() {}
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
More examples
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Log in (to a user called "Username", with a password "Password")
let gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
webtile
.start_game_seeded(&gameid[0], "1", false, "b", "i", "b")
.expect("Failed to start game");
// Process the messages
while let Some(message) = webtile.get_message() {
processor(&message);
}
// Depending on what is found in the "map" data, a move up may make sense (up to the
// bot to decide this) -- note this may if a north wall exists (no bot intelligence here).
write_key_bot(&mut webtile, "key_dir_n", "player").expect("Failed");
write_key_bot(&mut webtile, "key_dir_s", "player").expect("Failed");
// Quit game (same as dying)
webtile.quit_game().expect("Failed to quit");
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let _gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Get cookie from the game
let cookie = webtile.request_cookie().unwrap();
println!("{}", cookie);
// Empty message queue;
while webtile.get_message().is_some() {}
// Disconnect from DCSS Webtile
webtile.disconnect().expect("Failed to disconnect.");
// Connect (again) to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Login with cookie
let _gameid = webtile
.login_with_cookie(cookie.as_str())
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Disconnect from DCSS Webtile
webtile.disconnect().expect("Failed to disconnect.");
}
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Print the game id's that can be started
println!("{:?}", gameid);
// Empty message queue;
while webtile.get_message().is_some() {}
// Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
webtile
.start_game(&gameid[0], "b", "i", "b")
.expect("Failed to start game");
// Print the messages you get upon starting the game (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Move up and back
webtile.write_key("key_dir_n").expect("Failed to write key");
webtile.write_key("key_dir_s").expect("Failed to write key");
// Print the messages you while moving (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Quit game (same as dying)
webtile.quit_game().expect("Failed to quit");
// Print the messages after you quit game
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let _gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
webtile
.start_game("dcss-web-trunk", "b", "i", "b")
.expect("Failed to start game");
// Print the messages you get upon starting the game (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Open inventory, drop everything
webtile.write_key("i").expect("");
webtile
.read_until("menu", None, None)
.expect("Failed to read");
webtile.write_key("a").expect("");
webtile
.read_until("ui-push", None, None)
.expect("Failed to read");
webtile.write_key("d").expect("");
webtile
.read_until("player", None, None)
.expect("Failed to read");
webtile.write_key("i").expect("");
webtile
.read_until("menu", None, None)
.expect("Failed to read");
webtile.write_key("b").expect("");
webtile
.read_until("ui-push", None, None)
.expect("Failed to read");
webtile.write_key("d").expect("");
webtile
.read_until("player", None, None)
.expect("Failed to read");
// Print the messages you get upon doing these actions (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Try to pick up what was dropped.
webtile.write_key(",").expect("");
// Normally when picking up ONE item on the ground, you would read until
// DCSS Webtiles returns a "input_mode" of mode = 1 (ready for input),
// but since there are two items on the ground, a menu will pop up so you can
// select the item to pick up(can't be easily anticipated, so dealt with using
// a BlockingError).
match webtile.read_until("input_mode", Some("mode"), Some(1)) {
Ok(_) => (),
Err(e) => match e {
Error::Blocking(BlockingError::Pickup) => {
println!("Pickup menu pop-up -- decide what to do");
webtile.write_key("key_esc").expect(""); // Esc to ignore it
webtile
.read_until("msgs", None, None)
.expect("Failed to read");
}
_ => panic!("Unexpected Error"),
},
};
// Print the messages you get upon picking up an item (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Quit game (same as dying)
webtile.quit_game().expect("Failed to quit");
// Print the messages after you quit game
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
sourcepub fn disconnect(&mut self) -> Result<(), Error>
pub fn disconnect(&mut self) -> Result<(), Error>
Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let _gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Write RC File
webtile
.set_rc_file("seeded-web-trunk", "show_more = false\nrest_delay = -1")
.expect("Failed to set RC file.");
// Empty message queue;
while webtile.get_message().is_some() {}
// Read RC File
let rc_file = webtile
.get_rc_file("seeded-web-trunk")
.expect("Failed to get RC file.");
print!("RC FILE: \n\n {}\n\n", rc_file);
// Empty message queue;
while webtile.get_message().is_some() {}
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
More examples
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Log in (to a user called "Username", with a password "Password")
let gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
webtile
.start_game_seeded(&gameid[0], "1", false, "b", "i", "b")
.expect("Failed to start game");
// Process the messages
while let Some(message) = webtile.get_message() {
processor(&message);
}
// Depending on what is found in the "map" data, a move up may make sense (up to the
// bot to decide this) -- note this may if a north wall exists (no bot intelligence here).
write_key_bot(&mut webtile, "key_dir_n", "player").expect("Failed");
write_key_bot(&mut webtile, "key_dir_s", "player").expect("Failed");
// Quit game (same as dying)
webtile.quit_game().expect("Failed to quit");
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let _gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Get cookie from the game
let cookie = webtile.request_cookie().unwrap();
println!("{}", cookie);
// Empty message queue;
while webtile.get_message().is_some() {}
// Disconnect from DCSS Webtile
webtile.disconnect().expect("Failed to disconnect.");
// Connect (again) to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Login with cookie
let _gameid = webtile
.login_with_cookie(cookie.as_str())
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Disconnect from DCSS Webtile
webtile.disconnect().expect("Failed to disconnect.");
}
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Print the game id's that can be started
println!("{:?}", gameid);
// Empty message queue;
while webtile.get_message().is_some() {}
// Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
webtile
.start_game(&gameid[0], "b", "i", "b")
.expect("Failed to start game");
// Print the messages you get upon starting the game (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Move up and back
webtile.write_key("key_dir_n").expect("Failed to write key");
webtile.write_key("key_dir_s").expect("Failed to write key");
// Print the messages you while moving (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Quit game (same as dying)
webtile.quit_game().expect("Failed to quit");
// Print the messages after you quit game
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let _gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
webtile
.start_game("dcss-web-trunk", "b", "i", "b")
.expect("Failed to start game");
// Print the messages you get upon starting the game (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Open inventory, drop everything
webtile.write_key("i").expect("");
webtile
.read_until("menu", None, None)
.expect("Failed to read");
webtile.write_key("a").expect("");
webtile
.read_until("ui-push", None, None)
.expect("Failed to read");
webtile.write_key("d").expect("");
webtile
.read_until("player", None, None)
.expect("Failed to read");
webtile.write_key("i").expect("");
webtile
.read_until("menu", None, None)
.expect("Failed to read");
webtile.write_key("b").expect("");
webtile
.read_until("ui-push", None, None)
.expect("Failed to read");
webtile.write_key("d").expect("");
webtile
.read_until("player", None, None)
.expect("Failed to read");
// Print the messages you get upon doing these actions (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Try to pick up what was dropped.
webtile.write_key(",").expect("");
// Normally when picking up ONE item on the ground, you would read until
// DCSS Webtiles returns a "input_mode" of mode = 1 (ready for input),
// but since there are two items on the ground, a menu will pop up so you can
// select the item to pick up(can't be easily anticipated, so dealt with using
// a BlockingError).
match webtile.read_until("input_mode", Some("mode"), Some(1)) {
Ok(_) => (),
Err(e) => match e {
Error::Blocking(BlockingError::Pickup) => {
println!("Pickup menu pop-up -- decide what to do");
webtile.write_key("key_esc").expect(""); // Esc to ignore it
webtile
.read_until("msgs", None, None)
.expect("Failed to read");
}
_ => panic!("Unexpected Error"),
},
};
// Print the messages you get upon picking up an item (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Quit game (same as dying)
webtile.quit_game().expect("Failed to quit");
// Print the messages after you quit game
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
sourcepub fn read_until(
&mut self,
msg: &str,
key: Option<&str>,
value: Option<u64>
) -> Result<(), Error>
pub fn read_until( &mut self, msg: &str, key: Option<&str>, value: Option<u64> ) -> Result<(), Error>
Read the websocket messages until a specified message is found. Stores the
messages in a VecDeque that can be accessed by the user through the
Webtile::get_message()
function. Any known blocking message (e.g.
a ‘more’ log statement) will return a api_errors::BlockingError.
Will block forever if the expected message never comes.
Arguments
msg
- A &str that holds the value expected in the “msg” field of any returned message.key
- A optional &str with the name of the specific key in the json data to search for.value
- A optional u64 with the value of thekey
, only if u64. Specifically meant to distinguish between types of “modes” for the input_mode.
Example
// Read until the "close_all_menu" message is received
webtile.read_until("close_all_menus", None, None)
// Read until the "input_mode" message is received, with mode == 1
webtile.read_until("input_mode", Some("mode"), Some(1))
Examples found in repository?
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
fn write_key_bot(webtile: &mut Webtile, to_send: &str, to_receive: &str) -> Result<(), APIError> {
println!("SEND: {}", to_send);
webtile.write_key(to_send)?;
// Make sure you verify for blocking errors;
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)?,
}
}
// Process the data based on what was done (e.g. new map revealed, health of player...)
while let Some(message) = webtile.get_message() {
processor(&message);
}
Ok(())
}
More examples
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let _gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
webtile
.start_game("dcss-web-trunk", "b", "i", "b")
.expect("Failed to start game");
// Print the messages you get upon starting the game (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Open inventory, drop everything
webtile.write_key("i").expect("");
webtile
.read_until("menu", None, None)
.expect("Failed to read");
webtile.write_key("a").expect("");
webtile
.read_until("ui-push", None, None)
.expect("Failed to read");
webtile.write_key("d").expect("");
webtile
.read_until("player", None, None)
.expect("Failed to read");
webtile.write_key("i").expect("");
webtile
.read_until("menu", None, None)
.expect("Failed to read");
webtile.write_key("b").expect("");
webtile
.read_until("ui-push", None, None)
.expect("Failed to read");
webtile.write_key("d").expect("");
webtile
.read_until("player", None, None)
.expect("Failed to read");
// Print the messages you get upon doing these actions (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Try to pick up what was dropped.
webtile.write_key(",").expect("");
// Normally when picking up ONE item on the ground, you would read until
// DCSS Webtiles returns a "input_mode" of mode = 1 (ready for input),
// but since there are two items on the ground, a menu will pop up so you can
// select the item to pick up(can't be easily anticipated, so dealt with using
// a BlockingError).
match webtile.read_until("input_mode", Some("mode"), Some(1)) {
Ok(_) => (),
Err(e) => match e {
Error::Blocking(BlockingError::Pickup) => {
println!("Pickup menu pop-up -- decide what to do");
webtile.write_key("key_esc").expect(""); // Esc to ignore it
webtile
.read_until("msgs", None, None)
.expect("Failed to read");
}
_ => panic!("Unexpected Error"),
},
};
// Print the messages you get upon picking up an item (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Quit game (same as dying)
webtile.quit_game().expect("Failed to quit");
// Print the messages after you quit game
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
sourcepub fn write_json(&mut self, json_val: Value) -> Result<(), Error>
pub fn write_json(&mut self, json_val: Value) -> Result<(), Error>
Write a serde_json::Value to the websocket. Will only send if sufficient time has
elapsed since the last sent data, according to the Webtile::connect
speed_ms option.
Arguments
json_val
- A serde_json::Value to send to DCSS Webtiles.
Example
// Send the login command
webtile.write_json(json!({
"msg": "login",
"username": "Username",
"password": "Password",
}))?;
sourcepub fn write_key(&mut self, key: &str) -> Result<(), Error>
pub fn write_key(&mut self, key: &str) -> Result<(), Error>
Write a string slice (passed through [common::keys]) to the websocket. Special
characters starting with key_
will be sent as a keycode (e.g. key_esc
will
send the esc
character). Will only send if sufficient time has elapsed since
the last sent data, according to the Webtile::connect
speed_ms option.
Special keys:
- CTRL+char =
key_ctrl_a
tokey_ctrl_z
- Special chars =
key_tab
,key_esc
andkey_enter
- Cardinal directions:
key_dir_n
,key_dir_ne
,key_dir_e
,key_dir_se
,key_dir_s
,key_dir_sw
,key_dir_w
andkey_dir_nw
- Stairs:
key_stair_down
andkey_stair_up
Arguments
key
- A string slice to be sent to DCSS (passed through [common::keys]).
Example
// Send the `esc` key
webtile.write_key("key_esc")
// Send the 'a' key
webtile.write_key("a")
// Send a string of keys that will move left, open a menu and drop an item (slot a)
webtile.write_key("6iad")
Examples found in repository?
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
fn write_key_bot(webtile: &mut Webtile, to_send: &str, to_receive: &str) -> Result<(), APIError> {
println!("SEND: {}", to_send);
webtile.write_key(to_send)?;
// Make sure you verify for blocking errors;
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)?,
}
}
// Process the data based on what was done (e.g. new map revealed, health of player...)
while let Some(message) = webtile.get_message() {
processor(&message);
}
Ok(())
}
More examples
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Print the game id's that can be started
println!("{:?}", gameid);
// Empty message queue;
while webtile.get_message().is_some() {}
// Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
webtile
.start_game(&gameid[0], "b", "i", "b")
.expect("Failed to start game");
// Print the messages you get upon starting the game (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Move up and back
webtile.write_key("key_dir_n").expect("Failed to write key");
webtile.write_key("key_dir_s").expect("Failed to write key");
// Print the messages you while moving (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Quit game (same as dying)
webtile.quit_game().expect("Failed to quit");
// Print the messages after you quit game
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let _gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
webtile
.start_game("dcss-web-trunk", "b", "i", "b")
.expect("Failed to start game");
// Print the messages you get upon starting the game (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Open inventory, drop everything
webtile.write_key("i").expect("");
webtile
.read_until("menu", None, None)
.expect("Failed to read");
webtile.write_key("a").expect("");
webtile
.read_until("ui-push", None, None)
.expect("Failed to read");
webtile.write_key("d").expect("");
webtile
.read_until("player", None, None)
.expect("Failed to read");
webtile.write_key("i").expect("");
webtile
.read_until("menu", None, None)
.expect("Failed to read");
webtile.write_key("b").expect("");
webtile
.read_until("ui-push", None, None)
.expect("Failed to read");
webtile.write_key("d").expect("");
webtile
.read_until("player", None, None)
.expect("Failed to read");
// Print the messages you get upon doing these actions (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Try to pick up what was dropped.
webtile.write_key(",").expect("");
// Normally when picking up ONE item on the ground, you would read until
// DCSS Webtiles returns a "input_mode" of mode = 1 (ready for input),
// but since there are two items on the ground, a menu will pop up so you can
// select the item to pick up(can't be easily anticipated, so dealt with using
// a BlockingError).
match webtile.read_until("input_mode", Some("mode"), Some(1)) {
Ok(_) => (),
Err(e) => match e {
Error::Blocking(BlockingError::Pickup) => {
println!("Pickup menu pop-up -- decide what to do");
webtile.write_key("key_esc").expect(""); // Esc to ignore it
webtile
.read_until("msgs", None, None)
.expect("Failed to read");
}
_ => panic!("Unexpected Error"),
},
};
// Print the messages you get upon picking up an item (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Quit game (same as dying)
webtile.quit_game().expect("Failed to quit");
// Print the messages after you quit game
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
sourcepub fn get_message(&mut self) -> Option<Value>
pub fn get_message(&mut self) -> Option<Value>
Get the messages received by the DCSS Webtile (as serde_json::Value), in order of reception. Will return None if the queue is empty.
Example
// Print the messages received, until the queue is empty
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
Examples found in repository?
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let _gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Write RC File
webtile
.set_rc_file("seeded-web-trunk", "show_more = false\nrest_delay = -1")
.expect("Failed to set RC file.");
// Empty message queue;
while webtile.get_message().is_some() {}
// Read RC File
let rc_file = webtile
.get_rc_file("seeded-web-trunk")
.expect("Failed to get RC file.");
print!("RC FILE: \n\n {}\n\n", rc_file);
// Empty message queue;
while webtile.get_message().is_some() {}
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
More examples
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Log in (to a user called "Username", with a password "Password")
let gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
webtile
.start_game_seeded(&gameid[0], "1", false, "b", "i", "b")
.expect("Failed to start game");
// Process the messages
while let Some(message) = webtile.get_message() {
processor(&message);
}
// Depending on what is found in the "map" data, a move up may make sense (up to the
// bot to decide this) -- note this may if a north wall exists (no bot intelligence here).
write_key_bot(&mut webtile, "key_dir_n", "player").expect("Failed");
write_key_bot(&mut webtile, "key_dir_s", "player").expect("Failed");
// Quit game (same as dying)
webtile.quit_game().expect("Failed to quit");
// Disconnect from webtile
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)?;
// Make sure you verify for blocking errors;
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)?,
}
}
// Process the data based on what was done (e.g. new map revealed, health of player...)
while let Some(message) = webtile.get_message() {
processor(&message);
}
Ok(())
}
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let _gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Get cookie from the game
let cookie = webtile.request_cookie().unwrap();
println!("{}", cookie);
// Empty message queue;
while webtile.get_message().is_some() {}
// Disconnect from DCSS Webtile
webtile.disconnect().expect("Failed to disconnect.");
// Connect (again) to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Login with cookie
let _gameid = webtile
.login_with_cookie(cookie.as_str())
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Disconnect from DCSS Webtile
webtile.disconnect().expect("Failed to disconnect.");
}
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Print the game id's that can be started
println!("{:?}", gameid);
// Empty message queue;
while webtile.get_message().is_some() {}
// Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
webtile
.start_game(&gameid[0], "b", "i", "b")
.expect("Failed to start game");
// Print the messages you get upon starting the game (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Move up and back
webtile.write_key("key_dir_n").expect("Failed to write key");
webtile.write_key("key_dir_s").expect("Failed to write key");
// Print the messages you while moving (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Quit game (same as dying)
webtile.quit_game().expect("Failed to quit");
// Print the messages after you quit game
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
fn main() {
// Connect to DCSS Webtile
let mut webtile =
Webtile::connect("ws://localhost:8080/socket", 100, "0.29").expect("Failed to connect");
// Empty message queue;
while webtile.get_message().is_some() {}
// Log in (to a user called "Username", with a password "Password")
let _gameid = webtile
.login_with_credentials("Username", "Password")
.expect("Failed to login");
// Empty message queue;
while webtile.get_message().is_some() {}
// Start a random game on 'dcss-web-trunk', for Minotaur berserker with a mace.
webtile
.start_game("dcss-web-trunk", "b", "i", "b")
.expect("Failed to start game");
// Print the messages you get upon starting the game (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Open inventory, drop everything
webtile.write_key("i").expect("");
webtile
.read_until("menu", None, None)
.expect("Failed to read");
webtile.write_key("a").expect("");
webtile
.read_until("ui-push", None, None)
.expect("Failed to read");
webtile.write_key("d").expect("");
webtile
.read_until("player", None, None)
.expect("Failed to read");
webtile.write_key("i").expect("");
webtile
.read_until("menu", None, None)
.expect("Failed to read");
webtile.write_key("b").expect("");
webtile
.read_until("ui-push", None, None)
.expect("Failed to read");
webtile.write_key("d").expect("");
webtile
.read_until("player", None, None)
.expect("Failed to read");
// Print the messages you get upon doing these actions (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Try to pick up what was dropped.
webtile.write_key(",").expect("");
// Normally when picking up ONE item on the ground, you would read until
// DCSS Webtiles returns a "input_mode" of mode = 1 (ready for input),
// but since there are two items on the ground, a menu will pop up so you can
// select the item to pick up(can't be easily anticipated, so dealt with using
// a BlockingError).
match webtile.read_until("input_mode", Some("mode"), Some(1)) {
Ok(_) => (),
Err(e) => match e {
Error::Blocking(BlockingError::Pickup) => {
println!("Pickup menu pop-up -- decide what to do");
webtile.write_key("key_esc").expect(""); // Esc to ignore it
webtile
.read_until("msgs", None, None)
.expect("Failed to read");
}
_ => panic!("Unexpected Error"),
},
};
// Print the messages you get upon picking up an item (should be processed)
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Quit game (same as dying)
webtile.quit_game().expect("Failed to quit");
// Print the messages after you quit game
while let Some(message) = webtile.get_message() {
println!("{:?}", message)
}
// Disconnect from webtile
webtile.disconnect().expect("Failed to disconnect");
}