use std::env;
use std::time::Duration;
use wf_market::ws::{Subscription, WsEvent, WsUserStatus};
use wf_market::{Client, Credentials};
#[tokio::main]
async fn main() -> wf_market::Result<()> {
let _ = dotenv::dotenv();
let email = env::var("WFM_EMAIL").expect("WFM_EMAIL not set (use .env file or environment)");
let password =
env::var("WFM_PASSWORD").expect("WFM_PASSWORD not set (use .env file or environment)");
println!("=== Logging in ===");
let creds = Credentials::new(&email, &password, Credentials::generate_device_id());
let client = Client::from_credentials(creds).await?;
println!("Logged in!");
println!("\n=== Connecting to WebSocket ===");
let ws = client
.websocket()
.on_event(|event| async move {
match event {
WsEvent::Connected => {
println!("[WS] Connected to server");
}
WsEvent::Authenticated => {
println!("[WS] Authenticated successfully");
}
WsEvent::AuthenticationFailed { error } => {
println!("[WS] Authentication failed: {}", error);
}
WsEvent::OnlineCount {
connections,
authorized,
} => {
println!(
"[WS] Online: {} connections, {} authorized users",
connections, authorized
);
}
WsEvent::StatusUpdate {
status, activity, ..
} => {
println!("[WS] Status update: {:?}, Activity: {:?}", status, activity);
}
WsEvent::OrderCreated { order } => {
println!(
"[WS] New order: {} - {}p x{} by {}",
order.order.item_id,
order.order.platinum,
order.order.quantity,
order.user.ingame_name
);
}
WsEvent::Disconnected { reason } => {
println!("[WS] Disconnected: {}", reason);
}
WsEvent::SessionRevoked => {
println!("[WS] Session was revoked!");
}
WsEvent::Banned { until, message } => {
println!("[WS] Banned until {}: {}", until, message);
}
WsEvent::Warned { message } => {
println!("[WS] Warning: {}", message);
}
WsEvent::Unknown { route, .. } => {
println!("[WS] Unknown event: {}", route);
}
_ => {}
}
})
.subscribe(Subscription::all_new_orders())
.auto_reconnect(true)
.reconnect_delay(Duration::from_secs(5))
.connect()
.await?;
println!("WebSocket connected!");
println!("\n=== Dynamic Subscriptions ===");
ws.subscribe(Subscription::item("nikana_prime_set")).await?;
println!("Subscribed to nikana_prime_set updates");
println!("\n=== Setting Status ===");
ws.set_status(WsUserStatus::Online, Some(3600), None)
.await?;
println!("Status set to online for 1 hour");
println!("\n=== Listening for events (Ctrl+C to stop) ===");
println!("Watching for new orders...\n");
tokio::time::sleep(Duration::from_secs(60)).await;
ws.unsubscribe(&Subscription::item("nikana_prime_set"))
.await?;
ws.sign_out().await?;
println!("\nDone!");
Ok(())
}