public async Task IntegrationTestOrderAttack()
        {
            // Arrange
            CommandQueue repository       = new CommandQueue(DevelopmentStorageAccountConnectionString);
            Guid         targetRegionGuid = new Guid("8449A25B-363D-4F01-B3D9-7EF8C5D42047");

            // Act
            Guid operationGuid;

            using (IBatchOperationHandle batchOperation = new BatchOperationHandle(CommandTable))
            {
                operationGuid = await repository.OrderAttack(batchOperation, SessionGuid, SessionPhaseGuid, RegionGuid, "DummyEtag", targetRegionGuid, 5U);
            }

            // 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.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(5U, queuedCommand.NumberOfTroops);
        }
        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);
        }
Пример #3
0
        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));
        }
Пример #4
0
 public void RemoveCommand(IBatchOperationHandle batchOperationHandle, ICommandQueueMessage command)
 {
     lock (batchOperationHandle)
     {
         TableBatchOperation    batchOperation    = (batchOperationHandle as BatchOperationHandle).BatchOperation;
         CommandQueueTableEntry commandQueueEntry = command as CommandQueueTableEntry;
         commandQueueEntry.IsValid();
         batchOperation.Delete(commandQueueEntry);
     }
 }
Пример #5
0
        public async Task <Guid> Redeploy(Guid sessionId, Guid phaseId, String nationEtag, Guid sourceRegion, Guid targetRegion, UInt32 numberOfTroops)
        {
            CloudTable commandQueueTable = GetCommandQueueTableForSession(sessionId);

            // Create a new table entry
            CommandQueueTableEntry newCommand = CommandQueueTableEntry.CreateRedeployMessage(sessionId, phaseId, String.Empty, sourceRegion, targetRegion, numberOfTroops);

            // Kick off the insert operation
            TableOperation insertOperation = TableOperation.Insert(newCommand);
            await commandQueueTable.ExecuteAsync(insertOperation);

            return(newCommand.OperationId);
        }
        public async Task IntegrationTestRemoveCommands()
        {
            // Arrange
            CommandQueue repository         = new CommandQueue(DevelopmentStorageAccountConnectionString);
            Guid         sessionId          = Guid.NewGuid();
            CloudTable   randomCommandTable = CommandQueue.GetCommandQueueTableForSession(TableClient, sessionId);

            randomCommandTable.CreateIfNotExists();
            Guid attackId;
            Guid attackSecondId;
            Guid secondRegionId = Guid.NewGuid();

            using (IBatchOperationHandle batchOperation = new BatchOperationHandle(randomCommandTable))
            {
                attackId = await repository.OrderAttack(batchOperation, sessionId, sessionId, sessionId, String.Empty, sessionId, 0);

                attackSecondId = await repository.OrderAttack(batchOperation, sessionId, sessionId, sessionId, String.Empty, secondRegionId, 0);
            }
            var queuedAttacks = await repository.GetQueuedCommands(sessionId, sessionId);

            // Act
            using (BatchOperationHandle handle = new BatchOperationHandle(randomCommandTable))
            {
                repository.RemoveCommands(handle, new List <ICommandQueueMessage> {
                    queuedAttacks.Where(attack => attack.OperationId == attackId).First()
                });
            }

            // Assert
            var tableQuery = from entity in randomCommandTable.CreateQuery <CommandQueueTableEntry>()
                             select entity;
            var tableData = tableQuery.ToList();

            Assert.AreEqual(1, tableData.Count);
            CommandQueueTableEntry result = tableData.First();

            Assert.AreEqual(attackSecondId, result.OperationId);
        }
        public async Task IntegrationTestCreateReinforceMessage()
        {
            // Arrange
            CommandQueue repository = new CommandQueue(DevelopmentStorageAccountConnectionString);

            // Act
            Guid operationGuid = await repository.DeployReinforcements(SessionGuid, SessionPhaseGuid, RegionGuid, "DummyEtag", 10U);

            // Assert
            var operation = TableOperation.Retrieve <CommandQueueTableEntry>(SessionGuid.ToString(), "Command_" + operationGuid.ToString());
            var result    = await CommandTable.ExecuteAsync(operation);

            CommandQueueTableEntry queuedCommand = result.Result as CommandQueueTableEntry;

            Assert.IsNotNull(queuedCommand);
            Assert.AreEqual(operationGuid, queuedCommand.OperationId);
            Assert.AreEqual(SessionGuid, queuedCommand.SessionId);
            Assert.AreEqual(SessionPhaseGuid, queuedCommand.PhaseId);
            Assert.AreEqual(CommandQueueMessageType.Reinforce, queuedCommand.MessageType);
            Assert.AreEqual(RegionGuid, queuedCommand.TargetRegion);
            Assert.AreEqual("DummyEtag", queuedCommand.TargetRegionEtag);
            Assert.AreEqual(10U, queuedCommand.NumberOfTroops);
        }
        public async Task IntegrationTestRedeploy()
        {
            // Arrange
            CommandQueue repository       = new CommandQueue(DevelopmentStorageAccountConnectionString);
            Guid         targetRegionGuid = new Guid("8449A25B-363D-4F01-B3D9-7EF8C5D42047");

            // Act
            Guid operationGuid = await repository.Redeploy(SessionGuid, SessionPhaseGuid, String.Empty, RegionGuid, targetRegionGuid, 5U);

            // Assert
            var operation = TableOperation.Retrieve <CommandQueueTableEntry>(SessionGuid.ToString(), "Command_" + operationGuid.ToString());
            var result    = await CommandTable.ExecuteAsync(operation);

            CommandQueueTableEntry queuedCommand = result.Result as CommandQueueTableEntry;

            Assert.IsNotNull(queuedCommand);
            Assert.AreEqual(operationGuid, queuedCommand.OperationId);
            Assert.AreEqual(SessionGuid, queuedCommand.SessionId);
            Assert.AreEqual(SessionPhaseGuid, queuedCommand.PhaseId);
            Assert.AreEqual(CommandQueueMessageType.Redeploy, queuedCommand.MessageType);
            Assert.AreEqual(RegionGuid, queuedCommand.SourceRegion);
            Assert.AreEqual(targetRegionGuid, queuedCommand.TargetRegion);
            Assert.AreEqual(5U, queuedCommand.NumberOfTroops);
        }
Пример #9
0
        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);
        }