public static async Task CreateAllyAsync(GameServiceClient client) { var ally = new Ally(); ally.Name = ConsoleUtility.GetUserInput("Ally Name: "); ally.GamePackageId = await GamePackageUtility.SelectGamePackageId(client); ally.TeamId = await TeamUtility.SelectTeamId(client); ally.AbilityIds.AddRange(await AbilityUtility.SelectAbilityIds(client)); ally.Classes.AddRange(await ClassUtility.SelectClassIds(client)); if (!ConsoleUtility.ShouldContinue($"Creating Ally: '{ally.Name}', in gamePackage '{ally.GamePackageId}' on team '{ally.TeamId}' with abilities [{ally.AbilityIds.Select(x => x.ToString()).Join(", ")}]")) { await CreateAllyAsync(client); return; } var createRequest = new CreateAlliesRequest(); createRequest.Allies.Add(ally); var createReply = await client.CreateAlliesAsync(createRequest); if (createReply.Status.Code != 200) { ConsoleUtility.WriteLine($"Failed to create ally: {createReply.Status.Message}"); } else { ConsoleUtility.WriteLine($"Ally '{createReply.Allies.First().Name}' was created with Id '{createReply.Allies.First().Id}'"); } }
private static async ValueTask <IReadOnlyList <Ally> > CreateAllies(GameServiceClient client, IReadOnlyList <GamePackage> packages, IReadOnlyList <Ability> abilities, IReadOnlyList <Team> teams, IReadOnlyList <Class> classes) { ConsoleUtility.WriteLine("Creating allies"); List <Ally> result = (await AllyUtility.GetAlliesAsync(client, null)).ToList(); if (result.Any()) { return(result); } var noTeam = teams.First(x => x.Name == "None"); foreach (var file in Directory.EnumerateFiles(@"C:\Users\Ryan\SkyDrive\code\LegendaryGameStarter\LegendaryGameModel2\GameSets", s_fileMask)) { var doc = XDocument.Load(file); var name = doc.Element("Set").Attribute("Name").Value; var activeGamePackage = packages.FirstOrDefault(x => x.Name == name); if (activeGamePackage == null) { ConsoleUtility.WriteLine($"Failed to find matching game package for {file}"); } foreach (var allyElement in doc.Element("Set").Element("Cards").Elements("Card").Where(x => x?.Attribute("Area").Value == "Ally")) { var request = new CreateAlliesRequest(); request.CreateOptions.Add(CreateOptions.ErrorOnDuplicates); var ally = new Ally(); ally.Name = allyElement.Attribute("Name").Value; ally.GamePackageId = activeGamePackage.Id; ally.AbilityIds.AddRange(GetMatchingItems(allyElement.Attribute("Abilities")?.Value, name => abilities.First(x => x.Name == name)).Select(x => x.Id)); ally.TeamId = GetMatchingItems(allyElement.Attribute("Team")?.Value, name => teams.FirstOrDefault(x => x.Name == name, noTeam)).First().Id; ally.Classes.AddRange(GetMatchingItems(allyElement.Attribute("Classes")?.Value, name => ParseClassInfo(classes, name))); request.Allies.Add(ally); var reply = await client.CreateAlliesAsync(request); if (reply.Status.Code != 200) { ConsoleUtility.WriteLine($"Failed to create '{ally.Name}': {reply.Status.Message}"); } result.AddRange(reply.Allies); } ConsoleUtility.WriteLine($"Completed: {name}"); } return(result); }
public static async Task <CreateAlliesReply> CreateAlliesAsync(CreateAlliesRequest request, ServerCallContext context) { using var db = new LegendaryDatabase(); var connector = DbConnector.Create(db.Connection, new DbConnectorSettings { AutoOpen = true, LazyOpen = true }); var reply = new CreateAlliesReply { Status = new Status { Code = 200 } }; List <int> newAllyIds = new List <int>(); foreach (var ally in request.Allies) { // Validate the GamePackageId var packageRequest = new GetGamePackagesRequest(); packageRequest.GamePackageIds.Add(ally.GamePackageId); packageRequest.Fields.Add(GamePackageField.Id); var packageReply = await GamePackageUtility.GetGamePackagesAsync(packageRequest, context); if (packageReply.Status.Code != 200) { reply.Status = packageReply.Status; return(reply); } // Validate the AbilityIds var abilitiesRequest = new GetAbilitiesRequest(); abilitiesRequest.AbilityIds.AddRange(ally.AbilityIds); abilitiesRequest.AbilityFields.Add(AbilityField.Id); var abilitiesReply = await AbilityUtility.GetAbilitiesAsync(abilitiesRequest, context); if (abilitiesReply.Status.Code != 200) { reply.Status = abilitiesReply.Status; return(reply); } // Verify that this ally doesn't already exist var allyRequest = new GetAlliesRequest(); allyRequest.Name = ally.Name; allyRequest.Fields.AddRange(new[] { AllyField.AllyId, AllyField.AllyName, AllyField.AllyGamePackageId }); allyRequest.NameMatchStyle = NameMatchStyle.MixedCase; var allyReply = await GetAlliesAsync(allyRequest, context); if (allyReply.Status.Code == 200 && allyReply.Allies.Any()) { var matchingAlly = allyReply.Allies.First(); reply.Status = new Status { Code = 400, Message = $"Ally {matchingAlly.Id} with name '{matchingAlly.Name}' was found in game package '{matchingAlly.GamePackageId}'" }; return(reply); } // Verify that the class counts add up to at least 14 if (ally.Classes.Select(x => x.Count).Sum() < 14) { reply.Status = new Status { Code = 400, Message = $"Ally with name '{ally.Name}' must be supplied with at least 14 class cards." }; return(reply); } // Verify that the classIds are valid var classesRequest = new GetClassesRequest(); classesRequest.ClassIds.AddRange(ally.Classes.Select(x => x.ClassId)); classesRequest.Fields.Add(ClassField.ClassId); var classesReply = await ClassUtility.GetClassesAsync(classesRequest, context); if (classesReply.Status.Code != 200) { reply.Status = classesReply.Status; return(reply); } // Create the ally var newAllyId = ((int)(await connector.Command($@" insert into {DatabaseDefinition.DefaultTableName} ({DatabaseDefinition.ColumnName[AllyField.AllyName]}, {DatabaseDefinition.ColumnName[AllyField.AllyTeamId]}) values (@AllyName, @AllyTeamId); select last_insert_id();" , ("AllyName", ally.Name), ("AllyTeamId", ally.TeamId)) .QuerySingleAsync <ulong>())); // Add to game package await connector.Command( $@" insert into {TableNames.GamePackageAllies} ({DatabaseDefinition.ColumnName[AllyField.AllyId]}, {GamePackageUtility.DatabaseDefinition.ColumnName[GamePackageField.Id]}) values (@AllyId, @GamePackageId);" , ("AllyId", newAllyId), ("GamePackageId", ally.GamePackageId)) .ExecuteAsync(); // Link abilities foreach (var abilityId in ally.AbilityIds) { await connector.Command( $@" insert into {TableNames.AllyAbilities} ({DatabaseDefinition.ColumnName[AllyField.AllyId]}, {AbilityUtility.DatabaseDefinition.ColumnName[AbilityField.Id]}) values (@AllyId, @AbilityId);" , ("AllyId", newAllyId), ("AbilityId", abilityId)) .ExecuteAsync(); } // Add class info foreach (var classInfo in ally.Classes) { await connector.Command( $@" insert into {TableNames.AllyClasses} ({DatabaseDefinition.ColumnName[AllyField.AllyId]}, {ClassUtility.DatabaseDefinition.ColumnName[ClassField.ClassId]}, CardCount) values (@AllyId, @ClassId, @CardCount);" , ("AllyId", newAllyId), ("ClassId", classInfo.ClassId), ("CardCount", classInfo.Count)) .ExecuteAsync(); } newAllyIds.Add(newAllyId); } // Get all of the created ally var finalRequest = new GetAlliesRequest(); finalRequest.AllyIds.AddRange(newAllyIds); var finalReply = await GetAlliesAsync(finalRequest, context); reply.Status = finalReply.Status; reply.Allies.AddRange(finalReply.Allies); return(reply); }
public override async Task <CreateAlliesReply> CreateAllies(CreateAlliesRequest request, ServerCallContext context) { return(await AllyUtility.CreateAlliesAsync(request, context)); }