Example #1
0
        public async Task Initializes_loaded_worlds()
        {
            // Arrange
            var  world       = new DefaultWorld();
            bool worldLoaded = false;

            world.Loaded += (sender, args) => worldLoaded = true;
            world.AddTimeOfDayState(new TimeOfDayState());

            // Set up the world service to return our mocked World.
            var worldServiceMock = new Mock <IWorldService>();

            worldServiceMock
            .Setup(service => service.GetAllWorlds())
            .ReturnsAsync(new List <DefaultWorld> {
                world
            });

            var logService = this.container.Resolve <ILoggingService>();

            // Provide our custom service for this test instead of the shared
            // world service generated by the unit test set up code.
            var game = new DefaultGame(logService, worldServiceMock.Object);

            // Act
            await game.Initialize();

            // Assert
            Assert.IsTrue(worldLoaded);
        }
Example #2
0
        public async Task <IEnumerable <IWorld> > GetWorlds()
        {
            // Return our previously cached worlds.
            if (WorldRepository.worldCache.Count > 0)
            {
                return(new List <IWorld>(WorldRepository.worldCache));
            }

            IEnumerable <string> worldFiles = this.storageService.GetAllFilesByExtension(".world", "Worlds");
            var worlds = new List <IWorld>();

            foreach (string file in worldFiles)
            {
                IWorld world = new DefaultWorld();
                world.CreationDate = Convert.ToDateTime(await this.storageService.LoadValueFromKeyAsync(
                                                            file,
                                                            world.GetPropertyName(p => p.CreationDate)));

                // Restore the worlds Time of Day States
                IEnumerable <TimeOfDayState> availableStates = await this.timeOfDayStateRepository.GetTimeOfDayStates();

                IEnumerable <string> worldStates = await this.storageService.LoadMultipleValuesFromKeyAsync(
                    file,
                    world.GetPropertyName(p => p.TimeOfDayStates));

                foreach (TimeOfDayState state in availableStates.Where(s => worldStates.Any(worldState => worldState == s.Name)))
                {
                    world.AddTimeOfDayState(state);
                }

                // Restore the previous time of day state of the world
                // TODO: Some time look at preserving not just the state, but the state.CurrentTime as well.
                string currentTimeOfDayState = await this.storageService.LoadValueFromKeyAsync(
                    file,
                    world.GetPropertyName(p => p.CurrentTimeOfDay));

                world.CurrentTimeOfDay = world.TimeOfDayStates.First(state => state.Name == currentTimeOfDayState);

                world.GameDayToRealHourRatio = Convert.ToInt32(
                    await this.storageService.LoadValueFromKeyAsync(
                        file,
                        world.GetPropertyName(p => p.GameDayToRealHourRatio)));

                world.HoursPerDay = Convert.ToInt32(
                    await this.storageService.LoadValueFromKeyAsync(
                        file,
                        world.GetPropertyName(p => p.HoursPerDay)));

                Guid parsedGuid = Guid.Empty;
                if (!Guid.TryParse(
                        await this.storageService.LoadValueFromKeyAsync(
                            file,
                            world.GetPropertyName(p => p.Id)),
                        out parsedGuid))
                {
                    throw new InvalidOperationException($"Unable to restore the Id for {file}");
                }
                else
                {
                    world.Id = parsedGuid;
                }

                world.IsEnabled = Convert.ToBoolean(
                    await this.storageService.LoadValueFromKeyAsync(
                        file,
                        world.GetPropertyName(p => p.IsEnabled)));

                world.Name = await this.storageService.LoadValueFromKeyAsync(
                    file,
                    world.GetPropertyName(p => p.Name));
            }

            // WorldRepository.worldCache = new ConcurrentBag<DefaultWorld>(worlds);
            return(worlds);
        }
        /// <summary>
        /// Constructs the world.
        /// </summary>
        /// <param name="game">The game.</param>
        private async Task ConstructWorld(IGame game)
        {
            // Set up the Room
            var bedroom = new DefaultRoom {
                IsEnabled = true
            };
            var hallway = new DefaultRoom {
                IsEnabled = true
            };

            // Set up the Zone
            var weatherStates = new List <IWeatherState> {
                new ClearWeather(), new RainyWeather(), new ThunderstormWeather()
            };
            DefaultZone zone = new DefaultZone();

            zone.Name  = "Country Side";
            zone.Rooms = new List <DefaultRoom>()
            {
                bedroom
            };
            zone.WeatherStates          = weatherStates;
            zone.WeatherUpdateFrequency = 6;
            zone.WeatherChanged        += (sender, weatherArgs) => Console.WriteLine($"{zone.Name} zone weather has changed to {weatherArgs.CurrentState.Name}");
            DefaultZone zone2 = new DefaultZone();

            zone2.Name  = "Castle Rock";
            zone2.Rooms = new List <DefaultRoom> {
                hallway
            };
            zone2.WeatherStates          = weatherStates;
            zone2.WeatherUpdateFrequency = 2;
            zone2.WeatherChanged        += (sender, weatherArgs) => Console.WriteLine($"{zone2.Name} zone weather has changed to {weatherArgs.CurrentState.Name}");

            // Set up the World.
            IWorld world = new DefaultWorld();

            world.GameDayToRealHourRatio = 0.4;
            world.HoursPerDay            = 10;
            world.Name = "Sample World";
            world.SetNotificationManager(game.NotificationCenter);

            var morningState = new TimeOfDayState {
                Name = "Morning", StateStartTime = new TimeOfDay {
                    Hour = 2
                }
            };
            var afternoonState = new TimeOfDayState {
                Name = "Afternoon", StateStartTime = new TimeOfDay {
                    Hour = 5
                }
            };
            var nightState = new TimeOfDayState {
                Name = "Night", StateStartTime = new TimeOfDay {
                    Hour = 8
                }
            };

            world.AddTimeOfDayState(morningState);
            world.AddTimeOfDayState(afternoonState);
            world.AddTimeOfDayState(nightState);
            world.TimeOfDayChanged += World_TimeOfDayChanged;

            // Set up the Realm.
            DefaultRealm realm = new DefaultRealm(world, new TimeOfDay {
                Hour = 3, HoursPerDay = 10
            });

            realm.TimeZoneOffset = new TimeOfDay {
                Hour = 3, Minute = 10, HoursPerDay = world.HoursPerDay
            };
            realm.Name = "Realm 1";
            realm.SetNotificationManager(game.NotificationCenter);

            // Initialize the environment.
            Task task = world.Initialize();

            task.Wait();

            await world.AddRealmToWorld(realm);

            realm.AddZoneToRealm(zone);
            realm.AddZoneToRealm(zone2);
            await game.AddWorld(world);
        }