Start database interaction implementation
authorunc0rr
Sun, 16 Dec 2018 00:09:20 +0100
changeset 14456 a077aac9df01
parent 14455 df366c5b8440
child 14457 98ef2913ec73
Start database interaction implementation
rust/hedgewars-server/Cargo.toml
rust/hedgewars-server/src/server.rs
rust/hedgewars-server/src/server/actions.rs
rust/hedgewars-server/src/server/database.rs
rust/hedgewars-server/src/utils.rs
--- a/rust/hedgewars-server/Cargo.toml	Sat Dec 15 03:07:20 2018 +0100
+++ b/rust/hedgewars-server/Cargo.toml	Sun Dec 16 00:09:20 2018 +0100
@@ -5,7 +5,7 @@
 authors = [ "Andrey Korotaev <a.korotaev@hedgewars.org>" ]
 
 [features]
-official-server = ["openssl"]
+official-server = ["openssl", "mysql"]
 tls-connections = ["openssl"]
 default = []
 
@@ -23,6 +23,7 @@
 serde_yaml = "0.8"
 serde_derive = "1.0"
 openssl = { version = "0.10", optional = true }
+mysql = { version = "14.2", optional = true }
 
 [dev-dependencies]
-proptest = "0.8"
\ No newline at end of file
+proptest = "0.8"
--- a/rust/hedgewars-server/src/server.rs	Sat Dec 15 03:07:20 2018 +0100
+++ b/rust/hedgewars-server/src/server.rs	Sun Dec 16 00:09:20 2018 +0100
@@ -6,3 +6,5 @@
 pub mod coretypes;
 mod actions;
 mod handlers;
+#[cfg(feature = "official-server")]
+mod database;
--- a/rust/hedgewars-server/src/server/actions.rs	Sat Dec 15 03:07:20 2018 +0100
+++ b/rust/hedgewars-server/src/server/actions.rs	Sun Dec 16 00:09:20 2018 +0100
@@ -22,6 +22,9 @@
 };
 use rand::{thread_rng, Rng, distributions::Uniform};
 
+#[cfg(feature = "official-server")]
+use super::database;
+
 pub enum Destination {
     ToId(ClientId),
     ToSelf,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/hedgewars-server/src/server/database.rs	Sun Dec 16 00:09:20 2018 +0100
@@ -0,0 +1,69 @@
+use mysql;
+use mysql::{params, error::Error, error::DriverError};
+
+struct AccountInfo {
+    is_registered: bool,
+    is_admin: bool,
+    is_contributor: bool,
+}
+
+struct ServerStatistics {
+    rooms: u32,
+    players: u32,
+}
+
+struct Achievements {}
+
+trait DatabaseInterface {
+    fn check_account(username: &str, password: &str) -> AccountInfo;
+    fn store_stats(stats: &ServerStatistics) -> Result<(), ()>;
+    fn store_achievements(achievements: &Achievements) -> Result<(), ()>;
+    fn get_replay_name(replay_id: u32) -> Result<String, ()>;
+}
+
+struct Database {
+    pool: Option<mysql::Pool>,
+}
+
+impl Database {
+    fn new() -> Self {
+        Self { pool: None }
+    }
+
+    fn connect(&mut self, url: &str) -> Result<(), Error> {
+        self.pool = Some(mysql::Pool::new(url)?);
+
+        Ok(())
+    }
+
+    fn check_account(&mut self, username: &str, password: &str) -> AccountInfo {
+        AccountInfo {
+            is_registered: false,
+            is_admin: false,
+            is_contributor: false,
+        }
+    }
+
+    fn store_stats(&mut self, stats: &ServerStatistics) -> Result<(), Error> {
+        if let Some(pool) = &self.pool {
+        for mut stmt in pool.prepare(r"INSERT INTO gameserver_stats (players, rooms, last_update) VALUES (:players, :rooms, UNIX_TIMESTAMP())").into_iter() {
+                stmt.execute(params!{
+                "players" => stats.players,
+                "rooms" => stats.rooms,
+            })?;
+        }
+            Ok(())
+        } else {
+            Err(DriverError::SetupError.into())
+        }
+
+    }
+
+    fn store_achievements(&mut self, achievements: &Achievements) -> Result<(), ()> {
+        Ok(())
+    }
+
+    fn get_replay_name(&mut self, replay_id: u32) -> Result<String, ()> {
+        Err(())
+    }
+}
--- a/rust/hedgewars-server/src/utils.rs	Sat Dec 15 03:07:20 2018 +0100
+++ b/rust/hedgewars-server/src/utils.rs	Sun Dec 16 00:09:20 2018 +0100
@@ -62,6 +62,8 @@
         54 => "0.9.24-dev",
         55 => "0.9.24",
         56 => "0.9.25-dev",
+        57 => "0.9.25",
+        58 => "1.0.0-dev",
         _ => "Unknown"
     }
-}
\ No newline at end of file
+}