use std::net::TcpListener; use std::thread; use std::sync::{Arc, Mutex}; use std::collections::HashMap; mod message; use message::Message; mod code_generator; use code_generator::CodeGenerator; mod websocket; use websocket::WebsocketWrapper; fn main() { let code_generator = Arc::new(Mutex::new(CodeGenerator::default())); let server = TcpListener::bind("127.0.0.1:8080").unwrap(); let rooms = Arc::new(Mutex::new(HashMap::new())); for stream in server.incoming() { let code_generator = Arc::clone(&code_generator); let rooms = Arc::clone(&rooms); thread::spawn (move || { let ws = tungstenite::accept(stream.unwrap()).unwrap(); let mut ws = WebsocketWrapper::new(ws); ws.send(msg!("HOSTJOIN")); let msg = ws.recv(); match msg { None => (), Some(msg) => match msg.command.as_str() { "HOST" => { let code = code_generator.lock().unwrap().generate(); let mut room = Room::default(); let player = Player {}; room.players.push(player); rooms.lock().unwrap().insert(code.clone(), room); ws.send(msg!("CODE", code)); } _ => unimplemented!(), } } }); } } #[derive(Default)] struct Room { players: Vec, } struct Player { }