ezbrowsergameserver
Ever wanted to make and self-host one of those simple, usually text-based, multiplayer browser games?
Me too! And this is a small library to hopefully make that as ez as possible :)
Getting started
Before you do anything, know that ezbrowsergameserver isn't
a server for your website - it only provides the WebSocket
which will be used to keep all your players updated and in sync.
If you have python3 installed, you can use the script examples/server.sh as a quick
no-setup web server running on 0.0.0.0:8080.
For this example, you will use 0.0.0.0:8080/00_min.html:
Min - ezbrowsergameserver
Lobby ID:
Create new lobby
Join lobby by ID
This will let you create a new lobby or join an existing one by entering its ID in the text input.
The <script> sections use a WebSocket to connect to your game server - the server
you are about to create using this library.
But before we get to your very first ezbrowsergame, you need a lobby.
The lobby is the place where people gather before a game starts.
While in the lobby, people can change the gamemode, the game's settings,
or anything else you implement for your lobby.
The lobby is also the place for your global state, since it still exists when a game ends.
Lobbies can be accessed via their ID, formatted as a hex string ({id:X}).
This example will simply start the "game" every time a player joins the lobby. The "game" will last one second and show the new number of players in the lobby.
These are the imports you will need for the example:
use Instant;
use *;
First, create a struct for your global state:
Then, one for your game:
;
Now, implement LobbyState for your GlobalState struct:
Then, implement GameState for your PlayerCountGame:
And finally, add your main function:
async
That's it - we've created a "game".
If you want to try it out, go to the examples/ directory,
run ./server.sh, run cargo run --example 00_min,
then open 0.0.0.0:8080/00_min.html in a browser and create a new lobby.