Template https://github.com/heroiclabs/nakama-project-template
# build with rollup
npm run build
# migrate for the first time
nakama.exe migrate up --database.address postgres:[email protected]:5432
# run
nakama --config local.yml
When server start
-
Register handlers for the match
adventure_match
- matchInit,
- matchJoinAttempt,
- matchJoin,
- matchLeave,
- matchLoop,
- matchSignal,
- matchTerminate
-
Create the match
adventure_match
- Initialize empty match state
- Set tick rate 1 (game loop run once a sec)
interface AdventureState {
presences: {[userId: string]: nkruntime.Presence}
beastPosition: {[id: number]: number}
positionBeast: {[pixel: number]: number}
beastOnboard: number[]
}
Decide if user can join or not, currently do nothing other than return accept: true
.
Run once when user join, send user the current game state: positions of all monsters
Run once when user leave (or network disconnect)
- Remove map value
uuid -> pubkey[]
Run tickRate
time(s) (1 in this case) per second.
- Process login messages (opcode 0).
- Process onboard messages (opcode 1).
- Process all the monster's actions (opcode 2: move, opcode 3: shoot).
-
When open match, client join the match with uuid to get match realtime info.
-
When login, user send public key, current timestamp, signature for the timestamp to the server.
-
Server verify, store info in a map:
uuid -> pubkey[]
-
Get monster's owner on-chain, reject if the caller is not owner.
-
Get monster equipments, calculate its attributes: health, shoot range, move range...
-
If the position not empty put the monster landing request to waiting queue.
-
Check if action come from owner.
-
Process actions in order, update match state.
- move
- shoot
- equip
- client update onchain and inform server
- server confirm equipped data onchain, update game states
-
Calculate state changes, encode using flatbuffer and send to all clients.
Encode, decode game state updates
interface BeastActionI {
id: number
target: number
}
interface UpdateStateI {
beastMoves: BeastActionI[]
beastShoots: BeastActionI[]
beastDeaths: number[]
}
Server holds an admin account who has the priviledges to update some data on-chain.
- Onboard monster from a land (every monster starting from the central Pixelland).
- Give ownership of NFT equipment on the map to an account (when player collect item successfully on the map).
- Update NFT monster score, experience, level.
- Offboard monster, put to a land (when player enter a land to escape from the field successfully).