Exemplo n.º 1
0
        public static async Task <CreateNeutralsReply> CreateNeutralsAsync(CreateNeutralsRequest request, ServerCallContext context)
        {
            using var db = new LegendaryDatabase();
            var connector = DbConnector.Create(db.Connection, new DbConnectorSettings {
                AutoOpen = true, LazyOpen = true
            });

            var reply = new CreateNeutralsReply {
                Status = new Status {
                    Code = 200
                }
            };

            List <int> newNeutralIds = new List <int>();

            foreach (var neutral in request.Neutrals)
            {
                // Validate the GamePackageId
                var packageRequest = new GetGamePackagesRequest();
                packageRequest.GamePackageIds.Add(neutral.GamePackageId);
                packageRequest.Fields.Add(GamePackageField.Id);
                var packageReply = await GamePackageUtility.GetGamePackagesAsync(packageRequest, context);

                if (packageReply.Status.Code != 200)
                {
                    reply.Status = packageReply.Status;
                    return(reply);
                }

                // Verify that this neutral doesn't already exist
                var neutralRequest = new GetNeutralsRequest();
                neutralRequest.Name = neutral.Name;
                neutralRequest.Fields.AddRange(new[] { NeutralField.NeutralId, NeutralField.NeutralName, NeutralField.NeutralGamePackageId });
                neutralRequest.NameMatchStyle = NameMatchStyle.MixedCase;
                var neutralReply = await GetNeutralsAsync(neutralRequest, context);

                if (neutralReply.Status.Code == 200 && neutralReply.Neutrals.Any(x => x.GamePackageId == neutral.GamePackageId))
                {
                    var matchingNeutral = neutralReply.Neutrals.First();
                    reply.Status = new Status {
                        Code = 400, Message = $"Neutral {matchingNeutral.Id} with name '{matchingNeutral.Name}' was found in game package '{matchingNeutral.GamePackageId}'"
                    };
                    return(reply);
                }

                // Create the neutral
                var newNeutralId = ((int)(await connector.Command($@"
					insert
						into {DatabaseDefinition.DefaultTableName}
							({DatabaseDefinition.ColumnName[NeutralField.NeutralName]})
									values (@NeutralName);
								select last_insert_id();"                                ,
                                                                  ("NeutralName", neutral.Name))
                                          .QuerySingleAsync <ulong>()));

                // Add to game package
                await connector.Command(
                    $@"
						insert
							into {TableNames.GamePackageNeutrals}
								({DatabaseDefinition.ColumnName[NeutralField.NeutralId]}, {GamePackageUtility.DatabaseDefinition.ColumnName[GamePackageField.Id]})
							values (@NeutralId, @GamePackageId);"                            ,
                    ("NeutralId", newNeutralId),
                    ("GamePackageId", neutral.GamePackageId))
                .ExecuteAsync();

                newNeutralIds.Add(newNeutralId);
            }

            // Get all of the created neutrals
            var finalRequest = new GetNeutralsRequest();

            finalRequest.NeutralIds.AddRange(newNeutralIds);
            var finalReply = await GetNeutralsAsync(finalRequest, context);

            reply.Status = finalReply.Status;
            reply.Neutrals.AddRange(finalReply.Neutrals);

            return(reply);
        }
