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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
use {
std::{
borrow::Cow,
sync::Arc,
},
async_trait::async_trait,
futures::{
SinkExt as _,
stream::{
SplitSink,
SplitStream,
},
},
serde::Serialize,
serde_with::{
Map,
serde_as,
},
tokio::{
net::TcpStream,
sync::{
Mutex,
RwLock,
RwLockReadGuard,
},
},
tokio_tungstenite::{
MaybeTlsStream,
WebSocketStream,
tungstenite,
},
uuid::Uuid,
crate::{
Error,
model::*,
},
};
pub type WsStream = SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>;
pub type WsSink = SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, tungstenite::Message>;
/// A type passed to [`RaceHandler`] callback methods which can be used to check the current status of the race or send messages.
///
/// While a future returned from a callback method is running, [`data`](Self::data) will not change and no other callbacks will be processed.
/// [`Clone`] can be used to keep a value of this type past the end of a handler callback, e.g. by passing it into [`tokio::spawn`].
/// If you do so, [`data`](Self::data) can be called to check the current state of the race.
pub struct RaceContext<S: Send + Sync + ?Sized + 'static> {
pub global_state: Arc<S>,
pub(crate) data: Arc<RwLock<RaceData>>,
pub(crate) sender: Arc<Mutex<WsSink>>,
}
impl<S: Send + Sync + ?Sized + 'static> RaceContext<S> {
/// Returns the current state of the race.
///
/// See the type-level documentation for the semantics of using a [`RaceContext`] for an extended period of time.
pub async fn data(&self) -> RwLockReadGuard<'_, RaceData> {
self.data.read().await
}
async fn send_raw(&self, action: &'static str, data: impl Serialize) -> Result<(), Error> {
#[derive(Serialize)]
struct RawMessage<T> {
action: &'static str,
data: T,
}
self.sender.lock().await.send(tungstenite::Message::Text(serde_json::to_string(&RawMessage { action, data })?)).await?;
Ok(())
}
/// Send a simple chat message to the race room.
///
/// See [`send_message`](Self::send_message) for more options.
pub async fn say(&self, message: &str) -> Result<(), Error> {
#[derive(Serialize)]
struct Data<'a> {
message: &'a str,
guid: Uuid,
}
self.send_raw("message", Data {
guid: Uuid::new_v4(),
message,
}).await
}
/// Send a chat message to the race room.
///
/// * `message` should be the message string you want to send.
/// * If `pinned` is set to true, the sent message will be automatically pinned.
/// * If `actions` is provided, action buttons will appear below your message for users to click on. See [action buttons](https://github.com/racetimeGG/racetime-app/wiki/Category-bots#action-buttons) for more details.
pub async fn send_message(&self, message: &str, pinned: bool, actions: Vec<(&str, ActionButton)>) -> Result<(), Error> {
#[serde_as]
#[derive(Serialize)]
struct Data<'a> {
message: &'a str,
pinned: bool,
#[serde_as(as = "Map<_, _>")]
actions: Vec<(&'a str, ActionButton)>,
guid: Uuid,
}
self.send_raw("message", Data {
guid: Uuid::new_v4(),
message, pinned, actions,
}).await
}
/// Send a direct message only visible to its recipient.
///
/// * `message` should be the message string you want to send.
/// * `direct_to` should be the hashid of the user.
pub async fn send_direct_message(&self, message: &str, direct_to: &str) -> Result<(), Error> {
#[serde_as]
#[derive(Serialize)]
struct Data<'a> {
message: &'a str,
direct_to: &'a str,
guid: Uuid,
}
self.send_raw("message", Data {
guid: Uuid::new_v4(),
message, direct_to,
}).await
}
/// Pin a chat message.
///
/// `message_id` should be the `id` field of a [`ChatMessage`].
pub async fn pin_message(&self, message_id: &str) -> Result<(), Error> {
#[derive(Serialize)]
struct Data<'a> {
message: &'a str,
}
self.send_raw("pin_message", Data {
message: message_id,
}).await
}
/// Unpin a chat message.
///
/// `message_id` should be the `id` field of a [`ChatMessage`].
pub async fn unpin_message(&self, message_id: &str) -> Result<(), Error> {
#[derive(Serialize)]
struct Data<'a> {
message: &'a str,
}
self.send_raw("unpin_message", Data {
message: message_id,
}).await
}
/// Set the `info_bot` field on the race room's data.
pub async fn set_bot_raceinfo(&self, info: &str) -> Result<(), Error> {
#[derive(Serialize)]
struct Data<'a> {
info_bot: &'a str,
}
self.send_raw("setinfo", Data {
info_bot: info,
}).await
}
/// Set the `info_user` field on the race room's data.
///
/// `info` should be the information you wish to set, and `pos` the behavior in case there is existing info.
pub async fn set_user_raceinfo(&self, info: &str, pos: RaceInfoPos) -> Result<(), Error> {
#[derive(Serialize)]
struct Data<'a> {
info_user: Cow<'a, str>,
}
let info_user = match (&*self.data().await.info, pos) {
("", _) | (_, RaceInfoPos::Overwrite) => Cow::Borrowed(info),
(old_info, RaceInfoPos::Prefix) => Cow::Owned(format!("{info} | {old_info}")),
(old_info, RaceInfoPos::Suffix) => Cow::Owned(format!("{old_info} | {info}")),
};
self.send_raw("setinfo", Data {
info_user,
}).await
}
/// Set the room in an open state.
pub async fn set_open(&self) -> Result<(), Error> {
self.sender.lock().await.send(tungstenite::Message::Text(format!("{{\"action\": \"make_open\"}}"))).await?;
Ok(())
}
/// Set the room in an invite-only state.
pub async fn set_invitational(&self) -> Result<(), Error> {
self.sender.lock().await.send(tungstenite::Message::Text(format!("{{\"action\": \"make_invitational\"}}"))).await?;
Ok(())
}
/// Forces a start of the race.
pub async fn force_start(&self) -> Result<(), Error> {
self.sender.lock().await.send(tungstenite::Message::Text(format!("{{\"action\": \"begin\"}}"))).await?;
Ok(())
}
/// Forcibly cancels a race.
pub async fn cancel_race(&self) -> Result<(), Error> {
self.sender.lock().await.send(tungstenite::Message::Text(format!("{{\"action\": \"cancel\"}}"))).await?;
Ok(())
}
/// Invites a user to the race.
///
/// `user` should be the hashid of the user.
pub async fn invite_user(&self, user: &str) -> Result<(), Error> {
#[derive(Serialize)]
struct Data<'a> {
user: &'a str,
}
self.send_raw("invite", Data {
user,
}).await
}
/// Accepts a request to join the race room.
///
/// `user` should be the hashid of the user.
pub async fn accept_request(&self, user: &str) -> Result<(), Error> {
#[derive(Serialize)]
struct Data<'a> {
user: &'a str,
}
self.send_raw("accept_request", Data {
user,
}).await
}
/// Forcibly unreadies an entrant.
///
/// `user` should be the hashid of the user.
pub async fn force_unready(&self, user: &str) -> Result<(), Error> {
#[derive(Serialize)]
struct Data<'a> {
user: &'a str,
}
self.send_raw("force_unready", Data {
user,
}).await
}
/// Forcibly removes an entrant from the race.
///
/// `user` should be the hashid of the user.
pub async fn remove_entrant(&self, user: &str) -> Result<(), Error> {
#[derive(Serialize)]
struct Data<'a> {
user: &'a str,
}
self.send_raw("remove_entrant", Data {
user,
}).await
}
/// Adds a user as a race monitor.
///
/// `user` should be the hashid of the user.
pub async fn add_monitor(&self, user: &str) -> Result<(), Error> {
#[derive(Serialize)]
struct Data<'a> {
user: &'a str,
}
self.send_raw("add_monitor", Data {
user,
}).await
}
/// Removes a user as a race monitor.
///
/// `user` should be the hashid of the user.
pub async fn remove_monitor(&self, user: &str) -> Result<(), Error> {
#[derive(Serialize)]
struct Data<'a> {
user: &'a str,
}
self.send_raw("remove_monitor", Data {
user,
}).await
}
}
impl<S: Send + Sync + ?Sized + 'static> Clone for RaceContext<S> {
/// This is a cheap operation since this type is a wrapper around some [`Arc`]s. See the type-level documentation for the semantics of using a [`RaceContext`] for an extended period of time.
fn clone(&self) -> Self {
Self {
global_state: Arc::clone(&self.global_state),
data: Arc::clone(&self.data),
sender: Arc::clone(&self.sender),
}
}
}
/// This trait should be implemented using the [`macro@async_trait`] attribute.
#[async_trait]
pub trait RaceHandler<S: Send + Sync + ?Sized + 'static>: Send + Sized + 'static {
/// Called when a new race room is found. If this returns [`false`], that race is ignored entirely.
///
/// Equivalent to:
///
/// ```ignore
/// async fn should_handle(race_data: &RaceData, _state: Arc<S>) -> Result<bool, Error>;
/// ```
///
/// The default implementation returns [`true`] for any race whose status value is neither [`finished`](RaceStatusValue::Finished) nor [`cancelled`](RaceStatusValue::Cancelled).
async fn should_handle(race_data: &RaceData, _state: Arc<S>) -> Result<bool, Error> {
Ok(!matches!(race_data.status.value, RaceStatusValue::Finished | RaceStatusValue::Cancelled))
}
/// Called when a new race room is found and [`should_handle`](RaceHandler::should_handle) has returned [`true`].
///
/// Equivalent to:
///
/// ```ignore
/// async fn new(ctx: &RaceContext<S>) -> Result<Self, Error>;
/// ```
///
/// The `RaceHandler` this returns will receive events for that race.
async fn new(ctx: &RaceContext<S>) -> Result<Self, Error>;
/// Called for each chat message that starts with `!` and was not sent by the system or a bot.
///
/// Equivalent to:
///
/// ```ignore
/// async fn command(&mut self: _ctx: &RaceContext<S>, _cmd_name: String, _args: Vec<String>, _is_moderator: bool, _is_monitor: bool, _msg: &ChatMessage) -> Result<(), Error>;
/// ```
async fn command(&mut self, _ctx: &RaceContext<S>, _cmd_name: String, _args: Vec<String>, _is_moderator: bool, _is_monitor: bool, _msg: &ChatMessage) -> Result<(), Error> {
Ok(())
}
/// Determine if the handler should be terminated. This is checked after every receieved message.
///
/// Equivalent to:
///
/// ```ignore
/// async fn should_stop(&mut self, ctx: &RaceContext<S>) -> Result<bool, Error>;
/// ```
///
/// The default implementation checks [`should_handle`](RaceHandler::should_handle).
async fn should_stop(&mut self, ctx: &RaceContext<S>) -> Result<bool, Error> {
Ok(!Self::should_handle(&*ctx.data().await, ctx.global_state.clone()).await?)
}
/// Bot actions to perform just before disconnecting from a race room.
///
/// Equivalent to:
///
/// ```ignore
/// async fn end(self, _ctx: &RaceContext<S>) -> Result<(), Error>;
/// ```
///
/// The default implementation does nothing.
async fn end(self, _ctx: &RaceContext<S>) -> Result<(), Error> { Ok(()) }
/// Called when a `chat.history` message is received.
///
/// Equivalent to:
///
/// ```ignore
/// async fn chat_history(&mut self, _ctx: &RaceContext<S>: _msgs: Vec<ChatMessage>) -> Result<(), Error>;
/// ```
///
/// The default implementation does nothing.
async fn chat_history(&mut self, _ctx: &RaceContext<S>, _msgs: Vec<ChatMessage>) -> Result<(), Error> { Ok(()) }
/// Called when a `chat.message` message is received.
///
/// Equivalent to:
///
/// ```ignore
/// async fn chat_message(&mut self, ctx: &RaceContext<S>, message: ChatMessage) -> Result<(), Error>;
/// ```
///
/// The default implementation calls [`command`](RaceHandler::command) if appropriate.
async fn chat_message(&mut self, ctx: &RaceContext<S>, message: ChatMessage) -> Result<(), Error> {
if !message.is_bot && !message.is_system.unwrap_or(false /* Python duck typing strikes again */) && message.message.starts_with('!') {
let data = ctx.data().await;
let can_moderate = message.user.as_ref().map_or(false, |user| user.can_moderate);
let can_monitor = can_moderate || message.user.as_ref().map_or(false, |sender|
data.opened_by.as_ref().map_or(false, |creator| creator.id == sender.id) || data.monitors.iter().any(|monitor| monitor.id == sender.id)
);
if let Some(mut split) = shlex::split(&message.message[1..]) {
if !split.is_empty() {
self.command(ctx, split.remove(0), split, can_moderate, can_monitor, &message).await?;
}
}
}
Ok(())
}
/// Called when a `chat.pin` message is received.
///
/// Equivalent to:
///
/// ```ignore
/// async fn chat_pin(&mut self, _ctx: &RaceContext<S>, _message: ChatMessage) -> Result<(), Error>;
/// ```
///
/// The default implementation does nothing.
async fn chat_pin(&mut self, _ctx: &RaceContext<S>, _message: ChatMessage) -> Result<(), Error> { Ok(()) }
/// Called when a `chat.unpin` message is received.
///
/// Equivalent to:
///
/// ```ignore
/// async fn chat_unpin(&mut self, _ctx: &RaceContext<S>, _message: ChatMessage) -> Result<(), Error>;
/// ```
///
/// The default implementation does nothing.
async fn chat_unpin(&mut self, _ctx: &RaceContext<S>, _message: ChatMessage) -> Result<(), Error> { Ok(()) }
/// Called when a `chat.delete` message is received.
///
/// Equivalent to:
///
/// ```ignore
/// async fn chat_delete(&mut self, _ctx: &RaceContext<S>, _event: ChatDelete) -> Result<(), Error>;
/// ```
///
/// The default implementation does nothing.
async fn chat_delete(&mut self, _ctx: &RaceContext<S>, _event: ChatDelete) -> Result<(), Error> { Ok(()) }
/// Called when a `chat.purge` message is received.
///
/// Equivalent to:
///
/// ```ignore
/// async fn chat_purge(&mut self, _ctx: &RaceContext<S>, _event: ChatPurge) -> Result<(), Error>;
/// ```
///
/// The default implementation does nothing.
async fn chat_purge(&mut self, _ctx: &RaceContext<S>, _event: ChatPurge) -> Result<(), Error> { Ok(()) }
/// Called when an `error` message is received.
///
/// Equivalent to:
///
/// ```ignore
/// async fn error(&mut self, _ctx: &RaceContext<S>, errors: Vec<String>) -> Result<(), Error>;
/// ```
///
/// The default implementation returns the errors as `Error::Server`.
async fn error(&mut self, _ctx: &RaceContext<S>, errors: Vec<String>) -> Result<(), Error> {
Err(Error::Server(errors))
}
/// Called when a `pong` message is received.
///
/// Equivalent to:
///
/// ```ignore
/// async fn pong(&mut self, _ctx: &RaceContext<S>) -> Result<(), Error>;
/// ```
///
/// The default implementation does nothing.
async fn pong(&mut self, _ctx: &RaceContext<S>) -> Result<(), Error> { Ok(()) }
/// Called when a `race.data` message is received.
///
/// Equivalent to:
///
/// ```ignore
/// async fn race_data(&mut self, _ctx: &RaceContext<S>, _old_race_data: RaceData) -> Result<(), Error>;
/// ```
///
/// The new race data can be found in the [`RaceContext`] parameter. The [`RaceData`] parameter contains the previous data.
///
/// The default implementation does nothing.
async fn race_data(&mut self, _ctx: &RaceContext<S>, _old_race_data: RaceData) -> Result<(), Error> { Ok(()) }
/// Called when a `race.renders` message is received.
///
/// Equivalent to:
///
/// ```ignore
/// async fn race_renders(&mut self, _ctx: &RaceContext<S>) -> Result<(), Error>;
/// ```
///
/// The default implementation does nothing.
async fn race_renders(&mut self, _ctx: &RaceContext<S>) -> Result<(), Error> { Ok(()) }
/// Called when a `race.split` message is received.
///
/// Equivalent to:
///
/// ```ignore
/// async fn race_split(&mut self, _ctx: &RaceContext<S>) -> Result<(), Error>;
/// ```
///
/// The default implementation does nothing.
async fn race_split(&mut self, _ctx: &RaceContext<S>) -> Result<(), Error> { Ok(()) }
/// Called when a room handler task is created.
///
/// Equivalent to:
///
/// ```ignore
/// async fn task(_state: Arc<S>, _race_data: Arc<RwLock<RaceData>>, _join_handle: tokio::task::JoinHandle<()>) -> Result<(), Error>;
/// ```
///
/// The default implementation does nothing.
async fn task(_state: Arc<S>, _race_data: Arc<RwLock<RaceData>>, _join_handle: tokio::task::JoinHandle<()>) -> Result<(), Error> { Ok(()) }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RaceInfoPos {
/// Replace the existing race info with the new one.
Overwrite,
/// Add the new race info before the existing one, if any, like so: `new | old`
Prefix,
/// Add the new race info after the existing one, if any, like so: `old | new`
Suffix,
}