2019-05-22 20:53:24 +00:00
|
|
|
use client::{Client, Event};
|
2019-04-29 20:37:19 +00:00
|
|
|
use common::{clock::Clock, comp};
|
2019-05-08 16:22:52 +00:00
|
|
|
use log::{error, info};
|
2019-06-17 10:40:19 +00:00
|
|
|
use std::io;
|
2019-06-17 13:07:55 +00:00
|
|
|
use std::sync::mpsc;
|
|
|
|
use std::thread;
|
2019-06-17 10:40:50 +00:00
|
|
|
use std::time::Duration;
|
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");
|
|
|
|
|
|
|
|
buffer
|
|
|
|
}
|
|
|
|
|
2019-03-03 22:02:38 +00:00
|
|
|
fn main() {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Initialize logging.
|
2019-03-03 22:02:38 +00:00
|
|
|
pretty_env_logger::init();
|
|
|
|
|
|
|
|
info!("Starting chat-cli...");
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Set up an fps clock.
|
2019-03-03 22:02:38 +00:00
|
|
|
let mut clock = Clock::new();
|
|
|
|
|
2019-06-17 13:07:55 +00:00
|
|
|
println!("Enter your username");
|
|
|
|
let mut username = read_input();
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Create a client.
|
2019-04-29 20:37:19 +00:00
|
|
|
let mut client =
|
2019-05-19 01:06:34 +00:00
|
|
|
Client::new(([127, 0, 0, 1], 59003), None).expect("Failed to create client instance");
|
2019-03-03 22:02:38 +00:00
|
|
|
|
2019-05-08 16:22:52 +00:00
|
|
|
println!("Server info: {:?}", client.server_info);
|
|
|
|
|
2019-06-02 14:35:21 +00:00
|
|
|
println!("Players online: {:?}", client.get_players());
|
2019-04-24 07:59:42 +00:00
|
|
|
|
2019-06-17 10:40:19 +00:00
|
|
|
client.register(comp::Player::new(username, None));
|
2019-04-02 11:59:42 +00:00
|
|
|
|
2019-06-17 13:07:55 +00:00
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
thread::spawn(move || {
|
|
|
|
loop {
|
|
|
|
let msg = read_input();
|
|
|
|
tx.send(msg).unwrap();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-06-09 14:20:20 +00:00
|
|
|
let events = match client.tick(comp::Controller::default(), clock.get_last_delta()) {
|
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;
|
2019-04-29 20:37:19 +00:00
|
|
|
}
|
2019-03-05 00:00:11 +00:00
|
|
|
};
|
2019-03-03 22:02:38 +00:00
|
|
|
|
|
|
|
for event in events {
|
|
|
|
match event {
|
2019-06-17 10:40:19 +00:00
|
|
|
Event::Chat(msg) => println!("{}", msg),
|
2019-04-23 12:28:45 +00:00
|
|
|
Event::Disconnect => {} // TODO
|
2019-03-03 22:02:38 +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.
|
2019-06-08 23:49:48 +00:00
|
|
|
clock.tick(Duration::from_millis(1000 / TPS));
|
2019-03-03 22:02:38 +00:00
|
|
|
}
|
|
|
|
}
|