Ejemplo n.º 1
0
        public ISeedData GenerateSeed(IDictionary <string, string> options, string seed, CancellationToken cancellationToken)
        {
            int randoSeed;

            if (string.IsNullOrEmpty(seed))
            {
                randoSeed = System.Security.Cryptography.RandomNumberGenerator.GetInt32(0, int.MaxValue);
                seed      = randoSeed.ToString();
            }
            else
            {
                randoSeed = int.Parse(seed);

                /* The Random ctor takes the absolute value of a negative seed.
                 * This is an non-obvious behavior so we treat a negative value
                 * as out of range. */
                if (randoSeed < 0)
                {
                    throw new ArgumentOutOfRangeException("Expected the seed option value to be an integer value in the range [0, 2147483647]");
                }
            }

            var rnd    = new Random(randoSeed);
            var config = new Config(options);

            if (config.Race)
            {
                rnd = new Random(rnd.Next());
            }

            int players = options.ContainsKey("players") ? int.Parse(options["players"]) : 1;
            var worlds  = new List <World>();

            if (config.GameMode == GameMode.Normal || players == 1)
            {
                worlds.Add(new World(config, "Player", 0, new HexGuid()));
            }
            else
            {
                for (int p = 0; p < players; p++)
                {
                    var found = options.TryGetValue($"player-{p}", out var player);
                    if (!found)
                    {
                        throw new ArgumentException($"No name provided for player {p + 1}");
                    }
                    if (!legalCharacters.IsMatch(player))
                    {
                        throw new ArgumentException($"No alphanumeric characters found in name for player {p + 1}");
                    }
                    player = CleanPlayerName(player);
                    worlds.Add(new World(config, player, p, new HexGuid()));
                }
            }

            var filler = new Filler(worlds, config, rnd, cancellationToken);

            filler.Fill();

            var playthrough = new Playthrough(worlds, config);
            var spheres     = playthrough.Generate();

            var seedData = new SeedData {
                Guid        = new HexGuid(),
                Seed        = seed,
                Game        = Name,
                Logic       = config.Logic.ToLString(),
                Playthrough = config.Race ? new List <Dictionary <string, string> >() : spheres,
                Mode        = config.GameMode.ToLString(),
                Worlds      = new List <IWorldData>()
            };

            int patchSeed = rnd.Next();

            foreach (var world in worlds)
            {
                var patchRnd  = new Random(patchSeed);
                var patch     = new Patch(world, worlds, seedData.Guid, config.Race ? 0 : randoSeed, patchRnd);
                var worldData = new WorldData {
                    Id        = world.Id,
                    Guid      = world.Guid,
                    Player    = world.Player,
                    Patches   = patch.Create(),
                    Locations = world.Locations.Select(l => new LocationData()
                    {
                        LocationId = l.Id, ItemId = (int)l.Item.Type, ItemWorldId = l.Item.World.Id
                    }).ToList <ILocationData>()
                };

                seedData.Worlds.Add(worldData);
            }

            return(seedData);
        }
Ejemplo n.º 2
0
        public ISeedData GenerateSeed(IDictionary <string, string> options, string seed)
        {
            if (seed == "")
            {
                seed = System.Security.Cryptography.RandomNumberGenerator.GetInt32(0, int.MaxValue).ToString();
            }
            else
            {
                seed = seed.ToCharArray().Sum(x => x).ToString();
            }

            var rnd    = new Random(int.Parse(seed));
            var config = new Config(options);

            int players = options.ContainsKey("players") ? int.Parse(options["players"]) : 1;
            var worlds  = new List <World>();

            if (config.GameMode == GameMode.Normal || players == 1)
            {
                worlds.Add(new World(config, "Player", 0));
            }
            else
            {
                for (int p = 0; p < players; p++)
                {
                    worlds.Add(new World(config, options[$"player-{p}"], p));
                }
            }

            var guid = Guid.NewGuid().ToString();

            var filler = new Filler(worlds, config, rnd);

            filler.Fill();

            var playthrough = new Playthrough(worlds);
            var spheres     = playthrough.Generate();

            var seedData = new SeedData {
                Guid        = guid.Replace("-", ""),
                Seed        = seed,
                Game        = Name,
                Logic       = config.Logic.ToLString(),
                Playthrough = spheres,
                Mode        = config.GameMode.ToLString(),
                Worlds      = new List <IWorldData>()
            };

            foreach (var world in worlds)
            {
                var patch     = new Patch(world, worlds, seedData.Guid);
                var worldData = new WorldData {
                    Id      = world.Id,
                    Guid    = world.Guid.Replace("-", ""),
                    Player  = world.Player,
                    Patches = patch.Create()
                };

                seedData.Worlds.Add(worldData);
            }

            return(seedData);
        }