hexland-server/src/main.rs

54 lines
1.5 KiB
Rust
Raw Normal View History

2022-09-20 15:53:53 -05:00
use std::net::TcpListener;
use std::thread;
use std::sync::{Arc, Mutex};
2022-10-03 13:18:40 -05:00
use std::collections::HashMap;
2022-10-03 14:18:25 -05:00
mod message;
use message::Message;
2022-10-03 14:42:15 -05:00
mod code_generator;
use code_generator::CodeGenerator;
2022-10-03 16:49:32 -05:00
mod websocket;
use websocket::WebsocketWrapper;
2022-09-20 15:53:53 -05:00
fn main() {
2022-10-03 13:18:40 -05:00
let code_generator = Arc::new(Mutex::new(CodeGenerator::default()));
2022-09-20 15:53:53 -05:00
let server = TcpListener::bind("127.0.0.1:8080").unwrap();
2022-10-03 13:18:40 -05:00
let rooms = Arc::new(Mutex::new(HashMap::new()));
2022-09-20 15:53:53 -05:00
for stream in server.incoming() {
2022-10-03 13:18:40 -05:00
let code_generator = Arc::clone(&code_generator);
let rooms = Arc::clone(&rooms);
thread::spawn (move || {
2022-10-04 09:46:17 -05:00
let ws = tungstenite::accept(stream.unwrap()).unwrap();
2022-10-03 16:49:32 -05:00
let mut ws = WebsocketWrapper::new(ws);
2022-10-04 09:46:17 -05:00
2022-10-03 16:49:32 -05:00
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();
2022-10-04 09:46:17 -05:00
let player = Player {};
2022-10-03 16:49:32 -05:00
room.players.push(player);
rooms.lock().unwrap().insert(code.clone(), room);
ws.send(msg!("CODE", code));
2022-10-03 14:18:25 -05:00
}
_ => unimplemented!(),
2022-09-20 15:53:53 -05:00
}
}
2022-10-04 09:46:17 -05:00
2022-10-03 13:18:40 -05:00
});
2022-09-20 15:53:53 -05:00
}
}
2022-10-04 09:46:17 -05:00
2022-10-03 13:18:40 -05:00
#[derive(Default)]
2022-09-20 15:53:53 -05:00
struct Room {
players: Vec<Player>,
}
struct Player {
}