Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const WORLD_SYNC_DELAY_MS: u64 = 500;
struct State {
client_handle: Arc<RwLock<Option<Client>>>,
in_game: Arc<AtomicBool>,
chat_tx: Option<mpsc::UnboundedSender<String>>,
chat_tx: Option<mpsc::UnboundedSender<(Option<String>, String)>>,
}

impl Default for State {
Expand All @@ -32,7 +32,7 @@ impl Default for State {
pub struct TestBot {
client: Option<Arc<RwLock<Option<Client>>>>,
in_game: Option<Arc<AtomicBool>>,
chat_rx: Option<mpsc::UnboundedReceiver<String>>,
chat_rx: Option<mpsc::UnboundedReceiver<(Option<String>, String)>>,
}

impl TestBot {
Expand Down Expand Up @@ -85,10 +85,18 @@ impl TestBot {
tracing::info!("Bot in game state");
}
Event::Chat(m) => {
// Extract the message content and send it through the channel
// Extract the message content
let message = m.message().to_string();
// Try to get sender name (best effort)
// Fallback: parse "<Name>"
let sender = if message.starts_with('<') {
if let Some(end) = message.find('>') {
Some(message[1..end].to_string())
} else { None }
} else { None };

if let Some(ref tx) = state.chat_tx {
let _ = tx.send(message);
let _ = tx.send((sender, message));
}
}
_ => {}
Expand Down Expand Up @@ -145,7 +153,7 @@ impl TestBot {
}

/// Wait for a chat message with timeout
pub async fn recv_chat_timeout(&mut self, timeout: std::time::Duration) -> Option<String> {
pub async fn recv_chat_timeout(&mut self, timeout: std::time::Duration) -> Option<(Option<String>, String)> {
if let Some(ref mut rx) = self.chat_rx {
tokio::time::timeout(timeout, rx.recv())
.await
Expand Down Expand Up @@ -192,4 +200,16 @@ impl TestBot {
Ok(None)
}
}

/// Get the bot's current position
pub fn get_position(&self) -> Result<[i32; 3]> {
let client_guard = self.get_client()?;
let client = client_guard
.as_ref()
.ok_or_else(|| anyhow::anyhow!("Bot not initialized"))?;

let pos = client.position();
Ok([pos.x as i32, pos.y as i32, pos.z as i32])
}
}

Loading