or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Make multiplayer board game with react and frontend libraries only - no backend code nor database
TLDR;
end product
source code
Introduction
This tutorial will show you how to build a online poker 99 board game using react, material-ui and gamenet, with local hot seat players and ai players supported.
There is no need to code another backend like socket.io servers or use some realtime database like firebase. Gamenet is using WebRTC technology to build peer-to-peer state management network, backed by peer.js. As public peer.js server may not stable, it will be better to host your own peer.js broker server.
Prerequisite
Some react knowledge and a reactjs development environment. Best to have experience with redux/ useReducer since the API looks similar
Poker99 Game Rules
Poker99 is a simple poker game: easy to implement and easy to make AI, so this game is chosen as a demo. There is a bomb on the table and whenever player played a card, it will increase the point of the bomb. When a player cannot play a card that wont blow the bomb, he dies and next player continue. Players take turn to play card until only one player left and that is the winner. The card number represents the point, except 4, 5, J correspond to 0 point, where 4 reverses and 5 set the turn to the player of your choice, J just skip your turn; K set the point to 99; 10, Q you can choose to ±10 or ±20 respectively, spade A set the point to 1. Players hold 5 card at a time, after they played a card, they draw 1 card. Card will be recycled when all are drawn.
Steps
1. Prepare and Install
Initialize a react project using create-react-app (typescript is optional)
Install the dependencies
Start the react development server
2. Get GameNet running
This section will demonstrate how to make room joining and game start using components offered by
gamenet-material
from scratch2.1 Create a basic State class, Action type and reducer that just return itself
state, action and reducer are the three essential part you need to provide to set up the connection.
Each point in the network will have a copy of the same state, here we can modify the min and max players that the game allowed. If more than this amount of player join, they will become spectators
We dispatch actions (will essentially just an object) to update the state, they need to be extending NetworkAction and Union GenericBoardGameAction
Reducer handle the dispatched action and return a new state. For simplicity we are returning the old state for now. (GameNet has an additional reducer to handle generic game action like handling player join/ leave/ prepare, etc.)
2.2. Create Poker99Context and hook so you can use/update the network everywhere
Since gamenet is only providing an
useGameNetwork
hook for connection handling, eachuseGameNetwork
would be one point in the network. In order to make the entire game to use the same point instead of creating many points, we use a context to pass the point to all the components. We can use a higher order component to wrap app with a Poker99Context.Provider, and then we can use theusePoker99
hook to access the state or dispatch event etc.2.3. Create Empty Game.tsx for game interaction and update App.tsx for controlling the room flow
Game tsx that can display the network state
Wrap the app with withPoker99Network, so we can call usePoker99 here. import the Slider, Home, Room from gamenet-material, so you can have the room system automatically.
Then you can already create room, add "AI" and hot seat players, when you start game, you will see the network state. Then we can start implementing our game logic.
3. Implement the ordinary PVP Poker99 game
To implement that you can play with other people over the internet (no hot seat nor AI)
3.1 Design types and data structure
Define some common types that would be useful
Please notice the attributes from the base class, they are controlled by gamenet
3.2. Design the Actions
There are only two possible actions that the player can do:
Since there are some cards that require additional info, like 10 and Q you need to specify increase or decrease point, and 5 you need to specify a target player to change turn, for play card action you need a special typed payload
3.3. Implement the Card Logic and Reducer
add three types to
types.ts
(This is a very functional approach, try your best not to alter the value of parameter passed)
implement the util functions
minPossible: given the current point and cards i have, return the minimum possible point after playing a card, and the index of that card in the input array
getFullDeck: return all 52 cards of a poker set
implement some state mappers
implement PlayCard mappers
just put all PlayCard mappers here for you to copy paste (?)
Put things together in a reducer
3.4 Make a simple UI that can just play the poker99 game
4. Support AI
implement an aiAction function, which accept the state and playerId, to decide what action to return
then supply the aiAction to the useBoardGameNetwork in
withPoker99Network.tsx
5. Support local hot seat
obtain
myLocals, hideDeck, setHideDeck, renderedDeckId
fromusePoker99
myLocals
contains the name of all hotseat players you can control,hideDeck
is the flag to tell whether deck should be hidden, to prevent your friend next to you can see your deck, andsetHideDeck
essentially let you to toggle this flag, to reveal your cardrenderedDeckId
tell you which player's deck should you render6. Custom Peer Server
set these environment variables in
.env.production.local
and.env.development.local
. They correspond the options in peerjs Peer constructor