public async Task <int> Run() { using (var db = LiteDbFactory.New()) { var configCollection = db.GetCollection <Config>(); var config = configCollection.FindAll().SingleOrDefault() ?? new Config(); options.ApplyToConfig(config); if (!config.LeagueId.HasValue) { Console.Error.WriteLine("Error: League ID is not set"); return(1); } Console.WriteLine("Searching for players..."); var client = new YahooClient(config); var players = await client.SearchPlayers(config.LeagueId.Value, playerNameOrId); players.WriteTable(); } return(0); }
public Task <int> Run() { Console.WriteLine("Storing configuration values..."); using (var db = LiteDbFactory.New()) { var configCollection = db.GetCollection <Config>(); var config = configCollection.FindAll().SingleOrDefault() ?? new Config(); options.ApplyToConfig(config); if (!string.IsNullOrEmpty(clientId)) { config.ClientId = clientId; } if (!string.IsNullOrEmpty(clientSecret)) { config.ClientSecret = clientSecret; } configCollection.Upsert(config); } Console.WriteLine("Configuration values stored successfully"); return(Task.FromResult(0)); }
public async Task <int> Run() { using (var db = LiteDbFactory.New()) { var configCollection = db.GetCollection <Config>(); var config = configCollection.FindAll().SingleOrDefault() ?? new Config(); options.ApplyToConfig(config); if (string.IsNullOrWhiteSpace(config.ClientId)) { Console.Error.WriteLine("Error: Client ID is not set"); } if (string.IsNullOrWhiteSpace(config.ClientSecret)) { Console.Error.WriteLine("Error: Client secret is not set"); } if (string.IsNullOrWhiteSpace(config.RefreshToken)) { Console.Error.WriteLine("Error: Refresh token is not set"); } if (!config.LeagueId.HasValue) { Console.Error.WriteLine("Error: League ID is not set"); } if (!config.TeamId.HasValue) { Console.Error.WriteLine("Error: Team ID is not set"); } Console.WriteLine("Verifying connection to Yahoo!..."); var client = new YahooClient(config); await client.GetGameId(); Console.WriteLine("Successfully connected to Yahoo!"); var league = await client.GetLeague(config.LeagueId.GetValueOrDefault()); Console.WriteLine($"Successfully found league {league.Name} (#{config.LeagueId})"); var team = await client.GetTeam(config.LeagueId.GetValueOrDefault(), config.TeamId.GetValueOrDefault()); if (team.IsOwnedByCurrentLogin) { Console.WriteLine($"Successfully found team {team.Name} (#{config.TeamId})"); } else { Console.Error.WriteLine($"Error: Team {team.Name} (#{config.TeamId}) is not owned by current user"); } } return(0); }
public async Task <int> Run() { using (var db = LiteDbFactory.New()) { var configCollection = db.GetCollection <Config>(); var config = configCollection.FindAll().SingleOrDefault() ?? new Config(); options.ApplyToConfig(config); if (!config.LeagueId.HasValue) { Console.Error.WriteLine("Error: League ID is not set"); return(1); } if (!config.TeamId.HasValue) { Console.Error.WriteLine("Error: Team ID is not set"); return(1); } var client = new YahooClient(config); var addPlayers = await client.SearchPlayers(config.LeagueId.Value, addNameOrId); var dropPlayers = await client.SearchPlayers(config.LeagueId.Value, dropNameOrId); if (VerifySinglePlayer(addPlayers, "add") && VerifySinglePlayer(dropPlayers, "drop")) { var claimsCollection = db.GetCollection <Claim>(); var existingClaims = claimsCollection.FindAll(); var claim = new Claim { Add = new Player(addPlayers.Single()), Drop = new Player(dropPlayers.Single()), Priority = existingClaims .OrderByDescending(ec => ec.Priority) .Select(ec => ec.Priority) .FirstOrDefault() + 1 }; claimsCollection.Insert(claim); Console.WriteLine("Claim successfully added"); claimsCollection.FindAll().ToArray().WriteTable(); return(0); } else { return(1); } } }
public Task <int> Run() { using (var db = LiteDbFactory.New()) { var claimCollection = db.GetCollection <Claim>(); claimCollection.FindAll().ToArray().WriteTable(); } return(Task.FromResult(0)); }
private int RunSynchronous() { if (!claimId.HasValue || !skip.HasValue) { return(1); } using (var db = LiteDbFactory.New()) { var claimCollection = db.GetCollection <Claim>(); var claims = claimCollection.FindAll().ToDictionary(c => c.Id); var claimPriorities = claims.Values.ToDictionary(c => c.Id, c => (double)c.Priority); if (!claimPriorities.ContainsKey(claimId.Value)) { Console.Error.WriteLine($"Error: Claim {claimId} could not be found"); return(1); } // Move the specified claim to the proper priority location claimPriorities[claimId.Value] += skip.Value + Math.Sign(skip.Value) * 0.5; // Reprioritize claims to have integer priorities var orderedClaims = claimPriorities .OrderBy(kv => kv.Value) .Select(kv => claims[kv.Key]) .ToArray(); for (var i = 0; i < orderedClaims.Length; i++) { orderedClaims[i].Priority = i + 1; } claimCollection.Update(orderedClaims); orderedClaims.WriteTable(); } return(0); }
private int RunSynchronous() { if (claimIds.Any(id => !id.HasValue)) { return(1); } if (all && claimIds.Any()) { Console.Error.WriteLine("Error: Either specific claim IDs or all claims can be deleted, but not both"); return(1); } using (var db = LiteDbFactory.New()) { var claimCollection = db.GetCollection <Claim>(); if (all) { claimCollection.Delete(c => true); } else { var claimsToDelete = new List <Claim>(); foreach (var claimId in claimIds) { if (!claimCollection.Delete(claimId.Value)) { Console.Error.WriteLine($"Warning: Claim {claimId} could not be found"); } } } claimCollection.FindAll().ToArray().WriteTable(); } return(0); }
public async Task <int> Run() { using (var db = LiteDbFactory.New()) { var configCollection = db.GetCollection <Config>(); var config = configCollection.FindAll().SingleOrDefault() ?? new Config(); options.ApplyToConfig(config); if (!config.LeagueId.HasValue) { Console.Error.WriteLine("Error: League ID is not set"); return(1); } if (!config.TeamId.HasValue) { Console.Error.WriteLine("Error: Team ID is not set"); return(1); } var client = new YahooClient(config); var claimCollection = db.GetCollection <Claim>(); var claims = claimCollection.FindAll().OrderBy(c => c.Priority).ToList(); string claimsText(int claimCount) => claimCount.ToString("G") + (claimCount == 1 ? " claim" : " claims"); WriteMessage($"Attempting to process {claimsText(claims.Count)}..."); while (claims.Any()) { foreach (var claim in claims) { // Check if the added player is a free agent var potentialAddedPlayers = await client.SearchPlayersByName( config.LeagueId.Value, claim.Add.FullName, PlayerStatus.FreeAgent); var addedPlayer = potentialAddedPlayers .SingleOrDefault(p => p.PlayerId == claim.Add.PlayerId); if (addedPlayer != null) { WriteMessage($"{claim.Add.Description} is a free-agent. Submitting claim..."); try { await client.SubmitClaim( config.LeagueId.Value, config.TeamId.Value, claim.Add.PlayerId, claim.Drop.PlayerId); WriteMessage($"{claim.Add.Description} successfully added! {claim.Drop.Description} dropped"); } catch (ClientException ce) { WriteMessage($"Error: {claim.Add.Description} could not be added, dropping {claim.Drop.Description}", Console.Error); WriteMessage($"Error: {ce.Message}", Console.Error); } // Whether the claim succeeded or failed, delete it claimCollection.Delete(claim.Id); } else { WriteMessage($"{claim.Add.Description} is not (yet) a free-agent", verbose: true); } } // Reload the claims collection, to pick up new claims and eliminate deleted ones claims = claimCollection.FindAll().OrderBy(c => c.Priority).ToList(); // If outstanding claims remain, try again after a delay if (claims.Any()) { WriteMessage($"{claimsText(claims.Count)} still pending. Sleeping for {wait:G} seconds...", verbose: true); await Task.Delay(wait * 1000); WriteMessage($"Attempting to process {claimsText(claims.Count)}...", verbose: true); } } WriteMessage("All pending claims were processed (or failed). Exiting..."); } return(0); }