Hi,
I have a 2 player PvP turnbased game with which I plan to save any match events to the cloud storage and then reload these events when a player leaves and rejoins the same match.
Within the MatchLoop function, I save the OpCode and Data as per below:
let matchLoop: nkruntime.MatchLoopFunction = function (context: nkruntime.Context, logger: nkruntime.Logger, nakama: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, messages: nkruntime.MatchMessage[]) {
let gameState = state as GameState;
for (let message of messages) {
dispatcher.broadcastMessage(message.opCode, message.data, null, message.sender);
var matchStateCode = message.opCode
var matchStateData = message.data
saveDispatchedCode(matchStateCode, context.matchId, context.userId);
saveDispatchedData(matchStateData, context.matchId, context.userId);
};
return gameState.endMatch ? null : { state: gameState };
}
The SaveDispatchedCode & SaveDispatchedData functions are written as follows:
function saveDispatchedCode(code: number, matchID: string, userID: string) {
var value = { code };
var write: nkruntime.StorageWriteRequest = {
key: "MatchStates",
collection: "Codes",
value: value,
userId: userID,
permissionRead: 1,
permissionWrite: 0,
}
}
function saveDispatchedData(data: ArrayBuffer, matchID: string, userID: string) {
var value = { data };
var write: nkruntime.StorageWriteRequest = {
key: "MatchStates",
collection: "Data",
value: value,
userId: userID,
permissionRead: 1,
permissionWrite: 0,
}
}
No errors so far on the above, but I am struggling on how to write the code for reloading and broadcasting these match OpCodes and Data. This is what I have so far:
let reloadMatchState: nkruntime.RpcFunction = function (context: nkruntime.Context, logger: nkruntime.Logger, nakama: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, userID: string): string {
let storageReadCode: nkruntime.StorageReadRequest = {
key: "MatchStates",
collection: "Code",
userId: userID,
}
let storageReadData: nkruntime.StorageReadRequest = {
key: "MatchStates",
collection: "Data",
userId: userID,
}
let eventCode: nkruntime.StorageObject[] = nakama.storageRead([storageReadCode]);
let eventData: nkruntime.StorageObject[] = nakama.storageRead([storageReadData]);
for (let i = 0; i < eventCode.length; i++) {
dispatcher.broadcastMessage(eventCode[i],eventData[i]);
}
logger.info("Receiving Event Code: " + eventCode + "and Event Data: " + eventData);
}
Regarding the ‘dispatcher.broadcastMessage(eventCode[i],eventData[i])’ code, I get the error: “StorageObject is not assignable to parameter of type ‘number’” which I understand because ‘eventCode[i]’ is meant to be a number and not a StorageObject.
Can you please provide any advice on how to convert a StorageObject to a number? Are there any examples of saving and reloading match events I can look at and is what I have written so far on the right track?
Any help would be appreciated.
Thanks
- Versions: Nakama {3.5}, {Windows, Mac, Linux binary or Docker}, {client library (SDK) and version}
- Server Framework Runtime language (If relevant) {Go, TS/JS, Lua}
{code or log snippet}
Media: