WOLQuickMatch.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /******************************************************************************
  19. *
  20. * FILE
  21. * $Archive: /Commando/Code/Commando/WOLQuickMatch.cpp $
  22. *
  23. * DESCRIPTION
  24. *
  25. * PROGRAMMER
  26. * $Author: Denzil_l $
  27. *
  28. * VERSION INFO
  29. * $Revision: 37 $
  30. * $Modtime: 2/20/02 5:30p $
  31. *
  32. ******************************************************************************/
  33. #include "WOLQuickMatch.h"
  34. #include <WWOnline\PingProfile.h>
  35. #include <WWOnline\WaitCondition.h>
  36. #include <WWOnline\WOLProduct.h>
  37. #include <WWOnline\WOLConnect.h>
  38. #include <WWOnline\WOLServer.h>
  39. #include <WWOnline\WOLLadder.h>
  40. #include <WWLib\CPUDetect.h>
  41. #include <WWDebug\WWDebug.h>
  42. #include "cnetwork.h"
  43. #include "translatedb.h"
  44. #include "string_ids.h"
  45. using namespace WWOnline;
  46. typedef void (*QMDispatchFunc)(WOLQuickMatch*, const wchar_t*);
  47. struct QMResponseDispatch
  48. {
  49. const wchar_t* Token;
  50. QMDispatchFunc Dispatch;
  51. };
  52. #define QUICKMATCH_CHANNELNAME L"lob_39_0"
  53. #define QUICKMATCH_BOTNAME L"matchbot"
  54. /******************************************************************************
  55. *
  56. * NAME
  57. * WOLQuickMatch::Create
  58. *
  59. * DESCRIPTION
  60. * Create an instance to a quickmatch game matcher.
  61. *
  62. * INPUTS
  63. * NONE
  64. *
  65. * RESULT
  66. * Quickmatch - Quickmatch instance.
  67. *
  68. ******************************************************************************/
  69. WOLQuickMatch* WOLQuickMatch::Create(void)
  70. {
  71. WOLQuickMatch* match = new WOLQuickMatch;
  72. if (match)
  73. {
  74. if (match->FinalizeCreate())
  75. {
  76. return match;
  77. }
  78. match->Release_Ref();
  79. }
  80. return NULL;
  81. }
  82. /******************************************************************************
  83. *
  84. * NAME
  85. * WOLQuickMatch::WOLQuickMatch
  86. *
  87. * DESCRIPTION
  88. * Default constructor
  89. *
  90. * INPUTS
  91. * NONE
  92. *
  93. * RESULT
  94. * NONE
  95. *
  96. ******************************************************************************/
  97. WOLQuickMatch::WOLQuickMatch()
  98. {
  99. WWDEBUG_SAY(("WOLQuickMatch: Instantiating\n"));
  100. }
  101. /******************************************************************************
  102. *
  103. * NAME
  104. * WOLQuickMatch::~WOLQuickMatch
  105. *
  106. * DESCRIPTION
  107. * Destructor
  108. *
  109. * INPUTS
  110. * NONE
  111. *
  112. * RESULT
  113. * NONE
  114. *
  115. ******************************************************************************/
  116. WOLQuickMatch::~WOLQuickMatch()
  117. {
  118. WWDEBUG_SAY(("WOLQuickMatch: Destructing\n"));
  119. }
  120. /******************************************************************************
  121. *
  122. * NAME
  123. * WOLQuickMatch::FinalizeCreate
  124. *
  125. * DESCRIPTION
  126. * Creation initializaton / finalization
  127. *
  128. * INPUTS
  129. * NONE
  130. *
  131. * RESULT
  132. * Success - True if successful
  133. *
  134. ******************************************************************************/
  135. bool WOLQuickMatch::FinalizeCreate(void)
  136. {
  137. mWOLSession = Session::GetInstance(false);
  138. if (!mWOLSession.IsValid())
  139. {
  140. WWDEBUG_SAY(("WOLQuickMatch: ERROR - WWOnline not instantiated\n"));
  141. return false;
  142. }
  143. return true;
  144. }
  145. /******************************************************************************
  146. *
  147. * NAME
  148. * WOLQuickMatch::ConnectClient
  149. *
  150. * DESCRIPTION
  151. * Connect a game client to the quickmatch bot.
  152. *
  153. * INPUTS
  154. * ChannelName - Name of matching channel
  155. * BotName - Name of matching bot
  156. *
  157. * RESULT
  158. * Wait - Wait condition to process.
  159. *
  160. ******************************************************************************/
  161. RefPtr<WaitCondition> WOLQuickMatch::ConnectClient(void)
  162. {
  163. WWDEBUG_SAY(("WOLQuickMatch: Connecting client to '%S' '%S'\n",
  164. QUICKMATCH_CHANNELNAME, QUICKMATCH_BOTNAME));
  165. // Make sure that we are logged on to WWOnline
  166. if (mWOLSession->GetConnectionStatus() != ConnectionConnected)
  167. {
  168. WWDEBUG_SAY(("WOLQuickMatch: ERROR - Not connected to WWOnline server\n"));
  169. RefPtr<SingleWait> wait = SingleWait::Create(TRANSLATE(IDS_WOL_CONNECTING));
  170. wait->EndWait(WaitCondition::Error, TRANSLATE(IDS_WOL_NOTCONNECTED));
  171. return wait;
  172. }
  173. // Generate client connect wait condition
  174. RefPtr<SerialWait> connectWait = SerialWait::Create();
  175. if (connectWait.IsValid())
  176. {
  177. Observer<ServerError>::NotifyMe(*mWOLSession);
  178. Observer<ChatMessage>::NotifyMe(*mWOLSession);
  179. // Request channel list
  180. RefPtr<WaitCondition> channelListWait = mWOLSession->GetNewChatChannelList();
  181. connectWait->Add(channelListWait);
  182. // Join the matching channel
  183. RefPtr<Product> product = Product::Current();
  184. WWASSERT(product.IsValid() && "WOLProduct not initialized.");
  185. const wchar_t* password = product->GetChannelPassword();
  186. RefPtr<WaitCondition> joinWait = mWOLSession->JoinChannel(QUICKMATCH_CHANNELNAME, password, 0);
  187. connectWait->Add(joinWait);
  188. // Make sure the matching bot is there.
  189. RefPtr<WaitCondition> findBotWait = GetUserWait::Create(mWOLSession, QUICKMATCH_BOTNAME);
  190. connectWait->Add(findBotWait);
  191. }
  192. return connectWait;
  193. }
  194. /******************************************************************************
  195. *
  196. * NAME
  197. * WOLQuickMatch::Disconnect
  198. *
  199. * DESCRIPTION
  200. * Disconnect from quickmatch
  201. *
  202. * INPUTS
  203. * NONE
  204. *
  205. * RESULT
  206. * Wait - Disconnect wait condition to process.
  207. *
  208. ******************************************************************************/
  209. RefPtr<WaitCondition> WOLQuickMatch::Disconnect(void)
  210. {
  211. WWDEBUG_SAY(("WOLQuickMatch: Disconnecting\n"));
  212. Observer<ServerError>::StopObserving();
  213. Observer<ChatMessage>::StopObserving();
  214. // If we are in the matching channel then disconnect.
  215. RefPtr<ChannelData> channel = mWOLSession->GetCurrentChannel();
  216. if (channel.IsValid())
  217. {
  218. const WideStringClass& name = channel->GetName();
  219. if (name.Compare_No_Case(QUICKMATCH_CHANNELNAME) == 0)
  220. {
  221. return mWOLSession->LeaveChannel();
  222. }
  223. }
  224. return NULL;
  225. }
  226. /******************************************************************************
  227. *
  228. * NAME
  229. * WOLQuickMatch::SendClientInfo
  230. *
  231. * DESCRIPTION
  232. * Send client matching preferences to the quickmatch bot.
  233. *
  234. * INPUTS
  235. * NONE
  236. *
  237. * RESULT
  238. * True if successful.
  239. *
  240. ******************************************************************************/
  241. bool WOLQuickMatch::SendClientInfo(void)
  242. {
  243. unsigned long ver = cNetwork::Get_Exe_Key();
  244. // Get CPU speed
  245. int speed = CPUDetectClass::Get_Processor_Speed();
  246. // Get amount of physical memory
  247. MEMORYSTATUS memStatus;
  248. GlobalMemoryStatus(&memStatus);
  249. unsigned long memory = (memStatus.dwTotalPhys / 1048576);
  250. //-------------------------------------------------------------------------
  251. // Gather pings
  252. //-------------------------------------------------------------------------
  253. const PingProfile& pings = GetLocalPingProfile();
  254. char pseudoPings[18] = {0};
  255. EncodePingProfile(pings, pseudoPings);
  256. //-------------------------------------------------------------------------
  257. // Get clients ladder points
  258. //-------------------------------------------------------------------------
  259. RefPtr<UserData> client = mWOLSession->GetCurrentUser();
  260. WWASSERT(client.IsValid());
  261. if (client.IsValid() == false)
  262. {
  263. return false;
  264. }
  265. int tpoints = 0;
  266. unsigned int played = 0;
  267. RefPtr<LadderData> ladder = client->GetTeamLadder();
  268. if (ladder.IsValid())
  269. {
  270. tpoints = ladder->GetPoints();
  271. played = ladder->GetReserved2();
  272. }
  273. //-------------------------------------------------------------------------
  274. // Generate client information message
  275. //-------------------------------------------------------------------------
  276. WideStringClass clientMsg(256, true);
  277. clientMsg.Format(L"CINFO VER=%lu CPU=%lu MEM=%lu TPOINTS=%ld PLAYED=%lu PINGS=%S",
  278. ver, speed, memory, tpoints, played, pseudoPings);
  279. WWDEBUG_SAY(("WOLQuickMatch: '%S'\n", (const WCHAR*)clientMsg));
  280. return mWOLSession->SendPrivateMessage(QUICKMATCH_BOTNAME, (const WCHAR*)clientMsg);
  281. }
  282. /******************************************************************************
  283. *
  284. * NAME
  285. * WOLQuickMatch::SendServerInfo
  286. *
  287. * DESCRIPTION
  288. * Send information describing the game,
  289. *
  290. * INPUTS
  291. *
  292. * RESULT
  293. * NONE
  294. *
  295. ******************************************************************************/
  296. void WOLQuickMatch::SendServerInfo(const char* exInfo, const char* topic)
  297. {
  298. if (exInfo && topic)
  299. {
  300. #pragma message(__FILE__" *** HACK ALERT *** SINFO msg is imitating WOLAPI IRC topic!")
  301. // *** WARNING *** DANGER *** HACK ALERT ****
  302. //
  303. // The SINFO message sent to the matching bot is assembled in such
  304. // a way as to imitate the IRC topic string that WOLAPI produces.
  305. WideStringClass botMsg(0, true);
  306. botMsg.Format(L"SINFO %S%S", exInfo, topic);
  307. WWDEBUG_SAY(("WOLQuickMatch: '%S'\n", (const WCHAR*)botMsg));
  308. mWOLSession->SendPrivateMessage(QUICKMATCH_BOTNAME, botMsg);
  309. }
  310. }
  311. /******************************************************************************
  312. *
  313. * NAME
  314. * WOLQuickMatch::SendStatus
  315. *
  316. * DESCRIPTION
  317. *
  318. * INPUTS
  319. *
  320. * RESULT
  321. * NONE
  322. *
  323. ******************************************************************************/
  324. void WOLQuickMatch::SendStatus(const wchar_t* statusMsg)
  325. {
  326. WWDEBUG_SAY(("WOLQuickMatch: Status '%S'\n", statusMsg));
  327. WideStringClass msg(256, true);
  328. msg = statusMsg;
  329. QuickMatchEvent status(QuickMatchEvent::QMMSG, msg);
  330. NotifyObservers(status);
  331. }
  332. /******************************************************************************
  333. *
  334. * NAME
  335. * WOLQuickMatch::ParseResponse
  336. *
  337. * DESCRIPTION
  338. * Handle response messages from the quickmatch bot.
  339. *
  340. * INPUTS
  341. * Message - Response message from quickmatch bot.
  342. *
  343. * RESULT
  344. * NONE
  345. *
  346. ******************************************************************************/
  347. void WOLQuickMatch::ParseResponse(const wchar_t* message)
  348. {
  349. if (message)
  350. {
  351. static QMResponseDispatch _dispatch[] =
  352. {
  353. {L"INFO ", WOLQuickMatch::ProcessInfo},
  354. {L"ERROR ", WOLQuickMatch::ProcessError},
  355. {L"START ", WOLQuickMatch::ProcessStart},
  356. {NULL, WOLQuickMatch::ProcessUnknown}
  357. };
  358. int index = 0;
  359. const wchar_t* token = _dispatch[index].Token;
  360. while (token)
  361. {
  362. // Find the first occurance of the token in the message
  363. wchar_t* cmd = wcsstr(message, token);
  364. // If the token was found and it is at the start of the message
  365. // then return the type of message this is.
  366. if (cmd && cmd == message)
  367. {
  368. const wchar_t* data = (message + wcslen(token));
  369. _dispatch[index].Dispatch(this, data);
  370. }
  371. index++;
  372. token = _dispatch[index].Token;
  373. }
  374. }
  375. }
  376. /******************************************************************************
  377. *
  378. * NAME
  379. * WOLQuickMatch::ProcessInfo
  380. *
  381. * DESCRIPTION
  382. * Process information messages.
  383. *
  384. * INPUTS
  385. * Message -
  386. *
  387. * RESULT
  388. * NONE
  389. *
  390. ******************************************************************************/
  391. void WOLQuickMatch::ProcessInfo(WOLQuickMatch* quickmatch, const wchar_t* data)
  392. {
  393. WideStringClass msg(255, true);
  394. msg = data;
  395. QuickMatchEvent status(QuickMatchEvent::QMINFO, msg);
  396. quickmatch->NotifyObservers(status);
  397. }
  398. /******************************************************************************
  399. *
  400. * NAME
  401. * WOLQuickMatch::ProcessError
  402. *
  403. * DESCRIPTION
  404. * Process error messages from the quickmatch bot.
  405. *
  406. * INPUTS
  407. * Message - Error message
  408. *
  409. * RESULT
  410. * NONE
  411. *
  412. ******************************************************************************/
  413. void WOLQuickMatch::ProcessError(WOLQuickMatch* quickmatch, const wchar_t* data)
  414. {
  415. WideStringClass msg(255, true);
  416. msg = data;
  417. QuickMatchEvent status(QuickMatchEvent::QMERROR, msg);
  418. quickmatch->NotifyObservers(status);
  419. }
  420. /******************************************************************************
  421. *
  422. * NAME
  423. * WOLQuickMatch::ProcessStart
  424. *
  425. * DESCRIPTION
  426. * Process start message from the quickmatch bot.
  427. *
  428. * INPUTS
  429. * Data - Start game parameters
  430. *
  431. * RESULT
  432. * NONE
  433. *
  434. ******************************************************************************/
  435. void WOLQuickMatch::ProcessStart(WOLQuickMatch* quickmatch, const wchar_t* data)
  436. {
  437. // Send message indicating successful match
  438. WideStringClass msg(255, true);
  439. msg.Format(TRANSLATE(IDS_MENU_QM_MATCHED_WITH), data);
  440. QuickMatchEvent status(QuickMatchEvent::QMMSG, msg);
  441. quickmatch->NotifyObservers(status);
  442. // Send message that we are matched.
  443. msg = data;
  444. QuickMatchEvent matchedEvent(QuickMatchEvent::QMMATCHED, msg);
  445. quickmatch->NotifyObservers(matchedEvent);
  446. }
  447. /******************************************************************************
  448. *
  449. * NAME
  450. * WOLQuickMatch::ProcessUnknown
  451. *
  452. * DESCRIPTION
  453. * Process unknown messages from the quickmatch bot.
  454. *
  455. * INPUTS
  456. * Message - Unknown message
  457. *
  458. * RESULT
  459. * NONE
  460. *
  461. ******************************************************************************/
  462. void WOLQuickMatch::ProcessUnknown(WOLQuickMatch* quickmatch, const wchar_t* data)
  463. {
  464. WideStringClass msg(255, true);
  465. msg = data;
  466. QuickMatchEvent status(QuickMatchEvent::QMUNKNOWN, msg);
  467. quickmatch->NotifyObservers(status);
  468. }
  469. /******************************************************************************
  470. *
  471. * NAME
  472. * WOLQuickMatch::HandleNotification(ServerError)
  473. *
  474. * DESCRIPTION
  475. * Handle server errors.
  476. *
  477. * INPUTS
  478. * Error - Server error
  479. *
  480. * RESULT
  481. * NONE
  482. *
  483. ******************************************************************************/
  484. void WOLQuickMatch::HandleNotification(ServerError& error)
  485. {
  486. const wchar_t* errorMsg = error.GetDescription();
  487. WWDEBUG_SAY(("WOLQuickMatch: ERROR - ServerError '%S'\n", errorMsg));
  488. QuickMatchEvent status(QuickMatchEvent::QMERROR, errorMsg);
  489. NotifyObservers(status);
  490. }
  491. /******************************************************************************
  492. *
  493. * NAME
  494. * WOLQuickMatch::HandleNotification(ChatMessageEvent)
  495. *
  496. * DESCRIPTION
  497. * Handle private messages coming from the matchbot.
  498. *
  499. * INPUTS
  500. * Message - Chat message
  501. *
  502. * RESULT
  503. * NONE
  504. *
  505. ******************************************************************************/
  506. void WOLQuickMatch::HandleNotification(ChatMessage& message)
  507. {
  508. const WideStringClass& sender = message.GetSendersName();
  509. if (sender.Compare_No_Case(QUICKMATCH_BOTNAME) == 0)
  510. {
  511. WWDEBUG_SAY(("WOLQuickMatch: BotMsg - '%S'\n", message.GetMessage()));
  512. ParseResponse(message.GetMessage());
  513. }
  514. }