From 3f539662518609390964c60acf2f38d9a08aee6d Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Fri, 31 May 2019 04:17:35 +0200 Subject: Extract connention loop to seperate funtion --- game_server/src/scribble_group.rs | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/game_server/src/scribble_group.rs b/game_server/src/scribble_group.rs index 01090de..2eb16f2 100644 --- a/game_server/src/scribble_group.rs +++ b/game_server/src/scribble_group.rs @@ -33,24 +33,7 @@ impl Group for ScribbleGroup { 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)); } } @@ -58,4 +41,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>>) { + 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); + } } -- cgit v1.2.3-54-g00ecf From 1b6b5acf63d5d78207dcd746d080cafcd1bfbd84 Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Fri, 31 May 2019 04:20:08 +0200 Subject: Make reciver and sender of the Gameclient imutable --- game_server/src/server.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game_server/src/server.rs b/game_server/src/server.rs index 6294e2c..ea33e2a 100644 --- a/game_server/src/server.rs +++ b/game_server/src/server.rs @@ -65,7 +65,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) } } -- cgit v1.2.3-54-g00ecf From cdb4799231837e1e1a07f484b0b8a944819e6e78 Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Fri, 31 May 2019 04:57:08 +0200 Subject: Get rid of WarningGet rid of Warninss --- WebInterface/Web.config | 15 --------------- game_server/src/lobby.rs | 4 +++- game_server/src/scribble_group.rs | 2 +- game_server/src/server.rs | 14 ++++++++------ 4 files changed, 12 insertions(+), 23 deletions(-) delete mode 100644 WebInterface/Web.config 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 @@ - - - - - - - - \ No newline at end of file diff --git a/game_server/src/lobby.rs b/game_server/src/lobby.rs index d03bd45..ef164af 100644 --- a/game_server/src/lobby.rs +++ b/game_server/src/lobby.rs @@ -9,6 +9,7 @@ pub struct Lobby { groups: HashMap>, } +#[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, } } @@ -48,6 +49,7 @@ impl Lobby { } } +#[allow(dead_code)] pub struct GroupIterator<'a> { groups: std::collections::hash_map::Values<'a, GroupId, Box> } diff --git a/game_server/src/scribble_group.rs b/game_server/src/scribble_group.rs index 2eb16f2..0d2c0f1 100644 --- a/game_server/src/scribble_group.rs +++ b/game_server/src/scribble_group.rs @@ -29,7 +29,7 @@ impl Group for ScribbleGroup { fn add_client(&mut self, id: UserId, client: GameClient) { 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; diff --git a/game_server/src/server.rs b/game_server/src/server.rs index ea33e2a..0b76f15 100644 --- a/game_server/src/server.rs +++ b/game_server/src/server.rs @@ -8,7 +8,6 @@ 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; @@ -121,16 +120,19 @@ impl GameServer { } fn read_clients(&self) -> Receiver { - let (s, r): (Sender, Receiver) + let (sen, rec): (Sender, Receiver) = 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) -> Result<(), GameServerError> { + fn handle_requests(addr: SocketAddr, sen: &Sender) -> Result<(), GameServerError> { let server = match Server::::bind(addr) { Ok(v) => v, Err(e) => { @@ -140,7 +142,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(()) -- cgit v1.2.3-54-g00ecf From 031ba8a21d3899d4e24bd1423ec738ebf95ab947 Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Fri, 31 May 2019 06:49:25 +0200 Subject: Improve Exception Handling Add WebhoggGroup --- game_server/src/backend_connection.rs | 4 +- game_server/src/group.rs | 4 +- game_server/src/lobby.rs | 12 +++--- game_server/src/main.rs | 1 + game_server/src/scribble_group.rs | 10 +++-- game_server/src/server.rs | 24 ++++++++++-- game_server/src/webhogg_group.rs | 71 +++++++++++++++++++++++++++++++++++ 7 files changed, 108 insertions(+), 18 deletions(-) create mode 100644 game_server/src/webhogg_group.rs 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/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 ef164af..6d11a5f 100644 --- a/game_server/src/lobby.rs +++ b/game_server/src/lobby.rs @@ -1,9 +1,9 @@ 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>, @@ -31,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> { diff --git a/game_server/src/main.rs b/game_server/src/main.rs index ab73a97..a6cc608 100644 --- a/game_server/src/main.rs +++ b/game_server/src/main.rs @@ -1,5 +1,6 @@ mod group; mod scribble_group; +mod webhogg_group; mod lobby; mod server; mod backend_connection; diff --git a/game_server/src/scribble_group.rs b/game_server/src/scribble_group.rs index 0d2c0f1..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,13 +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, 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(()) } } diff --git a/game_server/src/server.rs b/game_server/src/server.rs index 0b76f15..5b1a7a9 100644 --- a/game_server/src/server.rs +++ b/game_server/src/server.rs @@ -8,8 +8,8 @@ use websocket::{OwnedMessage, use std::net::{SocketAddr, ToSocketAddrs, TcpStream}; use std::sync::{mpsc, mpsc::{Sender, Receiver}}; -use super::lobby::Lobby; -use super::backend_connection::BackendConnection; +use crate::lobby::Lobby; +use crate::backend_connection::BackendConnection; pub type ClientReceiver = receiver::Reader<::Reader>; pub type ClientSender = sender::Writer<::Writer>; @@ -24,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 { @@ -111,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 { 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>> +} + +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>>) { + 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); + } +} -- cgit v1.2.3-54-g00ecf From 3527f10f8767b3b62b876f8a3f3bc5bd3ba56b2d Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Fri, 31 May 2019 07:35:55 +0200 Subject: Add Vec2 --- game_server/src/maths.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 game_server/src/maths.rs diff --git a/game_server/src/maths.rs b/game_server/src/maths.rs new file mode 100644 index 0000000..635eed6 --- /dev/null +++ b/game_server/src/maths.rs @@ -0,0 +1,54 @@ +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: x + other.x, + y: y + other.y + } + } +} + +impl std::ops::Sub for Vec2 { + type Output = Self; + fn sub(self, other: Self) -> Self { + Self { + x: x - other.x, + y: y - other.y + } + } +} + +impl std::ops::Neg for Vec2 { + type Output = Self; + fn sub(self) -> Self { + Self { + x: -x, + y: -y + } + } +} + +impl std::cmp::PartialEq for Vec2 { + type Output = bool; + 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 + } +} -- cgit v1.2.3-54-g00ecf From 79db730d4e42ce592af753059422410979ff26b0 Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Fri, 31 May 2019 14:53:37 +0200 Subject: tart implementing game logic --- game_server/src/collide.rs | 36 +++++++++++ game_server/src/main.rs | 7 +++ game_server/src/maths.rs | 123 ++++++++++++++++++++++++++++++++++++++ game_server/src/webhogg_game.rs | 13 ++++ game_server/src/webhogg_group.rs | 71 ++++++++++++++++++++++ game_server/src/webhogg_player.rs | 3 + 6 files changed, 253 insertions(+) create mode 100644 game_server/src/collide.rs create mode 100644 game_server/src/maths.rs create mode 100644 game_server/src/webhogg_game.rs create mode 100644 game_server/src/webhogg_group.rs create mode 100644 game_server/src/webhogg_player.rs diff --git a/game_server/src/collide.rs b/game_server/src/collide.rs new file mode 100644 index 0000000..4169787 --- /dev/null +++ b/game_server/src/collide.rs @@ -0,0 +1,36 @@ +pub trait Collide { + fn collides(&self, other: &Rhs) -> bool; +} + +impl Collide for Vec2 { + fn collides(self, other: Self) { + self == other + } +} + +impl Collide for Box { + fn collides(self, other: Vec2) { + self.pos < other < self.pos + self.size + } +} + +impl Collide for Box { + fn collides(self, other: Self) { + self.collides(other.pos) + || other.collides(self.pos) + } +} + +impl Collide for RBox { + fn collides(self, other: Vec2) { + + || other.pos < self.pos < other.pos + other.size + } +} + +impl Collide for Box { + fn collides(self, other: Self) { + self.pos < other.pos < self.pos + self.size + || other.pos < self.pos < other.pos + other.size + } +} diff --git a/game_server/src/main.rs b/game_server/src/main.rs index ab73a97..e10fe3c 100644 --- a/game_server/src/main.rs +++ b/game_server/src/main.rs @@ -1,5 +1,12 @@ mod group; +mod maths; mod scribble_group; +<<<<<<< Updated upstream +======= +mod webhogg_group; +mod webhogg_game; +mod collide; +>>>>>>> Stashed changes 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..8844c6e --- /dev/null +++ b/game_server/src/maths.rs @@ -0,0 +1,123 @@ +#[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 { + 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 struct Box { + pub pos: Vec2, + /// the size may not be smaller than zero + pub size: Vec2, +} + +impl std::ops::Add for Box { + type Output = Self; + fn add(self, other: Vec2) -> Self { + Self { + p1: self.p1 + other, + p2: self.p2 + other, + } + } +} + +impl std::ops::Sub for Box { + type Output = Self; + fn sub(self, other: Vec2) -> Self { + Self { + pos: self.pos + other, + size: self.size + other + } + } +} + +pub struct RBox { + /// Point 1 + pub p1: Vec2, + /// Point 2 + pub p2: Vec2, + /// Width + pub w: f32, +} + +impl std::ops::Add for RBox { + type Output = Self; + fn add(self, other: Vec2) -> Self { + Self { + p1: self.p1 + other, + p2: self.p2 + other, + w: self.w, + } + } +} + +impl std::ops::Sub for RBox { + type Output = Self; + fn sub(self, other: Vec2) -> Self { + Self { + p1: self.p1 + other, + p2: self.p2 + other, + w: self.w, + } + } +} 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..5a326d8 --- /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>> +} + +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::launch_game(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 launch_game(self_uid: UserId, mut rec: ClientReceiver, senders_mutex: Arc>>) { + 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 { + +} -- cgit v1.2.3-54-g00ecf From 4cdb0d88a065df2456b3e12389836eebc9b2fa4a Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Fri, 31 May 2019 16:40:00 +0200 Subject: Fix issues in Collide --- game_server/src/collide.rs | 63 ++++++++++++++++++++++++++++++++++++---------- game_server/src/main.rs | 3 --- game_server/src/maths.rs | 14 +++++------ 3 files changed, 57 insertions(+), 23 deletions(-) diff --git a/game_server/src/collide.rs b/game_server/src/collide.rs index 4169787..4217e7f 100644 --- a/game_server/src/collide.rs +++ b/game_server/src/collide.rs @@ -1,36 +1,73 @@ +use crate::maths::{Vec2, Box, RBox}; + pub trait Collide { fn collides(&self, other: &Rhs) -> bool; } impl Collide for Vec2 { - fn collides(self, other: Self) { + fn collides(&self, other: &Self) -> bool { self == other } } impl Collide for Box { - fn collides(self, other: Vec2) { - self.pos < other < self.pos + self.size + fn collides(&self, other: &Vec2) -> bool { + self.pos < other && other < self.pos + self.size } } impl Collide for Box { - fn collides(self, other: Self) { - self.collides(other.pos) - || other.collides(self.pos) + fn collides(&self, other: &Self) -> bool { + self.collides(other.pos) + || self.collides(other.pos + Vec2{x: other.x, y: 0}) + || self.collides(other.pos + Vec2{x: 0, y: other.y}) + || self.collides(other.pos + other.size) + + || other.collides(self.pos) + || other.collides(self.pos + Vec2{x: self.x, y: 0}) + || other.collides(self.pos + Vec2{x: 0, y: self.y}) + || other.collides(self.pos + self.size) } } impl Collide for RBox { - fn collides(self, other: Vec2) { - - || other.pos < self.pos < other.pos + other.size + fn collides(&self, other: &Vec2) -> bool { + let dx = self.size.x; + let dy = self.size.y; + let len = f32::sqrt(dx*dx+dy*dy); + dx /= len; + dy /= len; + + let dax = other.x - self.p1.x; + let day = other.y - self.p1.y; + + let dot = dax * dx + day * dy; + let px = self.pos.x + dx * dot; + let py = self.pos.y + dy * dot; + + if !(self.pos < px && px < 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 for Box { - fn collides(self, other: Self) { - self.pos < other.pos < self.pos + self.size - || other.pos < self.pos < other.pos + other.size +impl Collide for RBox { + fn collides(&self, other: &Box) -> bool { + self.collides(other.pos) + || self.collides(other.pos + Vec2{x: other.x, y: 0}) + || self.collides(other.pos + Vec2{x: 0, y: other.y}) + || self.collides(other.pos + other.size) + + || other.collides(self.pos) + || other.collides(self.pos + Vec2{x: self.x, y: 0}) + || other.collides(self.pos + Vec2{x: 0, y: self.y}) + || other.collides(self.pos + self.size) + } } diff --git a/game_server/src/main.rs b/game_server/src/main.rs index e10fe3c..cfd9787 100644 --- a/game_server/src/main.rs +++ b/game_server/src/main.rs @@ -1,12 +1,9 @@ mod group; mod maths; mod scribble_group; -<<<<<<< Updated upstream -======= mod webhogg_group; mod webhogg_game; mod collide; ->>>>>>> Stashed changes mod lobby; mod server; mod backend_connection; diff --git a/game_server/src/maths.rs b/game_server/src/maths.rs index 8844c6e..a55c5ce 100644 --- a/game_server/src/maths.rs +++ b/game_server/src/maths.rs @@ -93,10 +93,10 @@ impl std::ops::Sub for Box { pub struct RBox { /// Point 1 - pub p1: Vec2, + pub pos: Vec2, /// Point 2 - pub p2: Vec2, - /// Width + pub size: Vec2, + /// Width Attention manhatten distance!!! pub w: f32, } @@ -104,8 +104,8 @@ impl std::ops::Add for RBox { type Output = Self; fn add(self, other: Vec2) -> Self { Self { - p1: self.p1 + other, - p2: self.p2 + other, + pos: self.p1 + other, + size: self.p2 + other, w: self.w, } } @@ -115,8 +115,8 @@ impl std::ops::Sub for RBox { type Output = Self; fn sub(self, other: Vec2) -> Self { Self { - p1: self.p1 + other, - p2: self.p2 + other, + pos: self.p1 + other, + size: self.p2 + other, w: self.w, } } -- cgit v1.2.3-54-g00ecf From 2f7b8cfb0c48b600ebd80bdfb3ef9f04410b0b91 Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Fri, 31 May 2019 17:05:22 +0200 Subject: Use references for Colision trait implementations --- game_server/src/collide.rs | 49 +++++++++++++++++++++++----------------------- game_server/src/maths.rs | 12 ++++++------ 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/game_server/src/collide.rs b/game_server/src/collide.rs index 4217e7f..ac046dd 100644 --- a/game_server/src/collide.rs +++ b/game_server/src/collide.rs @@ -12,40 +12,41 @@ impl Collide for Vec2 { impl Collide for Box { fn collides(&self, other: &Vec2) -> bool { - self.pos < other && other < self.pos + self.size + self.pos < *other && other < &(self.pos + self.size) } } impl Collide for Box { fn collides(&self, other: &Self) -> bool { - self.collides(other.pos) - || self.collides(other.pos + Vec2{x: other.x, y: 0}) - || self.collides(other.pos + Vec2{x: 0, y: other.y}) - || self.collides(other.pos + other.size) + 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.x, y: 0}) - || other.collides(self.pos + Vec2{x: 0, y: self.y}) - || other.collides(self.pos + self.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 for RBox { fn collides(&self, other: &Vec2) -> bool { - let dx = self.size.x; - let dy = self.size.y; - let len = f32::sqrt(dx*dx+dy*dy); + let mut dx = self.size.x; + let mut dy = self.size.y; + let len = self.size.distance(); dx /= len; dy /= len; - let dax = other.x - self.p1.x; - let day = other.y - self.p1.y; + let dax = other.x - self.pos.x; + let day = other.y - self.pos.y; let dot = dax * dx + day * dy; let px = self.pos.x + dx * dot; let py = self.pos.y + dy * dot; - if !(self.pos < px && px < self.pos + self.size) { + let p = Vec2{x: px, y: py}; + if !(self.pos < p && p < self.pos + self.size) { return false; } @@ -59,15 +60,15 @@ impl Collide for RBox { impl Collide for RBox { fn collides(&self, other: &Box) -> bool { - self.collides(other.pos) - || self.collides(other.pos + Vec2{x: other.x, y: 0}) - || self.collides(other.pos + Vec2{x: 0, y: other.y}) - || self.collides(other.pos + other.size) - - || other.collides(self.pos) - || other.collides(self.pos + Vec2{x: self.x, y: 0}) - || other.collides(self.pos + Vec2{x: 0, y: self.y}) - || other.collides(self.pos + self.size) + 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)) } } diff --git a/game_server/src/maths.rs b/game_server/src/maths.rs index a55c5ce..f5ceab4 100644 --- a/game_server/src/maths.rs +++ b/game_server/src/maths.rs @@ -75,8 +75,8 @@ impl std::ops::Add for Box { type Output = Self; fn add(self, other: Vec2) -> Self { Self { - p1: self.p1 + other, - p2: self.p2 + other, + pos: self.pos + other, + size: self.size + other, } } } @@ -104,8 +104,8 @@ impl std::ops::Add for RBox { type Output = Self; fn add(self, other: Vec2) -> Self { Self { - pos: self.p1 + other, - size: self.p2 + other, + pos: self.pos + other, + size: self.size + other, w: self.w, } } @@ -115,8 +115,8 @@ impl std::ops::Sub for RBox { type Output = Self; fn sub(self, other: Vec2) -> Self { Self { - pos: self.p1 + other, - size: self.p2 + other, + pos: self.pos + other, + size: self.size + other, w: self.w, } } -- cgit v1.2.3-54-g00ecf From 2f99c5e8b3cfcba29894505d25d8d6363d92ed3f Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Sat, 1 Jun 2019 02:02:10 +0200 Subject: Adress issues specified in pull qeruest --- game_server/src/collide.rs | 24 +++++++++++++----------- game_server/src/maths.rs | 19 +++++++++++++------ 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/game_server/src/collide.rs b/game_server/src/collide.rs index ac046dd..21ec99c 100644 --- a/game_server/src/collide.rs +++ b/game_server/src/collide.rs @@ -1,4 +1,4 @@ -use crate::maths::{Vec2, Box, RBox}; +use crate::maths::{Vec2, AABox, RBox}; pub trait Collide { fn collides(&self, other: &Rhs) -> bool; @@ -10,13 +10,13 @@ impl Collide for Vec2 { } } -impl Collide for Box { +impl Collide for AABox { fn collides(&self, other: &Vec2) -> bool { self.pos < *other && other < &(self.pos + self.size) } } -impl Collide for Box { +impl Collide for AABox { fn collides(&self, other: &Self) -> bool { self.collides(&other.pos) || self.collides(&(other.pos + Vec2{x: other.size.x, y: 0.0})) @@ -32,18 +32,14 @@ impl Collide for Box { impl Collide for RBox { fn collides(&self, other: &Vec2) -> bool { - let mut dx = self.size.x; - let mut dy = self.size.y; - let len = self.size.distance(); - dx /= len; - dy /= len; + let da = self.size.norm(); let dax = other.x - self.pos.x; let day = other.y - self.pos.y; - let dot = dax * dx + day * dy; + let dot = dax * da.x + day * da.y; let px = self.pos.x + dx * dot; - let py = self.pos.y + dy * 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) { @@ -58,7 +54,7 @@ impl Collide for RBox { } } -impl Collide for RBox { +impl Collide for RBox { fn collides(&self, other: &Box) -> bool { self.collides(&other.pos) || self.collides(&(other.pos + Vec2{x: other.size.x, y: 0.0})) @@ -72,3 +68,9 @@ impl Collide for RBox { } } + +impl> Collide for Vec { + fn collides(&self, other: &S) -> bool { + self.iter().any(|x| x.collides(other)) + } +} diff --git a/game_server/src/maths.rs b/game_server/src/maths.rs index f5ceab4..7b844dc 100644 --- a/game_server/src/maths.rs +++ b/game_server/src/maths.rs @@ -63,30 +63,37 @@ impl Vec2 { 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 Box { +pub struct AABox { pub pos: Vec2, /// the size may not be smaller than zero pub size: Vec2, } -impl std::ops::Add for Box { +impl std::ops::Add for AABox { type Output = Self; fn add(self, other: Vec2) -> Self { Self { pos: self.pos + other, - size: self.size + other, } } } -impl std::ops::Sub for Box { +impl std::ops::Sub for AABox { type Output = Self; fn sub(self, other: Vec2) -> Self { Self { pos: self.pos + other, - size: self.size + other + size: self.size } } } @@ -105,7 +112,7 @@ impl std::ops::Add for RBox { fn add(self, other: Vec2) -> Self { Self { pos: self.pos + other, - size: self.size + other, + size: self.size, w: self.w, } } -- cgit v1.2.3-54-g00ecf