card_collectionuser_cards:{userId: some-user-id-3fb1d6,// some arbitrary ID
permissionRead: 2,permissionWrite: 2,...// some other properties
value:{some-random-id:{// another arbitrary ID
type:1,level: 1,}...// more cards
}}
现在,如果我们想在以后访问这些牌,我们可以用请求对象调用 storageRead:
deck.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
functionloadUserCards(nk: nkruntime.Nakama,logger: nkruntime.Logger,userId: string):CardCollection{letstorageReadReq: nkruntime.StorageReadRequest={key: DeckCollectionKey,collection: DeckCollectionName,userId: userId,}letobjects: nkruntime.StorageObject[]=nk.storageRead([storageReadReq]);// Get the original collection for processing
letstoredCardCollection=objects[0].value;...}
constcurrencyKeyName="gems"// Can be changed to whatever you want!
...functionupdateWallet(nk: nkruntime.Nakama,userId: string,amount: number,metadata:{[key: string]:any}):nkruntime.WalletUpdateResult{constchangeset={[currencyKeyName]:amount,}letresult=nk.walletUpdate(userId,changeset,metadata,true);returnresult;}
constrpcAddUserGems: nkruntime.RpcFunction=function(ctx: nkruntime.Context,logger: nkruntime.Logger,nk: nkruntime.Nakama):string{letwalletUpdateResult=updateWallet(nk,ctx.userId,100,{});letupdateString=JSON.stringify(walletUpdateResult);logger.debug("Added 100 gems to user %s wallet: %s",ctx.userId,updateString);returnupdateString;}
从 Unity 调用它:
1
2
3
4
5
6
7
8
9
10
11
12
privatevoidAwake(){..._getFreeGemsButton.onClick.AddListener(HandleAddFreeGems);// Bind function to button}...privateasyncvoidHandleAddFreeGems(){// Call the RPC we just made!IApiRpcnewGems=await_connection.Client.RpcAsync(_connection.Session,"add_user_gems");// IMPORTANT: Update the account instance to get the latest results!_connection.Account=await_connection.Client.GetAccountAsync(_connection.Session);...}