Skip to content

Commit

Permalink
Use character selection workflow at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
vE5li committed Apr 28, 2024
1 parent 5f9bf67 commit 3069aa9
Showing 1 changed file with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::io::Write;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::thread::sleep;
use std::time::Duration;
Expand Down Expand Up @@ -48,6 +49,8 @@ impl MessageHistory {
40 chars. Your name is Simona."
.to_owned(),
},
// Llama will not be able to understand the history of the conversation if "user"
// and "assistant" roles don't alternate, so we insert this dummy response.
Message {
role: "assistant".to_owned(),
content: "Okey.".to_owned(),
Expand All @@ -65,8 +68,6 @@ async fn main() {
const OLLAMA_MODEL: &str = "llama2:13b";
const USERNAME: &str = "chatbot";
const PASSWORD: &str = "chatbot";
const CHARACTER_SLOT: usize = 0;
const CHARACTER_NAME: &str = "ChatbotBoy";

// Create the networking system and HTTP client.
let mut networking_system = NetworkingSystem::<()>::new();
Expand All @@ -75,6 +76,7 @@ async fn main() {
// Persistent data.
let mut saved_login_data = None;
let mut message_history = MessageHistory { hash_map: HashMap::new() };
let mut character_name = String::new();

// Kick of the bot by connecting to the login server.
networking_system.connect_to_login_server(SOCKET_ADDR, USERNAME.to_owned(), PASSWORD.to_owned());
Expand All @@ -88,7 +90,7 @@ async fn main() {
} => {
println!("[{}] Successfully connected to login server", "Setup".green());

networking_system.connect_to_character_server(&login_data, character_servers[CHARACTER_SLOT].clone());
networking_system.connect_to_character_server(&login_data, character_servers[0].clone());
saved_login_data = Some(login_data);
}
NetworkEvent::LoginServerConnectionFailed { reason } => {
Expand All @@ -100,7 +102,7 @@ async fn main() {
NetworkEvent::CharacterServerConnected { .. } => {
println!("[{}] Successfully connected to character server", "Setup".green());

networking_system.select_character(0);
networking_system.request_character_list();
}
NetworkEvent::CharacterServerConnectionFailed { reason } => {
panic!("Failed to connect to character server: {}", reason);
Expand All @@ -109,15 +111,43 @@ async fn main() {
panic!("Character server disconnected");
}
NetworkEvent::CharacterSelectionFailed { reason } => {
panic!("Failed to connect to select character: {}", reason);
panic!("Failed to select character: {}", reason);
}
NetworkEvent::CharacterInformation { characters } => {
for character in &characters {
println!(
"[{}] On slot {}: {}",
"Characters".magenta(),
character.character_number.green(),
character.name.magenta()
);
}

print!("[{}] Character to use: ", "Characters".magenta());
std::io::stdout().flush().unwrap();

let mut buffer = String::new();
let stdin = std::io::stdin();
stdin.read_line(&mut buffer).expect("Failed to receive input");

let character_slot = buffer.trim().parse::<usize>().expect("Failed to parse slot");
let character_name = characters
.into_iter()
.find(|character| character.character_number as usize == character_slot)
.expect("No player in that slot")
.name;

print!("[{}] Using character: {}", "Characters".magenta(), character_name.green());

networking_system.select_character(character_slot)
}
NetworkEvent::CharacterSelected { login_data, .. } => {
let login_login_data = saved_login_data.as_ref().unwrap();
networking_system.connect_to_map_server(login_login_data, login_data);
networking_system.map_loaded();
}
NetworkEvent::ChatMessage { text, .. } => {
if text.starts_with(CHARACTER_NAME) {
if text.starts_with(&character_name) {
continue;
}

Expand Down Expand Up @@ -159,7 +189,7 @@ async fn main() {
content: response.content.to_owned(),
});

networking_system.send_chat_message(CHARACTER_NAME, &response.content);
networking_system.send_chat_message(&character_name, &response.content);
}
}
_ => {}
Expand Down

0 comments on commit 3069aa9

Please sign in to comment.