rust/hedgewars-server/src/server/database.rs
author unc0rr
Sun, 16 Dec 2018 00:12:29 +0100
changeset 14457 98ef2913ec73
parent 14456 a077aac9df01
child 14779 f43ab2bd76ae
permissions -rw-r--r--
Apply rustfmt to all files
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14456
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
     1
use mysql;
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
     2
use mysql::{error::DriverError, error::Error, params};
14456
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
     3
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
     4
struct AccountInfo {
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
     5
    is_registered: bool,
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
     6
    is_admin: bool,
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
     7
    is_contributor: bool,
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
     8
}
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
     9
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    10
struct ServerStatistics {
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    11
    rooms: u32,
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    12
    players: u32,
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    13
}
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    14
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    15
struct Achievements {}
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    16
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    17
trait DatabaseInterface {
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    18
    fn check_account(username: &str, password: &str) -> AccountInfo;
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    19
    fn store_stats(stats: &ServerStatistics) -> Result<(), ()>;
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    20
    fn store_achievements(achievements: &Achievements) -> Result<(), ()>;
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    21
    fn get_replay_name(replay_id: u32) -> Result<String, ()>;
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    22
}
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    23
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    24
struct Database {
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    25
    pool: Option<mysql::Pool>,
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    26
}
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    27
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    28
impl Database {
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    29
    fn new() -> Self {
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    30
        Self { pool: None }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    31
    }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    32
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    33
    fn connect(&mut self, url: &str) -> Result<(), Error> {
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    34
        self.pool = Some(mysql::Pool::new(url)?);
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    35
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    36
        Ok(())
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    37
    }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    38
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    39
    fn check_account(&mut self, username: &str, password: &str) -> AccountInfo {
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    40
        AccountInfo {
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    41
            is_registered: false,
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    42
            is_admin: false,
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    43
            is_contributor: false,
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    44
        }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    45
    }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    46
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    47
    fn store_stats(&mut self, stats: &ServerStatistics) -> Result<(), Error> {
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    48
        if let Some(pool) = &self.pool {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    49
            for mut stmt in pool
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    50
                .prepare(
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    51
                    r"INSERT INTO gameserver_stats
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    52
            (players, rooms, last_update)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    53
            VALUES
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    54
            (:players, :rooms, UNIX_TIMESTAMP())",
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    55
                )
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    56
                .into_iter()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    57
            {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    58
                stmt.execute(params! {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    59
                    "players" => stats.players,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    60
                    "rooms" => stats.rooms,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    61
                })?;
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    62
            }
14456
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    63
            Ok(())
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    64
        } else {
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    65
            Err(DriverError::SetupError.into())
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    66
        }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    67
    }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    68
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    69
    fn store_achievements(&mut self, achievements: &Achievements) -> Result<(), ()> {
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    70
        Ok(())
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    71
    }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    72
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    73
    fn get_replay_name(&mut self, replay_id: u32) -> Result<String, ()> {
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    74
        Err(())
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    75
    }
a077aac9df01 Start database interaction implementation
unc0rr
parents:
diff changeset
    76
}