Server entity
Inheritance: BaseEntity
 /// <summary>
 /// Returns guild
 /// </summary>
 /// <param name="server">Guild server</param>
 /// <param name="guildName">Guild name</param>
 /// <param name="isDetailed">Shoul it be detailed data (like digimon size, real name, etc)</param>
 /// <param name="actualInterval">Interval of actual data in days</param>
 /// <returns>Guild</returns>
 public override Guild GetActualGuild(Server server, string guildName, bool isDetailed, int actualInterval)
 {
     bool fetchCurrent = false;
     using (IDatabaseContext context = DatabaseManager.CreateContext()) {
         Guild storedGuild = context.FindGuild(server, guildName);
         if (storedGuild != null && !(isDetailed && !storedGuild.IsDetailed) && storedGuild.UpdateTime != null) {
             TimeSpan timeDiff = (TimeSpan)(DateTime.Now - storedGuild.UpdateTime);
             if (timeDiff.Days < actualInterval) {
                 fetchCurrent = true;
             }
         }
         if (fetchCurrent) {
             OnStarted();
             OnStatusChanged(DMODownloadStatusCode.GETTING_GUILD, guildName, 0, 50);
             storedGuild = context.FetchGuild(server, guildName);
             OnCompleted(DMODownloadResultCode.OK, storedGuild);
             return storedGuild;
         }
     }
     return GetGuild(server, guildName, isDetailed);
 }
        private void MainWorkerFunc(object sender, DoWorkEventArgs e)
        {
            //Ротация в цикле
            while (true) {
                //Если источник не загружен
                if (!IsSourceLoaded) {
                    Profile currentProfile = ProfileManager.CurrentProfile;
                    //Добавляем задачу загрузки
                    TaskManager.AquireLock(LoadingTask);
                    //Показываем анимацию загрузки
                    IsLoadingAnim(true, true);
                    IsStatic = false;
                    IsErrorOccured = false;
                    //Получаем информацию, необходимую для ротации
                    GuildName = currentProfile.Rotation.Guild;
                    TamerName = currentProfile.Rotation.Tamer;

                    IConfiguration config = ConfigurationManager.GetConfiguration(currentProfile.GameModel);
                    IWebProvider webProvider = config.CreateWebProvider();
                    IServersProvider serversProvider = config.ServersProvider;
                    //Проверяем, доступен ли веб-профиль и необходимая информация

                    IsStatic = webProvider == null || string.IsNullOrEmpty(GuildName);
                    if (!IsStatic) {
                        Server = serversProvider.GetServerById(currentProfile.Rotation.ServerId);
                        //Регистрируем ивенты загрузки
                        webProvider.StatusChanged += EventProxy.OnStatusChanged;
                        webProvider.DownloadCompleted += EventProxy.OnDownloadCompleted;
                        //Получаем информацию о списках гильдии
                        webProvider.GetActualGuild(Server, GuildName, false, currentProfile.Rotation.UpdateInterval + 1);
                        //Убираем обработку ивентов
                        webProvider.DownloadCompleted -= EventProxy.OnDownloadCompleted;
                        webProvider.StatusChanged -= EventProxy.OnStatusChanged;
                    }
                    //Проверяем не произошла ли ошибка
                    if (!IsErrorOccured) {
                        //Закрываем анимацию, устанавливаем флаг загрузки
                        IsLoadingAnim(false);
                    }
                    TaskManager.ReleaseLock(LoadingTask);
                    IsSourceLoaded = true;
                }

                if (!IsErrorOccured && IsSourceLoaded) {
                    UpdateModel();
                    System.Threading.Thread.Sleep(ROTATION_INTERVAL);
                }
            }
        }
        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;
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new <see cref="Server"/> based on another
 /// </summary>
 /// <param name="server">Source <see cref="Server"/></param>
 public Server(Server server)
 {
     this.Identifier = server.Identifier;
     this.Name = server.Name;
     this.Type = server.Type;
 }
 /// <summary>
 /// Initializes a new instance of <see cref="AbstractServersProvider"/> for specified <see cref="Server.ServerType"/>.
 /// </summary>
 /// <param name="serverType">Server type</param>
 public AbstractServersProvider(Server.ServerType serverType)
 {
     this.ServerType = serverType;
 }
 /// <summary>
 /// Initializes a new instance of <see cref="DatabaseServersProvider"/> for specified <see cref="IDatabaseManager"/> and <see cref="Server.ServerType"/>.
 /// </summary>
 /// <param name="DatabaseManager">DatabaseManager API</param>
 /// <param name="serverType">Server type</param>
 public DatabaseServersProvider(IDatabaseManager DatabaseManager, Server.ServerType serverType)
     : base(serverType)
 {
     this.DatabaseManager = DatabaseManager;
 }
        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;
            }
        }
 public KoreaServersProvider(IDatabaseManager DatabaseManager, Server.ServerType serverType)
     : base(DatabaseManager, serverType)
 {
 }
 /// <summary>
 /// Returns guild
 /// </summary>
 /// <param name="server">Guild server</param>
 /// <param name="guildName">Guild name</param>
 /// <param name="isDetailed">Shoul it be detailed data (like digimon size, real name, etc)</param>
 /// <param name="actualInterval">Interval of actual data in days</param>
 /// <returns>Guild</returns>
 public abstract Guild GetActualGuild(Server server, string guildName, bool isDetailed, int actualInterval);
 /// <summary>
 /// Asynchronously starts guild obtaining
 /// </summary>
 /// <param name="server">Guild server</param>
 /// <param name="guildName">Guild name</param>
 /// <param name="isDetailed">Shoul it be detailed data (like digimon size, real name, etc)</param>
 /// <seealso cref="DownloadStarted"/>
 /// <seealso cref="DownloadCompleted"/>
 /// <seealso cref="StatusChanged"/>
 public void GetGuildAsync(Server server, string guildName, bool isDetailed)
 {
     BackgroundWorker bw = new BackgroundWorker();
     bw.DoWork += (s1, e2) => {
         GetGuild(server, guildName, isDetailed);
     };
     bw.RunWorkerAsync();
 }
 /// <summary>
 /// Returns guild
 /// </summary>
 /// <param name="server">Guild server</param>
 /// <param name="guildName">Guild name</param>
 /// <param name="isDetailed">Shoul it be detailed data (like digimon size, real name, etc)</param>
 /// <returns>Guild</returns>
 public abstract Guild GetGuild(Server server, string guildName, bool isDetailed);