Esempio n. 1
0
        public bool SetupWorld()
        {
            const string rules = "Data\\rules.json";
            string       path  = Path.Combine(Directory.GetCurrentDirectory(), rules);

            List <AreaRuleInfo> areaRules = null;

            //maybe move this to file utils
            try
            {
                using (StreamReader sr = File.OpenText(path))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    areaRules = (List <AreaRuleInfo>)serializer.Deserialize(sr, typeof(List <AreaRuleInfo>));
                }
            }
            catch (Exception ex)
            {
                Logger.GetLogger.Log("Tried to deserialise rules.json, Error: {1}", ex.Message);
                return(false);
            }

            foreach (AreaRuleInfo info in areaRules)
            {
                Area area = new Area(info.areaName);

                //adding area locations to the area and world lists
                foreach (var l in info.locations)
                {
                    string[] locationInfo = l.Split(':');

                    if (locationInfo.Length == 2)
                    {
                        //this will get changed when more than just shop locations matter
                        LocationType locationType = LocationType.Default;
                        if (locationInfo[0].Contains("Shop"))
                        {
                            locationType = LocationType.Shop;
                        }

                        Location location = new Location(locationInfo[0], area, locationType);
                        location.ruleTree = RuleTree.ParseAndBuildRules(locationInfo[1]);
                        area.locations.Add(location);

                        if (!locations.ContainsKey(location.name))
                        {
                            locations.Add(location.name, location);
                        }
                        else
                        {
                            Logger.GetLogger.Log("Location already exists {0} in area {1}.", location.name, area.name);
                            return(false);
                        }
                    }
                    else
                    {
                        Logger.GetLogger.Log("Area {0} contains an invlaid location string: {1}", area.name, l);
                        return(false);
                    }
                }
                //adding area exits to the area and world lists
                foreach (var e in info.exits)
                {
                    string[] exitInfo = e.Split(':');

                    if (exitInfo.Length == 2)
                    {
                        Connection exit = new Connection(exitInfo[0], area);
                        exit.ruleTree = RuleTree.ParseAndBuildRules(exitInfo[1]);
                        area.exits.Add(exit);
                    }
                    else
                    {
                        Logger.GetLogger.Log("Area {0} contains an invlaid exit string: {1}", area.name, e);
                        return(false);
                    }
                }

                if (!areas.ContainsKey(area.name))
                {
                    areas.Add(area.name, area);
                }
                else
                {
                    Logger.GetLogger.Log("Area already exists {0}.", area.name);
                    return(false);
                }
            }

            //Add entrances to areas so that when we try to see if wee can reach an area when can check its entraces rules
            foreach (var area in areas)
            {
                foreach (var exit in area.Value.exits)
                {
                    Area connectingArea;
                    if (areas.TryGetValue(exit.connectingAreaName, out connectingArea))
                    {
                        exit.connectingArea = connectingArea;
                        connectingArea.entrances.Add(exit);
                    }
                    else
                    {
                        Logger.GetLogger.Log("Tried to add entrances to an area that doesn't exist {0}, from exit {1}", exit.connectingAreaName, exit.name);
                        return(false);
                    }
                }
            }

            return(true);
        }