public void RefreshUserData()
        {
            GlobalProgressOptions globalProgressOptions = new GlobalProgressOptions(
                $"{PluginName} - {resources.GetString("LOCHowLongToBeatPluginGetUserView")}",
                false
                );

            globalProgressOptions.IsIndeterminate = true;

            _PlayniteApi.Dialogs.ActivateGlobalProgress((activateGlobalProgress) =>
            {
                HltbUserStats UserHltbData = howLongToBeatClient.GetUserData();

                if (UserHltbData != null)
                {
                    try
                    {
                        string PathHltbUserStats = Path.Combine(PluginUserDataPath, "HltbUserStats.json");
                        File.WriteAllText(PathHltbUserStats, JsonConvert.SerializeObject(UserHltbData));
                    }
                    catch (Exception ex)
                    {
                        Common.LogError(ex, "HowLongToBeat");
                    }

                    Database.UserHltbData = UserHltbData;
                }
            }, globalProgressOptions);
        }
Example #2
0
        public HltbUserStats LoadUserData()
        {
            string        PathHltbUserStats = Path.Combine(_plugin.GetPluginUserDataPath(), "HltbUserStats.json");
            HltbUserStats hltbDataUser      = new HltbUserStats();

            if (File.Exists(PathHltbUserStats))
            {
                try
                {
                    var JsonStringData = File.ReadAllText(PathHltbUserStats);
                    hltbDataUser = JsonConvert.DeserializeObject <HltbUserStats>(JsonStringData);
                }
                catch (Exception ex)
                {
                    Common.LogError(ex, "HowLongToBeat");
                }
            }

            return(hltbDataUser);
        }
Example #3
0
        public void RefreshUserDataTask()
        {
            Task.Run(() =>
            {
                HltbUserStats UserHltbData = howLongToBeatClient.GetUserData();

                if (UserHltbData != null)
                {
                    try
                    {
                        string PathHltbUserStats = Path.Combine(PluginUserDataPath, "HltbUserStats.json");
                        File.WriteAllText(PathHltbUserStats, JsonConvert.SerializeObject(UserHltbData));
                    }
                    catch (Exception ex)
                    {
                        Common.LogError(ex, "HowLongToBeat");
                    }

                    Database.UserHltbData = UserHltbData;
                }
            });
        }