Exemplo n.º 2
0
        public static async Task <CreateSchemesReply> CreateSchemesAsync(CreateSchemesRequest request, ServerCallContext context)
        {
            using var db = new LegendaryDatabase();
            var connector = DbConnector.Create(db.Connection, new DbConnectorSettings {
                AutoOpen = true, LazyOpen = true
            });

            var reply = new CreateSchemesReply {
                Status = new Status {
                    Code = 200
                }
            };

            List <int> newSchemeIds = new List <int>();

            var cardSetTypeInfo = await CardRequirementUtility.GetInsertCardTypeValue(CardSetType.CardSetScheme, connector);

            foreach (var scheme in request.Schemes)
            {
                // Validate the GamePackageId
                var packageRequest = new GetGamePackagesRequest();
                packageRequest.GamePackageIds.Add(scheme.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(scheme.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 scheme doesn't already exist
                var schemeRequest = new GetSchemesRequest();
                schemeRequest.Name = scheme.Name;
                schemeRequest.Fields.AddRange(new[] { SchemeField.SchemeId, SchemeField.SchemeName, SchemeField.SchemeGamePackageId });
                schemeRequest.NameMatchStyle = NameMatchStyle.MixedCase;
                var schemeReply = await GetSchemesAsync(schemeRequest, context);

                if (schemeReply.Status.Code == 200 && schemeReply.Schemes.Any())
                {
                    var matchingScheme = schemeReply.Schemes.First();
                    reply.Status = new Status {
                        Code = 400, Message = $"Scheme {matchingScheme.Id} with name '{matchingScheme.Name}' was found in game package '{matchingScheme.GamePackageId}'"
                    };
                    return(reply);
                }

                // Create the scheme
                var newSchemeId = ((int)(await connector.Command($@"
					insert
						into {DatabaseDefinition.DefaultTableName}
							({DatabaseDefinition.ColumnName[SchemeField.SchemeName]}, {DatabaseDefinition.ColumnName[SchemeField.SchemeEpicInformation]})
									values (@SchemeName, @HasEpicSide);
								select last_insert_id();"                                ,
                                                                 ("SchemeName", scheme.Name),
                                                                 ("HasEpicSide", scheme.HasEpicSide))
                                         .QuerySingleAsync <ulong>()));

                // Add to game package
                await connector.Command(
                    $@"
						insert
							into {TableNames.GamePackageSchemes}
								({DatabaseDefinition.ColumnName[SchemeField.SchemeId]}, {GamePackageUtility.DatabaseDefinition.ColumnName[GamePackageField.Id]})
							values (@SchemeId, @GamePackageId);"                            ,
                    ("SchemeId", newSchemeId),
                    ("GamePackageId", scheme.GamePackageId))
                .ExecuteAsync();

                // Link abilities
                foreach (var abilityId in scheme.AbilityIds)
                {
                    await connector.Command(
                        $@"
							insert
								into {TableNames.SchemeAbilities}
									({DatabaseDefinition.ColumnName[SchemeField.SchemeId]}, {AbilityUtility.DatabaseDefinition.ColumnName[AbilityField.Id]})
								values (@SchemeId, @AbilityId);"                                ,
                        ("SchemeId", newSchemeId),
                        ("AbilityId", abilityId))
                    .ExecuteAsync();
                }

                // Add card requirements
                foreach (var requirement in scheme.CardRequirements)
                {
                    var cardRequirementId = await CardRequirementUtility.AddCardRequirement(requirement);

                    await connector.Command(
                        $@"
						insert
							into {TableNames.MatchedCardRequirements}
								({CardRequirementUtility.DatabaseDefinition.ColumnName[CardRequirement.OwnerIdFieldNumber]}, {CardRequirementUtility.DatabaseDefinition.ColumnName[CardRequirement.CardRequirementIdFieldNumber]}, NumberOfPlayers, {cardSetTypeInfo.Name})
							values (@OwnerId, @CardRequirementId, @NumberOfPlayers, @{cardSetTypeInfo.Name});"                            ,
                        ("OwnerId", newSchemeId),
                        ("CardRequirementId", cardRequirementId),
                        ("NumberOfPlayers", requirement.PlayerCount),
                        (cardSetTypeInfo.Name, cardSetTypeInfo.Value))
                    .ExecuteAsync();
                }

                // Add twist requirements
                foreach (var twistRequirement in scheme.TwistRequirements)
                {
                    var twistRequirementId = await SchemeTwistRequirementUtility.AddSchemeTwistRequirementAsync(twistRequirement);

                    await connector.Command(
                        $@"
						insert
							into {TableNames.SchemeTwistRequirements}
								(SchemeId, TwistRequirementId, NumberOfPlayers)
							values (@SchemeId, @TwistRequirementId, @NumberOfPlayers);"                            ,
                        ("SchemeId", newSchemeId),
                        ("TwistRequirementId", twistRequirementId),
                        ("NumberOfPlayers", twistRequirement.PlayerCount))
                    .ExecuteAsync();
                }

                newSchemeIds.Add(newSchemeId);
            }

            // Get all of the created schemes
            var finalRequest = new GetSchemesRequest();

            finalRequest.SchemeIds.AddRange(newSchemeIds);
            var finalReply = await GetSchemesAsync(finalRequest, context);

            reply.Status = finalReply.Status;
            reply.Schemes.AddRange(finalReply.Schemes);

            return(reply);
        }
Exemplo n.º 3
0
        public static async Task <CreateHenchmenReply> CreateHenchmenAsync(CreateHenchmenRequest request, ServerCallContext context)
        {
            using var db = new LegendaryDatabase();
            var connector = DbConnector.Create(db.Connection, new DbConnectorSettings {
                AutoOpen = true, LazyOpen = true
            });

            var reply = new CreateHenchmenReply {
                Status = new Status {
                    Code = 200
                }
            };

            List <int> newHenchmenIds = new List <int>();

            foreach (var henchman in request.Henchmen)
            {
                // Validate the GamePackageId
                var packageRequest = new GetGamePackagesRequest();
                packageRequest.GamePackageIds.Add(henchman.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(henchman.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 henchman doesn't already exist
                var henchmanRequest = new GetHenchmenRequest();
                henchmanRequest.Name = henchman.Name;
                henchmanRequest.Fields.AddRange(new[] { HenchmanField.HenchmanId, HenchmanField.HenchmanName, HenchmanField.HenchmanGamePackageId });
                henchmanRequest.NameMatchStyle = NameMatchStyle.MixedCase;
                var henchmanReply = await GetHenchmenAsync(henchmanRequest, context);

                if (henchmanReply.Status.Code == 200 && henchmanReply.Henchmen.Any())
                {
                    var matchingHenchman = henchmanReply.Henchmen.First();
                    reply.Status = new Status {
                        Code = 400, Message = $"Henchman {matchingHenchman.Id} with name '{matchingHenchman.Name}' was found in game package '{matchingHenchman.GamePackageId}'"
                    };
                    return(reply);
                }

                // Create the henchman
                var newHenchmanId = ((int)(await connector.Command($@"
					insert
						into {DatabaseDefinition.DefaultTableName}
							({DatabaseDefinition.ColumnName[HenchmanField.HenchmanName]})
									values (@HenchmanName);
								select last_insert_id();"                                ,
                                                                   ("HenchmanName", henchman.Name))
                                           .QuerySingleAsync <ulong>()));

                // Add to game package
                await connector.Command(
                    $@"
						insert
							into {TableNames.GamePackageHenchmen}
								({DatabaseDefinition.ColumnName[HenchmanField.HenchmanId]}, {GamePackageUtility.DatabaseDefinition.ColumnName[GamePackageField.Id]})
							values (@HenchmanId, @GamePackageId);"                            ,
                    ("HenchmanId", newHenchmanId),
                    ("GamePackageId", henchman.GamePackageId))
                .ExecuteAsync();

                // Link abilities
                foreach (var abilityId in henchman.AbilityIds)
                {
                    await connector.Command(
                        $@"
							insert
								into {TableNames.HenchmanAbilities}
									({DatabaseDefinition.ColumnName[HenchmanField.HenchmanId]}, {AbilityUtility.DatabaseDefinition.ColumnName[AbilityField.Id]})
								values (@HenchmanId, @AbilityId);"                                ,
                        ("HenchmanId", newHenchmanId),
                        ("AbilityId", abilityId))
                    .ExecuteAsync();
                }

                newHenchmenIds.Add(newHenchmanId);
            }

            // Get all of the created henchmen
            var finalRequest = new GetHenchmenRequest();

            finalRequest.HenchmanIds.AddRange(newHenchmenIds);
            var finalReply = await GetHenchmenAsync(finalRequest, context);

            reply.Status = finalReply.Status;
            reply.Henchmen.AddRange(finalReply.Henchmen);

            return(reply);
        }
Exemplo n.º 4
0
        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);
        }