Crates.io | cg-game-server |
lib.rs | cg-game-server |
version | 1.0.2 |
source | src |
created_at | 2022-01-07 14:26:51.767028 |
updated_at | 2022-01-07 14:39:00.730889 |
description | A library to help build CodeGame game servers. |
homepage | https://github.com/code-game-project/cg-game-server |
repository | https://github.com/code-game-project/cg-game-server.git |
max_upload_size | |
id | 509432 |
size | 53,157 |
This is a library that consists mostly of traits that are meant to speed up and standardize the development of CodeGame game servers.
It's the server's job to keep the players connected to each other and prevent them from cheating using minimal server-side state. All decisions are made by the client but always verified by the server.
Add the following dependencies to your Cargo.toml:
# this lib
cg-game-server = "0.1.0"
# CodeGame uses actix as its web framework, the traits in this project are built for actix
actix = "0.10"
actix-web = {version = "3", features = ["openssl"]}
actix-web-actors = "3"
# the project uses serde and serde_json to communicate with the clients
serde = {version = "1.0.133", features = ["derive"]}
serde_json = "1.0.74"
TL;DR: For some examples, please see the official games.
Apart from the main.rs
module that starts the webserver, each CodeGame game server consists of three parts:
Here's a closer look at each:
Events are structs
that represent an action. Events flow between sockets and them and their clients, carrying data with them.
There are two types of events ReceiveableEvents
(R
in generics), which are sent to the server by a client (received by a socket), and SendableEvents
(S
in generics), which are sent between sockets.
When creating your own events it is recommended that you create two enums
. Be sure to include the standard events in your enums too. It should look something like this:
use cg_game_server::events::{Error, Join, Leave};
use actix::prelude::Message;
use serde::{Deserialize, Serialize};
use std::result::Result;
/// Deserialization `Result` for deserializing `ReceiveableEvents`
pub type ReceiveableEventSerde = Result<ReceiveableEvents, serde_json::Error>;
/// Events that the client may send to the server
#[derive(Serialize, Deserialize, Debug, Message, Clone)]
#[rtype(result = "()")]
#[serde(tag = "event_name")]
pub enum ReceiveableEvents {
#[serde(rename = "join")]
Join(Join),
#[serde(rename = "leave")]
Leave(Leave),
}
/// Events that the server may send to the client
#[derive(Serialize, Debug, Message, Clone)]
#[rtype(result = "()")]
#[serde(tag = "event_name")]
pub enum SendableEvents {
#[serde(rename = "error")]
Error(Error),
#[serde(rename = "join")]
Joined(Join),
#[serde(rename = "leave")]
Left(Leave),
}
The Socket
(C
in generics) is what client connects to. It contains the game logic and is responsible for interpreting events sent by the client. Furthermore it sends all events that it reiceives from other sockets to the client.
A socket is required to impl
ement the following:
actix::Actor
-> required by actix, can send and receive messagesactix::StreamHandler
-> required by actix, handles WebSocket communicationactix::Handler<EventWrapper<S>>
required by CodeGame, handles SendableEvents
sent to a socketactix::GameServerCommunications<G, R>
required by CodeGame, makes sure that the basic game server communication functions are available for every impl
ementation of Socket
The GameServer
(G
in generics) is the middle man. Its actix::Addr
is passed to each new socket. Sockets can send events to the game server which it either processes or relays to other sockets.
A server is required to impl
ement the following:
actix::Actor
-> required by actix, can send and receive messagesactix::Handler<EventWrapper<S>>
required by CodeGame, handles SendableEvents
sent to socketsactix::Handler<EventWrapper<Connect<C>>>
required by CodeGame, handles Connect
events sent to the serveractix::Handler<EventWrapper<Disconnect>>
required by CodeGame, handles Disconnect
events sent to the serveractix::Handler<EventWrapper<Join>>
required by CodeGame, handles Join
events sent to the serveractix::Handler<EventWrapper<Leave>>
required by CodeGame, handles Leave
events sent to the server