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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
use std::{
    future::Future,
    pin::Pin,
    sync::{
        atomic::{AtomicI32, Ordering},
        Arc,
    },
    task::{Context, Poll},
};

#[cfg(feature = "brs")]
use brickadia::save;

use dashmap::{mapref::entry::Entry, DashMap};
use resources::Player;
use serde_json::{Value, json};
use tokio::{
    io::{stdin, AsyncBufReadExt, BufReader},
    sync::{
        mpsc::{self, UnboundedReceiver},
        oneshot,
    },
};

use crate::resources::PlayerPosition;

pub mod resources;
pub mod rpc;

pub type RpcEventReceiver = UnboundedReceiver<rpc::Message>;

/// A future that waits for the server to respond, returning a [`Response`](crate::Response).
/// This will await indefinitely, so use with Tokio's `select!` macro to impose a timeout.
pub struct ResponseAwaiter(oneshot::Receiver<rpc::Response>);

impl Future for ResponseAwaiter {
    type Output = Result<Option<Value>, ResponseError>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        match Pin::new(&mut self.0).poll(cx) {
            // we received a response, filter between a real result or an RPC error
            Poll::Ready(Ok(response)) => Poll::Ready(match response.error {
                Some(e) => Err(ResponseError::Rpc(e)),
                None => Ok(response.result),
            }),

            // no response received, the channel errored
            Poll::Ready(Err(error)) => Poll::Ready(Err(ResponseError::Recv(error))),

            // we are still waiting
            Poll::Pending => Poll::Pending,
        }
    }
}

/// A response error. Either an RPC error (`rpc::Error`), or a receive error (`oneshot::error::RecvError`).
#[derive(Debug)]
pub enum ResponseError {
    Rpc(rpc::Error),
    Recv(oneshot::error::RecvError),
}

pub struct Omegga {
    pub awaiter_txs: Arc<DashMap<rpc::RequestId, oneshot::Sender<rpc::Response>>>,
    request_id: Arc<AtomicI32>,
}

impl Omegga {
    /// Create a new Omegga instance.
    pub fn new() -> Self {
        Self {
            awaiter_txs: Arc::new(DashMap::new()),
            request_id: Arc::new(AtomicI32::new(-1)),
        }
    }

    /// Spawn the listener.
    pub fn spawn(&self) -> RpcEventReceiver {
        let (tx, rx) = mpsc::unbounded_channel::<rpc::Message>();
        let awaiter_txs = Arc::clone(&self.awaiter_txs);
        tokio::spawn(async move {
            let reader = BufReader::new(stdin());
            let mut lines = reader.lines();
            while let Some(line) = lines.next_line().await.unwrap() {
                let message: rpc::Message = match serde_json::from_str(&line) {
                    Ok(v) => v,
                    Err(_) => continue,
                };

                match message {
                    // Handle responses
                    rpc::Message::Response {
                        id, result, error, ..
                    } => {
                        if let Entry::Occupied(entry) = awaiter_txs.entry(id) {
                            let (id, sender) = entry.remove_entry();
                            let _ = sender.send(rpc::Response { id, result, error });
                        }
                    }
                    // Otherwise, send everything else
                    _ => {
                        let _ = tx.send(message);
                    }
                };
            }
        });
        rx
    }

    /// Write out an RPC message.
    pub fn write(&self, message: rpc::Message) {
        println!("{}", serde_json::to_string(&message).unwrap());
    }

    /// Write out an RPC notification.
    pub fn write_notification(&self, method: impl Into<String>, params: Option<Value>) {
        self.write(rpc::Message::notification(method.into(), params));
    }

    /// Write out an RPC response.
    pub fn write_response(
        &self,
        id: rpc::RequestId,
        params: Option<Value>,
        error: Option<rpc::Error>,
    ) {
        self.write(rpc::Message::response(id, params, error));
    }

    /// Write out an RPC request.
    ///
    /// **Note:** This does not internally expect a response from the server.
    /// Prefer using [`request`](Omegga::request) over this for the ability to
    /// await a response from the RPC server.
    pub fn write_request(
        &self,
        id: rpc::RequestId,
        method: impl Into<String>,
        params: Option<Value>,
    ) {
        self.write(rpc::Message::request(id, method.into(), params));
    }

