2019-08-19 12:39:23 +00:00
|
|
|
#![deny(unsafe_code)]
|
2020-09-27 16:20:40 +00:00
|
|
|
#![deny(clippy::clone_on_ref_ptr)]
|
2019-08-19 12:39:23 +00:00
|
|
|
|
2019-04-29 20:37:19 +00:00
|
|
|
use common::{clock::Clock, comp};
|
2021-01-15 13:04:32 +00:00
|
|
|
use std::{
|
|
|
|
io,
|
|
|
|
sync::{mpsc, Arc},
|
|
|
|
thread,
|
|
|
|
time::Duration,
|
|
|
|
};
|
|
|
|
use tokio::runtime::Runtime;
|
2020-06-21 14:26:06 +00:00
|
|
|
use tracing::{error, info};
|
2021-02-21 23:48:30 +00:00
|
|
|
use veloren_client::{addr::ConnectionArgs, Client, Event};
|
2022-09-09 15:27:21 +00:00
|
|
|
use voxygen_chat_i18n::internationalisate_chat_message;
|
2019-03-03 22:02:38 +00:00
|
|
|
|
2019-06-08 23:49:48 +00:00
|
|
|
const TPS: u64 = 10; // Low value is okay, just reading messages.
|
2019-03-03 22:02:38 +00:00
|
|
|
|
2019-06-17 10:40:19 +00:00
|
|
|
fn read_input() -> String {
|
|
|
|
let mut buffer = String::new();
|
|
|
|
|
2019-06-17 10:40:50 +00:00
|
|
|
io::stdin()
|
|
|
|
.read_line(&mut buffer)
|
2019-06-17 10:40:19 +00:00
|
|
|
.expect("Failed to read input");
|
|
|
|
|
2019-06-28 14:22:25 +00:00
|
|
|
buffer.trim().to_string()
|
2019-06-17 10:40:19 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
fn main() {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Initialize logging.
|
2021-03-28 23:40:53 +00:00
|
|
|
common_frontend::init_stdout(None);
|
2019-03-03 22:02:38 +00:00
|
|
|
|
2022-09-09 15:27:21 +00:00
|
|
|
info!("locading localisation");
|
|
|
|
|
|
|
|
let localisation = voxygen_i18n::LocalizationHandle::load_expect("en");
|
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
info!("Starting chat-cli...");
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Set up an fps clock.
|
2020-11-12 02:47:22 +00:00
|
|
|
let mut clock = Clock::new(Duration::from_secs_f64(1.0 / TPS as f64));
|
2019-03-03 22:02:38 +00:00
|
|
|
|
2019-06-17 13:07:55 +00:00
|
|
|
println!("Enter your username");
|
2019-07-01 15:15:50 +00:00
|
|
|
let username = read_input();
|
2019-06-17 13:07:55 +00:00
|
|
|
|
2019-07-02 20:06:44 +00:00
|
|
|
println!("Enter the server address");
|
|
|
|
let server_addr = read_input();
|
|
|
|
|
2019-08-08 03:56:02 +00:00
|
|
|
println!("Enter your password");
|
|
|
|
let password = read_input();
|
|
|
|
|
2021-01-15 13:04:32 +00:00
|
|
|
let runtime = Arc::new(Runtime::new().unwrap());
|
2021-02-18 00:01:57 +00:00
|
|
|
let runtime2 = Arc::clone(&runtime);
|
2021-05-21 12:34:34 +00:00
|
|
|
let addr = ConnectionArgs::Tcp {
|
|
|
|
prefer_ipv6: false,
|
|
|
|
hostname: server_addr,
|
|
|
|
};
|
2021-01-15 13:04:32 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Create a client.
|
2021-02-18 00:01:57 +00:00
|
|
|
let mut client = runtime
|
2022-09-07 03:59:41 +00:00
|
|
|
.block_on(Client::new(
|
|
|
|
addr,
|
|
|
|
runtime2,
|
|
|
|
&mut None,
|
|
|
|
&username,
|
|
|
|
&password,
|
|
|
|
|provider| provider == "https://auth.veloren.net",
|
|
|
|
))
|
2021-02-18 00:01:57 +00:00
|
|
|
.expect("Failed to create client instance");
|
2019-03-03 22:02:38 +00:00
|
|
|
|
2020-12-05 11:47:31 +00:00
|
|
|
println!("Server info: {:?}", client.server_info());
|
2019-05-08 16:22:52 +00:00
|
|
|
|
2022-09-09 15:27:21 +00:00
|
|
|
let mut player_printed = false;
|
2019-04-24 07:59:42 +00:00
|
|
|
|
2019-06-17 13:07:55 +00:00
|
|
|
let (tx, rx) = mpsc::channel();
|
2020-02-01 20:39:39 +00:00
|
|
|
thread::spawn(move || {
|
|
|
|
loop {
|
|
|
|
let msg = read_input();
|
|
|
|
tx.send(msg).unwrap();
|
|
|
|
}
|
2019-06-17 13:07:55 +00:00
|
|
|
});
|
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
loop {
|
2019-06-17 13:07:55 +00:00
|
|
|
for msg in rx.try_iter() {
|
|
|
|
client.send_chat(msg)
|
|
|
|
}
|
|
|
|
|
2020-11-10 12:30:01 +00:00
|
|
|
let events = match client.tick(comp::ControllerInputs::default(), clock.dt(), |_| {}) {
|
2019-03-05 00:00:11 +00:00
|
|
|
Ok(events) => events,
|
|
|
|
Err(err) => {
|
2019-04-24 07:59:42 +00:00
|
|
|
error!("Error: {:?}", err);
|
2019-03-05 00:00:11 +00:00
|
|
|
break;
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-03-05 00:00:11 +00:00
|
|
|
};
|
2019-03-03 22:02:38 +00:00
|
|
|
|
2020-06-24 05:46:29 +00:00
|
|
|
const SHOW_NAME: bool = false;
|
2019-03-03 22:02:38 +00:00
|
|
|
for event in events {
|
|
|
|
match event {
|
2022-09-09 15:27:21 +00:00
|
|
|
Event::Chat(m) => println!(
|
|
|
|
"{}",
|
|
|
|
internationalisate_chat_message(
|
|
|
|
m,
|
|
|
|
|msg| client.lockup_msg_context(msg),
|
|
|
|
&localisation.read(),
|
|
|
|
SHOW_NAME,
|
|
|
|
)
|
|
|
|
.message
|
|
|
|
),
|
2020-02-01 20:39:39 +00:00
|
|
|
Event::Disconnect => {}, // TODO
|
2019-10-22 03:46:12 +00:00
|
|
|
Event::DisconnectionNotification(time) => {
|
|
|
|
let message = match time {
|
|
|
|
0 => String::from("Goodbye!"),
|
|
|
|
_ => format!("Connection lost. Kicking in {} seconds", time),
|
|
|
|
};
|
|
|
|
|
|
|
|
println!("{}", message)
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-06-25 18:50:04 +00:00
|
|
|
_ => {},
|
2019-03-03 22:02:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-22 03:46:12 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Clean up the server after a tick.
|
2019-03-03 22:02:38 +00:00
|
|
|
client.cleanup();
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Wait for the next tick.
|
2020-11-10 12:30:01 +00:00
|
|
|
clock.tick();
|
2022-09-09 15:27:21 +00:00
|
|
|
|
|
|
|
if !player_printed {
|
|
|
|
println!("Players online: {:?}", client.players().collect::<Vec<_>>());
|
|
|
|
player_printed = true;
|
|
|
|
}
|
2019-03-03 22:02:38 +00:00
|
|
|
}
|
2022-09-09 15:27:21 +00:00
|
|
|
}
|