A multiplayer board game in Rust and WebAssembly
mattkeeter.com
mattkeeter.com
[0] Of course, for smoothness/convenience one could do some basic checks on the client.
For example, in the online multiplayer version of Ludo I'm currently building, I trigger the die roll animation on the client but the actual dice are rolled on the server, which then calculates what moves are possible based on the game state and sends that + the value of the roll to the client. The client doesn't know anything about the rules of Ludo, it simply knows how to parse and render the move format as well as send the ID of the picked move to its source of truth (in this case, the server).
I made a System inspired by boardgame.io (but it was too integrated with react for me), which means i can write the game on the server without writing any client code at all. the client gets a list of valid moves, and my debug view gets error messages and a bunch of buttons & drop downs for parameters.
https://github.com/NewChromantics/PopNotPoker/blob/master/Se... here's multiplayer minesweeper:) Http://not.poker
All the real work is in the lobby code anyway.
Maybe I can go back, although I'm writing game logic much faster now I'm in NIH territory, it would be nice to contribute to someone else's project
Having to wait for the server for the set of legal moves feels like a hack. It's way simpler to have the client have its own copy of the game state, and allow "query" methods (methods that fetches data about the game state but don't modify it). This allows you to make _powerful_ UI, which can show any kind of information the user needs about the game like predictions about what would happen if a move is done, computing things so the user doesn't need to.
If I had to redo it, I would use a paradigm similar to how boardgame.io[2] works, which is similar to how fixed lockstep works. The server and the clients all keep a full state of the game and when a player makes an action, it is transmitted to all the actors, who apply this action to their own state of the game. Since the game rules are deterministic, the final state of the game will be the same for each actor. The only 2 complex to handle are randomness and secrets (the former can actually be solved by the later), but overally, the complexity is managed elegantly and you can separate networking code from gameplay code easily.
Actually, one of my future possible project would be to make a library similar to boardgame.io, but less opiniated, and in the future, offer a platform to easily host and launch a game coded with it.
But you are waiting for the server anyway - that's the "turn" in "turn-based". The way I build, the notification about your turn simply comes with the moves you can make as well - and the moves you can make don't change, because _it's your turn_, so there's no limit to how powerful a UI you can make with it.
Take chess for example (it was while implementing chess that I worked out this paradigm, actually) - a move from the server (sent in a packed binary format) was a mapping of start and end positions and a metadata byte that linked it to a kill, a promotion, or castling. and it was rare indeed that a complete payload (with a full set of currently legal moves + effects) would be up to two kilobytes. The client would deserialise that and of course could show you possible moves when you selected a piece and so on.
> The server and the clients all keep a full state of the game and when a player makes an action, it is transmitted to all the actors, who apply this action to their own state of the game.
This is actually what my current approach was born out of! But then I wanted to build a second client app, and I felt it was a pain to rewrite the game logic in another language - one solution to that is to have an isomorphic application, but in my case I decided to try out moving as much as possible to the server and just focusing on data parsing and rendering on the client and it worked out surprisingly well. It also let me do things like implement custom rulesets with much greater ease, because the rule logic exists solely on the server and you can deploy/modify that without requiring the clients to update.
I can understand the reasoning if you need to code a client in a different language, then you would effectively need to code the whole game once again. I feel like it would quickly bore me to code the rendering of each set of moves for each possible action. I feel like it's easier to have access to the complete state of the game client-side when you need to code a UI. There might be a solution if the server broadcasts the diff of the game state to the clients after each action has been performed (instead of letting the clients re-apply the action on their game state), but I'm not sure it would be nough.
How do you handle state synchronization, though? When a player makes an action, how do you propagate the changes to the game state?
For example: Playing backgammon, you choose a piece, you get highlights on where it can go. That requires the full working ruleset on the client side. There is no case to be made for requiring this feedback from the server no matter how confident you are that your server can handle it or how good client's internet connection is.
Like I mentioned, these are _turn-based games_. To an extent, you're thinking about them like they're real time, and that's where the problem is. With backgammon (as with Ludo, which I'm building at the moment, so I have direct experience with this) you _have_ to roll the dice on the server or else the enterprising player is just going to game them. And if you're returning the value of the rolled die from the server, you can simply return the set of legal moves as well.
That way, you don't have to complicate the server by making it calculate future moves and write yet another client which is purposefully less featured, but still intelligent enough to parse the returned moves and map them to whichever piece is selected and you have less code to maintain.
You can keep on gambiting around the problem with "concessions" which is easily solved by activaing code you've already written on the client side but that's no more than a mental exercise and has little to do with efficiency in problem solving.
Your techniques may work for you but don't make the mistake of thinking they're universal.
Imagine Advance Wars. Their unit movement is very subtle.
You can choose the path the units travel, which can turn in to a forced action if you stumble onto an ambush unit. When you reach the end it tells you the outcome of your battle before you confirm you want to engage in it.
Doing logic only on the server and sending down a list of possible moves is going to get pretty heavy. Querying for each square of movement is going to be a latency nightmare.
Edit: actually, I think the movement ambush is from a clone. Although the arrow movement from the original is still complicated. That doesn't weaken the argument: imagine getting half way in to dev and realizing you can't add that feature with your architecture.
I also appreciate the load testing! For the curious, we're at 30 open rooms, and I'm seeing:
- 0.0 server load
- 2.6 MB of RAM used by nginx + its worker process
- 4 MB used by pont-server
The rooms persist as long as there's at least one connected player in them. Exiting + rejoining a room under the same name will let you continue to accumulate points under that name.
(Rejoining isn't perfectly seamless: you won't necessarily be dealt the exact same hand, and if you exit + rejoin while it's your turn, your turn will be skipped)
Here is a Svelte starter template for Rust (webasm) + Svelte:
I started such a thing a while back, but progressively decided the design wasn’t right for Rust, and so headed in a somewhat different direction, and then shelved the project after deciding to implement what I had had in mind differently.
`import module from './crate/Cargo.toml'`
from Svelte directly, and get Hot Module Reloading for free ?
I'm using a very similar architecture for https://boardgamelab.app/.
The WebAssembly performance boost will make a difference once I've implemented MCTS bots for the platform.
Do you have the option to generate different bot difficulty levels?
The basic version just uses Monte Carlo Tree Search and the game's victory condition to play the game. This won't result in a very strong bot for most games, but is a reasonable opponent to play your game against (which is very useful when designing a game).
Options to make the bot stronger involve giving it hints and shorter term objectives.
As someone interested in Rust but not currently using it, what is the state of async? An expansion on the bullet points following regarding runtime scope would be interest.
You can get into a state where there are no valid moves though: https://imgur.com/1WLufcx
I considered adding some kind of fix for that, but a fresh game is a mere refresh away, and I liked the opinion on Board Game Geek [1]:
> It's probably also worth noting that if this happens then all players involved need to re-visit some ideas of basic strategy.
[1] https://boardgamegeek.com/thread/861192/if-game-stuck-perfec...
I'd rather not have it in the main repo, since I won't be maintaining it. If you make your own pont-docker repo that contains the Dockerfile, I'd happily accept a PR that links to it in the README.