PageRenderTime 30ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Code/GameSDK/GameDll/Network/Lobby/ScriptBind_MatchMaking.cpp

https://gitlab.com/blackbird91/CS188_AI_Game
C++ | 490 lines | 339 code | 82 blank | 69 comment | 44 complexity | 698908d28640110c6964827006f35baf MD5 | raw file
  1. /*
  2. * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. * its licensors.
  4. *
  5. * For complete copyright and license terms please see the LICENSE at the root of this
  6. * distribution (the "License"). All use of this software is governed by the License,
  7. * or, if provided, by the license below or the license accompanying this file. Do not
  8. * remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. *
  11. */
  12. // Original file Copyright Crytek GMBH or its affiliates, used under license.
  13. // Description: Script bindings needed to implement matchmaking in Lua
  14. #include "StdAfx.h"
  15. //////////////////////////////////////////////////////////////////////////
  16. // This Include
  17. #include "ScriptBind_MatchMaking.h"
  18. #include "MatchMakingHandler.h"
  19. #include "DLCManager.h"
  20. #include "Network/Squad/SquadManager.h"
  21. #include "GameLobbyManager.h"
  22. #include "GameCVars.h"
  23. //------------------------------------------------------------------------
  24. // Constructor
  25. CScriptBind_MatchMaking::CScriptBind_MatchMaking( ISystem *pSystem )
  26. : m_pSystem( pSystem ),
  27. m_pLobbyManager( NULL )
  28. {
  29. Init( pSystem->GetIScriptSystem(), m_pSystem, 1 );
  30. RegisterMethods();
  31. RegisterGlobals();
  32. }
  33. //------------------------------------------------------------------------
  34. // Destructor
  35. CScriptBind_MatchMaking::~CScriptBind_MatchMaking()
  36. {
  37. }
  38. //------------------------------------------------------------------------
  39. void CScriptBind_MatchMaking::RegisterMethods()
  40. {
  41. #undef SCRIPT_REG_CLASSNAME
  42. #define SCRIPT_REG_CLASSNAME &CScriptBind_MatchMaking::
  43. SCRIPT_REG_TEMPLFUNC( IsSquadLeaderOrSolo, "");
  44. SCRIPT_REG_TEMPLFUNC( CancelSearch, "" );
  45. SCRIPT_REG_TEMPLFUNC( IsJoiningSession, "" );
  46. SCRIPT_REG_TEMPLFUNC( IsSessionHost, "" );
  47. SCRIPT_REG_TEMPLFUNC( IsInSession, "" );
  48. SCRIPT_REG_TEMPLFUNC( HasGameStarted, "" );
  49. SCRIPT_REG_TEMPLFUNC( HaveEnoughPlayersToStart, "" );
  50. SCRIPT_REG_TEMPLFUNC( GetNumPlayersInCurrentSession, "" );
  51. SCRIPT_REG_TEMPLFUNC( GetNumPlayersInSquad, "" );
  52. SCRIPT_REG_TEMPLFUNC( GetMaxNumPlayers, "" );
  53. SCRIPT_REG_TEMPLFUNC( GetCurrentRegion, "" );
  54. SCRIPT_REG_TEMPLFUNC( GetCurrentLanguage, "" );
  55. SCRIPT_REG_TEMPLFUNC( GetAverageSkillScore, "" );
  56. SCRIPT_REG_TEMPLFUNC( GetCurrentMatchMakingVersionNum, "" );
  57. SCRIPT_REG_TEMPLFUNC( GetCurrentPlaylist, "" );
  58. SCRIPT_REG_TEMPLFUNC( GetCurrentVariant, "" );
  59. SCRIPT_REG_TEMPLFUNC( GetCurrentPing, "" );
  60. // SCRIPT_REG_TEMPLFUNC( GetCurrentGameMode, "" );
  61. // SCRIPT_REG_TEMPLFUNC( GetCurrentMap, "" );
  62. SCRIPT_REG_TEMPLFUNC( GetAvailableDLCs, "" );
  63. SCRIPT_REG_TEMPLFUNC( StartSearch, "freeSlots,maxResults,searchParams" );
  64. SCRIPT_REG_TEMPLFUNC( MergeWithServer, "sessionId" );
  65. SCRIPT_REG_TEMPLFUNC( JoinServer, "sessionId" );
  66. SCRIPT_REG_TEMPLFUNC( CreateServer, "sessionParams" );
  67. SCRIPT_REG_TEMPLFUNC( RequestUpdateCall, "timeToCall" );
  68. SCRIPT_REG_TEMPLFUNC( MMLog, "message,isError" );
  69. }
  70. //------------------------------------------------------------------------
  71. void CScriptBind_MatchMaking::RegisterGlobals()
  72. {
  73. //operators for MM searches
  74. SCRIPT_REG_GLOBAL( eCSSO_Equal );
  75. SCRIPT_REG_GLOBAL( eCSSO_NotEqual );
  76. SCRIPT_REG_GLOBAL( eCSSO_LessThan );
  77. SCRIPT_REG_GLOBAL( eCSSO_LessThanEqual );
  78. SCRIPT_REG_GLOBAL( eCSSO_GreaterThan );
  79. SCRIPT_REG_GLOBAL( eCSSO_GreaterThanEqual );
  80. SCRIPT_REG_GLOBAL( eCSSO_BitwiseAndNotEqualZero );
  81. }
  82. //------------------------------------------------------------------------
  83. void CScriptBind_MatchMaking::AttachTo( CMatchMakingHandler* pMatchMaking, CGameLobbyManager *pLobbyManager )
  84. {
  85. m_pLobbyManager = pLobbyManager;
  86. IScriptTable *pScriptTable = pMatchMaking->GetScriptTable();
  87. if (pScriptTable)
  88. {
  89. SmartScriptTable thisTable(m_pSS);
  90. thisTable->Delegate(GetMethodsTable());
  91. pScriptTable->SetValue("bindings", thisTable);
  92. }
  93. }
  94. //------------------------------------------------------------------------
  95. int CScriptBind_MatchMaking::IsSquadLeaderOrSolo( IFunctionHandler *pH )
  96. {
  97. if( g_pGame )
  98. {
  99. if( CSquadManager* pSquadManager = g_pGame->GetSquadManager() )
  100. {
  101. if( (!pSquadManager->InSquad()) || pSquadManager->InCharge() )
  102. {
  103. return pH->EndFunction(1);
  104. }
  105. }
  106. }
  107. return pH->EndFunction(0);
  108. }
  109. //------------------------------------------------------------------------
  110. int CScriptBind_MatchMaking::CancelSearch( IFunctionHandler *pH )
  111. {
  112. if( CMatchMakingHandler* pMMHandler = m_pLobbyManager->GetMatchMakingHandler() )
  113. {
  114. pMMHandler->CancelSearch();
  115. }
  116. return pH->EndFunction();
  117. }
  118. //------------------------------------------------------------------------
  119. int CScriptBind_MatchMaking::IsJoiningSession( IFunctionHandler *pH )
  120. {
  121. //if lobby state is joining we are joining
  122. if( CGameLobby* pLobby = m_pLobbyManager->GetGameLobby() )
  123. {
  124. if( pLobby->IsCreatingOrJoiningSession() )
  125. {
  126. return pH->EndFunction(1);
  127. }
  128. }
  129. //also check next lobby
  130. if( CGameLobby* pNextLobby = m_pLobbyManager->GetNextGameLobby() )
  131. {
  132. if( pNextLobby->IsCreatingOrJoiningSession() )
  133. {
  134. return pH->EndFunction(1);
  135. }
  136. }
  137. return pH->EndFunction(0);
  138. }
  139. //------------------------------------------------------------------------
  140. int CScriptBind_MatchMaking::IsSessionHost( IFunctionHandler *pH )
  141. {
  142. //assume this means are we the primary session host
  143. if( CGameLobby* pLobby = m_pLobbyManager->GetGameLobby() )
  144. {
  145. if( pLobby->IsServer() )
  146. {
  147. return pH->EndFunction(1);
  148. }
  149. }
  150. return pH->EndFunction(0);
  151. }
  152. //------------------------------------------------------------------------
  153. int CScriptBind_MatchMaking::IsInSession( IFunctionHandler *pH )
  154. {
  155. if( CGameLobby* pLobby = m_pLobbyManager->GetGameLobby() )
  156. {
  157. if( pLobby->IsCurrentlyInSession() )
  158. {
  159. return pH->EndFunction(1);
  160. }
  161. }
  162. return pH->EndFunction(0);
  163. }
  164. //------------------------------------------------------------------------
  165. int CScriptBind_MatchMaking::HasGameStarted( IFunctionHandler *pH )
  166. {
  167. //if lobby state is lobby, game etc. we are in a session
  168. if( CGameLobby* pLobby = m_pLobbyManager->GetGameLobby() )
  169. {
  170. ELobbyState currentState = pLobby->GetState();
  171. if( currentState == eLS_PreGame || currentState == eLS_Game || currentState == eLS_EndSession || currentState == eLS_GameEnded )
  172. {
  173. return pH->EndFunction(1);
  174. }
  175. }
  176. return pH->EndFunction(0);
  177. }
  178. //------------------------------------------------------------------------
  179. int CScriptBind_MatchMaking::HaveEnoughPlayersToStart( IFunctionHandler *pH )
  180. {
  181. if( CGameLobby* pLobby = m_pLobbyManager->GetGameLobby() )
  182. {
  183. int players = pLobby->GetSessionNames().Size();
  184. int playersNeeded = g_pGameCVars->g_minplayerlimit;
  185. if( players >= playersNeeded )
  186. {
  187. return pH->EndFunction( 1 );
  188. }
  189. }
  190. return pH->EndFunction(0);
  191. }
  192. //------------------------------------------------------------------------
  193. int CScriptBind_MatchMaking::GetNumPlayersInCurrentSession( IFunctionHandler *pH )
  194. {
  195. if( CGameLobby* pLobby = m_pLobbyManager->GetGameLobby() )
  196. {
  197. int players = pLobby->GetSessionNames().Size();
  198. return pH->EndFunction( players );
  199. }
  200. return pH->EndFunction(0);
  201. }
  202. //------------------------------------------------------------------------
  203. int CScriptBind_MatchMaking::GetNumPlayersInSquad( IFunctionHandler* pH )
  204. {
  205. if( CSquadManager* pSquadMan = g_pGame->GetSquadManager() )
  206. {
  207. int players = pSquadMan->GetSquadSize();
  208. return pH->EndFunction( players );
  209. }
  210. return pH->EndFunction(0);
  211. }
  212. //------------------------------------------------------------------------
  213. int CScriptBind_MatchMaking::GetMaxNumPlayers( IFunctionHandler* pH )
  214. {
  215. return pH->EndFunction( MAX_PLAYER_LIMIT );
  216. }
  217. const int numSearchIdTypes = 12;
  218. struct SearchKeyStringIDPair
  219. {
  220. const char* key;
  221. ELOBBYIDS id;
  222. };
  223. SearchKeyStringIDPair idLookup[ numSearchIdTypes ] =
  224. {
  225. { "gamemode", LID_MATCHDATA_GAMEMODE },
  226. { "gamemap", LID_MATCHDATA_MAP },
  227. { "active", LID_MATCHDATA_ACTIVE },
  228. { "version", LID_MATCHDATA_VERSION },
  229. { "required_dlcs", LID_MATCHDATA_REQUIRED_DLCS },
  230. { "playlist", LID_MATCHDATA_PLAYLIST },
  231. { "variant", LID_MATCHDATA_VARIANT },
  232. { "skill", LID_MATCHDATA_SKILL },
  233. { "language", LID_MATCHDATA_LANGUAGE },
  234. // Misc search parameters require support
  235. // { "misc_1", LID_MATCHDATA_MISC1 },
  236. // { "misc_2", LID_MATCHDATA_MISC2 },
  237. // { "misc_3", LID_MATCHDATA_MISC3 },
  238. };
  239. //------------------------------------------------------------------------
  240. int CScriptBind_MatchMaking::StartSearch( IFunctionHandler *pH, int freeSlotsRequired, int maxResults, SmartScriptTable searchParams )
  241. {
  242. if( CMatchMakingHandler* pMatchMaking = m_pLobbyManager->GetMatchMakingHandler() )
  243. {
  244. int dataIndex = 0;
  245. SCrySessionSearchData data[ FIND_GAMES_SEARCH_NUM_DATA ];
  246. //for every type of data we can search on
  247. for( uint32 iKey = 0; iKey < numSearchIdTypes; iKey++ )
  248. {
  249. //see if there is a key for it in search params table
  250. SmartScriptTable entry;
  251. if( searchParams->GetValue( idLookup[ iKey ].key, entry ) )
  252. {
  253. //if there is read the value and operator
  254. ScriptAnyValue valueVal;
  255. int operatorVal;
  256. if( entry->GetValueAny( "val", valueVal ) )
  257. {
  258. if( entry->GetValue( "operator", operatorVal ) )
  259. {
  260. //and set them into the search data
  261. data[ dataIndex ].m_data.m_id = idLookup[ iKey ].id;
  262. switch( valueVal.type )
  263. {
  264. case ANY_TNUMBER:
  265. data[ dataIndex ].m_data.m_type = eCLUDT_Int32;
  266. data[ dataIndex ].m_data.m_int32 = (int32)valueVal.number;
  267. break;
  268. case ANY_TBOOLEAN:
  269. data[ dataIndex ].m_data.m_type = eCLUDT_Int32;
  270. data[ dataIndex ].m_data.m_int32 = (int32)valueVal.b;
  271. break;
  272. case ANY_THANDLE:
  273. data[ dataIndex ].m_data.m_type = eCLUDT_Int32;
  274. data[ dataIndex ].m_data.m_int32 = (int32)(TRUNCATE_PTR)valueVal.ptr;
  275. break;
  276. default:
  277. MMLog( pH, "MMLua: Unsupported type in search data", true );
  278. }
  279. //CryLog( "MMLua: Search Session Parameter, id %d, value %d, operator %d", data[ dataIndex ].m_data.m_id, data[ dataIndex ].m_data.m_int32, operatorVal );
  280. data[ dataIndex ].m_operator = static_cast<ECrySessionSearchOperator>(operatorVal);
  281. dataIndex++;
  282. }
  283. }
  284. }
  285. }
  286. //pass the final search on to the handler
  287. pMatchMaking->Search( freeSlotsRequired, maxResults, data, dataIndex );
  288. }
  289. return pH->EndFunction( true );
  290. }
  291. int CScriptBind_MatchMaking::JoinServer( IFunctionHandler *pH, int sessionId )
  292. {
  293. if( CMatchMakingHandler* pMatchMaking = m_pLobbyManager->GetMatchMakingHandler() )
  294. {
  295. pMatchMaking->Join( sessionId );
  296. }
  297. return pH->EndFunction();
  298. }
  299. int CScriptBind_MatchMaking::CreateServer( IFunctionHandler *pH, SmartScriptTable sessionParams )
  300. {
  301. if( CMatchMakingHandler* pMatchMaking = m_pLobbyManager->GetMatchMakingHandler() )
  302. {
  303. //get rid of any stale settings
  304. pMatchMaking->ClearSessionParameters();
  305. //process the parameters
  306. //for every type of data sessions can broadcast
  307. for( uint32 iKey = 0; iKey < numSearchIdTypes; iKey++ )
  308. {
  309. //see if there is a key for it in session params table
  310. ScriptAnyValue valueVal;
  311. if( sessionParams->GetValueAny( idLookup[ iKey ].key, valueVal ) )
  312. {
  313. if( valueVal.type != ANY_TNIL )
  314. {
  315. //if there is add it to our parameters
  316. pMatchMaking->NewSessionParameter( idLookup[ iKey ].id, valueVal );
  317. }
  318. }
  319. }
  320. }
  321. //ask the networking to create a session
  322. if( CGameLobby* pLobby = m_pLobbyManager->GetGameLobby() )
  323. {
  324. pLobby->FindGameCreateGame();
  325. }
  326. return pH->EndFunction();
  327. }
  328. int CScriptBind_MatchMaking::MergeWithServer( IFunctionHandler *pH, int sessionId )
  329. {
  330. if( CMatchMakingHandler* pMatchMaking = m_pLobbyManager->GetMatchMakingHandler() )
  331. {
  332. pMatchMaking->Merge( sessionId );
  333. }
  334. return pH->EndFunction();
  335. }
  336. int CScriptBind_MatchMaking::RequestUpdateCall( IFunctionHandler* pH, float timeToCall )
  337. {
  338. if( CMatchMakingHandler* pMatchMaking = m_pLobbyManager->GetMatchMakingHandler() )
  339. {
  340. pMatchMaking->RequestSubscribedUpdate( timeToCall );
  341. }
  342. return pH->EndFunction();
  343. }
  344. int CScriptBind_MatchMaking::MMLog( IFunctionHandler *pH, const char* message, bool isError )
  345. {
  346. if( CMatchMakingHandler* pMatchMaking = m_pLobbyManager->GetMatchMakingHandler() )
  347. {
  348. pMatchMaking->MMLog( message, isError );
  349. }
  350. return pH->EndFunction();
  351. }
  352. int CScriptBind_MatchMaking::GetCurrentRegion( IFunctionHandler* pH )
  353. {
  354. //TODO: sort out how we want to handle region/country filtering on each platform (C2 PC did no region filtering)
  355. int regValue = 0;
  356. #if GAMELOBBY_USE_COUNTRY_FILTERING
  357. regValue = g_pGame->GetUserRegion();
  358. #endif
  359. return pH->EndFunction( ScriptHandle( regValue ) );
  360. }
  361. int CScriptBind_MatchMaking::GetCurrentLanguage( IFunctionHandler* pH )
  362. {
  363. return pH->EndFunction( gEnv->pSystem->GetPlatformOS()->GetSystemLanguage() );
  364. }
  365. int CScriptBind_MatchMaking::GetCurrentMatchMakingVersionNum( IFunctionHandler* pH )
  366. {
  367. return pH->EndFunction( ScriptHandle( GameLobbyData::GetVersion() ) );
  368. }
  369. int CScriptBind_MatchMaking::GetCurrentPlaylist( IFunctionHandler* pH )
  370. {
  371. return pH->EndFunction( ScriptHandle( (int)GameLobbyData::GetPlaylistId() ) );
  372. }
  373. int CScriptBind_MatchMaking::GetCurrentVariant( IFunctionHandler* pH )
  374. {
  375. return pH->EndFunction( GameLobbyData::GetVariantId() );
  376. }
  377. int CScriptBind_MatchMaking::GetAvailableDLCs( IFunctionHandler* pH )
  378. {
  379. return pH->EndFunction( g_pGame->GetDLCManager()->GetSquadCommonDLCs() );
  380. }
  381. int CScriptBind_MatchMaking::GetAverageSkillScore( IFunctionHandler* pH )
  382. {
  383. if( CGameLobby* pLobby = m_pLobbyManager->GetGameLobby() )
  384. {
  385. return pH->EndFunction( pLobby->CalculateAverageSkill() );
  386. }
  387. return pH->EndFunction( 0 );
  388. }
  389. int CScriptBind_MatchMaking::GetCurrentPing( IFunctionHandler* pH )
  390. {
  391. if( CGameLobby* pLobby = m_pLobbyManager->GetGameLobby() )
  392. {
  393. return pH->EndFunction( pLobby->GetCurrentPingToHost() );
  394. }
  395. return pH->EndFunction( 0 );
  396. }
  397. /*
  398. //TODO - support individual map + game mode searches if required
  399. int CScriptBind_MatchMaking::GetCurrentGameMode( IFunctionHandler* pH )
  400. {
  401. }
  402. int CScriptBind_MatchMaking::GetCurrentMap( IFunctionHandler* pH )
  403. {
  404. }
  405. */