protected Dictionary <string, List <string> > GetTranslations(IArtifactDirectory directory)
        {
            var name = HarvesterNameAttribute.GetShort(typeof(THarvester));

            var translations = new Dictionary <string, List <string> >();

            foreach (var file in directory.GetFiles($"{name}.??.txt"))
            {
                Logger.Info("Read translation from file {filename}", Path.GetFileName(file));

                var language = Regex.Match(file, $@"{name}\.(?<name>(.+))\.txt$").Groups["name"].Value.ToLowerInvariant();
                using (var reader = directory.ReadTextFile(file))
                {
                    Func <string, string> transformationFunc = null;
                    if (TranslationTransformings?.TryGetValue(language, out transformationFunc) == false)
                    {
                        Logger.Warn("No translation transformation function found for {lang}", language);
                    }

                    translations.Add(language, ReadTranslation(reader, transformationFunc));
                }
            }

            return(translations);
        }
Example #2
0
        public Task Harvest(XmlWriter xmlWriter, IArtifactDirectory directory, HttpClient httpClient)
        {
            ParseDares(xmlWriter);
            ParseTruths(xmlWriter);
            ParseBadLuck(xmlWriter);

            return(Task.CompletedTask);
        }
 public abstract Task Harvest(XmlWriter xmlWriter, IArtifactDirectory directory, HttpClient httpClient);
Example #4
0
        public override async Task Harvest(XmlWriter xmlWriter, IArtifactDirectory directory, HttpClient httpClient)
        {
            var originalTextsTask = GetOriginalTexts(httpClient);
            var name = HarvesterNameAttribute.GetShort(typeof(THarvester));

            var translations = GetTranslations(directory);

            var(originalLanguage, originalTexts) = await originalTextsTask;
            var counter = 0;
            var hashIds = CardIdProvider.GetHashIds();

            using (var translatableWriter = directory.CreateTextFile($"{name}.translatable.txt"))
            {
                for (var i = 0; i < originalTexts.Count; i++)
                {
                    var originalText = HtmlUtilities.RemoveTags(originalTexts[i]);

                    translatableWriter.WriteLine(TransformForTranslation(originalText));
                    translatableWriter.WriteLine();

                    xmlWriter.WriteStartElement(CardName);

                    xmlWriter.WriteAttributeString("id", hashIds.Encode(SourceId, counter));
                    WriteAttributes(xmlWriter, originalText);

                    WriteElements(xmlWriter);

                    IEnumerable <(string lang, string text)> GetTexts()
                    {
                        yield return(originalLanguage, originalText);

                        foreach (var translation in translations)
                        {
                            if (translation.Value.Count == counter - 1)
                            {
                                Logger.Error(
                                    "Translation for language {lang} has not enough lines for the site. The site may have received an update.",
                                    translation.Key);
                                continue;
                            }

                            if (translation.Value.Count <= counter)
                            {
                                continue;
                            }

                            yield return(translation.Key, translation.Value[counter]);
                        }
                    }

                    foreach (var(lang, text) in GetTexts())
                    {
                        xmlWriter.WriteStartElement("Text");
                        xmlWriter.WriteAttributeString("lang", lang);
                        xmlWriter.WriteString(text);
                        xmlWriter.WriteEndElement();
                    }

                    xmlWriter.WriteEndElement();

                    counter++;
                }
            }

            Logger.Info("{counter} {name} texts were created!", counter, name);
        }
