This section assumes you have completed tutorials 1 and 2.
Introduction
Let's experience the customization capabilities of Diarkis by implementing a custom command.
In this tutorial, we will implement a command with the following features:
A command to attack enemies in a Room
Command version: 2 (versions 0 and 1 are used for built-in commands)
Command ID: 300
Request: Send a command specifying an attack type (type) (1: Melee, 2: Ranged)
Response: Return who dealt damage, the damage value, and the total damage value
Push notifications to all room members
The implementation flow is as follows:
Write code to generate a payload using puffer (a data serializer provided by Diarkis)
Implement the custom command on the server
Implement the command in the test client
Puffer Module (Data Serializer provided by Diarkis)
Puffer is a tool that generates code to serialize and deserialize data from JSON.
The output supports C++, C#, and Go, simplifying the implementation of packet data for clients developed with Unreal or Unity and servers developed with Go.
For more details on Puffer, please refer to the API reference.
Run the following command to generate code. The code is output to each language's directory under puffer.
$makegen# Code is output to each language's directory under pufferpuffer├──cpp/custom/Attack.h├──cpp/custom/AttackResult.h├──cs/custom/Attack.cs├──cs/custom/AttackResult.cs├──go/custom/attack.go└──go/custom/attackresult.go
Implementing the attack Command Handler on the Server
Let's implement the attack command on the server.
💾 Add ./cmds/custom/attack.go.
Comments within the code describe what is being implemented, so please read them as well.
packagecustomcmdsimport ("errors"// pattack is the puffer package we generated earlier. pattack "handson/puffer/go/custom""github.com/Diarkis/diarkis/derror""github.com/Diarkis/diarkis/room""github.com/Diarkis/diarkis/server""github.com/Diarkis/diarkis/user""github.com/Diarkis/diarkis/util")// attack performs an attack on enemies within a Room.// // All Diarkis commands take specific arguments.// - ver: Command version// - cmd: Command ID// - payload: Data passed to the command// - userData: User datafuncattack(ver uint8, cmd uint16, payload []byte, userData *user.User, next func(error)) {// Since the attack command is to attack enemies in a Room,// an error occurs if the user is not participating in a Room.// Check whether the user is participating by obtaining the roomID. roomID := room.GetRoomID(userData)// If the user is not participating in a room, roomID is returned as empty, so handle the error.if roomID =="" {// Return an error response to the user with userData.ServerRespond(). err := errors.New("not in the room") userData.ServerRespond(derror.ErrData(err.Error(), derror.NotAllowed(0)), ver, cmd, server.Bad, true)// After returning the response, call next(err) and return.next(err)return }// After calling pattack.NewAttack(), deserialize the payload with req.Unpack(payload). req := pattack.NewAttack() req.Unpack(payload)// Calculate damage based on the Type. damage :=0switch req.Type {case1: // Melee attack (D20) damage = util.RandomInt(1, 20)case2: // Ranged attack (D12 + 3) damage = util.RandomInt(1, 12) +3default: err := errors.New("invalid attack type") userData.ServerRespond(derror.ErrData(err.Error(), derror.InvalidParameter(0)), ver, cmd, server.Bad, true)next(err)return }// Add the calculated damage to the total damage of the enemy.// You can save information as a Property in the Room.// Here, damage is added against the key "DAMAGE".// By using room.IncrProperty, you can get the incremented value.// There are other functions available to handle Property. Please see the following for details.// https://docs.diarkis.io/docs/server/v1.0.0/diarkis/room/index.html updatedDamage, updated := room.IncrProperty(roomID, "DAMAGE", int64(damage))if!updated { err := errors.New("incr property failed") userData.ServerRespond(derror.ErrData(err.Error(), derror.Internal(0)), ver, cmd, server.Err, true)next(err)return } logger.Info("Room %s has been attacked by %s using %s attack by %d damage. Total damage: %d", roomID, userData.ID, req.Type, damage, updatedDamage)
// Return who dealt damage, the damage value, and the total damage value, and notify room members.// After calling res := pattack.NewAttackResult(), set the return data. res := pattack.NewAttackResult() res.Type = req.Type res.Uid = userData.ID res.Damage =uint16(damage) res.TotalDamage =uint16(updatedDamage)// Use userData.ServerRespond() to respond to the user who executed the command.// Use room.Relay() to notify the other members in the room of the result. userData.ServerRespond(res.Pack(), ver, cmd, server.Ok, true) room.Relay(roomID, userData, ver, cmd, res.Pack(), true)// After returning the response, call next(nil) and return.next(nil)}
Exposing the attack Command
After implementing the command, it needs to be exposed externally.
💾 Edit ./cmds/custom/main.go and add the following.
Add diarkisexec.SetServerCommandHandler() to Expose() to expose the attack command.
To execute the server's attack command, add the attack command to the test client.
💾 Add ./testcli/handson/attack.go.
In addition to the setup for executing the client, we implement Attack() for sending commands to the server, onResponse() for handling responses, and onPush() for handling push notifications.