@@ -45,7 +45,21 @@ const User = new Steam({
45
45
dataDirectory : "SteamData" ,
46
46
} ) ;
47
47
48
+ let playingOnOtherSession = false ;
49
+ let currentNotification ;
50
+ let authenticated = false ;
51
+ let MIN_REQUEST_TIME = 60 * 1000 ;
52
+ let LOG_ON_INTERVAL = 10 * 60 * 1000 ;
53
+ let REFRESH_GAMES_INTERVAL = 5 * 60 * 1000 ;
54
+ let lastGameRefreshTime = new Date ( 0 ) ;
55
+ let lastLogOnTime = new Date ( 0 ) ;
56
+ let onlyLogInAfter = new Date ( 0 ) ;
57
+
48
58
const logOn = ( ) => {
59
+ if ( authenticated ) return ;
60
+ if ( Date . now ( ) - lastLogOnTime <= MIN_REQUEST_TIME ) return ;
61
+ if ( Date . now ( ) < onlyLogInAfter ) return ;
62
+ console . log ( "Logging in..." ) ;
49
63
User . logOn ( {
50
64
accountName : USERNAME ,
51
65
password : PASSWORD ,
@@ -56,22 +70,24 @@ const logOn = () => {
56
70
: undefined ,
57
71
autoRelogin : true ,
58
72
} ) ;
73
+ lastLogOnTime = Date . now ( ) ;
59
74
} ;
60
75
61
76
const panic = ( message = "Exiting..." ) => {
62
77
console . error ( message ) ;
63
78
process . exit ( 1 ) ;
64
79
} ;
65
80
66
- let playingOnOtherSession = false ;
67
- let currentNotification ;
68
81
const refreshGames = ( ) => {
82
+ if ( ! authenticated ) return ;
69
83
let notification ;
70
84
if ( playingOnOtherSession ) {
71
85
notification = "Farming is paused." ;
72
86
} else {
87
+ if ( Date . now ( ) - lastGameRefreshTime <= MIN_REQUEST_TIME ) return ;
73
88
User . gamesPlayed ( SHOULD_PLAY ) ;
74
89
notification = "Farming..." ;
90
+ lastGameRefreshTime = Date . now ( ) ;
75
91
}
76
92
if ( currentNotification !== notification ) {
77
93
currentNotification = notification ;
@@ -95,22 +111,36 @@ User.on("playingState", (blocked, app) => {
95
111
} ) ;
96
112
97
113
User . on ( "loggedOn" , ( ) => {
114
+ authenticated = true ;
98
115
console . log ( `Successfully logged in to Steam with ID ${ User . steamID } ` ) ;
99
116
if ( PERSONA !== undefined ) User . setPersona ( PERSONA ) ;
100
-
101
- // Allow time to receive the playingState event to see if playing on another session if that's the case
102
- setTimeout ( refreshGames , 5000 ) ;
117
+ refreshGames ( ) ;
103
118
} ) ;
104
119
105
120
User . on ( "error" , ( e ) => {
106
- if ( e . eresult === Steam . EResult . LoggedInElsewhere ) {
107
- console . log ( "Got kicked by other Steam session. Logging in again..." ) ;
108
- logOn ( ) ;
109
- return ;
121
+ switch ( e . eresult ) {
122
+ case Steam . EResult . LoggedInElsewhere : {
123
+ authenticated = false ;
124
+ console . log (
125
+ "Got kicked by other Steam session. Will log in shortly..."
126
+ ) ;
127
+ logOn ( ) ;
128
+ return ;
129
+ }
130
+ case Steam . EResult . RateLimitExceeded : {
131
+ authenticated = false ;
132
+ onlyLogInAfter = Date . now ( ) + 31 * 60 * 1000 ;
133
+ console . log (
134
+ "Got rate limited by Steam. Will try logging in again in 30 minutes."
135
+ ) ;
136
+ return ;
137
+ }
138
+ default : {
139
+ panic ( `Got an error from Steam: "${ e . message } ".` ) ;
140
+ }
110
141
}
111
- panic ( `Got an error from Steam: "${ e . message } ".` ) ;
112
142
} ) ;
113
143
114
144
logOn ( ) ;
115
-
116
- setInterval ( refreshGames , 20 * 1000 ) ;
145
+ setInterval ( logOn , LOG_ON_INTERVAL ) ;
146
+ setInterval ( refreshGames , REFRESH_GAMES_INTERVAL ) ;
0 commit comments