Example #4
0
        public HltbUserStats GetUserData()
        {
            if (GetIsUserLoggedIn())
            {
                hltbUserStats            = new HltbUserStats();
                hltbUserStats.Login      = (UserLogin.IsNullOrEmpty()) ? HowLongToBeat.PluginDatabase.Database.UserHltbData.Login : UserLogin;
                hltbUserStats.UserId     = (UserId == 0) ? HowLongToBeat.PluginDatabase.Database.UserHltbData.UserId : UserId;
                hltbUserStats.TitlesList = new List <TitleList>();

                try
                {
                    List <HttpCookie> Cookies = webViews.GetCookies();
                    Cookies = Cookies.Where(x => x.Domain.Contains("howlongtobeat")).ToList();
#if DEBUG
                    logger.Debug($"HowLongToBeat [Ignored] - Cookies: {JsonConvert.SerializeObject(Cookies)}");
#endif

                    // Get list games
                    var formContent = new FormUrlEncodedContent(new[]
                    {
                        new KeyValuePair <string, string>("n", UserLogin),
                        new KeyValuePair <string, string>("c", "user_beat"),
                        new KeyValuePair <string, string>("p", string.Empty),
                        new KeyValuePair <string, string>("y", string.Empty)
                    });

                    string response = Web.PostStringDataCookies(UrlUserStatsGameList, formContent, Cookies).GetAwaiter().GetResult();

                    HtmlParser    parser       = new HtmlParser();
                    IHtmlDocument htmlDocument = parser.Parse(response);

                    foreach (var ListGame in htmlDocument.QuerySelectorAll("table.user_game_list tbody"))
                    {
                        TitleList titleList = new TitleList();

                        var tr = ListGame.QuerySelectorAll("tr");
                        var td = tr[0].QuerySelectorAll("td");

                        titleList.UserGameId = ListGame.GetAttribute("id").Replace("user_sel_", string.Empty).Trim();
                        titleList.GameName   = td[0].QuerySelector("a").InnerHtml.Trim();
                        titleList.Platform   = td[0].QuerySelector("span").InnerHtml.Trim();
                        titleList.Id         = int.Parse(td[0].QuerySelector("a").GetAttribute("href").Replace("game?id=", string.Empty));

                        // Get game details
                        formContent = new FormUrlEncodedContent(new[]
                        {
                            new KeyValuePair <string, string>("option", titleList.UserGameId),
                            new KeyValuePair <string, string>("option_b", "comp_all")
                        });

                        response = Web.PostStringDataCookies(UrlUserStatsGameDetails, formContent, Cookies).GetAwaiter().GetResult();

                        parser       = new HtmlParser();
                        htmlDocument = parser.Parse(response);

                        var GameDetails = htmlDocument.QuerySelectorAll("div.user_game_detail > div");

                        // Game status type
                        titleList.GameStatuses = new List <GameStatus>();
                        foreach (var GameStatus in GameDetails[0].QuerySelectorAll("span"))
                        {
                            switch (GameStatus.InnerHtml.ToLower())
                            {
                            case "playing":
                                titleList.GameStatuses.Add(new GameStatus {
                                    Status = StatusType.Playing
                                });
                                break;

                            case "backlog":
                                titleList.GameStatuses.Add(new GameStatus {
                                    Status = StatusType.Backlog
                                });
                                break;

                            case "replays":
                                titleList.GameStatuses.Add(new GameStatus {
                                    Status = StatusType.Replays
                                });
                                break;

                            case "custom tab":
                                titleList.GameStatuses.Add(new GameStatus {
                                    Status = StatusType.CustomTab
                                });
                                break;

                            case "completed":
                                titleList.GameStatuses.Add(new GameStatus {
                                    Status = StatusType.Completed
                                });
                                break;

                            case "retired":
                                titleList.GameStatuses.Add(new GameStatus {
                                    Status = StatusType.Retired
                                });
                                break;
                            }
                        }

                        // Game status time
                        int iPosUserData = 1;
                        if (GameDetails[1].InnerHtml.ToLower().Contains("<h5>progress</h5>"))
                        {
                            List <string> ListTime = GameDetails[1].QuerySelector("span").InnerHtml
                                                     .Replace("<strong>", string.Empty).Replace("</strong>", string.Empty)
                                                     .Split('/').ToList();

                            for (int i = 0; i < titleList.GameStatuses.Count; i++)
                            {
                                titleList.GameStatuses[i].Time = ConvertStringToLongUser(ListTime[i]);
                            }

                            iPosUserData = 2;
                        }

                        // User data
                        titleList.HltbUserData = new HltbData();
                        for (int i = 0; i < GameDetails[iPosUserData].Children.Count(); i++)
                        {
                            string tempTime = string.Empty;

                            // Completion date
                            if (GameDetails[iPosUserData].Children[i].InnerHtml.ToLower().Contains("completion"))
                            {
                                if (GameDetails[iPosUserData].Children[i].QuerySelectorAll("p").FirstOrDefault() != null)
                                {
                                    tempTime             = GameDetails[iPosUserData].Children[i].QuerySelectorAll("p").FirstOrDefault().InnerHtml;
                                    titleList.Completion = Convert.ToDateTime(tempTime);
                                }
                            }


                            if (GameDetails[iPosUserData].Children[i].InnerHtml.ToLower().Contains("main story"))
                            {
                                i++;
                                tempTime = GameDetails[iPosUserData].Children[i].QuerySelector("span").InnerHtml;
                                titleList.HltbUserData.MainStory = ConvertStringToLongUser(tempTime);
                            }

                            if (GameDetails[iPosUserData].Children[i].InnerHtml.ToLower().Contains("main+extras"))
                            {
                                i++;
                                tempTime = GameDetails[iPosUserData].Children[i].QuerySelector("span").InnerHtml;
                                titleList.HltbUserData.MainExtra = ConvertStringToLongUser(tempTime);
                            }

                            if (GameDetails[iPosUserData].Children[i].InnerHtml.ToLower().Contains("completionist"))
                            {
                                i++;
                                tempTime = GameDetails[iPosUserData].Children[i].QuerySelector("span").InnerHtml;
                                titleList.HltbUserData.Completionist = ConvertStringToLongUser(tempTime);
                            }


                            if (GameDetails[iPosUserData].Children[i].InnerHtml.ToLower().Contains("solo"))
                            {
                                i++;
                                tempTime = GameDetails[iPosUserData].Children[i].QuerySelector("span").InnerHtml;
                                titleList.HltbUserData.Solo = ConvertStringToLongUser(tempTime);
                            }

                            if (GameDetails[iPosUserData].Children[i].InnerHtml.ToLower().Contains("co-op"))
                            {
                                i++;
                                tempTime = GameDetails[iPosUserData].Children[i].QuerySelector("span").InnerHtml;
                                titleList.HltbUserData.CoOp = ConvertStringToLongUser(tempTime);
                            }

                            if (GameDetails[iPosUserData].Children[i].InnerHtml.ToLower().Contains("vs"))
                            {
                                i++;
                                tempTime = GameDetails[iPosUserData].Children[i].QuerySelector("span").InnerHtml;
                                titleList.HltbUserData.Vs = ConvertStringToLongUser(tempTime);
                            }
                        }
#if DEBUG
                        logger.Debug($"HowLongToBeat [Ignored] - titleList: {JsonConvert.SerializeObject(titleList)}");
#endif

                        hltbUserStats.TitlesList.Add(titleList);
                    }
                }
                catch (Exception ex)
                {
                    Common.LogError(ex, "HowLongToBeat");

                    _PlayniteApi.Notifications.Add(new NotificationMessage(
                                                       "HowLongToBeat-Import-Error",
                                                       "HowLongToBeat" + System.Environment.NewLine +
                                                       ex.Message,
                                                       NotificationType.Error,
                                                       () => _plugin.OpenSettingsView()));

                    return(null);
                }

                return(hltbUserStats);
            }
            else
            {
                _PlayniteApi.Notifications.Add(new NotificationMessage(
                                                   "HowLongToBeat-Import-Error",
                                                   "HowLongToBeat" + System.Environment.NewLine +
                                                   resources.GetString("LOCNotLoggedIn"),
                                                   NotificationType.Error,
                                                   () => _plugin.OpenSettingsView()));

                return(null);
            }
        }