Guild entity
Inheritance: BaseEntity
 public void SetGuild(Guild guild)
 {
     CURRENT_GUILD = guild;
     DigimonModel.ClearCache();
     TamerModel.ClearCache();
     TamerModel.LoadDataAsync(guild.Tamers);
 }
        public void OnDownloadCompleted(object sender, DownloadCompleteEventArgs e)
        {
            if (!this.Dispatcher.CheckAccess()) {
                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new DownloadCompleteEventHandler((s, e2) => {
                    OnDownloadCompleted(s, e2);
                }), sender, e);
                return;
            }

            if (e.Code != DMODownloadResultCode.OK) {
                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(() => {
                    loader.Title = LanguageManager.Model.ErrorOccured + " [" + e.Code + "]";
                    switch (e.Code) {
                        case DMODownloadResultCode.CANT_GET:
                            {
                                loader.Summary = LanguageManager.Model.CantGetError;
                                break;
                            }
                        case DMODownloadResultCode.NOT_FOUND:
                            {
                                loader.Summary = LanguageManager.Model.GuildNotFoundError;
                                break;
                            }
                        case DMODownloadResultCode.WEB_ACCESS_ERROR:
                            {
                                loader.Summary = LanguageManager.Model.ConnectionError;
                                break;
                            }
                    }
                    IsErrorOccured = true;
                }));
                return;
            }
            Guild = MergeHelper.Merge(e.Guild);
        }
 /// <summary>
 /// Initializes a new instance of <see cref="DownloadCompleteEventArgs"/> for
 /// specified <see cref="DMODownloadResultCode"/> and <see cref="Guild"/>.
 /// </summary>
 /// <param name="Code">Result code</param>
 /// <param name="Guild">Guild instance</param>
 public DownloadCompleteEventArgs(DMODownloadResultCode Code, Guild Guild)
 {
     this.Code = Code;
     this.Guild = Guild;
 }
        public override Guild GetGuild(Server server, string guildName, bool isDetailed)
        {
            OnStarted();
            Guild guild = new Guild() {
                Server = server,
                IsDetailed = isDetailed
            };
            HtmlDocument doc = new HtmlDocument();
            OnStatusChanged(DMODownloadStatusCode.GETTING_GUILD, guildName, 0, 50);
            try {
                string html = DownloadContent(string.Format(STR_URL_GUILD_RANK, guildName, server.Identifier));
                doc.LoadHtml(html);

                bool isFound = false;
                HtmlNode ranking = doc.DocumentNode;

                string guildMaster = null;

                try {
                    ranking = doc.DocumentNode.SelectNodes("//div[@id='body']//table[@class='forum_list'][1]//tbody//tr[not(@onmouseover)]")[4];
                    guild.Rank = CheckRankNode(ranking.SelectSingleNode(".//td[1]"));
                    guild.Name = ClearStr(ranking.SelectSingleNode(".//td[2]").InnerText);
                    guild.Rep = Convert.ToInt32(ClearStr(ranking.SelectSingleNode(".//td[4]").InnerText));
                    guildMaster = ClearStr(ranking.SelectSingleNode(".//td[5]").InnerText);

                    Regex r1 = new Regex(STR_GUILD_ID_REGEX, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                    string link = ranking.SelectSingleNode(".//td[7]").InnerHtml;
                    Match m1 = r1.Match(link);
                    if (m1.Success) {
                        guild.Identifier = Convert.ToInt32(m1.Groups[4].ToString());
                        isFound = true;
                    }
                } catch {
                    isFound = false;
                }

                if (!isFound) {
                    OnCompleted(DMODownloadResultCode.NOT_FOUND, guild); // guild not found
                    return guild;
                }

                List<DigimonType> types = GetDigimonTypes();
                using (IDatabaseContext context = DatabaseManager.CreateContext()) {
                    foreach (DigimonType type in types) {
                        context.AddOrUpdateDigimonType(type, true);
                    }
                    context.SaveChanges();
                }

                if (GetGuildInfo(ref guild, isDetailed)) {
                    guild.Tamers.First(t => t.Name == guildMaster).IsMaster = true;
                    guild.UpdateTime = DateTime.Now;
                    OnCompleted(DMODownloadResultCode.OK, guild);
                    return guild;
                } else {
                    OnCompleted(DMODownloadResultCode.CANT_GET, guild); // can't get guild info
                    return guild;
                }
            } catch (WebException) {
                OnCompleted(DMODownloadResultCode.WEB_ACCESS_ERROR, guild);
                return guild;
            }
        }
        protected override bool GetGuildInfo(ref Guild guild, bool isDetailed)
        {
            List<Tamer> tamerList = new List<Tamer>();
            HtmlDocument doc = new HtmlDocument();
            if (LogManager != null) {
                LogManager.InfoFormat("Obtaining info of {0}", guild.Name);
            }
            string html = DownloadContent(string.Format(STR_URL_GUILD_PAGE, guild.Identifier, "srv" + guild.Server.Identifier));
            doc.LoadHtml(html);

            HtmlNode ranking = doc.DocumentNode.SelectNodes("//table[@class='forum_list']//tbody")[1];
            HtmlNodeCollection tlist = ranking.SelectNodes(".//tr");
            if (tlist != null) {
                using (IDatabaseContext context = DatabaseManager.CreateContext()) {
                    for (int i = 0; i < tlist.Count; i++) {
                        try {
                            Tamer tamer = new Tamer() {
                                Guild = guild,
                                Name = ClearStr(tlist[i].SelectSingleNode(".//td[3]").InnerText),
                                Rank = CheckRankNode(tlist[i].SelectSingleNode(".//td[2]"))
                            };
                            OnStatusChanged(DMODownloadStatusCode.GETTING_TAMER, tamer.Name, i, tlist.Count);

                            Regex regex = new Regex(STR_TAMER_TYPE_REGEX, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                            Match match = regex.Match(tlist[i].SelectSingleNode(".//td[3]").InnerHtml);
                            if (match.Success) {
                                tamer.Type = context.FindTamerTypeByCode(Convert.ToInt32(match.Groups[2].ToString()));
                            }

                            regex = new Regex(STR_TAMER_LVL_REGEX, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                            match = regex.Match(tlist[i].SelectSingleNode(".//td[4]").InnerHtml);
                            if (match.Success) {
                                tamer.Level = Convert.ToByte(match.Groups[2].ToString());
                            }

                            regex = new Regex(STR_TAMER_ID_REGEX, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                            match = regex.Match(tlist[i].SelectSingleNode(".//td[7]").InnerHtml);
                            if (match.Success) {
                                tamer.AccountId = Convert.ToInt32(match.Groups[2].ToString());
                            }

                            if (tamer.Level != 0 && tamer.AccountId != 0) {
                                tamer.Digimons = GetDigimons(tamer, isDetailed);
                                if (tamer.Digimons.Count == 0) {
                                    return false;
                                }
                                Digimon partner = tamer.Digimons.FirstOrDefault(d => d.Type.IsStarter);
                                if (partner != null) {
                                    partner.Name = ClearStr(tlist[i].SelectSingleNode(".//td[5]").InnerText);
                                }
                                tamerList.Add(tamer);
                                if (LogManager != null) {
                                    LogManager.InfoFormat("Found tamer \"{0}\"", tamer.Name);
                                }
                            }
                        } catch {
                        }
                    }
                }
            }

            if (tamerList.Count == 0) {
                return false;
            }
            guild.Tamers = tamerList;
            return true;
        }
        public void OnDownloadCompleted(object sender, DownloadCompleteEventArgs e)
        {
            if (!this.Dispatcher.CheckAccess()) {
                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new DownloadCompleteEventHandler((s, e2) => {
                    OnDownloadCompleted(s, e2);
                }), sender, e);
                return;
            }

            BlockControls(false);

            webProvider.DownloadStarted -= Proxy.OnDownloadStarted;
            webProvider.DownloadCompleted -= Proxy.OnDownloadCompleted;
            webProvider.StatusChanged -= Proxy.OnStatusChanged;

            ProgressBlock.Visibility = Visibility.Collapsed;
            switch (e.Code) {
                case DMODownloadResultCode.OK:
                    {
                        CurrentGuild = MergeHelper.Merge(e.Guild);
                        GuildInfoModel.LoadData(CurrentGuild);
                        TDBlock_.SetGuild(CurrentGuild);
                        break;
                    }
                case DMODownloadResultCode.CANT_GET:
                    {
                        DialogManager.ShowErrorDialog(LanguageManager.Model.CantGetError);
                        break;
                    }
                case DMODownloadResultCode.NOT_FOUND:
                    {
                        DialogManager.ShowErrorDialog(LanguageManager.Model.GuildNotFoundError);
                        break;
                    }
                case DMODownloadResultCode.WEB_ACCESS_ERROR:
                    {
                        DialogManager.ShowErrorDialog(LanguageManager.Model.ConnectionError);
                        break;
                    }
            }
        }
        public override Guild GetGuild(Server server, string guildName, bool isDetailed)
        {
            OnStarted();
            Guild guild = new Guild() {
                Server = server,
                IsDetailed = isDetailed
            };
            OnStatusChanged(DMODownloadStatusCode.GETTING_GUILD, guildName, 0, 50);
            try {
                HtmlNode ranking = tryLoadNode(string.Format(STR_URL_GUILD_RANK, guildName, "srv" + server.Identifier), STR_RANKING_NODE);
                HtmlNodeCollection tlist = ranking.SelectNodes("//tr/td[@class='guild']");
                bool isFound = false;
                if (tlist != null) {
                    List<DigimonType> types = GetDigimonTypes();
                    using (IDatabaseContext context = DatabaseManager.CreateContext()) {
                        foreach (DigimonType type in types) {
                            context.AddOrUpdateDigimonType(type, false);
                        }
                        context.SaveChanges();
                    }

                    HtmlNode e = null;
                    string guildMaster = null;
                    for (int i = 0; i < tlist.Count - 1; i++) {
                        try {
                            e = ranking.SelectNodes("//td[@class='guild']")[i];
                        } catch {
                        };
                        if (e != null)
                            if (ClearStr(e.InnerText) == guildName) {
                                Regex r = new Regex(STR_GUILD_ID_REGEX, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                                Match m = r.Match(ranking.SelectNodes("//td[@class='detail']")[i].InnerHtml);
                                if (m.Success) {
                                    guild.Identifier = Convert.ToInt32(m.Groups[2].ToString());
                                    guildMaster = ranking.SelectNodes("//td[@class='master']")[i].InnerText;
                                    guildMaster = guildMaster.Substring(0, guildMaster.IndexOf(' '));
                                    guild.Name = guildName;
                                    guild.Rank = Convert.ToInt32(ClearStr(ranking.SelectNodes("//td[@class='ranking']")[i].InnerText));
                                    guild.Rep = Convert.ToInt32(ClearStr(ranking.SelectNodes("//td[@class='reputation']")[i].InnerText));
                                    isFound = true;
                                    break;
                                }
                            }
                    }
                    if (!isFound) {
                        OnCompleted(DMODownloadResultCode.NOT_FOUND, guild); // guild not found
                        return guild;
                    }

                    if (GetGuildInfo(ref guild, isDetailed)) {
                        guild.Tamers.First(t => t.Name == guildMaster).IsMaster = true;
                        guild.UpdateTime = DateTime.Now;
                        OnCompleted(DMODownloadResultCode.OK, guild);
                        return guild;
                    } else {
                        OnCompleted(DMODownloadResultCode.CANT_GET, guild); // can't get guild info
                        return guild;
                    }
                } else {
                    OnCompleted(DMODownloadResultCode.NOT_FOUND, guild);//wrong web page
                    return guild;
                }
            } catch (Exception e) {
                if (LogManager != null) {
                    LogManager.Error("An error occured while guild info receiving", e);
                }
                OnCompleted(DMODownloadResultCode.WEB_ACCESS_ERROR, guild);
                return guild;
            }
        }
        protected override bool GetGuildInfo(ref Guild guild, bool isDetailed)
        {
            if (LogManager != null) {
                LogManager.InfoFormat("Obtaining info of {0}", guild.Name);
            }
            List<Tamer> tamerList = new List<Tamer>();
            HtmlNode ranking = tryLoadNode(string.Format(STR_URL_GUILD_PAGE, guild.Identifier, "srv" + guild.Server.Identifier), STR_RANKING_NODE);
            HtmlNodeCollection tlist = ranking.SelectNodes("//tr/td[@class='level']");
            using (IDatabaseContext context = DatabaseManager.CreateContext()) {
                for (int i = 0; i < tlist.Count; i++) {
                    Tamer tamer = new Tamer() {
                        Guild = guild,
                        Name = ClearStr(ranking.SelectNodes("//td[@class='guild']")[i].InnerText),
                        Level = Convert.ToByte(ClearStr(ranking.SelectNodes("//td[@class='level']")[i].InnerText)),
                        Rank = Convert.ToInt32(ClearStr(ranking.SelectNodes("//td[@class='ranking']")[i].InnerText))
                    };
                    OnStatusChanged(DMODownloadStatusCode.GETTING_TAMER, tamer.Name, i, tlist.Count - 1);

                    string tamerType = ranking.SelectNodes("//td[@class='guild']//img")[i].GetAttributeValue("src", null);
                    tamerType = tamerType.Substring(tamerType.LastIndexOf('/') + 1, tamerType.LastIndexOf('.') - tamerType.LastIndexOf('/') - 1);
                    tamer.Type = context.FindTamerTypeByCode(Convert.ToInt32(tamerType));

                    Regex r = new Regex(STR_TAMER_ID_REGEX, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                    Match m = r.Match(ranking.SelectNodes("//td[@class='detail']")[i].InnerHtml);
                    if (m.Success) {
                        tamer.AccountId = Convert.ToInt32(m.Groups[2].ToString());
                        tamer.Digimons = GetDigimons(tamer, isDetailed);

                        Digimon partner = tamer.Digimons.FirstOrDefault(d => d.Type.IsStarter);
                        if (partner != null) {
                            partner.Name = ClearStr(ranking.SelectNodes("//td[@class='partner']")[i].InnerText);
                        }

                        tamerList.Add(tamer);
                        if (LogManager != null) {
                            LogManager.InfoFormat("Found tamer \"{0}\"", tamer.Name);
                        }
                    }
                }
            }
            if (tamerList.Count == 0) {
                return false;
            }
            guild.Tamers = tamerList;
            return true;
        }
 /// <summary>
 /// Calls on download complete
 /// </summary>
 /// <param name="code">Result code</param>
 /// <param name="result">Result guild instance</param>
 protected virtual void OnCompleted(DMODownloadResultCode code, Guild result)
 {
     if (LogManager != null) {
         LogManager.Info(String.Format("GuildInfo obtaining completed: code={0}, result={1}", code, result));
     }
     if (DownloadCompleted != null) {
         DownloadCompleted(this, new DownloadCompleteEventArgs(code, result));
     }
 }
 /// <summary>
 /// Updates guild information
 /// </summary>
 /// <param name="guild">Guild reference</param>
 /// <param name="isDetailed">Shoul it be detailed data (like digimon size, real name, etc)</param>
 /// <returns><b>True</b> on success</returns>
 protected abstract bool GetGuildInfo(ref Guild guild, bool isDetailed);