    /// Request a response from the RPC server.
    /// This returns a `ResponseAwaiter`, a `Future` that awaits a response.
    pub fn request(&self, method: impl Into<String>, params: Option<Value>) -> ResponseAwaiter {
        // fetch the next ID
        let id = self.request_id.fetch_sub(-1, Ordering::SeqCst);

        // write out the request
        self.write_request(rpc::RequestId::Int(id), method, params);

        // create a channel to send the response over
        let (tx, rx) = oneshot::channel::<rpc::Response>();

        // insert the transmitter into the dashmap
        self.awaiter_txs.insert(rpc::RequestId::Int(id), tx);

        // return back with an awaiter to await the receiver
        ResponseAwaiter(rx)
    }
    
    /// Prints a message to the Omegga console.
    pub fn log(&self, line: impl Into<String>) {
        self.write_notification("log", Some(Value::String(line.into())));
    }

    /// Prints a message to the Omegga console in error color.
    pub fn error(&self, line: impl Into<String>) {
        self.write_notification("error", Some(Value::String(line.into())));
    }

    /// Prints a message to the Omegga console in info color.
    pub fn info(&self, line: impl Into<String>) {
        self.write_notification("info", Some(Value::String(line.into())));
    }

    /// Prints a message to the Omegga console in warn color.
    pub fn warn(&self, line: impl Into<String>) {
        self.write_notification("warn", Some(Value::String(line.into())));
    }

    /// Prints a message to the Omegga console in trace color.
    pub fn trace(&self, line: impl Into<String>) {
        self.write_notification("trace", Some(Value::String(line.into())));
    }

    /// Gets an object from the store.
    pub async fn store_get(&self, key: impl Into<String>) -> Result<Option<Value>, ResponseError> {
        self.request("store.get", Some(Value::String(key.into()))).await
    }

    /// Sets an object in the store.
    pub async fn store_set(&self, key: impl Into<String>, value: Value) -> Result<(), ResponseError> {
        self.request("store.set", Some(json!([key.into(), value]))).await.map(|_| ())
    }

    /// Deletes an object from the store.
    pub async fn store_delete(&self, key: impl Into<String>) -> Result<(), ResponseError> {
        self.request("store.delete", Some(Value::String(key.into()))).await.map(|_| ())
    }

    /// Wipes the store.
    pub async fn store_wipe(&self) -> Result<(), ResponseError> {
        self.request("store.wipe", None).await.map(|_| ())
    }

    /// Gets a list of keys in the store.
    pub async fn store_keys(&self) -> Result<Vec<String>, ResponseError> {
        self.request("store.keys", None).await.map(|r| match r {
            Some(r) => serde_json::from_value::<Vec<String>>(r).unwrap_or_else(|_| vec![]),
            None => vec![],
        })
    }

    /// Writes a line out to the Brickadia server.
    pub fn writeln(&self, line: impl Into<String>) {
        self.write_notification("exec", Some(Value::String(line.into())));
    }

    /// Broadcasts a line.
    pub fn broadcast(&self, line: impl Into<String>) {
        self.write_notification("broadcast", Some(Value::String(line.into())));
    }

    /// Whispers a line to a user by their name.
    pub fn whisper(&self, username: impl Into<String>, line: impl Into<String>) {
        self.write_notification("whisper", Some(json!({"target": username.into(), "line": line.into()})));
    }

    /// Gets a list of all players.
    pub async fn get_players(&self) -> Result<Vec<Player>, ResponseError> {
        self.request("getPlayers", None).await.map(|r| match r {
            Some(r) => serde_json::from_value::<Vec<Player>>(r).unwrap_or_else(|_| vec![]),
            None => vec![],
        })
    }

    /// Get a player's position.
    pub async fn get_player_position(&self, target: impl Into<String>) -> Result<Option<(f64, f64, f64)>, ResponseError> {
        self.request("getPlayerPosition", Some(Value::String(target.into()))).await.map(|r| match r {
            Some(r) => serde_json::from_value::<(f64, f64, f64)>(r).ok(),
            None => None
        })
    }

    /// Get all player positions.
    pub async fn get_all_player_positions(&self) -> Result<Vec<PlayerPosition>, ResponseError> {
        self.request("getAllPlayerPositions", None).await.map(|r| match r {
            Some(r) => serde_json::from_value::<Vec<PlayerPosition>>(r).unwrap_or_else(|_| vec![]),
            None => vec![],
        })
    }

    /// Get the role setup.
    pub async fn get_role_setup(&self) -> Result<Value, ResponseError> {
        // TODO: write a type for this instead of using a serde_json::Value
        self.request("getRoleSetup", None).await.map(Option::unwrap)
    }

