@@ -2,21 +2,57 @@ use std::env;
22
33use crate :: structs:: old_games;
44use bf_sparta:: cookie:: Cookie ;
5+ use bf_sparta:: sparta_api;
56use bson:: Document ;
67use chrono:: { DateTime , Utc } ;
8+ use futures:: StreamExt ;
79use mongodb:: error:: Result ;
10+ use mongodb:: options:: FindOptions ;
811use mongodb:: { options:: ReplaceOptions , results:: UpdateResult , Client , Collection } ;
912use serde:: { Deserialize , Serialize } ;
1013
1114pub struct MongoClient {
1215 pub backend_cookies : Collection < BackendCookie > ,
16+ pub community_cookies : Collection < CommunityCookie > ,
17+ pub cookie_check : Collection < CookieCheck > ,
1318 pub community_servers : Collection < Document > ,
1419 pub community_groups : Collection < Document > ,
1520 pub player_list : Collection < Document > ,
1621 pub logging : Collection < Document > ,
1722 pub old_games_servers : Collection < old_games:: OldGameServerList > ,
1823}
1924
25+ #[ derive( Serialize , Deserialize , Debug , Clone ) ]
26+ pub struct CookieCheck {
27+ pub _id : String ,
28+ pub prefix : String ,
29+ #[ serde( rename = "personaId" ) ]
30+ pub persona_id : String ,
31+ #[ serde( rename = "sessionId" ) ]
32+ pub session_id : String ,
33+ }
34+
35+ #[ derive( Serialize , Deserialize , Debug , Clone ) ]
36+ pub struct CommunityCookie {
37+ pub _id : String ,
38+ pub sid : String ,
39+ pub remid : String ,
40+ #[ serde( rename = "personaId" ) ]
41+ pub persona_id : String ,
42+ pub username : String ,
43+ #[ serde( rename = "supportedGames" ) ]
44+ pub supported_games : Vec < String > ,
45+ }
46+
47+ impl From < CommunityCookie > for Cookie {
48+ fn from ( cookie : CommunityCookie ) -> Self {
49+ Cookie {
50+ remid : cookie. remid ,
51+ sid : cookie. sid ,
52+ }
53+ }
54+ }
55+
2056#[ derive( Serialize , Deserialize , Debug , Clone ) ]
2157pub struct BackendCookie {
2258 pub _id : String ,
@@ -71,6 +107,8 @@ impl MongoClient {
71107 let gamestats_db = client. database ( "gamestats" ) ;
72108
73109 Ok ( MongoClient {
110+ cookie_check : db. collection ( "cookieCheck" ) ,
111+ community_cookies : db. collection ( "communityCookies" ) ,
74112 backend_cookies : db. collection ( "backendCookies" ) ,
75113 community_servers : db. collection ( "communityServers" ) ,
76114 community_groups : db. collection ( "communityGroups" ) ,
@@ -166,6 +204,56 @@ impl MongoClient {
166204 ) )
167205 }
168206
207+ pub async fn get_random_cookie ( & mut self ) -> anyhow:: Result < Cookie > {
208+ let mut cookie_check = self
209+ . cookie_check
210+ . find ( bson:: doc! { } )
211+ . with_options (
212+ FindOptions :: builder ( )
213+ . sort ( bson:: doc! { "timeStamp" : -1 } )
214+ . build ( ) ,
215+ )
216+ . await ?;
217+ let mut result_cookie = None ;
218+ while let Some ( maybe_check) = cookie_check. next ( ) . await {
219+ let check = maybe_check?;
220+ match self
221+ . community_cookies
222+ . find_one ( bson:: doc! { "_id" : check. _id} )
223+ . await
224+ {
225+ Ok ( e) => {
226+ if let Some ( cookie) = e {
227+ match sparta_api:: get_token (
228+ cookie. clone ( ) . into ( ) ,
229+ "pc" ,
230+ "tunguska" ,
231+ "en-us" ,
232+ )
233+ . await
234+ {
235+ Ok ( _) => {
236+ result_cookie = Some ( cookie) ;
237+ break ;
238+ }
239+ Err ( e) => {
240+ log:: info!( "Cookie not valid, trying another one - {}" , e) ;
241+ }
242+ }
243+ } else {
244+ continue ;
245+ }
246+ }
247+ Err ( _) => continue ,
248+ } ;
249+ }
250+
251+ Ok ( match result_cookie {
252+ Some ( cookie) => cookie. into ( ) ,
253+ None => anyhow:: bail!( "no cookie found" ) ,
254+ } )
255+ }
256+
169257 pub async fn gather_old_title (
170258 & mut self ,
171259 game_name : & str ,
0 commit comments