Esempio n. 1
0
        public async Task <IActionResult> Generate(string randomizerId, [FromBody] Dictionary <string, string> options, CancellationToken cancellationToken)
        {
            if (options.Count < 1)
            {
                return(new StatusCodeResult(400));
            }

            try {
                /* Initialize the randomizer and generate a seed with the given options */
                IRandomizer randomizer = randomizers.FirstOrDefault(x => x.Id == randomizerId);

                if (randomizer == null)
                {
                    return(new StatusCodeResult(400));
                }

                var seedData = randomizer.GenerateSeed(options, options["seed"], cancellationToken);

                /* Store this seed to the database */
                var seed = new Seed {
                    GameName    = seedData.Game,
                    GameVersion = randomizer.Version.ToString(),
                    GameId      = randomizer.Id,
                    Guid        = seedData.Guid,
                    Players     = seedData.Worlds.Count,
                    SeedNumber  = seedData.Seed,
                    Spoiler     = JsonConvert.SerializeObject(seedData.Playthrough),
                    Hash        = GetSeedHashNames(seedData.Worlds[0].Patches, randomizer.Id),
                    Mode        = seedData.Mode,
                    Worlds      = new List <World>()
                };

                /* If race mode is enabled, blank out the seed number to not reveal it to the client */
                if (options.ContainsKey("race") && options["race"] == "true")
                {
                    seed.SeedNumber = "";
                    options["seed"] = "";
                }

                foreach (var seedWorld in seedData.Worlds)
                {
                    var world = new World {
                        WorldId   = seedWorld.Id,
                        Guid      = seedWorld.Guid,
                        Settings  = JsonConvert.SerializeObject(options),
                        Player    = seedWorld.Player,
                        Patch     = ConvertPatch(seedWorld.Patches),
                        Locations = seedWorld.Locations.Select(l => new Location()
                        {
                            LocationId = l.LocationId, ItemId = l.ItemId, ItemWorldId = l.ItemWorldId
                        }).ToList()
                    };
                    seed.Worlds.Add(world);
                }

                context.Add(seed);
                await context.SaveChangesAsync();

                /* If this is a multiworld seed, we also create a new multiworld session with the same session guid as the seed guid */
                if (seed.Players > 1)
                {
                    var session = new Session {
                        Clients = new List <Client>(),
                        Guid    = seed.Guid,
                        Seed    = seed,
                        State   = SessionState.Created
                    };

                    context.Add(session);
                    await context.SaveChangesAsync();
                }

                return(new OkObjectResult(seed));
            }
            catch {
                return(new StatusCodeResult(500));
            }
        }
        public async Task <IActionResult> Generate(string randomizerId, [FromBody] Dictionary <string, string> options)
        {
            if (options.Count < 1)
            {
                return(new StatusCodeResult(400));
            }

            try {
                /* Initialize the randomizer and generate a seed with the given options */
                IRandomizer randomizer = randomizers.FirstOrDefault(x => x.Id == randomizerId);

                if (randomizer == null)
                {
                    return(new StatusCodeResult(400));
                }

                var seedData = randomizer.GenerateSeed(options, options["seed"]);

                /* Store this seed to the database */
                var seed = new Seed {
                    GameName   = seedData.Game,
                    GameId     = randomizer.Id,
                    Guid       = seedData.Guid,
                    Players    = seedData.Worlds.Count,
                    SeedNumber = seedData.Seed,
                    Spoiler    = JsonConvert.SerializeObject(seedData.Playthrough),
                    Type       = seedData.Mode,
                    Worlds     = new List <World>()
                };

                foreach (var seedWorld in seedData.Worlds)
                {
                    var world = new World {
                        WorldId = seedWorld.Id,
                        Guid    = seedWorld.Guid,
                        Logic   = seedData.Logic,
                        Player  = seedWorld.Player,
                        Patch   = ConvertPatch(seedWorld.Patches)
                    };
                    seed.Worlds.Add(world);
                }

                context.Add(seed);
                await context.SaveChangesAsync();

                /* If this is a multiworld seed, we also create a new multiworld session with the same session guid as the seed guid */
                if (seed.Players > 1)
                {
                    var session = new Session {
                        Clients = new List <Client>(),
                        Guid    = seed.Guid,
                        Seed    = seed,
                        State   = SessionState.Created
                    };

                    context.Add(session);
                    await context.SaveChangesAsync();
                }

                return(new OkObjectResult(seed));
            }
            catch {
                return(new StatusCodeResult(500));
            }
        }