Exemplo n.º 1
0
        async void CreateGameAsync()
        {
            // Create the game and insert it into the DB.
            var game = new GameDto
            {
                GameName          = this.GameName,
                CurrentMoveNumber = 1,
                Date = DateTime.Now
            };
            GameDto insertedGame = await m_gamesService.AddOrUpdateAsync(game);

            // Generate resources rates for this game.
            await CreateResourcesRatesAsync(insertedGame);

            // Create wallets for this game.
            await CreateWalletsAsync(insertedGame);

            // Add gold to the gold wallet.
            int goldId     = 6; // The id of the gold is 6.
            var goldWallet = await m_walletService.GetAsync(w => w.GameId == insertedGame.GameId && w.ResourceId == goldId);

            await AddResourcesToWalletAsync(goldWallet, 10);

            OnGameCreated(new GameEventArgs(insertedGame.GameId));
        }
Exemplo n.º 2
0
 async Task AddResourcesToWalletAsync(ResourceWalletDto wallet, double count)
 {
     var tran = new ResourceWalletTransactionDto
     {
         GameId        = wallet.GameId,
         ResourceId    = wallet.ResourceId,
         ResourceCount = count
     };
     await m_walletTransactionService.AddOrUpdateAsync(tran);
 }
Exemplo n.º 3
0
        /// <summary>
        /// Send the resources from the wallet to another one asynchronously.
        /// </summary>
        public static async Task SendResourcesAsync
        (
            this IGenericService <ResourceWalletTransaction, ResourceWalletTransactionDto, int> service,
            ResourceWalletDto sourceWallet,
            ResourceWalletDto destWallet,
            double amountToWithdrawFromSourceWallet,
            double amountToSendToDestWallet
        )
        {
            // Check source wallet balance.

            double?sourceWalletBalance = await service.CallSfGetWalletBalanceAsync(sourceWallet.GameId, sourceWallet.ResourceId);

            if (sourceWalletBalance == null || sourceWalletBalance < amountToWithdrawFromSourceWallet)
            {
                throw new InvalidOperationException("Not enough resources on the source wallet");
            }

            // Withdraw recources from source wallet.

            var withdrawTran = new ResourceWalletTransactionDto
            {
                GameId        = sourceWallet.GameId,
                ResourceId    = sourceWallet.ResourceId,
                ResourceCount = -amountToWithdrawFromSourceWallet
            };
            await service.AddOrUpdateAsync(withdrawTran);

            // Add resources to the destination wallet.

            var addTran = new ResourceWalletTransactionDto
            {
                GameId        = destWallet.GameId,
                ResourceId    = destWallet.ResourceId,
                ResourceCount = +amountToSendToDestWallet
            };
            await service.AddOrUpdateAsync(addTran);
        }