Run rustfmt
This commit is contained in:
parent
bf84433d5b
commit
4c0f571317
@ -1,22 +1,14 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use futures::{select, FutureExt};
|
||||
use tokio::sync::Mutex;
|
||||
use futures::{
|
||||
select,
|
||||
FutureExt,
|
||||
};
|
||||
|
||||
|
||||
use hexland_server::{
|
||||
GlobalState,
|
||||
Channel,
|
||||
channel_pair,
|
||||
GameController,
|
||||
message::{Message, MessageWebSocket},
|
||||
msg,
|
||||
msg, Channel, GameController, GlobalState,
|
||||
};
|
||||
|
||||
|
||||
pub struct Client {
|
||||
global_state: Arc<GlobalState>,
|
||||
ws: MessageWebSocket,
|
||||
@ -28,7 +20,7 @@ impl Client {
|
||||
Client {
|
||||
global_state,
|
||||
ws,
|
||||
channel: None
|
||||
channel: None,
|
||||
}
|
||||
}
|
||||
pub async fn run(&mut self) {
|
||||
@ -60,7 +52,11 @@ impl Client {
|
||||
game_controller.channels.push(server_channel);
|
||||
|
||||
let game_controller = Arc::new(Mutex::new(game_controller));
|
||||
self.global_state.rooms.lock().await.insert(room_code.clone(), Arc::clone(&game_controller));
|
||||
self.global_state
|
||||
.rooms
|
||||
.lock()
|
||||
.await
|
||||
.insert(room_code.clone(), Arc::clone(&game_controller));
|
||||
tokio::spawn(async move { game_loop(game_controller) });
|
||||
self.ws.send(msg!(ROOM_CODE, room_code)).await.unwrap();
|
||||
}
|
||||
@ -82,12 +78,14 @@ impl Client {
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => if let Some(channel) = &self.channel {
|
||||
_ => {
|
||||
if let Some(channel) = &self.channel {
|
||||
// Forward message to the server
|
||||
channel.tx.send(msg).await.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
async fn handle_server_msg(&mut self, _msg: Message) {
|
||||
todo!();
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
use rand::RngCore;
|
||||
use sha2::{Sha256, Digest};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
pub struct CodeGenerator {
|
||||
counter: u64,
|
||||
@ -24,10 +24,7 @@ impl Default for CodeGenerator {
|
||||
let mut salt = [0; 32];
|
||||
rand::thread_rng().fill_bytes(&mut salt);
|
||||
|
||||
CodeGenerator {
|
||||
counter: 0,
|
||||
salt,
|
||||
}
|
||||
CodeGenerator { counter: 0, salt }
|
||||
}
|
||||
}
|
||||
|
||||
|
14
src/lib.rs
14
src/lib.rs
@ -1,12 +1,9 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::Arc,
|
||||
};
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use tokio::sync::{Mutex, mpsc};
|
||||
use tokio::sync::{mpsc, Mutex};
|
||||
|
||||
pub mod message;
|
||||
use message::{Message};
|
||||
use message::Message;
|
||||
mod code_generator;
|
||||
use code_generator::CodeGenerator;
|
||||
|
||||
@ -24,10 +21,7 @@ pub struct Channel {
|
||||
pub fn channel_pair() -> (Channel, Channel) {
|
||||
let (atx, brx) = mpsc::channel(32);
|
||||
let (btx, arx) = mpsc::channel(32);
|
||||
(
|
||||
Channel { tx: atx, rx: arx },
|
||||
Channel { tx: btx, rx: brx },
|
||||
)
|
||||
(Channel { tx: atx, rx: arx }, Channel { tx: btx, rx: brx })
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
|
17
src/main.rs
17
src/main.rs
@ -1,14 +1,9 @@
|
||||
use std::{
|
||||
sync::Arc,
|
||||
io::Error as IoError,
|
||||
};
|
||||
use std::{io::Error as IoError, sync::Arc};
|
||||
|
||||
use tokio::{
|
||||
net::{TcpListener},
|
||||
};
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
use hexland_server::GlobalState;
|
||||
use hexland_server::message::MessageWebSocket;
|
||||
use hexland_server::GlobalState;
|
||||
|
||||
mod client;
|
||||
use client::Client;
|
||||
@ -27,9 +22,11 @@ async fn main() -> Result<(), IoError> {
|
||||
let global_state = Arc::clone(&global_state);
|
||||
tokio::spawn(async move {
|
||||
// Upgrade to a WS connection
|
||||
let ws = MessageWebSocket(tokio_tungstenite::accept_async(stream)
|
||||
let ws = MessageWebSocket(
|
||||
tokio_tungstenite::accept_async(stream)
|
||||
.await
|
||||
.expect("Could not establish connection"));
|
||||
.expect("Could not establish connection"),
|
||||
);
|
||||
println!("Connected to {}", addr);
|
||||
Client::new(ws, global_state).run().await;
|
||||
});
|
||||
|
@ -1,12 +1,9 @@
|
||||
use std::convert::{From, TryFrom};
|
||||
|
||||
use tokio::net::TcpStream;
|
||||
use tokio_tungstenite::{
|
||||
WebSocketStream,
|
||||
tungstenite::Message as WsMessage,
|
||||
};
|
||||
use futures::{StreamExt, SinkExt};
|
||||
use futures::{SinkExt, StreamExt};
|
||||
use lazy_static::lazy_static;
|
||||
use tokio::net::TcpStream;
|
||||
use tokio_tungstenite::{tungstenite::Message as WsMessage, WebSocketStream};
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub struct Message {
|
||||
@ -35,7 +32,10 @@ impl Message {
|
||||
Err(Error::BadParse)
|
||||
} else {
|
||||
let command = captures.get(1).unwrap().as_str().to_string();
|
||||
let args = captures.get(2).unwrap().as_str()
|
||||
let args = captures
|
||||
.get(2)
|
||||
.unwrap()
|
||||
.as_str()
|
||||
.split(',')
|
||||
.map(|s| s.trim().to_string())
|
||||
.collect();
|
||||
@ -53,7 +53,7 @@ impl TryFrom<WsMessage> for Message {
|
||||
fn try_from(ws_message: WsMessage) -> Result<Self> {
|
||||
let text = match ws_message {
|
||||
WsMessage::Text(text) => text,
|
||||
_ => return Err(Error::NonText)
|
||||
_ => return Err(Error::NonText),
|
||||
};
|
||||
Message::parse(text)
|
||||
}
|
||||
@ -102,7 +102,6 @@ macro_rules! msg {
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
impl std::fmt::Display for Message {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
||||
if self.args.is_empty() {
|
||||
|
Loading…
Reference in New Issue
Block a user