|
1 use mysql; |
|
2 use mysql::{params, error::Error, error::DriverError}; |
|
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 { |
|
49 for mut stmt in pool.prepare(r"INSERT INTO gameserver_stats (players, rooms, last_update) VALUES (:players, :rooms, UNIX_TIMESTAMP())").into_iter() { |
|
50 stmt.execute(params!{ |
|
51 "players" => stats.players, |
|
52 "rooms" => stats.rooms, |
|
53 })?; |
|
54 } |
|
55 Ok(()) |
|
56 } else { |
|
57 Err(DriverError::SetupError.into()) |
|
58 } |
|
59 |
|
60 } |
|
61 |
|
62 fn store_achievements(&mut self, achievements: &Achievements) -> Result<(), ()> { |
|
63 Ok(()) |
|
64 } |
|
65 |
|
66 fn get_replay_name(&mut self, replay_id: u32) -> Result<String, ()> { |
|
67 Err(()) |
|
68 } |
|
69 } |