public static async Task StartGame(GameTableEntity entity) { if (String.IsNullOrWhiteSpace(ConnectionString) || ConnectionString == "DONOTINPUTSECRETSHERE") { // Ignore if no connection string is configured return; } try { // edit the entity if (entity.GameStarted) { return; } entity.GameStarted = true; // Retrieve a reference to the table. var table = GetTableClient(); // Create the TableOperation object that updates the entity var result = await table.ExecuteAsync(TableOperation.Replace(entity)); if (result.HttpStatusCode != 204) { throw new Exception(JsonConvert.SerializeObject(result)); } } catch (Exception error) { Log.Logger.LogInformation($"Ran into an error when updating history table {error.ToString()}"); throw; } }
public static async Task AddUserToGame(string userId, string userName, GameTableEntity entity) { if (String.IsNullOrWhiteSpace(ConnectionString) || ConnectionString == "DONOTINPUTSECRETSHERE") { // Ignore if no connection string is configured return; } try { // edit the entity List <User> users = JsonConvert.DeserializeObject <List <User> >(entity.UsersArray); if (users.Any(u => u.Id == userId) && !string.IsNullOrEmpty(entity.LeaderUserId)) { // No leader update and this user is gone already return; } users.Add(new User() { Id = userId, Name = userName, Color = GetRandomColor(users), InitialX = GetRandomNum(MaxXPosition), InitialY = GetRandomNum(MaxYPosition), }); users = users.Distinct().ToList(); entity.UsersArray = JsonConvert.SerializeObject(users); if (string.IsNullOrEmpty(entity.LeaderUserId)) { entity.LeaderUserId = users[0].Id; } // Retrieve a reference to the table. var table = GetTableClient(); // Create the TableOperation object that updates the entity var result = await table.ExecuteAsync(TableOperation.Replace(entity)); if (result.HttpStatusCode != 204) { throw new Exception(JsonConvert.SerializeObject(result)); } } catch (Exception error) { Log.Logger.LogInformation($"Ran into an error when updating history table {error.ToString()}"); throw; } }
public static async Task <GameTableEntity> CreateGame(string gameId, string leaderId, string leaderName) { // Create a new entity. GameTableEntity entry = new GameTableEntity(gameId) { LeaderUserId = leaderId, UsersArray = JsonConvert.SerializeObject(new List <User>() { new User() { Id = leaderId, Name = leaderName, Color = GetRandomColor(), InitialX = GetRandomNum(MaxXPosition), InitialY = GetRandomNum(MaxYPosition), }, }), }; if (String.IsNullOrWhiteSpace(ConnectionString) || ConnectionString == "DONOTINPUTSECRETSHERE") { // Ignore if no connection string is configured return(entry); } try { // Retrieve a reference to the table. var table = GetTableClient(); // Create the TableOperation object that inserts the entity await table.ExecuteAsync(TableOperation.Insert(entry)); return(entry); } catch (Exception error) { Log.Logger.LogInformation($"Ran into an error when updating history table {error.ToString()}"); throw; } }
internal static async Task RemoveUserFromGame(string userId, GameTableEntity entity) { if (String.IsNullOrWhiteSpace(ConnectionString) || ConnectionString == "DONOTINPUTSECRETSHERE") { // Ignore if no connection string is configured return; } try { // edit the entity List <User> users = JsonConvert.DeserializeObject <List <User> >(entity.UsersArray); if (!users.Any(u => u.Id == userId) && entity.LeaderUserId != userId) { return; } var userToRemove = users.SingleOrDefault(u => u.Id == userId); users.Remove(userToRemove); entity.UsersArray = JsonConvert.SerializeObject(users); if (entity.LeaderUserId == userId) { entity.LeaderUserId = users.Count > 0 ? users[0].Id : ""; } // Retrieve a reference to the table. var table = GetTableClient(); // Create the TableOperation object that updates the entity var result = await table.ExecuteAsync(TableOperation.Replace(entity)); if (result.HttpStatusCode != 204) { throw new Exception(JsonConvert.SerializeObject(result)); } } catch (Exception error) { Log.Logger.LogInformation($"Ran into an error when updating history table {error.ToString()}"); throw; } }