cg_game_server/
events.rs

1//! This module contains standard event structs, the `EventWrapper` struct and
2//! the `EventTarget` enum used to communicate between the different `Actor`s.
3
4use crate::server::{GameID, SocketID};
5use actix::{Message, Addr};
6use serde::{Deserialize, Serialize};
7
8/// A wrapper for an event `E` so that `to` and `from` properties can be added
9#[derive(Message, Debug, Clone, Serialize, Deserialize)]
10#[rtype(result = "()")]
11pub struct EventWrapper<E> {
12  /// The id of socket that sent/triggered the event
13  pub from: SocketID,
14  /// The target of the event / which sockets receive the event
15  pub to: EventTarget,
16  /// The actual event
17  pub event: E,
18}
19
20/// Target of an event, meant to be used as `to` property of `EventWrapper`
21#[derive(Debug, Clone, Serialize, Deserialize)]
22pub enum EventTarget {
23  /// Specifies the target of the event as the socket that it comes from
24  /// 
25  /// This can be used when emitting errors or other private events that the other players should __not__ know about
26  SocketSelf,
27  /// Specifies the target of the event as the `server::GameServer`
28  /// 
29  /// This can be used when dealing with events that make the `server::GameServer` do something more than just relay the event
30  Server,
31  /// Specifies the target of the event as another `socket::Socket` connected to the `server::GameServer`
32  /// 
33  /// This can be used in games where there is a need to privately coordinate
34  Socket(SocketID),
35  /// Specifies the target of the event as all the `socket::Socket`s in associated with a `GameID`
36  /// 
37  /// This is the type that will be used most in most games
38  Game(GameID),
39}
40
41// A few events that should be part of every `Socket`'s events
42/// A struct that contains a `reason` field and can be serialized and sent to a client
43#[derive(Serialize, Deserialize, Debug, Clone)]
44pub struct Error {
45  /// The reason the error occurred
46  /// 
47  /// Reasons are allowed to be technical, since the users are mostly (if not all) developers
48  /// and will benefit from error messages that actually say what is wrong, but should also
49  /// not be too technical, because many users are not experts at rust or programming in general.
50  pub reason: String,
51}
52/// A struct that contains the address of a `socket::Socket`
53/// 
54/// This event is used to transmit a `socket::Socket`s `Addr` to the `server::GameServer`
55#[derive(Message, Debug, Clone)]
56#[rtype(result = "()")]
57pub struct Connect<C> where C: actix::Actor {
58  /// The `Addr` of an `Actor`
59  pub addr: Addr<C>
60}
61/// A struct with no data
62/// 
63/// This event is used to tell the `server::GameServer` that the client is disconnecting from the `server::GameServer`
64#[derive(Message, Serialize, Deserialize, Debug, Clone)]
65#[rtype(result = "()")]
66pub struct Disconnect;
67/// A struct that contains the `username` of the sender and the `GameID` of the game that they want to join
68/// 
69/// This event is used by a `socket::Socket` to join a game
70#[derive(Message, Serialize, Deserialize, Debug, Clone)]
71#[rtype(result = "()")]
72pub struct Join {
73  /// `GameID` of the game the game that is to be joined
74  pub game_id: GameID,
75  /// `username` of the `socket::Socket` joining
76  pub username: String,
77}
78/// A struct that contains the `username` of the sender and the `GameID` of the game that they want to leave
79/// 
80/// This event is used by a `socket::Socket` to leave the current game
81#[derive(Message, Serialize, Deserialize, Debug, Clone)]
82#[rtype(result = "()")]
83pub struct Leave {
84  /// `GameID` of the game the `socket::Socket` is part of
85  pub game_id: GameID,
86  /// `username` of the `socket::Socket` leaving
87  pub username: String,
88}
89/// A struct that contains a `reason`
90/// 
91/// This event can be sent to any `socket::Socket` to terminate its connection to the entire server.
92/// The event along with the `reason` will be sent to the client just before termination so that they
93/// know why they were kicked.
94#[derive(Message, Serialize, Deserialize, Debug, Clone)]
95#[rtype(result = "()")]
96pub struct Kick {
97  /// The reason for kicking this player
98  pub reason: String,
99}