summaryrefslogtreecommitdiff
path: root/2026/games_submissions/rpc2dot0/src/computer/os.rs
blob: 797e07e59353cc02a2eda01ebf69f1168550e48f (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use std::fmt::Debug;

use crate::computer::ComputerPhase;
use crate::message::Message;
use crate::{assets::VIDEO_ASSETS, random::RandomState};
use crate::{constants::*, game};
use teleia::context;
use web_sys::{Blob, BlobPropertyBag, Url};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Os {
    state: OsState,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OsState {
    Booting,
    On,
    ShutDown,
    Off,
}

// TODO ???
impl From<OsState> for ComputerPhase {
    fn from(value: OsState) -> Self {
        match value {
            OsState::Booting => ComputerPhase::Booting,
            OsState::On => ComputerPhase::On,
            OsState::ShutDown => ComputerPhase::ShutDown,
            OsState::Off => ComputerPhase::Off,
        }
    }
}

impl From<ComputerPhase> for OsState {
    fn from(value: ComputerPhase) -> Self {
        match value {
            ComputerPhase::Off => OsState::Off,
            ComputerPhase::Booting => OsState::Booting,
            ComputerPhase::On => OsState::On,
            ComputerPhase::ShutDown => OsState::ShutDown,
        }
    }
}

impl Os {
    pub fn new() -> Self {
        Self {
            state: OsState::Off,
        }
    }
    pub fn from(phase: ComputerPhase) -> Self {
        Self {
            state: OsState::from(phase),
        }
    }

    pub fn set(&mut self, phase: ComputerPhase) {
        self.state = OsState::from(phase);
    }
}

impl Default for Os {
    fn default() -> Self {
        Self {
            state: OsState::Off,
        }
    }
}

impl Os {
    pub fn boot(rng: &mut RandomState) {
        let idx = rng.next_range(0.0, VIDEO_ASSETS.len() as f32) as usize;
        let bytes = VIDEO_ASSETS[idx].1;

        // huh ? `of1` nice api name
        let parts = js_sys::Array::of1(&js_sys::Uint8Array::from(bytes));
        let blob_props = BlobPropertyBag::new();
        blob_props.set_type("video/mp4");

        let blob = match Blob::new_with_u8_array_sequence_and_options(&parts, &blob_props) {
            Ok(b) => b,
            Err(_) => {
                log::error!("eatyourgreens:: failed to create blob");
                return;
            }
        };

        let url = match Url::create_object_url_with_blob(&blob) {
            Ok(s) => s,
            Err(_) => {
                log::error!("eatyourgreens:: failed to create object URL");
                return;
            }
        };
        if let Some(window) = web_sys::window() {
            let msg = Message {
                op: "play_video",
                verb: None,
                win: None,
                score: None,
                video_url: Some(url),
            };
            Message::post(&window, &msg);
        } else {
            log::error!("eatyourgreens:: failed to get window and send os msg");
        }
    }

    pub fn os_play(&self, ctx: &context::Context) {
        log::debug!("eatyourgreens:: os play game:");
    }

    pub fn os_quit() {
        log::debug!("eatyourgreens:: os quit");
    }
}