Hello all,
I’ve got a query that’s been bugging me for a while regarding the TypeScript runtime. There’s some methods that don’t quite behave like expected. The biggest one, that is causing me a lot of headache is moving items from one array to another. I’ll explain below.
My game has the following interfaces:
interface Pile {
cardTypes: number[];
cards: Card[];
}
interface Card {
cardID: string;
battleCardID: string;
position: number;
counters: number;
}
And the following function (I’ve tried about 5-6 different ways of achieving this, always the same result):
function moveElements(source: Pile, target: Pile, battlecardID: string) {
for (var i = source.cards.length - 1; i >= 0; i--) {
if (source.cards[i].battleCardID == battlecardID) {
var elem = source.cards.splice(i, 1)[0];
console.log("Moving card: " + elem.cardID);
var cardsAdded = target.cards.push(elem);
console.log("Cards Added: " + cardsAdded);
break;
}
}
console.log("Cards Left: " + source.cards.length);
console.log("Cards In New Pile: " + target.cards.length);
}
I want to move a Card from one pile to another. This is an example of 2 piles:
var pile1: Pile = {
"cardTypes": [ 7 ],
"cards": [
{
"battleCardID": "8ddc1899-6b05-46b0-bec3-fbc0d9cab368",
"cardID": "RH-OVR-APU1",
"counters": 0,
"position": 4
},
{
"battleCardID": "a1b751ba-cace-4110-9824-df1f93f12c36",
"cardID": "RH-UNO-PCR1",
"counters": 0,
"position": 35
},
{
"battleCardID": "1f7e09b7-2772-4a45-884d-71f69e4344f5",
"cardID": "RH-OVR-BCR2",
"counters": 0,
"position": 5
},
{
"battleCardID": "7c16482f-8132-4b15-a565-02899b24d263",
"cardID": "RH-SYS-BCR1",
"counters": 0,
"position": 18
},
{
"battleCardID": "7c16482f-8132-4b15-a565-02899b242263",
"cardID": "RH-SYS-ACR2",
"counters": 0,
"position": 12
},
]
}
var pile2: Pile = { cardTypes: [], cards: [] };
This is the call that is supposed to move the card:
moveElements(pile1,pile2,"1f7e09b7-2772-4a45-884d-71f69e4344f5")
Testing the above in a couple of TS playgrounds prints the below (which is the correct behavior):
[LOG]: "Moving card: RH-OVR-BCR2"
[LOG]: "Cards Added: 1"
[LOG]: "Cards Left: 4"
[LOG]: "Cards In New Pile: 1"
But in the Nakama console, I get the following:
{"level":"debug","caller":"server/runtime_javascript_logger.go:104","msg":"Moving card: RH-OVR-BCR2"}
{"level":"debug","caller":"server/runtime_javascript_logger.go:104","msg":"Cards Added: 1"}
{"level":"debug","caller":"server/runtime_javascript_logger.go:104","msg":"Cards Left: 5"}
{"level":"debug","caller":"server/runtime_javascript_logger.go:104","msg":"Cards In New Pile: 0"}
In the match state in Nakama, the card is actually removed from the array, but leaves a null in its place. Pile2 still has a blank array.
Is this to do with something that Nakama processes differently internally? Something to do with the tsconfig compilerOptions (added below), perhaps? I’ve matched these in the TS Playground.
"compilerOptions": {
"target": "es2015",
"typeRoots": [
"./node_modules"
],
"outFile": "./build/index.js",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}