public Task <Guid> OrderAttack(IBatchOperationHandle batchOperationHandleInterface, Guid sessionId, Guid phaseId, Guid sourceRegion, String sourceRegionEtag, Guid targetRegion, UInt32 numberOfTroops) { BatchOperationHandle batchOperationHandle = batchOperationHandleInterface as BatchOperationHandle; CloudTable commandQueueTable = GetCommandQueueTableForSession(sessionId); Guid operationId = Guid.NewGuid(); TableOperation operation = TableOperation.Retrieve <CommandQueueTableEntry>(sessionId.ToString(), "Command_" + sourceRegion.ToString() + "_" + targetRegion.ToString()); var orderAttackTask = commandQueueTable.ExecuteAsync(operation) .ContinueWith(getExistingTask => { TableResult getExistingResult = getExistingTask.Result; if (getExistingResult.Result != null) { // Update existing entry CommandQueueTableEntry existingCommand = getExistingResult.Result as CommandQueueTableEntry; existingCommand.OperationId = operationId; existingCommand.RawNumberOfTroops += (int)numberOfTroops; // Kick off update operation batchOperationHandle.BatchOperation.Replace(existingCommand); } else { // Create a new table entry CommandQueueTableEntry newCommand = CommandQueueTableEntry.CreateAttackMessage(operationId, sessionId, phaseId, sourceRegion, sourceRegionEtag, targetRegion, numberOfTroops); // Kick off the insert operation batchOperationHandle.BatchOperation.Insert(newCommand); } }); batchOperationHandle.AddPrerequisite(orderAttackTask, 1); return(Task.FromResult(operationId)); }
public async Task IntegrationTestOrderAttack_MergeWithExisting() { // Arrange CommandQueue repository = new CommandQueue(DevelopmentStorageAccountConnectionString); Guid targetRegionGuid = new Guid("7E3BCD95-EB8B-4401-A987-F613A3428676"); Guid initialOperationId = Guid.NewGuid(); CommandQueueTableEntry newCommand = CommandQueueTableEntry.CreateAttackMessage(initialOperationId, SessionGuid, SessionPhaseGuid, RegionGuid, "DummyEtag", targetRegionGuid, 5U); CommandTable.Execute(TableOperation.Insert(newCommand)); // Act Guid operationGuid; using (IBatchOperationHandle batchOperation = new BatchOperationHandle(CommandTable)) { operationGuid = await repository.OrderAttack(batchOperation, SessionGuid, SessionPhaseGuid, RegionGuid, "DummyEtag", targetRegionGuid, 4U); } // Assert var operation = TableOperation.Retrieve <CommandQueueTableEntry>(SessionGuid.ToString(), "Command_" + RegionGuid.ToString() + "_" + targetRegionGuid.ToString()); var result = await CommandTable.ExecuteAsync(operation); CommandQueueTableEntry queuedCommand = result.Result as CommandQueueTableEntry; Assert.IsNotNull(queuedCommand); Assert.AreEqual(operationGuid, queuedCommand.OperationId); Assert.AreNotEqual(operationGuid, initialOperationId); Assert.AreEqual(SessionGuid, queuedCommand.SessionId); Assert.AreEqual(SessionPhaseGuid, queuedCommand.PhaseId); Assert.AreEqual(CommandQueueMessageType.Attack, queuedCommand.MessageType); Assert.AreEqual(RegionGuid, queuedCommand.SourceRegion); Assert.AreEqual("DummyEtag", queuedCommand.SourceRegionEtag); Assert.AreEqual(targetRegionGuid, queuedCommand.TargetRegion); Assert.AreEqual(9U, queuedCommand.NumberOfTroops); }
internal static async Task <UInt32> BulkRandomlyAttack(GameController game, WorldController world, RegionRepository regionRepository, SessionRepository sessionRepository, Guid sessionId, String ownerId, UInt32 troopCount, UInt32 numberOfAttacks) { ISession session = await game.GetSession(sessionId); // Get owned regions List <CommandQueueTableEntry> attacksToQueue = new List <CommandQueueTableEntry>(); IEnumerable <Guid> ownedRegions = await GetCurrentlyOwnedRegions(world, sessionId, ownerId); IEnumerable <IRegionData> worldRegionList = await regionRepository.GetRegions(sessionId); Dictionary <Guid, IRegionData> worldRegionLookup = worldRegionList.ToDictionary(region => region.RegionId); UInt32 numberOfRequestedAttacks = numberOfAttacks; // Create attack table entries foreach (Guid ownedRegionId in ownedRegions) { IRegionData details = worldRegionLookup[ownedRegionId]; if (details.TroopCount > 1 && numberOfAttacks > 0) { foreach (Guid adjacentRegionId in details.ConnectedRegions) { IRegionData targetDetails = worldRegionLookup[adjacentRegionId]; UInt32 troopsInRegionToAttackWith = details.TroopCount - 1; if (targetDetails.OwnerId != ownerId) { UInt32 troopsToAttackWith = Math.Min(details.TroopCount - 1, troopCount); attacksToQueue.Add(CommandQueueTableEntry.CreateAttackMessage(Guid.NewGuid(), session.GameId, session.PhaseId, details.RegionId, details.CurrentEtag, targetDetails.RegionId, troopsToAttackWith)); troopsInRegionToAttackWith -= troopsToAttackWith; numberOfAttacks -= 1; } if (numberOfAttacks == 0 || troopsInRegionToAttackWith == 0) { break; } } } if (numberOfAttacks == 0) { break; } } // Batch insert operations using (BatchOperationHandle batchOperation = new BatchOperationHandle(sessionRepository.GetTableForSessionData(sessionId))) { for (int counter = 0; counter < attacksToQueue.Count; ++counter) { batchOperation.BatchOperation.Insert(attacksToQueue[counter]); if (batchOperation.RemainingCapacity == 0) { await batchOperation.CommitBatch(); } } } // End attack phase await game.PostEndPhase(session.GameId, session.PhaseId); return(numberOfRequestedAttacks - numberOfAttacks); }