vbgs is the core business logic behind vikebot. It runs the game-server itself and processes game actions to render-able data for graphical interfaces
Create a new file named opName.go
where Name
is single identifier (Camel-case) for the operation you want to implement. For example if you want to create the operation Radar you create opRadar.go
in the repositories root folder.
In the file created in step one now add a namePacket
and nameObj
struct as shown below, where name
is the identifier of the file in Pascal-case.
type radarObj struct {
}
type radarPacket struct {
Type string `json:"type"`
Obj radarObj `json:"obj"`
}
If you need to pass parameters to your operation from the client side then add them to your nameObj
struct as pointer fields. Only if we use pointers we can differentiate between the following two cases.
- The client didn't sent the additional parameters
- The client sent invalid fmt/values/etc for the parameters
Fields in the
namePacket
struct mustn't be pointers because they are checked in general by thepacketHandler
anddispatcher
.
Below you can see an example from the move operation
type moveObj struct {
Direction *string `json:"direction"`
}
type movePacket struct {
Type string `json:"type"`
Obj moveObj `json:"obj"`
}
Add the function that is responsible for handling a packet of your type. This function should be named after your filename (of course without the suffix .go
) and accept a *ntcpclient
and a packet of your type.
func opRadar(c *ntcpclient, packet radarPacket) {
}
In order to register your operation endpoint you need to add your identifier as a case in dispatcher.go
's packet switch. In the case you need to unmarshal the data
, check for err
and call your own dispatch endpoint afterwards. For good examples look at the other operations already existing.
case "radar":
var radar radarPacket
err = json.Unmarshal(data, &radar)
if err != nil {
c.Respond(statusInvalidJSON)
return
}
opRadar(c, radar)
return
You can now implement the operation itself. It should get prechecked and dispatched to your func. To get a feeling how good implementations look like look at the already existsing examples. In general you should keep a few things in mind:
- Add testing!!!
- Check for
nil
fields in your customnameObj
structs - Don't forget to push updates to the
updatePush
network
+--------------+-------------------+-----------------------+
| IV (16bytes) | Encrypted payload | Suffix (1byte = '\n') |
+--------------+-------------------+-----------------------+
JsonPacket format -> Buffer -> Encrypt -> Base64