summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornatrixaeria <janng@gmx.de>2019-05-11 06:26:26 +0200
committernatrixaeria <janng@gmx.de>2019-05-11 06:26:26 +0200
commit73eb78e3ac5eb7d382c1aca98da84cd866d54570 (patch)
tree92fb23c91eb812adfc6769e139537758d5002b4e
parente2d83e09f8af95c82385967f61e2ec8342a4b2c9 (diff)
Create a fundamental project structure
-rwxr-xr-xgame_server/build.sh8
-rw-r--r--game_server/src/group.rs19
-rw-r--r--game_server/src/lobby.rs35
-rw-r--r--game_server/src/main.rs15
4 files changed, 70 insertions, 7 deletions
diff --git a/game_server/build.sh b/game_server/build.sh
index 9def83e..c42a7ac 100755
--- a/game_server/build.sh
+++ b/game_server/build.sh
@@ -1,4 +1,8 @@
#!/usr/bin/env sh
-rustup run nightly cargo build
-RUST_LOG=trace target/debug/game-server
+if rustup run nightly cargo build; then
+ echo build success!
+ RUST_LOG=trace target/debug/game-server
+else
+ echo build failed!
+fi
diff --git a/game_server/src/group.rs b/game_server/src/group.rs
index 3241eee..9774979 100644
--- a/game_server/src/group.rs
+++ b/game_server/src/group.rs
@@ -1,7 +1,22 @@
-struct Group {
- id: u32,
+pub type GroupId = u32;
+
+pub struct Group {
+ id: GroupId,
name: String,
}
impl Group {
+ pub(crate) fn new(id: GroupId, name: String) -> Group {
+ Group { id, name }
+ }
+
+ pub(crate) fn get_id(&self) -> GroupId {
+ self.id
+ }
+
+ pub fn run(&self) {
+ let id = self.id;
+ std::thread::spawn(move ||
+ loop {println!("group id: {} meldet sich", id)});
+ }
}
diff --git a/game_server/src/lobby.rs b/game_server/src/lobby.rs
index c0717d3..8808b7f 100644
--- a/game_server/src/lobby.rs
+++ b/game_server/src/lobby.rs
@@ -1,2 +1,35 @@
-struct Lobby {
+use std::collections::HashMap;
+
+use super::group::{Group, GroupId};
+
+pub struct Lobby {
+ groups: HashMap<GroupId, Group>,
+}
+
+impl Lobby {
+ pub fn new() -> Lobby {
+ Self {
+ groups: HashMap::new(),
+ }
+ }
+
+ pub fn add_group(&mut self, group: Group) {
+ self.groups.insert(group.get_id(), group);
+ }
+
+ pub fn iter<'a>(&'a self) -> GroupIterator<'a> {
+ GroupIterator { groups: self.groups.values() }
+ }
+}
+
+pub struct GroupIterator<'a> {
+ groups: std::collections::hash_map::Values<'a, GroupId, Group>
+}
+
+impl<'a> Iterator for GroupIterator<'a> {
+ type Item = &'a Group;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ self.groups.next()
+ }
}
diff --git a/game_server/src/main.rs b/game_server/src/main.rs
index 685c633..7e08b46 100644
--- a/game_server/src/main.rs
+++ b/game_server/src/main.rs
@@ -1,11 +1,22 @@
-mod lobby;
mod group;
+mod lobby;
#[macro_use] extern crate log;
use pretty_env_logger;
+use group::Group;
+use lobby::Lobby;
+
fn main() {
pretty_env_logger::init();
- trace!("test info");
+ let mut lobby = Lobby::new();
+ lobby.add_group(Group::new(0, "Test".to_string()));
+ lobby.add_group(Group::new(1, "Very Serious".to_string()));
+
+ for group in lobby.iter() {
+ group.run()
+ }
+
+ loop {}
}