1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use stdweb::Value;

use {constants::ReturnCode, objects::StructureController};

simple_accessors! {
    StructureController;
    (level -> level -> u32),
    (progress -> progress -> u32),
    (progress_total -> progressTotal -> u32),
    (safe_mode -> sameMode -> u32),
    (safe_mode_available -> sameModeAvailable -> u32),
    (safe_mode_cooldown -> sameModeCooldown -> u32),
    (ticks_to_downgrade -> ticksToDowngrade -> u32),
    (upgrade_blocked -> upgradeBlocked -> u32)
}

#[derive(Debug)]
pub struct Reservation {
    pub username: String,
    pub ticks_to_end: u32,
}

#[derive(Debug)]
pub struct Sign {
    pub username: String,
    pub text: String,
    pub time: u32,
    pub datetime: String, // todo: use real date type
}

impl StructureController {
    pub fn activate_safe_mode(&self) -> ReturnCode {
        js_unwrap! {@{self.as_ref()}.activateSafeMode()}
    }

    pub fn reservation(&self) -> Option<Reservation> {
        if let Value::Reference(r) = js!(return @{self.as_ref()}.reservation;) {
            Some(Reservation {
                username: js_unwrap!(@{&r}.username),
                ticks_to_end: js_unwrap!(@{&r}.ticks_to_end),
            })
        } else {
            None
        }
    }

    pub fn sign(&self) -> Option<Sign> {
        if let Value::Reference(r) = js!(return @{self.as_ref()}.sign;) {
            Some(Sign {
                username: js_unwrap!(@{&r}.username),
                text: js_unwrap!(@{&r}.text),
                time: js_unwrap!(@{&r}.time),
                datetime: js_unwrap!(@{&r}.datetime.toString()),
            })
        } else {
            None
        }
    }

    pub fn unclaim(&self) -> ReturnCode {
        js_unwrap! {@{self.as_ref()}.unclaim()}
    }
}