@@ -8,12 +8,14 @@ import { resolve, jsonDb, datetime, filenamify, prompt, confirm, notify, html_ga
88import { cfg } from './src/config.js' ;
99import { getMobileGames } from './src/epic-games-mobile.js' ;
1010import { gpUrlToStoreUrls } from './src/gp.js' ;
11+ import { getUsername , setUsername , writeAccountsDb } from './src/accounts.js' ;
1112
1213const screenshot = ( ...a ) => resolve ( cfg . dir . screenshots , 'epic-games' , ...a ) ;
1314
1415const URL_CLAIM = 'https://store.epicgames.com/en-US/free-games' ;
1516const URL_LOGIN = 'https://www.epicgames.com/id/login?lang=en-US&noHostRedirect=true&redirectUrl=' + URL_CLAIM ;
1617const GAMERPOWER_API_URL = 'https://www.gamerpower.com/api/giveaways?platform=epic-games-store&type=game' ;
18+ const PLATFORM = 'epic-games' ;
1719
1820console . log ( datetime ( ) , 'started checking epic-games' ) ;
1921
@@ -26,16 +28,17 @@ function extractGameIdFromUrl(url) {
2628}
2729
2830function isGpGameAlreadyClaimed ( storeUrl ) {
31+ const username = getUsername ( PLATFORM , cfg . eg_email ) ;
32+ if ( ! username ) return false ; // Username not known yet
33+
2934 const game_id = extractGameIdFromUrl ( storeUrl ) ;
30-
31- // Check if any user has claimed this game
32- for ( const [ username , games ] of Object . entries ( db . data ) ) {
33- if ( games [ game_id ] ?. status === 'claimed' || games [ game_id ] ?. status === 'existed' ) {
34- console . log ( `[GamerPower] Already claimed by ${ username } : ${ storeUrl } -> ${ game_id } ` ) ;
35- return true ;
36- }
35+ const games = db . data [ username ] ;
36+
37+ if ( games ?. [ game_id ] ?. status === 'claimed' || games ?. [ game_id ] ?. status === 'existed' ) {
38+ console . log ( `[GamerPower] Already claimed by ${ username } : ${ storeUrl } -> ${ game_id } ` ) ;
39+ return true ;
3740 }
38-
41+
3942 return false ;
4043}
4144
@@ -56,7 +59,7 @@ async function getUnclaimedGpUrls() {
5659 // Filter out already claimed games
5760 const unclaimed = epicGames . filter ( g => ! isGpGameAlreadyClaimed ( g . storeUrl ) ) ;
5861 console . log ( `[GamerPower] ${ unclaimed . length } unclaimed games` ) ;
59-
62+
6063 return unclaimed . map ( g => g . storeUrl ) ;
6164}
6265
@@ -66,10 +69,15 @@ async function getUnclaimedGpUrls() {
6669
6770if ( cfg . time ) console . time ( 'startup' ) ;
6871
72+ // EG_CHECK_GP requires EG_EMAIL to be set for claimed status tracking
73+ if ( cfg . eg_check_gp && ! cfg . eg_email ) {
74+ throw new Error ( 'EG_CHECK_GP requires EG_EMAIL to be set' ) ;
75+ }
76+
6977// Check GamerPower first (before starting main browser)
7078const gpUrls = await getUnclaimedGpUrls ( ) ;
7179
72- // If EG_CHECK_GP is enabled and no unclaimed games , exit early without opening browser
80+ // If EG_CHECK_GP is enabled and no unclaimed giveaways , exit early without opening browser
7381if ( cfg . eg_check_gp && gpUrls . length === 0 ) {
7482 console . log ( 'No unclaimed GamerPower giveaways. Exiting.' ) ;
7583 process . exit ( 0 ) ;
@@ -186,7 +194,14 @@ try {
186194 }
187195 user = await page . locator ( 'egs-navigation' ) . getAttribute ( 'displayname' ) ; // 'null' if !isloggedin
188196 console . log ( `Signed in as ${ user } ` ) ;
197+
198+ // Save email -> username mapping if email is configured
199+ if ( cfg . eg_email ) {
200+ setUsername ( PLATFORM , cfg . eg_email , user ) ;
201+ await writeAccountsDb ( ) ;
202+ }
189203 db . data [ user ] ||= { } ;
204+
190205 if ( cfg . time ) console . timeEnd ( 'login' ) ;
191206 if ( cfg . time ) console . time ( 'claim all games' ) ;
192207
0 commit comments