Example #5
0
        public override Task Harvest(XmlWriter xmlWriter, IArtifactDirectory directory, HttpClient httpClient)
        {
            var translations = GetTranslations(directory);

            var counter = 0;
            var hashIds = CardIdProvider.GetHashIds();

            var(nonDependent, dependent) = GroupRules(_csvData);
            using (var translatableWriter = directory.CreateTextFile("Picolo.translatable.txt"))
            {
                (string text, int type) Extract(string[] data)
                {
                    var text = data[2];
                    var type = int.Parse(data[3]);

                    text = text.Replace(" ", " ");
                    text = text.Replace("  ", " ").Trim();

                    translatableWriter.WriteLine(TransformForTranslation(text, type));
                    translatableWriter.WriteLine();

                    return(text, type);
                }

                IEnumerable <(string lang, string text)> GetTexts(string text, int type)
                {
                    yield return("de", TransformText(text, type));

                    foreach (var translation in translations)
                    {
                        if (translation.Value.Count == counter - 1)
                        {
                            Logger.Error(
                                "Translation for language {lang} has not enough lines for the site. The site may have received an update.",
                                translation.Key);
                            continue;
                        }

                        if (translation.Value.Count <= counter)
                        {
                            continue;
                        }

                        var translatedText = translation.Value[counter];
                        if (_typeSpecificTranslationTransformings.TryGetValue((translation.Key, type),
                                                                              out var typeSpecificTransforming))
                        {
                            translatedText = typeSpecificTransforming(translatedText);
                        }

                        if (translatedText == null)
                        {
                            translatedText = "[TODO]" + translation.Value[counter];
                        }

                        yield return(translation.Key, translatedText);
                    }
                }

                foreach (var data in nonDependent)
                {
                    var(text, type) = Extract(data);

                    void WriteCard(string cardName)
                    {
                        xmlWriter.WriteStartElement(cardName);
                        xmlWriter.WriteAttributeString("id", hashIds.Encode((int)SourceId, counter));

                        foreach (var(lang, translation) in GetTexts(text, type))
                        {
                            xmlWriter.WriteStartElement("Text");
                            xmlWriter.WriteAttributeString("lang", lang);
                            xmlWriter.WriteString(translation);
                            xmlWriter.WriteEndElement();
                        }

                        xmlWriter.WriteEndElement();
                    }

                    switch (type)
                    {
                    case 1:     //Core
                        WriteCard("DrinkCard");
                        break;

                    case 4:     //NoIdeaLoses
                        WriteCard("NoIdeaLosesCard");
                        break;

                    case 5:     //Down
                        WriteCard("DownCard");
                        break;

                    case 14:     //Would you rather
                        WriteCard("WyrCard");
                        break;

                    case 15:     //SocialMedia
                        WriteCard("SocialMediaCard");
                        break;

                    case 23:
                        WriteCard("GroupGameCard");
                        break;

                    default:
                        Logger.Warn("The card {card} was skipped because no writer is assigned (type: {type}).", text, type);
                        break;
                    }

                    counter++;
                }

                foreach (var keyValuePair in dependent)
                {
                    var(startingCards, endingCards) = keyValuePair.Value;
                    var startingCard = startingCards.Single();

                    var(text, type) = Extract(startingCard);

                    if (!endingCards.Any())
                    {
                        throw new InvalidOperationException($"The card {text} has no ending card.");
                    }

                    void WriteMultiCard(string cardName)
                    {
                        xmlWriter.WriteStartElement(cardName);
                        xmlWriter.WriteAttributeString("id", hashIds.Encode((int)SourceId, counter));

                        foreach (var(lang, translation) in GetTexts(text, type))
                        {
                            xmlWriter.WriteStartElement("Text");
                            xmlWriter.WriteAttributeString("lang", lang);
                            xmlWriter.WriteString(translation);
                            xmlWriter.WriteEndElement();
                        }

                        xmlWriter.WriteStartElement($"{cardName}.followUp");

                        if (endingCards.Count == 1)
                        {
                            var endingCard = endingCards.Single();
                            var(endingCardText, _) = Extract(endingCard);

                            counter++;
                            foreach (var(lang, translation) in GetTexts(endingCardText, type))
                            {
                                xmlWriter.WriteStartElement("Text");
                                xmlWriter.WriteAttributeString("lang", lang);
                                xmlWriter.WriteString(translation);
                                xmlWriter.WriteEndElement();
                            }
                        }
                        else
                        {
                            foreach (var endingCard in endingCards)
                            {
                                var(endingCardText, _) = Extract(endingCard);

                                counter++;
                                xmlWriter.WriteStartElement("Case");
                                foreach (var(lang, translation) in GetTexts(endingCardText, type))
                                {
                                    xmlWriter.WriteStartElement("Text");
                                    xmlWriter.WriteAttributeString("lang", lang);
                                    xmlWriter.WriteString(translation);
                                    xmlWriter.WriteEndElement();
                                }
                                xmlWriter.WriteEndElement();
                            }
                        }

                        xmlWriter.WriteEndElement();
                        xmlWriter.WriteEndElement();
                    }

                    switch (type)
                    {
                    case 2:     //Virus
                    case 3:
                        WriteMultiCard("VirusCard");
                        break;

                    case 1:
                        WriteMultiCard("DrinkCard");
                        break;

                    case 23:
                        WriteMultiCard("GroupGameCard");
                        break;

                    case 4:
                        WriteMultiCard("NoIdeaLosesCard");
                        break;

                    case 14:
                        WriteMultiCard("WyrCard");
                        break;

                    default:
                        Logger.Warn("The card {card} was skipped because no writer is assigned (type: {type}).", text, type);
                        break;
                    }

                    counter++;
                }
            }

            Logger.Info("{counter} {name} texts were created!", counter, "Picolo");
            return(Task.CompletedTask);
        }