game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
hltb_service.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// hltb_service.h
4// ==============================================================================
5// HowLongToBeat.com integration for game time estimates
6// ==============================================================================
7// Provides time-to-beat estimates for games by querying the HowLongToBeat
8// database. Supports main story, main+extras, and completionist playtimes.
9//
10// ## API Source
11// Uses the unofficial HowLongToBeat API (reverse-engineered from their website).
12// This is the same approach used by Playnite, JAST, and other launchers.
13//
14// ## Thread Safety
15// All methods are thread-safe.
16// ==============================================================================
17
18#pragma once
19
20#include "game.h"
21#include "lru_cache.h"
22
23#include <functional>
24#include <mutex>
25#include <string>
26#include <vector>
27
28// ==============================================================================
29// HowLongToBeat Search Result
30// ==============================================================================
31
32/**
33 * @struct HLTBSearchResult
34 * @brief A single search result from HowLongToBeat
35 */
37{
38 int gameId = 0; ///< HLTB game ID
39 std::string name; ///< Game name on HLTB
40 std::string imageUrl; ///< Cover image URL
41 int releaseYear = 0; ///< Release year
42
43 // Time estimates in hours
44 double mainStory = 0.0; ///< Main story time
45 double mainExtras = 0.0; ///< Main + extras time
46 double completionist = 0.0; ///< 100% completion time
47 double allStyles = 0.0; ///< Average across all styles
48
49 // Match score for fuzzy matching
50 double similarity = 0.0; ///< How well the name matches (0-1)
51};
52
53// ==============================================================================
54// HowLongToBeat Service
55// ==============================================================================
56
57/**
58 * @class HLTBService
59 * @brief Service for fetching game time estimates from HowLongToBeat
60 *
61 * Usage:
62 * @code
63 * auto info = HLTBService::getGameInfo("The Witcher 3: Wild Hunt");
64 * if (info.found) {
65 * Logger::info("Main story: {} hours", info.mainStoryHours);
66 * }
67 * @endcode
68 */
70{
71 public:
72 // Callback types
73 using InfoCallback = std::function<void(const HowLongToBeatInfo&)>;
74 using SearchCallback = std::function<void(const std::vector<HLTBSearchResult>&)>;
75
76 // ==========================================================================
77 // Synchronous API
78 // ==========================================================================
79
80 /**
81 * @brief Get time estimates for a game by name
82 * @param gameName Game name to search for
83 * @return HowLongToBeatInfo with time estimates
84 *
85 * Searches HLTB and returns the best matching result.
86 * Results are cached with a 30-day TTL.
87 */
88 static HowLongToBeatInfo getGameInfo(const std::string& gameName);
89
90 /**
91 * @brief Get time estimates by HLTB game ID
92 * @param hltbId HowLongToBeat game ID
93 * @return HowLongToBeatInfo with time estimates
94 */
95 static HowLongToBeatInfo getGameInfoById(int hltbId);
96
97 /**
98 * @brief Search for games on HLTB
99 * @param query Search query
100 * @param maxResults Maximum results to return
101 * @return Vector of search results
102 */
103 static std::vector<HLTBSearchResult> search(const std::string& query, int maxResults = 10);
104
105 // ==========================================================================
106 // Asynchronous API
107 // ==========================================================================
108
109 /**
110 * @brief Get game info asynchronously
111 * @param gameName Game name to search for
112 * @param callback Function called with results
113 */
114 static void getGameInfoAsync(const std::string& gameName, const InfoCallback& callback);
115
116 /**
117 * @brief Search asynchronously
118 * @param query Search query
119 * @param callback Function called with results
120 * @param maxResults Maximum results
121 */
122 static void searchAsync(const std::string& query, const SearchCallback& callback,
123 int maxResults = 10);
124
125 // ==========================================================================
126 // Batch Operations
127 // ==========================================================================
128
129 /**
130 * @brief Fetch HLTB info for multiple games
131 * @param games Games to fetch info for (updates in place)
132 * @param progressCallback Optional callback for progress updates
133 *
134 * Efficiently fetches HLTB data for multiple games, skipping those
135 * that already have fresh data.
136 */
137 static void
138 batchFetch(std::vector<Game*>& games,
139 const std::function<void(int completed, int total)>& progressCallback = nullptr);
140
141 // ==========================================================================
142 // Cache Management
143 // ==========================================================================
144
145 /**
146 * @brief Clear the HLTB cache
147 */
148 static void clearCache();
149
150 /**
151 * @brief Get cache statistics
152 */
154
155 private:
156 // HTTP helpers
157 static std::string httpPost(const std::string& url, const std::string& body,
158 const std::string& contentType);
159 static std::string urlEncode(const std::string& str);
160
161 // Parsing
162 static std::vector<HLTBSearchResult> parseSearchResponse(const std::string& json);
163 static HowLongToBeatInfo parseGameDetails(const HLTBSearchResult& result);
164
165 // Fuzzy matching
166 static double calculateSimilarity(const std::string& a, const std::string& b);
167 static std::string normalizeTitle(const std::string& title);
168};
Service for fetching game time estimates from HowLongToBeat.
static void getGameInfoAsync(const std::string &gameName, const InfoCallback &callback)
Get game info asynchronously.
std::function< void(const HowLongToBeatInfo &)> InfoCallback
static HowLongToBeatInfo getGameInfoById(int hltbId)
Get time estimates by HLTB game ID.
static std::vector< HLTBSearchResult > search(const std::string &query, int maxResults=10)
Search for games on HLTB.
static void clearCache()
Clear the HLTB cache.
static void batchFetch(std::vector< Game * > &games, const std::function< void(int completed, int total)> &progressCallback=nullptr)
Fetch HLTB info for multiple games.
static HowLongToBeatInfo getGameInfo(const std::string &gameName)
Get time estimates for a game by name.
std::function< void(const std::vector< HLTBSearchResult > &)> SearchCallback
static LRUCache< std::string, HowLongToBeatInfo >::Stats getCacheStats()
Get cache statistics.
static void searchAsync(const std::string &query, const SearchCallback &callback, int maxResults=10)
Search asynchronously.
nlohmann::json json
A single search result from HowLongToBeat.
std::string imageUrl
Cover image URL.
double completionist
100% completion time
double similarity
How well the name matches (0-1).
std::string name
Game name on HLTB.
int releaseYear
Release year.
double allStyles
Average across all styles.
int gameId
HLTB game ID.
double mainStory
Main story time.
double mainExtras
Main + extras time.
Time-to-beat estimates from HowLongToBeat.com.
Definition game.h:672