diff options
-rw-r--r-- | WebInterface/Web.config | 15 | ||||
-rw-r--r-- | game_server/src/backend_connection.rs | 4 | ||||
-rw-r--r-- | game_server/src/collide.rs | 76 | ||||
-rw-r--r-- | game_server/src/group.rs | 4 | ||||
-rw-r--r-- | game_server/src/lobby.rs | 16 | ||||
-rw-r--r-- | game_server/src/main.rs | 4 | ||||
-rw-r--r-- | game_server/src/maths.rs | 130 | ||||
-rw-r--r-- | game_server/src/scribble_group.rs | 50 | ||||
-rw-r--r-- | game_server/src/server.rs | 40 | ||||
-rw-r--r-- | game_server/src/webhogg_game.rs | 13 | ||||
-rw-r--r-- | game_server/src/webhogg_group.rs | 71 | ||||
-rw-r--r-- | game_server/src/webhogg_player.rs | 3 |
12 files changed, 366 insertions, 60 deletions
diff --git a/WebInterface/Web.config b/WebInterface/Web.config deleted file mode 100644 index 741b7d8..0000000 --- a/WebInterface/Web.config +++ /dev/null @@ -1,15 +0,0 @@ -<?xml version="1.0"?> -<configuration> - <!-- - Eine Beschreibung der Änderungen von 'web.config' finden Sie unter 'http://go.microsoft.com/fwlink/?LinkId=235367'. - - Die folgenden Attribute können für die <httpRuntime>-Kennung festgelegt werden. - <system.Web> - <httpRuntime targetFramework="4.5" /> - </system.Web> - --> - <system.web> - <compilation debug="false" targetFramework="4.5"/> - <pages controlRenderingCompatibilityVersion="4.0"/> - </system.web> -</configuration>
\ No newline at end of file diff --git a/game_server/src/backend_connection.rs b/game_server/src/backend_connection.rs index d32c58e..a751b30 100644 --- a/game_server/src/backend_connection.rs +++ b/game_server/src/backend_connection.rs @@ -1,8 +1,8 @@ use reqwest::{Response, Client, Url, UrlError, Error as ReqError}; use std::sync::mpsc::{Sender, Receiver}; use std::sync::mpsc; -use super::server::{UserId, Token}; -use super::group::GroupId; +use crate::server::{UserId, Token}; +use crate::group::GroupId; pub struct BackendConnection { host: String, diff --git a/game_server/src/collide.rs b/game_server/src/collide.rs new file mode 100644 index 0000000..21ec99c --- /dev/null +++ b/game_server/src/collide.rs @@ -0,0 +1,76 @@ +use crate::maths::{Vec2, AABox, RBox}; + +pub trait Collide<Rhs> { + fn collides(&self, other: &Rhs) -> bool; +} + +impl Collide<Vec2> for Vec2 { + fn collides(&self, other: &Self) -> bool { + self == other + } +} + +impl Collide<Vec2> for AABox { + fn collides(&self, other: &Vec2) -> bool { + self.pos < *other && other < &(self.pos + self.size) + } +} + +impl Collide<AABox> for AABox { + fn collides(&self, other: &Self) -> bool { + self.collides(&other.pos) + || self.collides(&(other.pos + Vec2{x: other.size.x, y: 0.0})) + || self.collides(&(other.pos + Vec2{x: 0.0, y: other.size.y})) + || self.collides(&(other.pos + other.size)) + + || other.collides(&(self.pos)) + || other.collides(&(self.pos + Vec2{x: self.size.x, y: 0.0})) + || other.collides(&(self.pos + Vec2{x: 0.0, y: self.size.y})) + || other.collides(&(self.pos + self.size)) + } +} + +impl Collide<Vec2> for RBox { + fn collides(&self, other: &Vec2) -> bool { + + let da = self.size.norm(); + let dax = other.x - self.pos.x; + let day = other.y - self.pos.y; + + let dot = dax * da.x + day * da.y; + let px = self.pos.x + dx * dot; + let py = self.pos.y + da.y * dot; + + let p = Vec2{x: px, y: py}; + if !(self.pos < p && p < self.pos + self.size) { + return false; + } + + let ddx = other.x-px; + let ddy = other.y-py; + let manhattenDistance = ddx + ddy; + + manhattenDistance < self.w + } +} + +impl Collide<AABox> for RBox { + fn collides(&self, other: &Box) -> bool { + self.collides(&other.pos) + || self.collides(&(other.pos + Vec2{x: other.size.x, y: 0.0})) + || self.collides(&(other.pos + Vec2{x: 0.0, y: other.size.y})) + || self.collides(&(other.pos + other.size)) + + || other.collides(&(self.pos)) + || other.collides(&(self.pos + Vec2{x: self.size.x, y: 0.0})) + || other.collides(&(self.pos + Vec2{x: 0.0, y: self.size.y})) + || other.collides(&(self.pos + self.size)) + + } +} + +impl<S, T: Collide<S>> Collide<S> for Vec<T> { + fn collides(&self, other: &S) -> bool { + self.iter().any(|x| x.collides(other)) + } +} diff --git a/game_server/src/group.rs b/game_server/src/group.rs index fcda12a..74a04f7 100644 --- a/game_server/src/group.rs +++ b/game_server/src/group.rs @@ -1,4 +1,4 @@ -use super::server::{UserId, GameClient}; +use crate::server::{UserId, GameClient, GameServerError}; pub type GroupId = u32; @@ -9,5 +9,5 @@ pub trait Group { fn run(&mut self); - fn add_client(&mut self, id: UserId, client: GameClient); + fn add_client(&mut self, id: UserId, client: GameClient) -> Result<(), GameServerError>; } diff --git a/game_server/src/lobby.rs b/game_server/src/lobby.rs index d03bd45..6d11a5f 100644 --- a/game_server/src/lobby.rs +++ b/game_server/src/lobby.rs @@ -1,14 +1,15 @@ use std::collections::HashMap; -use super::group::{Group, GroupId}; -use super::scribble_group::ScribbleGroup; +use crate::group::{Group, GroupId}; +use crate::scribble_group::ScribbleGroup; -use super::server::{UserId, GameClient}; +use crate::server::{UserId, GameClient, GameServerError}; pub struct Lobby { groups: HashMap<GroupId, Box<Group>>, } +#[allow(dead_code)] impl Lobby { pub fn new() -> Self { Self { @@ -21,7 +22,7 @@ impl Lobby { "scribble" => { Some(Box::new(ScribbleGroup::new(id, name.to_string()))) }, - other => None, + _ => None, } } @@ -30,17 +31,17 @@ impl Lobby { } pub fn add_client(&mut self, group_type: &str, group_id: GroupId, group_name: &str, - user_id: UserId, client: GameClient) { + user_id: UserId, client: GameClient) -> Result<(), GameServerError> { if !self.groups.contains_key(&group_id) { let mut group = match Self::generate_group(group_type, group_id, group_name) { Some(x) => x, - _ => return, + _ => return Err(GameServerError::GroupCreationError(format!("failed to generate '{}' group", group_type))), }; group.run(); self.groups.insert(group_id, group); } let group = self.groups.get_mut(&group_id).unwrap(); - group.add_client(user_id, client); + group.add_client(user_id, client) } pub fn iter<'b>(&'b self) -> GroupIterator<'b> { @@ -48,6 +49,7 @@ impl Lobby { } } +#[allow(dead_code)] pub struct GroupIterator<'a> { groups: std::collections::hash_map::Values<'a, GroupId, Box<Group>> } diff --git a/game_server/src/main.rs b/game_server/src/main.rs index ab73a97..cfd9787 100644 --- a/game_server/src/main.rs +++ b/game_server/src/main.rs @@ -1,5 +1,9 @@ mod group; +mod maths; mod scribble_group; +mod webhogg_group; +mod webhogg_game; +mod collide; mod lobby; mod server; mod backend_connection; diff --git a/game_server/src/maths.rs b/game_server/src/maths.rs new file mode 100644 index 0000000..7b844dc --- /dev/null +++ b/game_server/src/maths.rs @@ -0,0 +1,130 @@ +#[derive(Clone, Copy)] +pub struct Vec2 { + pub x: f32, + pub y: f32, +} + +impl std::ops::Add for Vec2 { + type Output = Self; + fn add(self, other: Self) -> Self { + Self { + x: self.x + other.x, + y: self.y + other.y + } + } +} + +impl std::ops::Sub for Vec2 { + type Output = Self; + fn sub(self, other: Self) -> Self { + Self { + x: self.x - other.x, + y: self.y - other.y + } + } +} + +impl std::ops::Neg for Vec2 { + type Output = Self; + fn neg(self) -> Self { + Self { + x: -self.x, + y: -self.y + } + } +} + +impl std::cmp::PartialOrd for Vec2 { + fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { + Some(if self.x < other.x && self.y < other.y { + std::cmp::Ordering::Less + } else if self.x > other.x && self.y > other.y { + std::cmp::Ordering::Greater + } else { + std::cmp::Ordering::Equal + }) + } +} + +impl std::cmp::PartialEq for Vec2 { + fn eq(&self, other: &Self) -> bool { + f32::abs(self.x - other.x) < 1e-8 + && f32::abs(self.y - other.y) < 1e-8 + } +} + +impl std::cmp::Eq for Vec2 {} + +impl Vec2 { + pub fn distance(&self) -> f32 { + f32::sqrt(self.distance2()) + } + + pub fn distance2(&self) -> f32 { + self.x * self.x + self.y * self.y + } + + pub fn norm(&self) -> Vec2 { + let len = self.size.distance(); + Vec2 { + x: self.x / len, + y: self.y / len, + } + } +} + +pub struct AABox { + pub pos: Vec2, + /// the size may not be smaller than zero + pub size: Vec2, +} + +impl std::ops::Add<Vec2> for AABox { + type Output = Self; + fn add(self, other: Vec2) -> Self { + Self { + pos: self.pos + other, + } + } +} + +impl std::ops::Sub<Vec2> for AABox { + type Output = Self; + fn sub(self, other: Vec2) -> Self { + Self { + pos: self.pos + other, + size: self.size + } + } +} + +pub struct RBox { + /// Point 1 + pub pos: Vec2, + /// Point 2 + pub size: Vec2, + /// Width Attention manhatten distance!!! + pub w: f32, +} + +impl std::ops::Add<Vec2> for RBox { + type Output = Self; + fn add(self, other: Vec2) -> Self { + Self { + pos: self.pos + other, + size: self.size, + w: self.w, + } + } +} + +impl std::ops::Sub<Vec2> for RBox { + type Output = Self; + fn sub(self, other: Vec2) -> Self { + Self { + pos: self.pos + other, + size: self.size + other, + w: self.w, + } + } +} diff --git a/game_server/src/scribble_group.rs b/game_server/src/scribble_group.rs index 01090de..8980e7b 100644 --- a/game_server/src/scribble_group.rs +++ b/game_server/src/scribble_group.rs @@ -1,6 +1,7 @@ -use super::group::{Group, GroupId}; -use super::server::{UserId, GameClient, - ClientSender, ClientReceiver}; +use crate::group::{Group, GroupId}; +use crate::server::{UserId, GameClient, + ClientSender, ClientReceiver, + GameServerError}; use std::collections::HashMap; use std::sync::{Arc, Mutex}; @@ -27,30 +28,14 @@ impl Group for ScribbleGroup { info!("a new group {}:'{}' runs now", self.id, self.name); } - fn add_client(&mut self, id: UserId, client: GameClient) { + fn add_client(&mut self, id: UserId, client: GameClient) -> Result<(), GameServerError> { debug!("user {} joined the group {}:'{}'", id, self.id, self.name); - let (sen, mut rec) = client.split(); + let (sen, rec) = client.split(); self.senders.lock().unwrap().insert(id, sen); let senders_mutex = self.senders.clone(); let self_uid = id; - std::thread::spawn(move || { - loop { - let message = match rec.recv_message() { - Ok(x) => x, - _ => break - }; - //trace!("got message: '{:?}'", message); - let mut senders = senders_mutex.lock().unwrap(); - for (uid, sender) in senders.iter_mut() { - if self_uid != *uid { - sender.send_message(&message) - .unwrap_or_else(|_| debug!("tried to send message to {}, but failed", *uid)); - } - } - } - senders_mutex.lock().unwrap().remove(&self_uid); - info!("client {} has left", self_uid); - }); + std::thread::spawn(move || Self::broadcast_clients(self_uid, rec, senders_mutex)); + Ok(()) } } @@ -58,4 +43,23 @@ impl ScribbleGroup { pub fn new(id: GroupId, name: String) -> Self { Self { id, name, senders: Arc::new(Mutex::new(HashMap::new())) } } + + fn broadcast_clients(self_uid: UserId, mut rec: ClientReceiver, senders_mutex: Arc<Mutex<HashMap<UserId, ClientSender>>>) { + loop { + let message = match rec.recv_message() { + Ok(x) => x, + _ => break + }; + //trace!("got message: '{:?}'", message); + let mut senders = senders_mutex.lock().unwrap(); + for (uid, sender) in senders.iter_mut() { + if self_uid != *uid { + sender.send_message(&message) + .unwrap_or_else(|_| debug!("tried to send message to {}, but failed", *uid)); + } + } + } + senders_mutex.lock().unwrap().remove(&self_uid); + info!("client {} has left", self_uid); + } } diff --git a/game_server/src/server.rs b/game_server/src/server.rs index 6294e2c..5b1a7a9 100644 --- a/game_server/src/server.rs +++ b/game_server/src/server.rs @@ -8,9 +8,8 @@ use websocket::{OwnedMessage, use std::net::{SocketAddr, ToSocketAddrs, TcpStream}; use std::sync::{mpsc, mpsc::{Sender, Receiver}}; -use std::collections::HashMap; -use super::lobby::Lobby; -use super::backend_connection::BackendConnection; +use crate::lobby::Lobby; +use crate::backend_connection::BackendConnection; pub type ClientReceiver = receiver::Reader<<TcpStream as Splittable>::Reader>; pub type ClientSender = sender::Writer<<TcpStream as Splittable>::Writer>; @@ -25,7 +24,22 @@ pub enum GameServerError { BindError(std::io::Error), HandshakeRequestError, InvalidProtocolError, - AcceptError(std::io::Error) + AcceptError(std::io::Error), + GroupError(String), + GroupCreationError(String), +} + +impl std::fmt::Display for GameServerError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + match self { + GameServerError::BindError(e) => write!(f, "BindError: {}", e), + GameServerError::HandshakeRequestError => write!(f, "HandshakeRequestError"), + GameServerError::InvalidProtocolError => write!(f, "InvalidProtocolError"), + GameServerError::AcceptError(e) => write!(f, "AcceptError: {}", e), + GameServerError::GroupError(e) => write!(f, "GroupError: {}", e), + GameServerError::GroupCreationError(e) => write!(f, "GroupCreationError: {}", e), + } + } } pub struct GameServer { @@ -65,7 +79,7 @@ impl GameClient { } pub fn split(self) -> (ClientSender, ClientReceiver) { - let (mut rec, mut sen) = self.client.split().unwrap(); + let (rec, sen) = self.client.split().unwrap(); (sen, rec) } } @@ -112,7 +126,8 @@ impl GameServer { user_id, token, client.host_name(), group_name); //clients.lock().unwrap().insert(token, client); self.lobby.add_client(&group_type, group_id, - &group_name, user_id, client); + &group_name, user_id, client) + .unwrap_or_else(|e| warn!("failed to add client: {}", e)); } } } else { @@ -121,16 +136,19 @@ impl GameServer { } fn read_clients(&self) -> Receiver<ClientConnection> { - let (s, r): (Sender<ClientConnection>, Receiver<ClientConnection>) + let (sen, rec): (Sender<ClientConnection>, Receiver<ClientConnection>) = mpsc::channel(); let addr = self.addr; std::thread::spawn(move || { - let result = Self::handle_requests(addr, &s).or_else(|e| s.send(Err(e))); + match Self::handle_requests(addr, &sen) { + Err(e) => sen.send(Err(e)).unwrap(), + _ => (), + } }); - r + rec } - fn handle_requests(addr: SocketAddr, s: &Sender<ClientConnection>) -> Result<(), GameServerError> { + fn handle_requests(addr: SocketAddr, sen: &Sender<ClientConnection>) -> Result<(), GameServerError> { let server = match Server::<NoTlsAcceptor>::bind(addr) { Ok(v) => v, Err(e) => { @@ -140,7 +158,7 @@ impl GameServer { }; info!("webserver is being launched"); for req in server { - s.send(Ok(Self::handle_request(req)?)).unwrap(); + sen.send(Ok(Self::handle_request(req)?)).unwrap(); } info!("webserver is being shut down"); Ok(()) diff --git a/game_server/src/webhogg_game.rs b/game_server/src/webhogg_game.rs new file mode 100644 index 0000000..7b94fcb --- /dev/null +++ b/game_server/src/webhogg_game.rs @@ -0,0 +1,13 @@ +use crate::maths::Vec2; + +pub struct WebhoggPlayer { + pos: Vec2, +} + +pub struct WebhoggGame { + player1: WebhoggPlayer, + player2: WebhoggPlayer, +} + +impl WebhoggGame { +} diff --git a/game_server/src/webhogg_group.rs b/game_server/src/webhogg_group.rs new file mode 100644 index 0000000..091f7f8 --- /dev/null +++ b/game_server/src/webhogg_group.rs @@ -0,0 +1,71 @@ +use crate::group::{Group, GroupId}; +use crate::server::{UserId, GameClient, + ClientSender, ClientReceiver, + GameServerError}; +use std::collections::HashMap; +use std::sync::{Arc, Mutex}; + +pub struct WebhoggGroup { + id: GroupId, + name: String, + senders: Arc<Mutex<HashMap<UserId, ClientSender>>> +} + +impl Group for WebhoggGroup { + fn id(&self) -> GroupId { + self.id + } + + fn group_type(&self) -> String { + "webhogg".to_string() + } + + fn name(&self) -> String { + self.name.clone() + } + + fn run(&mut self) { + info!("a new group {}:'{}' runs now", self.id, self.name); + } + + fn add_client(&mut self, id: UserId, client: GameClient) -> Result<(), GameServerError> { + if self.senders.lock().unwrap().len() > 1 { + return Err(GameServerError::GroupError( + format!("user {} was not able to join the {} group, {}", + "because the client limit has been exceeded", + id, self.name))); + } + debug!("user {} joined the group {}:'{}'", id, self.id, self.name); + let (sen, rec) = client.split(); + self.senders.lock().unwrap().insert(id, sen); + let senders_mutex = self.senders.clone(); + let self_uid = id; + std::thread::spawn(move || Self::broadcast_clients(self_uid, rec, senders_mutex)); + Ok(()) + } +} + +impl WebhoggGroup { + pub fn new(id: GroupId, name: String) -> Self { + Self { id, name, senders: Arc::new(Mutex::new(HashMap::new())) } + } + + fn broadcast_clients(self_uid: UserId, mut rec: ClientReceiver, senders_mutex: Arc<Mutex<HashMap<UserId, ClientSender>>>) { + loop { + let message = match rec.recv_message() { + Ok(x) => x, + _ => break + }; + //trace!("got message: '{:?}'", message); + let mut senders = senders_mutex.lock().unwrap(); + for (uid, sender) in senders.iter_mut() { + if self_uid != *uid { + sender.send_message(&message) + .unwrap_or_else(|_| debug!("tried to send message to {}, but failed", *uid)); + } + } + } + senders_mutex.lock().unwrap().remove(&self_uid); + info!("client {} has left", self_uid); + } +} diff --git a/game_server/src/webhogg_player.rs b/game_server/src/webhogg_player.rs new file mode 100644 index 0000000..38b9596 --- /dev/null +++ b/game_server/src/webhogg_player.rs @@ -0,0 +1,3 @@ +pub struct WebhoggPlayer { + +} |