Пример #1
0
            public override void Process(ResourceManager resourceManager)
            {
                // NOTE: This (re)initializes a static data structure used for
                // resolving names into sector locations, so needs to be run
                // before any other objects (e.g. Worlds) are loaded.
                SectorMap.Milieu map = SectorMap.ForMilieu(resourceManager, GetStringOption("milieu"));
                Sector           sector;

                SectorSerializeOptions options = new SectorSerializeOptions()
                {
                    sscoords        = GetBoolOption("sscoords", defaultValue: false),
                    includeMetadata = GetBoolOption("metadata", defaultValue: true),
                    includeHeader   = GetBoolOption("header", defaultValue: true),
                    includeRoutes   = GetBoolOption("routes", defaultValue: false)
                };

                if (Context.Request.HttpMethod == "POST")
                {
                    bool        lint   = GetBoolOption("lint", defaultValue: false);
                    ErrorLogger errors = null;
                    if (lint)
                    {
                        bool hide_uwp = GetBoolOption("hide-uwp", defaultValue: false);
                        bool hide_tl  = GetBoolOption("hide-tl", defaultValue: false);
                        Func <ErrorLogger.Record, bool> filter = (ErrorLogger.Record record) =>
                        {
                            if (hide_uwp && record.message.StartsWith("UWP"))
                            {
                                return(false);
                            }
                            if (hide_tl && record.message.StartsWith("UWP: TL"))
                            {
                                return(false);
                            }
                            return(true);
                        };
                        errors = new ErrorLogger(filter);
                    }

                    sector = new Sector(Context.Request.InputStream, new ContentType(Context.Request.ContentType).MediaType, errors);
                    if (lint && !errors.Empty)
                    {
                        throw new HttpError(400, "Bad Request", errors.ToString());
                    }
                    options.includeMetadata = false;
                }
                else if (HasOption("sx") && HasOption("sy"))
                {
                    int sx = GetIntOption("sx", 0);
                    int sy = GetIntOption("sy", 0);

                    sector = map.FromLocation(sx, sy) ??
                             throw new HttpError(404, "Not Found", $"The sector at {sx},{sy} was not found.");
                }
                else if (HasOption("sector"))
                {
                    string sectorName = GetStringOption("sector");
                    sector = map.FromName(sectorName) ??
                             throw new HttpError(404, "Not Found", $"The specified sector '{sectorName}' was not found.");
                }
                else
                {
                    throw new HttpError(400, "Bad Request", "No sector specified.");
                }

                if (HasOption("subsector"))
                {
                    string subsector = GetStringOption("subsector");
                    int    index     = sector.SubsectorIndexFor(subsector);
                    if (index == -1)
                    {
                        throw new HttpError(404, "Not Found", $"The specified subsector '{subsector}' was not found.");
                    }
                    options.filter = (World world) => (world.Subsector == index);
                }
                else if (HasOption("quadrant"))
                {
                    string quadrant = GetStringOption("quadrant");
                    int    index    = Sector.QuadrantIndexFor(quadrant);
                    if (index == -1)
                    {
                        throw new HttpError(400, "Bad Request", $"The specified quadrant '{quadrant}' is invalid.");
                    }
                    options.filter = (World world) => (world.Quadrant == index);
                }

                string   mediaType = GetStringOption("type");
                Encoding encoding;

                switch (mediaType)
                {
                case "SecondSurvey":
                case "TabDelimited":
                    encoding = Util.UTF8_NO_BOM;
                    break;

                default:
                    encoding = Encoding.GetEncoding(1252);
                    break;
                }

                string data;

                using (var writer = new StringWriter())
                {
                    // Content
                    //
                    sector.Serialize(resourceManager, writer, mediaType, options);
                    data = writer.ToString();
                }
                SendResult(data, encoding);
            }
Пример #2
0
        public override void Serialize(TextWriter writer, IEnumerable <World> worlds, SectorSerializeOptions options)
        {
            List <string> cols = new List <string> {
                "Hex",
                "Name",
                "UWP",
                "Remarks",
                "{Ix}",
                "(Ex)",
                "[Cx]",
                "N",
                "B",
                "Z",
                "PBG",
                "W",
                "A",
                "Stellar"
            };

            if (options.includeRoutes)
            {
                cols.Add("Routes");
            }

            ColumnSerializer formatter = new ColumnSerializer(cols);

            formatter.SetMinimumWidth("Name", 20);
            formatter.SetMinimumWidth("Remarks", 20);

            foreach (World world in worlds.OrderBy(world => world.SS))
            {
                List <string> row = new List <string> {
                    options.sscoords?world.SubsectorHex : world.Hex,
                           world.Name,
                           world.UWP,
                           world.Remarks,
                           world.Importance ?? "",
                           world.Economic ?? "",
                           world.Cultural ?? "",
                    DashIfEmpty(world.Nobility ?? ""),
                    DashIfEmpty(world.Bases),
                    DashIfEmpty(world.Zone),
                    world.PBG,
                    world.Worlds > 0 ? world.Worlds.ToString() : "",
                    world.Allegiance,
                    world.Stellar
                };
                if (options.includeRoutes)
                {
                    row.Add(world.Routes ?? "");
                }
                formatter.AddRow(row);
            }
            formatter.Serialize(writer, options.includeHeader);
        }
