public static async Task <bool> CheckHasPlayedSet(SdlPlayer player) { string offset = null; string errorMessage = null; List <AirtableRecord> records = new List <AirtableRecord>(); using (AirtableBase airtableBase = new AirtableBase(Globals.BotSettings.AppKey, Globals.BotSettings.BaseId)) { do { Logger.Info($"Retrieving data with offset {offset}."); Task <AirtableListRecordsResponse> task = airtableBase.ListRecords( "Draft Log", offset, null, null, null, null ); AirtableListRecordsResponse response = await task; if (response.Success) { Logger.Info($"Success! Continuing with offset \"{response.Offset}\""); records.AddRange(response.Records.ToList()); offset = response.Offset; } else if (response.AirtableApiError != null) { errorMessage = response.AirtableApiError.ErrorMessage; break; } else { errorMessage = "Unknown error"; break; } } while (offset != null); } if (!string.IsNullOrEmpty(errorMessage)) { SdlAirTableException airTableException = new SdlAirTableException( errorMessage, SdlAirTableException.AirtableErrorType.CommunicationError); Logger.Error(airTableException); throw airTableException; } return(records.Any(x => ((JArray)x.Fields["Alpha Players"]).Any(y => y.Value <string>() == player.AirtableId) || ((JArray)x.Fields["Bravo Players"]).Any(y => y.Value <string>() == player.AirtableId))); }
private static async Task <AirtableRecord[]> GetAllPlayerRecords() { string offset = null; string errorMessage = null; List <AirtableRecord> records = new List <AirtableRecord>(); using (AirtableBase airtableBase = new AirtableBase(Globals.BotSettings.AppKey, Globals.BotSettings.BaseId)) { do { Logger.Info($"Retrieving data with offset {offset}."); Task <AirtableListRecordsResponse> task = airtableBase.ListRecords( "Draft Standings", offset, null, null, null, null ); AirtableListRecordsResponse response = await task; if (response.Success) { Logger.Info($"Success! Continuing with offset \"{response.Offset}\""); records.AddRange(response.Records.ToList()); offset = response.Offset; } else if (response.AirtableApiError != null) { errorMessage = response.AirtableApiError.ErrorMessage; break; } else { errorMessage = "Unknown error"; break; } } while (offset != null); } if (!string.IsNullOrEmpty(errorMessage)) { SdlAirTableException airTableException = new SdlAirTableException( errorMessage, SdlAirTableException.AirtableErrorType.CommunicationError); Logger.Error(airTableException); throw airTableException; } return(records.ToArray()); }
private static async Task <AirtableRecord> GetPlayerRecord(ulong discordId) { string offset = null; string errorMessage = null; List <AirtableRecord> records = new List <AirtableRecord>(); using (AirtableBase airtableBase = new AirtableBase(Globals.BotSettings.AppKey, Globals.BotSettings.BaseId)) { do { Logger.Info($"Retrieving data with offset {offset}."); Task <AirtableListRecordsResponse> task = airtableBase.ListRecords( "Draft Standings", offset, null, null, null, null ); AirtableListRecordsResponse response = await task; if (response.Success) { Logger.Info($"Success! Continuing with offset \"{response.Offset}\""); records.AddRange(response.Records.ToList()); offset = response.Offset; } else if (response.AirtableApiError != null) { errorMessage = response.AirtableApiError.ErrorMessage; break; } else { errorMessage = "Unknown error"; break; } } while (offset != null); } if (!string.IsNullOrEmpty(errorMessage)) { SdlAirTableException airTableException = new SdlAirTableException( errorMessage, SdlAirTableException.AirtableErrorType.CommunicationError); Logger.Error(airTableException); throw airTableException; } Logger.Info("Searching for needed player id."); try { records = records.Where(e => e.Fields["DiscordID"].ToString() == discordId.ToString()).ToList(); if (records.Count > 1) { SdlAirTableException dupeAirtableException = new SdlAirTableException( $"There are multiple records in the Draft Standings table with the id {discordId}!", SdlAirTableException.AirtableErrorType.UnexpectedDuplicate); Logger.Error(dupeAirtableException); throw dupeAirtableException; } if (records.Count == 0) { SdlAirTableException noneAirTableException = new SdlAirTableException( $"There are no players registered with the discord id {discordId}!", SdlAirTableException.AirtableErrorType.NotFound); Logger.Warn(noneAirTableException); throw noneAirTableException; } return(records.First()); } catch (Exception e) { if (!(e is SdlAirTableException sdlAirTableException)) { sdlAirTableException = new SdlAirTableException( e.Message, SdlAirTableException.AirtableErrorType.Generic); } Logger.Error(sdlAirTableException); throw sdlAirTableException; } }
public static async Task <SdlPlayer> RetrieveSdlPlayer(ulong discordId) { try { AirtableRecord playerRecord = await GetPlayerRecord(discordId); SdlPlayer sdlPlayer = new SdlPlayer(discordId) { AirtableName = playerRecord.Fields.ContainsKey("Name") ? playerRecord.Fields["Name"].ToString() : string.Empty, PowerLevel = Convert.ToDouble(playerRecord.Fields["Power"].ToString()), SwitchFriendCode = playerRecord.Fields.ContainsKey("Friend Code") ? playerRecord.Fields["Friend Code"].ToString() : string.Empty, AirtableId = playerRecord.Id, Role = playerRecord.Fields.ContainsKey("Role") ? playerRecord.Fields["Role"].ToString() : string.Empty }; try { if (playerRecord.Fields.ContainsKey("W%")) { sdlPlayer.OverallWinRate = Convert.ToDouble(playerRecord.Fields["W%"]); } else { sdlPlayer.OverallWinRate = -1; } } catch (Exception e) { Logger.Warn(e); } try { if (playerRecord.Fields.ContainsKey("SZ W%")) { sdlPlayer.WinRates[GameMode.SplatZones] = Convert.ToDouble(playerRecord.Fields["SZ W%"]); } } catch (Exception e) { Logger.Warn(e); } try { if (playerRecord.Fields.ContainsKey("TC W%")) { sdlPlayer.WinRates[GameMode.TowerControl] = Convert.ToDouble(playerRecord.Fields["TC W%"]); } } catch (Exception e) { Logger.Warn(e); } try { if (playerRecord.Fields.ContainsKey("RM W%")) { sdlPlayer.WinRates[GameMode.Rainmaker] = Convert.ToDouble(playerRecord.Fields["RM W%"]); } } catch (Exception e) { Logger.Warn(e); } try { if (playerRecord.Fields.ContainsKey("CB W%")) { sdlPlayer.WinRates[GameMode.ClamBlitz] = Convert.ToDouble(playerRecord.Fields["CB W%"]); } } catch (Exception e) { Logger.Warn(e); } return(sdlPlayer); } catch (Exception e) { SdlAirTableException caughtAirTableException = new SdlAirTableException( e.Message, SdlAirTableException.AirtableErrorType.Generic); Logger.Error(caughtAirTableException); throw caughtAirTableException; } }
public static async Task PenalizePlayer(ulong discordId, int points, string notes) { AirtableRecord playerRecord = await GetPlayerRecord(discordId); using (AirtableBase airtableBase = new AirtableBase(Globals.BotSettings.AppKey, Globals.BotSettings.BaseId)) { Fields adjustmentsFields = new Fields(); adjustmentsFields.AddField("Player", playerRecord.Id); adjustmentsFields.AddField("Points", -points); adjustmentsFields.AddField("Notes", notes); Task <AirtableCreateUpdateReplaceRecordResponse> createRecordTask = airtableBase.CreateRecord("Adjustments", adjustmentsFields, true); AirtableCreateUpdateReplaceRecordResponse createRecordResponse = await createRecordTask; if (!createRecordResponse.Success) { string errorMessage = createRecordResponse.AirtableApiError != null ? createRecordResponse.AirtableApiError.ErrorMessage : "Unknown error"; SdlAirTableException exception = new SdlAirTableException( errorMessage, SdlAirTableException.AirtableErrorType.CommunicationError); Logger.Error(exception); throw exception; } AirtableRecord record = createRecordResponse.Record; if (!playerRecord.Fields.ContainsKey("Adjustments")) { playerRecord.Fields["Adjustments"] = new JArray(); } IEnumerable <JToken> updatedAdjustmentIds = ((JArray)playerRecord.Fields["Adjustments"]).Append(record.Id); Fields updatePlayerFields = new Fields(); updatePlayerFields.AddField("Adjustments", updatedAdjustmentIds.ToArray()); Task <AirtableCreateUpdateReplaceRecordResponse> updateRecordTask = airtableBase.UpdateRecord("Draft Standings", updatePlayerFields, playerRecord.Id, true); AirtableCreateUpdateReplaceRecordResponse updateRecordResponse = await updateRecordTask; if (!updateRecordResponse.Success) { string errorMessage = updateRecordResponse.AirtableApiError != null ? updateRecordResponse.AirtableApiError.ErrorMessage : "Unknown error"; SdlAirTableException exception = new SdlAirTableException( errorMessage, SdlAirTableException.AirtableErrorType.CommunicationError); Logger.Error(exception); throw exception; } } }
public static async Task <Stage[]> GetMapList() { string offset = null; string errorMessage = null; List <AirtableRecord> records = new List <AirtableRecord>(); using (AirtableBase airtableBase = new AirtableBase(Globals.BotSettings.AppKey, Globals.BotSettings.BaseId)) { do { Logger.Info($"Retrieving data with offset {offset}."); Task <AirtableListRecordsResponse> task = airtableBase.ListRecords( "Map List", offset, null, null, null, null ); AirtableListRecordsResponse response = await task; if (response.Success) { Logger.Info($"Success! Continuing with offset \"{response.Offset}\""); records.AddRange(response.Records.ToList()); offset = response.Offset; } else if (response.AirtableApiError != null) { errorMessage = response.AirtableApiError.ErrorMessage; break; } else { errorMessage = "Unknown error"; break; } } while (offset != null); } if (!string.IsNullOrEmpty(errorMessage)) { SdlAirTableException airTableException = new SdlAirTableException( errorMessage, SdlAirTableException.AirtableErrorType.CommunicationError); Logger.Error(airTableException); throw airTableException; } List <Stage> resultStages = new List <Stage>(); foreach (AirtableRecord airtableRecord in records) { string mapInfo = airtableRecord.Fields["Name"].ToString(); string mapName = mapInfo.Substring(0, mapInfo.Length - 3); string modeInfo = mapInfo.Substring(mapInfo.Length - 2); Stage currentStage = new Stage { MapName = mapName, Mode = Stage.GetModeFromAcronym(modeInfo) }; resultStages.Add(currentStage); } return(resultStages.ToArray()); }