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
use {
std::sync::Arc,
async_trait::async_trait,
futures::{
SinkExt as _,
stream::{
SplitSink,
SplitStream,
},
},
serde_json::{
Value as Json,
json,
},
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.
#[derive(Clone)]
pub struct RaceContext {
pub(crate) data: Arc<RwLock<RaceData>>,
pub sender: Arc<Mutex<WsSink>>,
}
impl RaceContext {
/// Returns the current state of the race.
pub async fn data(&self) -> RwLockReadGuard<'_, RaceData> {
self.data.read().await
}
/// Sends a raw JSON message to the server.
///
/// The methods [`set_raceinfo`](RaceContext::set_raceinfo) through [`remove_monitor`](RaceContext::remove_monitor) should be preferred.
pub async fn send_raw(&self, message: &Json) -> Result<(), Error> {
self.sender.lock().await.send(tungstenite::Message::Text(serde_json::to_string(&message)?)).await?;
Ok(())
}
/// Send a chat message to the race room.
///
/// `message` should be the message string you want to send.
pub async fn send_message(&self, message: &str) -> Result<(), Error> {
self.send_raw(&json!({
"action": "message",
"data": {
"message": message,
"guid": Uuid::new_v4(),
},
})).await?;
Ok(())
}
/// Set the `info_bot` field on the race room's data.
pub async fn set_bot_raceinfo(&self, info: &str) -> Result<(), Error> {
self.send_raw(&json!({
"action": "setinfo",
"data": {"info_bot": info},
})).await?;
Ok(())
}
/// 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> {
let info = match (&*self.data().await.info, pos) {
("", _) | (_, RaceInfoPos::Overwrite) => info.to_owned(),
(old_info, RaceInfoPos::Prefix) => format!("{info} | {old_info}"),
(old_info, RaceInfoPos::Suffix) => format!("{old_info} | {info}"),
};
self.send_raw(&json!({
"action": "setinfo",
"data": {"info_user": info},
})).await?;
Ok(())
}
/// Set the room in an open state.
pub async fn set_open(&self) -> Result<(), Error> {
self.send_raw(&json!({
"action": "make_open",
})).await?;
Ok(())
}
/// Set the room in an invite-only state.
pub async fn set_invitational(&self) -> Result<(), Error> {
self.send_raw(&json!({
"action": "make_invitational",
})).await?;
Ok(())
}
/// Forces a start of the race.
pub async fn force_start(&self) -> Result<(), Error> {
self.send_raw(&json!({
"action": "begin",
})).await?;
Ok(())
}
/// Forcibly cancels a race.
pub async fn cancel_race(&self) -> Result<(), Error> {
self.send_raw(&json!({
"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> {
self.send_raw(&json!({
"action": "invite",
"data": {
"user": user,
},
})).await?;
Ok(())
}
/// 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> {
self.send_raw(&json!({
"action": "accept_request",
"data": {
"user": user,
},
})).await?;
Ok(())
}
/// Forcibly unreadies an entrant.
///
/// `user` should be the hashid of the user.
pub async fn force_unready(&self, user: &str) -> Result<(), Error> {
self.send_raw(&json!({
"action": "force_unready",
"data": {
"user": user,
},
})).await?;
Ok(())
}
/// 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> {
self.send_raw(&json!({
"action": "remove_entrant",
"data": {
"user": user,
},
})).await?;
Ok(())
}
/// 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> {
self.send_raw(&json!({
"action": "add_monitor",
"data": {
"user": user,
},
})).await?;
Ok(())
}
/// 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> {
self.send_raw(&json!({
"action": "remove_monitor",
"data": {
"user": user,
},
})).await?;
Ok(())
}
}
/// This trait should be implemented using the [`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.
///
/// The default implementation returns [`true`] for any race whose status value is neither [`finished`](RaceStatusValue::Finished) nor [`cancelled`](RaceStatusValue::Cancelled).
fn should_handle(race_data: &RaceData) -> 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, state: Arc<T>) -> Result<Self, Error>;
/// ```
///
/// The `RaceHandler` this returns will receive events for that race.
async fn new(ctx: &RaceContext, state: Arc<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, _cmd_name: String, _args: Vec<String>, _is_moderator: bool, _is_monitor: bool, _msg: &ChatMessage) -> Result<(), Error>;
/// ```
async fn command(&mut self, _ctx: &RaceContext, _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) -> Result<bool, Error>;
/// ```
///
/// The default implementation checks [`should_handle`](RaceHandler::should_handle).
async fn should_stop(&mut self, ctx: &RaceContext) -> Result<bool, Error> {
Ok(!Self::should_handle(&*ctx.data().await)?)
}
/// Bot actions to perform just before disconnecting from a race room.
///
/// Equivalent to:
///
/// ```ignore
/// async fn end(self, _ctx: &RaceContext) -> Result<(), Error>;
/// ```
///
/// The default implementation does nothing.
async fn end(self, _ctx: &RaceContext) -> Result<(), Error> { Ok(()) }
/// Called when a `chat.history` message is received.
///
/// Equivalent to:
///
/// ```ignore
/// async fn chat_history(&mut self, _ctx: &RaceContext: _msgs: Vec<ChatMessage>) -> Result<(), Error>;
/// ```
///
/// The default implementation does nothing.
async fn chat_history(&mut self, _ctx: &RaceContext, _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, message: ChatMessage) -> Result<(), Error>;
/// ```
///
/// The default implementation calls [`command`](RaceHandler::command) if appropriate.
async fn chat_message(&mut self, ctx: &RaceContext, 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_monitor = 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,
message.user.as_ref().map_or(false, |user| user.can_moderate),
can_monitor,
&message,
).await?;
}
}
}
Ok(())
}
/// Called when a `chat.delete` message is received.
///
/// Equivalent to:
///
/// ```ignore
/// async fn chat_delete(&mut self, _ctx: &RaceContext, _event: ChatDelete) -> Result<(), Error>;
/// ```
///
/// The default implementation does nothing.
async fn chat_delete(&mut self, _ctx: &RaceContext, _event: ChatDelete) -> Result<(), Error> { Ok(()) }
/// Called when a `chat.purge` message is received.
///
/// Equivalent to:
///
/// ```ignore
/// async fn chat_purge(&mut self, _ctx: &RaceContext, _event: ChatPurge) -> Result<(), Error>;
/// ```
///
/// The default implementation does nothing.
async fn chat_purge(&mut self, _ctx: &RaceContext, _event: ChatPurge) -> Result<(), Error> { Ok(()) }
/// Called when an `error` message is received.
///
/// Equivalent to:
///
/// ```ignore
/// async fn error(&mut self, _ctx: &RaceContext, errors: Vec<String>) -> Result<(), Error>;
/// ```
///
/// The default implementation returns the errors as `Error::Server`.
async fn error(&mut self, _ctx: &RaceContext, 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) -> Result<(), Error>;
/// ```
///
/// The default implementation does nothing.
async fn pong(&mut self, _ctx: &RaceContext) -> Result<(), Error> { Ok(()) }
/// Called when a `race.data` message is received.
///
/// Equivalent to:
///
/// ```ignore
/// async fn race_data(&mut self, _ctx: &RaceContext, _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, _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) -> Result<(), Error>;
/// ```
///
/// The default implementation does nothing.
async fn race_renders(&mut self, _ctx: &RaceContext) -> Result<(), Error> { Ok(()) }
/// Called when a `race.split` message is received.
///
/// Equivalent to:
///
/// ```ignore
/// async fn race_split(&mut self, _ctx: &RaceContext) -> Result<(), Error>;
/// ```
///
/// The default implementation does nothing.
async fn race_split(&mut self, _ctx: &RaceContext) -> 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,
}