14461
|
1 |
use mysql;
|
14462
|
2 |
use mysql::{error::DriverError, error::Error, params};
|
14461
|
3 |
|
|
4 |
struct AccountInfo {
|
|
5 |
is_registered: bool,
|
|
6 |
is_admin: bool,
|
|
7 |
is_contributor: bool,
|
|
8 |
}
|
|
9 |
|
|
10 |
struct ServerStatistics {
|
|
11 |
rooms: u32,
|
|
12 |
players: u32,
|
|
13 |
}
|
|
14 |
|
|
15 |
struct Achievements {}
|
|
16 |
|
|
17 |
trait DatabaseInterface {
|
|
18 |
fn check_account(username: &str, password: &str) -> AccountInfo;
|
|
19 |
fn store_stats(stats: &ServerStatistics) -> Result<(), ()>;
|
|
20 |
fn store_achievements(achievements: &Achievements) -> Result<(), ()>;
|
|
21 |
fn get_replay_name(replay_id: u32) -> Result<String, ()>;
|
|
22 |
}
|
|
23 |
|
|
24 |
struct Database {
|
|
25 |
pool: Option<mysql::Pool>,
|
|
26 |
}
|
|
27 |
|
|
28 |
impl Database {
|
|
29 |
fn new() -> Self {
|
|
30 |
Self { pool: None }
|
|
31 |
}
|
|
32 |
|
|
33 |
fn connect(&mut self, url: &str) -> Result<(), Error> {
|
|
34 |
self.pool = Some(mysql::Pool::new(url)?);
|
|
35 |
|
|
36 |
Ok(())
|
|
37 |
}
|
|
38 |
|
|
39 |
fn check_account(&mut self, username: &str, password: &str) -> AccountInfo {
|
|
40 |
AccountInfo {
|
|
41 |
is_registered: false,
|
|
42 |
is_admin: false,
|
|
43 |
is_contributor: false,
|
|
44 |
}
|
|
45 |
}
|
|
46 |
|
|
47 |
fn store_stats(&mut self, stats: &ServerStatistics) -> Result<(), Error> {
|
|
48 |
if let Some(pool) = &self.pool {
|
14462
|
49 |
for mut stmt in pool
|
|
50 |
.prepare(
|
|
51 |
r"INSERT INTO gameserver_stats
|
|
52 |
(players, rooms, last_update)
|
|
53 |
VALUES
|
|
54 |
(:players, :rooms, UNIX_TIMESTAMP())",
|
|
55 |
)
|
|
56 |
.into_iter()
|
|
57 |
{
|
|
58 |
stmt.execute(params! {
|
|
59 |
"players" => stats.players,
|
|
60 |
"rooms" => stats.rooms,
|
|
61 |
})?;
|
|
62 |
}
|
14461
|
63 |
Ok(())
|
|
64 |
} else {
|
|
65 |
Err(DriverError::SetupError.into())
|
|
66 |
}
|
|
67 |
}
|
|
68 |
|
|
69 |
fn store_achievements(&mut self, achievements: &Achievements) -> Result<(), ()> {
|
|
70 |
Ok(())
|
|
71 |
}
|
|
72 |
|
|
73 |
fn get_replay_name(&mut self, replay_id: u32) -> Result<String, ()> {
|
|
74 |
Err(())
|
|
75 |
}
|
|
76 |
}
|