Exemplo n.º 1
0
            private World ResolveLocation(HttpContext context, string field, ResourceManager manager, SectorMap.Milieu map)
            {
                string query = context.Request.QueryString[field];

                if (string.IsNullOrWhiteSpace(query))
                {
                    throw new HttpError(400, "Bad Request", $"Missing {field} location");
                }

                query = query.Trim();

                Match match = Regex.Match(query, @"^(?<sector>.+?)\s+(?<hex>\d\d\d\d)$");

                if (!match.Success)
                {
                    int           x   = GetIntOption("x", 0);
                    int           y   = GetIntOption("y", 0);
                    WorldLocation loc = SearchEngine.FindNearestWorldMatch(query, GetStringOption("milieu"), x, y);
                    if (loc == null)
                    {
                        throw new HttpError(404, "Not Found", $"Location not found: {query}");
                    }

                    Sector loc_sector;
                    World  loc_world;
                    loc.Resolve(map, manager, out loc_sector, out loc_world);
                    return(loc_world);
                }

                Sector sector = map.FromName(match.Groups["sector"].Value);

                if (sector == null)
                {
                    throw new HttpError(404, "Not Found", $"Sector not found: {sector}");
                }

                string hexString = match.Groups["hex"].Value;
                Hex    hex       = new Hex(hexString);

                if (!hex.IsValid)
                {
                    throw new HttpError(400, "Not Found", $"Invalid hex: {hexString}");
                }

                World world = sector.GetWorlds(manager)[hex.ToInt()];

                if (world == null)
                {
                    throw new HttpError(404, "Not Found", $"No such world: {sector.Names[0].Text} {hexString}");
                }

                return(world);
            }