WOLUser.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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/WWOnline/WOLUser.cpp $
  22. *
  23. * DESCRIPTION
  24. *
  25. * PROGRAMMER
  26. * $Author: Steve_t $
  27. *
  28. * VERSION INFO
  29. * $Revision: 19 $
  30. * $Modtime: 10/10/02 10:38a $
  31. *
  32. ******************************************************************************/
  33. #include "always.h"
  34. #include "WOLUser.h"
  35. #include "WOLChannel.h"
  36. #include "WOLSquad.h"
  37. #include "WOLLadder.h"
  38. #include <WWDebug\WWDebug.h>
  39. namespace WWOnline {
  40. /******************************************************************************
  41. *
  42. * NAME
  43. * UserData::Create
  44. *
  45. * DESCRIPTION
  46. * Create a new User data instance
  47. *
  48. * INPUTS
  49. * WOLUser - Wolapi user structure
  50. *
  51. * RESULT
  52. * User -
  53. *
  54. ******************************************************************************/
  55. RefPtr<UserData> UserData::Create(const WOL::User& user)
  56. {
  57. return new UserData(user);
  58. }
  59. /******************************************************************************
  60. *
  61. * NAME
  62. * UserData::Create
  63. *
  64. * DESCRIPTION
  65. * Create a new User data instance
  66. *
  67. * INPUTS
  68. * Name - Name of user
  69. *
  70. * RESULT
  71. * User -
  72. *
  73. ******************************************************************************/
  74. RefPtr<UserData> UserData::Create(const wchar_t* name)
  75. {
  76. if (name)
  77. {
  78. WOL::User user;
  79. memset(&user, 0, sizeof(user));
  80. wcstombs((char*)user.name, name, sizeof(user.name));
  81. user.name[sizeof(user.name) - 1] = 0;
  82. return Create(user);
  83. }
  84. return RefPtr<UserData>();
  85. }
  86. /******************************************************************************
  87. *
  88. * NAME
  89. * UserData::UserData
  90. *
  91. * DESCRIPTION
  92. * Constructor
  93. *
  94. * INPUTS
  95. *
  96. * RESULT
  97. * NONE
  98. *
  99. ******************************************************************************/
  100. UserData::UserData(const WOL::User& user) :
  101. mUserName((char*)user.name),
  102. mLocation(USERLOCATION_UNKNOWN)
  103. {
  104. WWDEBUG_SAY(("WOL: Instantiating UserData '%S'\n", (const WCHAR*)mUserName));
  105. memcpy(&mData, &user, sizeof(mData));
  106. mKickTimer = 0;
  107. mData.next = NULL;
  108. }
  109. /******************************************************************************
  110. *
  111. * NAME
  112. * UserData::~UserData
  113. *
  114. * DESCRIPTION
  115. * Destructor
  116. *
  117. * INPUTS
  118. * NONE
  119. *
  120. * RESULT
  121. * NONE
  122. *
  123. ******************************************************************************/
  124. UserData::~UserData()
  125. {
  126. WWDEBUG_SAY(("WOL: Destructing UserData '%S'\n", (const WCHAR*)mUserName));
  127. }
  128. /******************************************************************************
  129. *
  130. * NAME
  131. * UserData::UpdateData
  132. *
  133. * DESCRIPTION
  134. *
  135. * INPUTS
  136. * WOlUser - WOL user structure to update with.
  137. *
  138. * RESULT
  139. * NONE
  140. *
  141. ******************************************************************************/
  142. void UserData::UpdateData(const WOL::User& wolUser)
  143. {
  144. wchar_t name[64];
  145. mbstowcs(name, (const char*)wolUser.name, sizeof(wolUser.name));
  146. name[sizeof(wolUser.name) - 1] = 0;
  147. WWASSERT(wcslen(name) && "Empty user name");
  148. bool isValid = (!mUserName.Is_Empty() && mUserName.Compare_No_Case(name) == 0);
  149. WWASSERT(isValid && "WOLUserData::UpdateData() mismatch");
  150. if (isValid)
  151. {
  152. if (mData.squadID != wolUser.squadID)
  153. {
  154. mSquad.Release();
  155. mData.squadID = wolUser.squadID;
  156. }
  157. }
  158. }
  159. /******************************************************************************
  160. *
  161. * NAME
  162. * UserData::Squelch
  163. *
  164. * DESCRIPTION
  165. *
  166. * INPUTS
  167. * OnOff - Squelch state
  168. *
  169. * RESULT
  170. * NONE
  171. *
  172. ******************************************************************************/
  173. void UserData::Squelch(bool onoff)
  174. {
  175. mData.flags = (onoff == true) ?
  176. (mData.flags | CHAT_USER_SQUELCHED) : (mData.flags ^ CHAT_USER_SQUELCHED);
  177. }
  178. /******************************************************************************
  179. *
  180. * NAME
  181. * UserData::SetLocation
  182. *
  183. * DESCRIPTION
  184. * Set the users location.
  185. *
  186. * INPUTS
  187. * Location - User's location
  188. *
  189. * RESULT
  190. * NONE
  191. *
  192. ******************************************************************************/
  193. void UserData::SetLocation(UserLocation location)
  194. {
  195. mLocation = location;
  196. }
  197. /******************************************************************************
  198. *
  199. * NAME
  200. * UserData::SetChannel
  201. *
  202. * DESCRIPTION
  203. * Set the channel the user is in.
  204. *
  205. * INPUTS
  206. * Channel - Channel user is in.
  207. *
  208. * RESULT
  209. * NONE
  210. *
  211. ******************************************************************************/
  212. void UserData::SetChannel(const RefPtr<ChannelData>& channel)
  213. {
  214. mChannel = channel;
  215. }
  216. /******************************************************************************
  217. *
  218. * NAME
  219. * UserData::SetSquad
  220. *
  221. * DESCRIPTION
  222. * Associate a squad for this user.
  223. *
  224. * INPUTS
  225. * Squad
  226. *
  227. * RESULT
  228. * NONE
  229. *
  230. ******************************************************************************/
  231. void UserData::SetSquad(const RefPtr<SquadData>& squadData)
  232. {
  233. mSquad = squadData;
  234. if (squadData.IsValid())
  235. {
  236. mData.squadID = squadData->GetID();
  237. // Syncronize common data with updated squad information.
  238. strncpy((char*)mData.squadname, squadData->GetName(), sizeof(mData.squadname));
  239. mData.squadname[sizeof(mData.squadname) - 1] = 0;
  240. strncpy((char*)mData.squadabbrev, squadData->GetAbbr(), sizeof(mData.squadabbrev));
  241. mData.squadabbrev[sizeof(mData.squadabbrev) - 1] = 0;
  242. }
  243. else
  244. {
  245. mData.squadname[0] = 0;
  246. mData.squadabbrev[0] = 0;
  247. }
  248. }
  249. /******************************************************************************
  250. *
  251. * NAME
  252. * UserData::SetTeam
  253. *
  254. * DESCRIPTION
  255. *
  256. * INPUTS
  257. *
  258. * RESULT
  259. * NONE
  260. *
  261. ******************************************************************************/
  262. void UserData::SetTeam(int team)
  263. {
  264. mData.team = team;
  265. }
  266. /******************************************************************************
  267. *
  268. * NAME
  269. * UserData::SetLocale
  270. *
  271. * DESCRIPTION
  272. *
  273. * INPUTS
  274. *
  275. * RESULT
  276. * NONE
  277. *
  278. ******************************************************************************/
  279. void UserData::SetLocale(WOL::Locale locale)
  280. {
  281. if (locale >= WOL::LOC_UNKNOWN && locale <= WOL::LOC_TURKEY)
  282. {
  283. mData.locale = locale;
  284. }
  285. }
  286. /******************************************************************************
  287. *
  288. * NAME
  289. * UserData::SetLadder
  290. *
  291. * DESCRIPTION
  292. *
  293. * INPUTS
  294. *
  295. * RESULT
  296. * NONE
  297. *
  298. ******************************************************************************/
  299. void UserData::SetLadder(const RefPtr<LadderData>& ladder)
  300. {
  301. mUserLadder = ladder;
  302. }
  303. /******************************************************************************
  304. *
  305. * NAME
  306. *
  307. * DESCRIPTION
  308. *
  309. * INPUTS
  310. *
  311. * RESULT
  312. *
  313. ******************************************************************************/
  314. RefPtr<LadderData> UserData::GetClanLadder(void) const
  315. {
  316. if (mSquad.IsValid())
  317. {
  318. return mSquad->GetLadder();
  319. }
  320. return NULL;
  321. }
  322. /******************************************************************************
  323. *
  324. * NAME
  325. * UserData::SetTeamLadder
  326. *
  327. * DESCRIPTION
  328. *
  329. * INPUTS
  330. *
  331. * RESULT
  332. * NONE
  333. *
  334. ******************************************************************************/
  335. void UserData::SetTeamLadder(const RefPtr<LadderData>& ladder)
  336. {
  337. mTeamLadder = ladder;
  338. }
  339. /******************************************************************************
  340. *
  341. * NAME
  342. * UserData::
  343. *
  344. * DESCRIPTION
  345. *
  346. * INPUTS
  347. *
  348. * RESULT
  349. * NONE
  350. *
  351. ******************************************************************************/
  352. void UserData::SetLadderFromType(const RefPtr<LadderData>& ladder, LadderType type)
  353. {
  354. if (type == LadderType_Team)
  355. {
  356. SetTeamLadder(ladder);
  357. return;
  358. }
  359. if (type == LadderType_Clan)
  360. {
  361. if (mSquad.IsValid())
  362. {
  363. mSquad->SetLadder(ladder);
  364. }
  365. return;
  366. }
  367. SetLadder(ladder);
  368. }
  369. /******************************************************************************
  370. *
  371. * NAME
  372. * UserData::
  373. *
  374. * DESCRIPTION
  375. *
  376. * INPUTS
  377. *
  378. * RESULT
  379. *
  380. ******************************************************************************/
  381. RefPtr<LadderData> UserData::GetLadderFromType(LadderType type)
  382. {
  383. if (type == LadderType_Team)
  384. {
  385. return GetTeamLadder();
  386. }
  387. if (type == LadderType_Clan)
  388. {
  389. return GetClanLadder();
  390. }
  391. return GetLadder();
  392. }
  393. /******************************************************************************
  394. *
  395. * NAME
  396. * NativeWOLUserList::
  397. *
  398. * DESCRIPTION
  399. *
  400. * INPUTS
  401. *
  402. * RESULT
  403. * NONE
  404. *
  405. ******************************************************************************/
  406. NativeWOLUserList::NativeWOLUserList(const UserList& users) :
  407. mNativeList(NULL)
  408. {
  409. int count = users.size();
  410. if (count > 0)
  411. {
  412. mNativeList = new WOL::User[count];
  413. if (mNativeList)
  414. {
  415. for (int index = 0; index < count; index++)
  416. {
  417. WOL::User* wolUser = &users[index]->GetData();
  418. memcpy(&mNativeList[index], wolUser, sizeof(WOL::User));
  419. if (index == (count - 1))
  420. {
  421. mNativeList[index].next = NULL;
  422. }
  423. else
  424. {
  425. mNativeList[index].next = &mNativeList[index + 1];
  426. }
  427. }
  428. }
  429. }
  430. }
  431. /******************************************************************************
  432. *
  433. * NAME
  434. * NativeWOLUserList::
  435. *
  436. * DESCRIPTION
  437. * Destructor
  438. *
  439. * INPUTS
  440. * NONE
  441. *
  442. * RESULT
  443. * NONE
  444. *
  445. ******************************************************************************/
  446. NativeWOLUserList::~NativeWOLUserList()
  447. {
  448. if (mNativeList)
  449. {
  450. delete []mNativeList;
  451. }
  452. }
  453. /******************************************************************************
  454. *
  455. * NAME
  456. * FindUserInList
  457. *
  458. * DESCRIPTION
  459. * Search for a user with the specified name in the list.
  460. *
  461. * INPUTS
  462. * Name - Name of user to search for.
  463. * List - UserData list to search.
  464. *
  465. * RESULT
  466. * User -
  467. *
  468. ******************************************************************************/
  469. RefPtr<UserData> FindUserInList(const wchar_t* username, const UserList& list)
  470. {
  471. for (unsigned int index = 0; index < list.size(); index++)
  472. {
  473. const WideStringClass& name = list[index]->GetName();
  474. if (name.Compare_No_Case(username) == 0)
  475. {
  476. return list[index];
  477. }
  478. }
  479. return RefPtr<UserData>();
  480. }
  481. /******************************************************************************
  482. *
  483. * NAME
  484. * RemoveUserInList
  485. *
  486. * DESCRIPTION
  487. * Remove the
  488. *
  489. * INPUTS
  490. * Name - Name of user to remove.
  491. * List - UserData list to search.
  492. *
  493. * RESULT
  494. * Removed - User removed from list.
  495. *
  496. ******************************************************************************/
  497. RefPtr<UserData> RemoveUserInList(const wchar_t* username, UserList& list)
  498. {
  499. RefPtr<UserData> removedUser;
  500. UserList::iterator iter = list.begin();
  501. while (iter != list.end())
  502. {
  503. const WideStringClass& name = (*iter)->GetName();
  504. if (name.Compare_No_Case(username) == 0)
  505. {
  506. removedUser = *iter;
  507. list.erase(iter);
  508. break;
  509. }
  510. iter++;
  511. }
  512. return removedUser;
  513. }
  514. } // namespace WWOnline