    /// Get the ban list.
    pub async fn get_ban_list(&self) -> Result<Value, ResponseError> {
        // TODO: write a type for this instead of using a serde_json::Value
        self.request("getBanList", None).await.map(Option::unwrap)
    }

    /// Get a list of the server's saves.
    pub async fn get_saves(&self) -> Result<Vec<String>, ResponseError> {
        self.request("getSaves", None).await.map(|r| match r {
            Some(r) => serde_json::from_value::<Vec<String>>(r).unwrap_or_else(|_| vec![]),
            None => vec![],
        })
    }

    /// Get the path to a specific save.
    pub async fn get_save_path(&self, save: impl Into<String>) -> Result<Option<String>, ResponseError> {
        self.request("getSavePath", Some(Value::String(save.into()))).await.map(|r| match r {
            Some(r) => serde_json::from_value::<String>(r).ok(),
            None => None,
        })
    }

    /// Gets the server's current save data.
    #[cfg(not(feature = "brs"))]
    pub async fn get_save_data(&self) -> Result<Value, ResponseError> {
        self.request("getSaveData", None).await.map(Option::unwrap)
    }

    /// Gets the server's current save data as a brickadia-rs save object.
    #[cfg(feature = "brs")]
    pub async fn get_save_data(&self) -> Result<save::SaveData, ResponseError> {
        self.request("getSaveData", None).await.map(|r| serde_json::from_value::<save::SaveData>(r.unwrap()).unwrap())
    }

    /// Clears a player's bricks by their name.
    pub fn clear_bricks(&self, target: impl Into<String>, quiet: bool) {
        self.write_notification("clearBricks", Some(json!({"target": target.into(), "quiet": quiet})));
    }

    /// Clear all bricks.
    pub fn clear_all_bricks(&self, quiet: bool) {
        self.write_notification("clearAllBricks", Some(json!({"quiet": quiet})));
    }

    /// Save bricks to a named save.
    pub async fn save_bricks(&self, name: impl Into<String>) -> Result<(), ResponseError> {
        self.request("saveBricks", Some(Value::String(name.into()))).await.map(|_| ())
    }

    /// Load a save, provided an offset in the world.
    pub async fn load_bricks(&self, name: impl Into<String>, quiet: bool, offset: (i32, i32, i32)) -> Result<(), ResponseError> {
        self.request("loadBricks", Some(json!({"name": name.into(), "quiet": quiet, "offX": offset.0, "offY": offset.1, "offZ": offset.2}))).await.map(|_| ())
    }

    /// Reads a save (from a save file), and returns its data.
    #[cfg(not(feature = "brs"))]
    pub async fn read_save_data(&self, name: impl Into<String>) -> Result<Option<Value>, ResponseError> {
        self.request("readSaveData", Some(Value::String(name.into()))).await
    }

    /// Reads a save (from a save file), and returns its data as a brickadia-rs save object.
    #[cfg(feature = "brs")]
    pub async fn read_save_data(&self, name: impl Into<String>) -> Result<Option<save::SaveData>, ResponseError> {
        self.request("readSaveData", Some(Value::String(name.into()))).await.map(|r| match r {
            Some(r) => serde_json::from_value::<save::SaveData>(r).ok(),
            None => None,
        })
    }

    /// Loads a save (from a JSON value) into the world, provided an offset.
    #[cfg(not(feature = "brs"))]
    pub async fn load_save_data(&self, data: Value, quiet: bool, offset: (i32, i32, i32)) -> Result<(), ResponseError> {
        self.request("loadSaveData", Some(json!({"data": data, "quiet": quiet, "offX": offset.0, "offY": offset.1, "offZ": offset.2}))).await.map(|_| ())
    }

    /// Loads a save (from brickadia-rs save data) into the world, provided an offset.
    #[cfg(feature = "brs")]
    pub async fn load_save_data(&self, data: save::SaveData, quiet: bool, offset: (i32, i32, i32)) -> Result<(), ResponseError> {
        self.request("loadSaveData", Some(json!({"data": data, "quiet": quiet, "offX": offset.0, "offY": offset.1, "offZ": offset.2}))).await.map(|_| ())
    }

    /// Changes the map.
    pub async fn change_map(&self, map: impl Into<String>) -> Result<(), ResponseError> {
        self.request("changeMap", Some(Value::String(map.into()))).await.map(|_| ())
    }
}

impl Default for Omegga {
    fn default() -> Self {
        Self::new()
    }
}