Пример #3
0
        public override void Serialize(TextWriter writer, IEnumerable <World> worlds, SectorSerializeOptions options)
        {
            if (options.includeHeader)
            {
                foreach (var line in new string[] {
                    " 1-14: Name",
                    "15-18: HexNbr",
                    "20-28: UWP",
                    "   31: Bases",
                    "33-47: Codes & Comments",
                    "   49: Zone",
                    "52-54: PBG",
                    "56-57: Allegiance",
                    "59-74: Stellar Data",
                    "",
                    "....+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8",
                    ""
                })
                {
                    writer.WriteLine(line);
                }
            }

            const string worldFormat = "{0,-14}{1,4} {2,9}  {3,1} {4,-15} {5,1}  {6,3} {7,2} {8,-15}";

            foreach (World world in worlds.OrderBy(world => world.SS))
            {
                writer.WriteLine(worldFormat,
                                 world.Name.Truncate(14),
                                 options.sscoords ? world.SubsectorHex : world.Hex,
                                 world.UWP,
                                 world.LegacyBaseCode,
                                 world.Remarks.Truncate(15),
                                 world.Zone,
                                 world.PBG,
                                 world.LegacyAllegiance.Truncate(2),
                                 world.Stellar.Truncate(15)
                                 );
            }
        }
Пример #4
0
 public override void Serialize(TextWriter writer, IEnumerable <World> worlds, SectorSerializeOptions options)
 {
     if (options.includeHeader)
     {
         writer.WriteLine(string.Join("\t", new string[] {
             "Sector", "SS", "Hex", "Name", "UWP", "Bases", "Remarks", "Zone", "PBG", "Allegiance", "Stars",
             "{Ix}", "(Ex)", "[Cx]", "Nobility", "W", "RU"
         }));
     }
     foreach (World world in worlds.OrderBy(world => world.Subsector))
     {
         writer.WriteLine(string.Join("\t", new string[] {
             world.Sector?.Abbreviation ?? "",
             world.SS,
             options.sscoords ? world.SubsectorHex : world.Hex,
             world.Name,
             world.UWP,
             world.Bases,
             world.Remarks,
             world.Zone,
             world.PBG,
             world.Allegiance,
             world.Stellar,
             world.Importance ?? "",
             world.Economic ?? "",
             world.Cultural ?? "",
             world.Nobility ?? "",
             world.Worlds > 0 ? world.Worlds.ToString(CultureInfo.InvariantCulture) : "",
             world.ResourceUnits.ToString(CultureInfo.InvariantCulture)
         }));
     }
 }
Пример #5
0
 public void Serialize(TextWriter writer, string?mediaType, SectorSerializeOptions options)
 {
     SectorFileSerializer.ForType(mediaType).Serialize(writer,
                                                       options.filter == null ? this : this.Where(world => options.filter(world)), options);
 }
Пример #6
0
        internal void Serialize(ResourceManager resourceManager, TextWriter writer, string mediaType, SectorSerializeOptions options)
        {
            WorldCollection worlds = GetWorlds(resourceManager);

            // TODO: less hacky T5 support
            bool isT5 = (mediaType == "TabDelimited" || mediaType == "SecondSurvey");

            if (mediaType == "TabDelimited")
            {
                worlds?.Serialize(writer, mediaType, options);
                return;
            }

            if (options.includeMetadata)
            {
                // Header
                //
                writer.WriteLine("# Generated by https://travellermap.com");
                writer.WriteLine("# " + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz", DateTimeFormatInfo.InvariantInfo));
                writer.WriteLine();

                writer.WriteLine($"# {Names[0]}");
                writer.WriteLine($"# {X},{Y}");

                writer.WriteLine();
                foreach (var name in Names)
                {
                    if (name.Lang != null)
                    {
                        writer.WriteLine($"# Name: {name.Text} ({name.Lang})");
                    }
                    else
                    {
                        writer.WriteLine($"# Name: {name}");
                    }
                }

                writer.WriteLine();
                writer.WriteLine($"# Milieu: {CanonicalMilieu}");

                if (Credits != null)
                {
                    string stripped = Regex.Replace(Credits, "<.*?>", "");
                    stripped = Regex.Replace(stripped, @"\s+", " ");
                    stripped = stripped.Trim();
                    writer.WriteLine();
                    writer.WriteLine($"# Credits: {stripped}");
                }

                if (DataFile != null)
                {
                    writer.WriteLine();
                    if (DataFile.Author != null)
                    {
                        writer.WriteLine($"# Author:    {DataFile.Author}");
                    }
                    if (DataFile.Publisher != null)
                    {
                        writer.WriteLine($"# Publisher: {DataFile.Publisher}");
                    }
                    if (DataFile.Copyright != null)
                    {
                        writer.WriteLine($"# Copyright: {DataFile.Copyright}");
                    }
                    if (DataFile.Source != null)
                    {
                        writer.WriteLine($"# Source:    {DataFile.Source}");
                    }
                    if (DataFile.Ref != null)
                    {
                        writer.WriteLine($"# Ref:       {DataFile.Ref}");
                    }
                }

                writer.WriteLine();
                for (int i = 0; i < 16; ++i)
                {
                    char      c  = (char)('A' + i);
                    Subsector ss = Subsector(c);
                    writer.WriteLine($"# Subsector {c}: {ss?.Name ?? ""}");
                }
                writer.WriteLine();
            }

            if (worlds == null)
            {
                if (options.includeMetadata)
                {
                    writer.WriteLine("# No world data available");
                }
                return;
            }

            // Allegiances
            if (options.includeMetadata)
            {
                // Use codes as present in the data, to match the worlds
                foreach (string code in worlds.AllegianceCodes().OrderBy(s => s))
                {
                    var alleg = GetAllegianceFromCode(code);
                    if (alleg != null)
                    {
                        var a = isT5 ? code : SecondSurvey.T5AllegianceCodeToLegacyCode(code);
                        writer.WriteLine($"# Alleg: {a}: \"{alleg.Name}\"");
                    }
                }
                writer.WriteLine();
            }

            // Worlds
            worlds.Serialize(writer, mediaType, options);
        }