public async Task <bool> ReservePlayerColour(Guid sessionId, String sessionEtag, PlayerColour colour) { ISessionData sessionData = await GetSession(sessionId); SessionTableEntry session = sessionData as SessionTableEntry; if (!session.IsColourUsed(colour)) { if (session.ETag == sessionEtag) { try { session.AddUsedColour(colour); // Write entry back (fails on write conflict) TableOperation insertOperation = TableOperation.Replace(session); await SessionTable.ExecuteAsync(insertOperation); return(true); } catch (StorageException exception) { if (exception.RequestInformation.HttpStatusCode == (int)HttpStatusCode.PreconditionFailed) { throw new ConcurrencyException(); } else { throw exception; } } } else { throw new ConcurrencyException(); } } else { return(false); } }
public async Task IntegrationTestSetSessionPhase_WithCreateCommandQueueTable() { // Arrange SessionRepository repository = new SessionRepository(DevelopmentStorageAccountConnectionString); Guid validGuid = new Guid("ADECC70C-7964-4648-9E4C-F4C71EA4502A"); ISession sessionDetails = await repository.SetupSession(validGuid, "CreatingUser"); // Act await repository.SetSessionPhase(validGuid, sessionDetails.PhaseId, SessionPhase.Reinforcements); // Assert TableOperation operation = TableOperation.Retrieve <SessionTableEntry>(validGuid.ToString(), "CreatingUser"); TableResult result = await SessionTable.ExecuteAsync(operation); Assert.IsNotNull(result.Result); Assert.IsInstanceOfType(result.Result, typeof(SessionTableEntry)); SessionTableEntry resultStronglyTyped = result.Result as SessionTableEntry; Assert.AreNotEqual(sessionDetails.PhaseId, resultStronglyTyped.PhaseId); Assert.AreEqual(SessionPhase.Reinforcements, resultStronglyTyped.PhaseType); }
public async Task IntegrationTestSetSessionPhase() { // Arrange SessionRepository repository = new SessionRepository(DevelopmentStorageAccountConnectionString); Guid validGuid = new Guid("46DAC828-3EFC-45E2-9294-B39AE9403DAA"); ISession sessionDetails = await repository.SetupSession(validGuid, "CreatingUser"); // Act await repository.SetSessionPhase(validGuid, sessionDetails.PhaseId, SessionPhase.SpoilsOfWar); // Assert TableOperation operation = TableOperation.Retrieve <SessionTableEntry>(validGuid.ToString(), "CreatingUser"); TableResult result = await SessionTable.ExecuteAsync(operation); Assert.IsNotNull(result.Result); Assert.IsInstanceOfType(result.Result, typeof(SessionTableEntry)); SessionTableEntry resultStronglyTyped = result.Result as SessionTableEntry; Assert.AreNotEqual(sessionDetails.PhaseId, resultStronglyTyped.PhaseId); Assert.AreEqual(SessionPhase.SpoilsOfWar, resultStronglyTyped.PhaseType); }
public async Task SetSessionPhase(Guid sessionId, Guid currentPhaseId, SessionPhase newPhase) { ISessionData sessionData = await GetSession(sessionId); SessionTableEntry session = sessionData as SessionTableEntry; if (session.PhaseId == currentPhaseId) { try { session.PhaseId = Guid.NewGuid(); session.RawPhaseType = (Int32)newPhase; // Increase the round number (if required) if (newPhase == SessionPhase.Victory) { session.RawRound += 1; } // Write entry back (fails on write conflict) TableOperation insertOperation = TableOperation.Replace(session); await SessionTable.ExecuteAsync(insertOperation); } catch (StorageException exception) { if (exception.RequestInformation.HttpStatusCode == (int)HttpStatusCode.PreconditionFailed) { throw new ConcurrencyException(); } else { throw exception; } } } else { throw new ConcurrencyException(); } }
public async Task IntegrationTestReservePlayerColour() { // Arrange SessionRepository repository = new SessionRepository(DevelopmentStorageAccountConnectionString); Guid validGuid = new Guid("E5894BE3-6074-4516-93FB-BC851C1E4246"); ISessionData sessionData = await repository.SetupSession(validGuid, "CreatingUser"); // Act bool isReserved = await repository.ReservePlayerColour(validGuid, sessionData.CurrentEtag, PlayerColour.Blue); // Assert Assert.AreEqual(true, isReserved); TableOperation operation = TableOperation.Retrieve <SessionTableEntry>(validGuid.ToString(), "CreatingUser"); TableResult result = await SessionTable.ExecuteAsync(operation); Assert.IsNotNull(result.Result); Assert.IsInstanceOfType(result.Result, typeof(SessionTableEntry)); SessionTableEntry resultStronglyTyped = result.Result as SessionTableEntry; Assert.IsTrue(resultStronglyTyped.IsColourUsed(PlayerColour.Blue)); Assert.AreNotEqual(resultStronglyTyped.CurrentEtag, sessionData.CurrentEtag); }
public async Task <Guid> CreateSession(String userId, PlayerColour colour) { Guid newSessionGuid = Guid.NewGuid(); // Create a new table to store all the data for this session var dataTable = GetTableForSessionData(newSessionGuid); await dataTable.CreateIfNotExistsAsync(); // Create a new table entry SessionTableEntry newSession = new SessionTableEntry(userId, newSessionGuid); // Kick off the insert operation TableOperation insertOperation = TableOperation.Insert(newSession); await SessionTable.ExecuteAsync(insertOperation); // Add the player to the session await ReservePlayerColour(newSessionGuid, newSession.ETag, colour); await JoinSession(newSessionGuid, userId, colour); // Return the new session GUID return(newSessionGuid); }
public async Task IntegrationTestCreateSession() { // Arrange SessionRepository repository = new SessionRepository(DevelopmentStorageAccountConnectionString); String dummyUserId = "DummyUserId"; // Act Guid newSessionGuid = await repository.CreateSession(dummyUserId, PlayerColour.Black); // Assert Assert.IsNotNull(newSessionGuid); var dataTable = repository.GetTableForSessionData(newSessionGuid); TableOperation operation = TableOperation.Retrieve <SessionTableEntry>(newSessionGuid.ToString(), dummyUserId); TableResult result = await SessionTable.ExecuteAsync(operation); Assert.IsNotNull(result.Result); Assert.IsInstanceOfType(result.Result, typeof(SessionTableEntry)); SessionTableEntry resultStronglyTyped = result.Result as SessionTableEntry; Assert.AreEqual(dummyUserId, resultStronglyTyped.OwnerId); Assert.AreEqual(Guid.Empty, resultStronglyTyped.PhaseId); Assert.AreEqual(SessionPhase.NotStarted, resultStronglyTyped.PhaseType); Assert.IsTrue(resultStronglyTyped.IsColourUsed(PlayerColour.Black)); operation = TableOperation.Retrieve <NationTableEntry>(newSessionGuid.ToString(), "Nation_" + dummyUserId); result = await dataTable.ExecuteAsync(operation); Assert.IsNotNull(result.Result); Assert.IsInstanceOfType(result.Result, typeof(NationTableEntry)); NationTableEntry resultPlayerStronglyTyped = result.Result as NationTableEntry; Assert.AreEqual(newSessionGuid, resultPlayerStronglyTyped.SessionId); Assert.AreEqual(dummyUserId, resultPlayerStronglyTyped.UserId); Assert.AreEqual(Guid.Empty, resultPlayerStronglyTyped.CompletedPhase); }