ZoneSquad.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*-------------------------------------------------------------------------
  2. * wintrek\ZoneSquad.cpp
  3. *
  4. * Implementation of IZoneSquad and IZonePlayer
  5. *
  6. * These two classes are in the same file because are closely related.
  7. *
  8. * Owner:
  9. *
  10. * Copyright 1986-2000 Microsoft Corporation, All Rights Reserved
  11. *-----------------------------------------------------------------------*/
  12. #include "pch.h"
  13. class CZonePlayerImpl;
  14. class CZonePlayerImpl:
  15. public IZonePlayer
  16. {
  17. public:
  18. CZonePlayerImpl(const char *szName, int nID) :
  19. m_nID(nID),
  20. m_rank(IMemberBase::RANK_UNKNOWN)
  21. {
  22. strncpy(m_szName, szName, sizeof(m_szName)-1);
  23. }
  24. void Randomize() // fill in random data for testing
  25. {
  26. #ifdef DEBUG
  27. static i = 0;
  28. m_rank = (Rank) (i % 3 + 1);
  29. switch (i % 3)
  30. {
  31. case 0:
  32. strcpy (m_szName, "John Doe");
  33. break;
  34. case 1:
  35. strcpy (m_szName, "Susan Wilson");
  36. break;
  37. case 2:
  38. strcpy (m_szName, "Bill Smith");
  39. break;
  40. }
  41. ++i;
  42. #endif
  43. }
  44. char * GetName()
  45. {
  46. return m_szName;
  47. }
  48. void SetName(PCC szName) { strcpy(m_szName, szName); }
  49. int GetID() { return m_nID; }
  50. void SetID(int nID) { m_nID = nID; }
  51. char * GetLastPlayedDate(IZoneSquad & squad);
  52. void SetLastPlayedDate(IZoneSquad & squad, PCC szLastPlayedDate);
  53. Rank GetRank()
  54. {
  55. return m_rank;
  56. }
  57. void SetRank(Rank rank)
  58. {
  59. m_rank = rank;
  60. }
  61. float GetHours(IZoneSquad & squad);
  62. void SetHours(IZoneSquad & squad, float fHours);
  63. Position GetPosition(IZoneSquad & squad);
  64. void SetPosition(IZoneSquad & squad, Position pos); // setting to leader should only be done from IZoneSquad::SetOwner()
  65. DetailedStatus GetStatus(IZoneSquad & squad);
  66. bool IsMemberOf(IZoneSquad & squad) // is this player a member of pSquad
  67. {
  68. DetailedStatus status = GetStatus(squad);
  69. if (status == DSTAT_MEMBER || status == DSTAT_ASL || status == DSTAT_LEADER)
  70. return true;
  71. return false;
  72. }
  73. bool IsPendingFor(IZoneSquad & squad)
  74. {
  75. return GetStatus(squad) == DSTAT_PENDING;
  76. }
  77. private:
  78. char m_szName[40];
  79. int m_nID;
  80. Rank m_rank;
  81. };
  82. /*
  83. The spec called for the screen to know how many hours a player has been with a particular squad,
  84. so I made CMembership. It turns out that the database isn't going to support this, but I'm
  85. using CMembership for remembering position.
  86. */
  87. class CMembership:
  88. public IObject
  89. {
  90. TRef<IZonePlayer> m_pPlayer;
  91. IZonePlayer::DetailedStatus m_dstatus;
  92. char m_szLastPlayedDate[9];
  93. // float m_fHours;
  94. public:
  95. CMembership() :
  96. // m_fHours(0),
  97. m_pPlayer(NULL),
  98. m_dstatus(IZonePlayer::DSTAT_NO_ASSOCIATION)
  99. {
  100. strcpy(m_szLastPlayedDate, " ");
  101. }
  102. // void SetHours(float fHours) { asset(0);/*m_fHours = fHours; */}
  103. // float GetHours() { assert(0); return 0;/*return m_fHours; */}
  104. IZonePlayer::Position GetPosition()
  105. {
  106. return IZonePlayer::Position(m_dstatus - ((int)IZonePlayer::DSTAT_MEMBER - (int)IZonePlayer::POS_MEMBER));
  107. }
  108. void SetPosition(IZonePlayer::Position pos)
  109. {
  110. m_dstatus = IZonePlayer::DetailedStatus(pos + (IZonePlayer::DSTAT_MEMBER - IZonePlayer::POS_MEMBER));
  111. }
  112. IMemberBase::DetailedStatus GetStatus()
  113. {
  114. return m_dstatus;
  115. }
  116. char * GetLastPlayedDate()
  117. {
  118. return m_szLastPlayedDate;
  119. }
  120. void SetLastPlayedDate(PCC szLastPlayedDate)
  121. {
  122. strncpy (m_szLastPlayedDate, szLastPlayedDate, sizeof(m_szLastPlayedDate));
  123. }
  124. void SetStatus(IMemberBase::DetailedStatus detailedstatus)
  125. {
  126. m_dstatus = detailedstatus;
  127. }
  128. TRef<IZonePlayer> GetPlayer() { return m_pPlayer; }
  129. void SetPlayer(TRef<IZonePlayer> pPlayer) { m_pPlayer = pPlayer; }
  130. };
  131. class CZoneSquadImpl:
  132. public IZoneSquad
  133. {
  134. public:
  135. char *GetName() { return m_szName; }
  136. void SetName(PCC szName) { strcpy(m_szName, szName); }
  137. char *GetDescription() { return m_szDescription; }
  138. void SetDescription(PCC szDescription) { strcpy(m_szDescription, szDescription); }
  139. int GetID() { return m_nID; }
  140. void SetID(int nID) { m_nID = nID; }
  141. int GetCreationID() { return m_nCreationID; }
  142. void SetCreationID(int nCreationID) { m_nCreationID = nCreationID; }
  143. char *GetURL() { return m_szURL; }
  144. void SetURL(PCC szURL) { strcpy(m_szURL, szURL); }
  145. char *GetInceptionDate() { return m_szInceptionDate; }
  146. void SetInceptionDate(char *szInceptionDate) { strcpy(m_szInceptionDate, szInceptionDate); }
  147. TRef<IZonePlayer> GetOwner()
  148. {
  149. return m_pOwner;
  150. }
  151. void SetOwner(TRef<IZonePlayer> pOwner);
  152. const char * GetRank()
  153. {
  154. Rank rank = Rank(m_nScore / 100);
  155. if (rank < RANK_START || rank >= RANK_MAX)
  156. return Rank2String(RANK_UNKNOWN);
  157. return Rank2String(rank);
  158. }
  159. int GetRanking(SQUAD_SORT_COLUMN column)
  160. {
  161. return m_nRanking[column];
  162. }
  163. void SetRanking(SQUAD_SORT_COLUMN column, int nRanking)
  164. {
  165. m_nRanking[column] = nRanking;
  166. }
  167. int GetWins()
  168. {
  169. return m_nWins;
  170. }
  171. void SetWins(int nWins)
  172. {
  173. m_nWins = nWins;
  174. }
  175. int GetLosses()
  176. {
  177. return m_nLosses;
  178. }
  179. void SetLosses(int nLosses)
  180. {
  181. m_nLosses = nLosses;
  182. }
  183. CivID GetCivID()
  184. {
  185. return m_idCiv;
  186. }
  187. void SetCivID(int id)
  188. {
  189. m_idCiv = id;
  190. }
  191. short GetScore()
  192. {
  193. return m_nScore;
  194. }
  195. void SetScore(short nScore)
  196. {
  197. m_nScore = nScore;
  198. }
  199. void SetDetailsReceived()
  200. {
  201. m_bDetailsReceived = true;
  202. }
  203. bool WereDetailsReceived()
  204. {
  205. return m_bDetailsReceived;
  206. }
  207. void AddPlayer(IZonePlayer & pPlayer); // consider using SquadScreen::AddPlayerToSquad() over this
  208. void QuitPlayer(IZonePlayer & pPlayer); // consider using SquadScreen::RemovePlayerFromSquad() over this
  209. void RejectPlayer(IZonePlayer & pPlayer);
  210. void BootPlayer(IZonePlayer & pPlayer);
  211. void AddJoinRequest(IZonePlayer & pPlayer);
  212. void CancelJoinRequest(IZonePlayer & pPlayer);
  213. void SetEmpty();
  214. void AddPlayersToList(XZonePlayers & list);
  215. void SetOwnershipLog(char * szLog)
  216. {
  217. m_listStrOwnershipLog.SetEmpty();
  218. if(!szLog)
  219. return;
  220. const char * szDelimit = ";";
  221. // set bLastOneTruncated
  222. int nLen = strlen(szLog);
  223. int c = 0;
  224. for (int i = 0; i < nLen; ++i)
  225. if (szLog[i] == ';') c++;
  226. bool bLastOneTruncated = c % 2 != 0;
  227. char * szToken;
  228. // establish string and get the first token
  229. szToken = strtok(szLog, szDelimit);
  230. while (szToken)
  231. {
  232. // szToken is Name
  233. ZString strLine(szToken);
  234. szToken = strtok(NULL, szDelimit);
  235. if (!szToken)
  236. break;
  237. strLine += " ";
  238. // szToken is date
  239. strLine += szToken;
  240. szToken = strtok(NULL, szDelimit);
  241. m_listStrOwnershipLog.PushFront(strLine);
  242. }
  243. if(bLastOneTruncated)
  244. m_listStrOwnershipLog.PopFront();
  245. }
  246. void SetStatus(IZonePlayer & player, IMemberBase::DetailedStatus detailedstatus);
  247. void AddPlayer(IZonePlayer & player, IMemberBase::DetailedStatus detailedstatus)
  248. {
  249. SetStatus(player, detailedstatus);
  250. }
  251. TRef<CMembership> FindMembership(IZonePlayer & player);
  252. IMemberBase::DetailedStatus FindStatus(IZonePlayer & player);
  253. TList<ZString> * GetOwnershipLog() { return &m_listStrOwnershipLog; }
  254. CZoneSquadImpl() :
  255. m_rank(RANK_UNKNOWN),
  256. m_nWins(0),
  257. m_nLosses(0),
  258. m_idCiv(NA),
  259. m_nScore(0),
  260. m_bDetailsReceived(false), // false here means data for this squad is incomplete
  261. m_listMemberships(),
  262. m_bWasJustMade(false), // assume not recently made by interactive user
  263. m_pOwner(NULL)
  264. {
  265. for (int i = 0; i < SSC_MAX; ++i)
  266. m_nRanking[i] = -1;
  267. m_szName[0] = 0;
  268. m_szDescription[0] = 0;
  269. m_szURL[0] = 0;
  270. m_szInceptionDate[0] = 0;
  271. }
  272. private:
  273. char m_szName[32];
  274. char m_szDescription[512];
  275. char m_szURL[256];
  276. char m_szInceptionDate[32];
  277. Rank m_rank;
  278. int m_nRanking[SSC_MAX];
  279. int m_nWins;
  280. int m_nLosses;
  281. CivID m_idCiv;
  282. short m_nScore;
  283. bool m_bDetailsReceived; // have the details been gotten for the server yet?
  284. TRef<IZonePlayer> m_pOwner;
  285. XMemberships m_listMemberships; // members of, along with hours been with (and position data)
  286. TList<ZString> m_listStrOwnershipLog; // TODO: make this a a simple ZString, not a list
  287. int m_nID; // True ID determined by Zone database servers
  288. int m_nCreationID; // Creation ID is valid on client only
  289. bool m_bWasJustMade; // mainly for interactive user
  290. };
  291. TRef<IZoneSquad> CreateZoneSquad()
  292. {
  293. return new CZoneSquadImpl;
  294. }
  295. /*-------------------------------------------------------------------------
  296. * GetStatus()
  297. *-------------------------------------------------------------------------
  298. */
  299. IMemberBase::DetailedStatus CZonePlayerImpl::GetStatus(IZoneSquad & squad)
  300. {
  301. TRef<CMembership> pMembership = ((CZoneSquadImpl*)(&squad))->FindMembership(*this);
  302. if (pMembership)
  303. return pMembership->GetStatus();
  304. return DSTAT_NO_ASSOCIATION;
  305. }
  306. /*-------------------------------------------------------------------------
  307. * SetPosition()
  308. *-------------------------------------------------------------------------
  309. * Remarks: setting pos to leader should only be done from IZoneSquad::SetOwner()
  310. */
  311. void CZonePlayerImpl::SetPosition(IZoneSquad & squad, Position pos)
  312. {
  313. TRef<CMembership> pMembership = ((CZoneSquadImpl*)(&squad))->FindMembership(*this);
  314. assert(pMembership); // Cannot set position if player in not a member of the squad
  315. if (pMembership)
  316. {
  317. pMembership->SetPosition(pos);
  318. }
  319. }
  320. /*-------------------------------------------------------------------------
  321. * SetHours()
  322. *-------------------------------------------------------------------------
  323. */
  324. void CZonePlayerImpl::SetHours(IZoneSquad & squad, float fHours)
  325. {
  326. assert(0); // not impl
  327. }
  328. /*-------------------------------------------------------------------------
  329. * GetPosition()
  330. *-------------------------------------------------------------------------
  331. */
  332. IZonePlayer::Position CZonePlayerImpl::GetPosition(IZoneSquad & squad)
  333. {
  334. TRef<CMembership> pMembership = ((CZoneSquadImpl*)(&squad))->FindMembership(*this);
  335. if (pMembership)
  336. {
  337. return pMembership->GetPosition();
  338. }
  339. else return POS_UNKNOWN;
  340. }
  341. /*-------------------------------------------------------------------------
  342. * GetLastPlayedDate()
  343. *-------------------------------------------------------------------------
  344. */
  345. char * CZonePlayerImpl::GetLastPlayedDate(IZoneSquad & squad)
  346. {
  347. TRef<CMembership> pMembership = ((CZoneSquadImpl*)(&squad))->FindMembership(*this);
  348. if (pMembership)
  349. {
  350. return pMembership->GetLastPlayedDate();
  351. }
  352. else return " ";
  353. }
  354. /*-------------------------------------------------------------------------
  355. * SetLastPlayedDate()
  356. *-------------------------------------------------------------------------
  357. */
  358. void CZonePlayerImpl::SetLastPlayedDate(IZoneSquad & squad, PCC szLastPlayedDate)
  359. {
  360. TRef<CMembership> pMembership = ((CZoneSquadImpl*)(&squad))->FindMembership(*this);
  361. assert(pMembership); // Cannot set position if player in not a member of the squad
  362. if (pMembership)
  363. {
  364. pMembership->SetLastPlayedDate(szLastPlayedDate);
  365. }
  366. }
  367. /*-------------------------------------------------------------------------
  368. * GetHours()
  369. *-------------------------------------------------------------------------
  370. */
  371. float CZonePlayerImpl::GetHours(IZoneSquad & squad)
  372. {
  373. assert(0); // not impl
  374. return 0;
  375. }
  376. /*-------------------------------------------------------------------------
  377. * MakePlayerList()
  378. *-------------------------------------------------------------------------
  379. */
  380. void CZoneSquadImpl::AddPlayersToList(XZonePlayers & list)
  381. {
  382. XMembershipsIt it(m_listMemberships);
  383. while(!it.End())
  384. {
  385. if (!list.Find(it.Value()->GetPlayer())) // no duplicates
  386. list.PushEnd(it.Value()->GetPlayer());
  387. it.Next();
  388. }
  389. }
  390. /*-------------------------------------------------------------------------
  391. * FindMembership()
  392. *-------------------------------------------------------------------------
  393. */
  394. TRef<CMembership> CZoneSquadImpl::FindMembership(IZonePlayer & player)
  395. {
  396. XMembershipsIt it(m_listMemberships);
  397. while(!it.End())
  398. {
  399. if (it.Value()->GetPlayer() == &player)
  400. {
  401. return it.Value();
  402. }
  403. it.Next();
  404. }
  405. return NULL;
  406. }
  407. /*-------------------------------------------------------------------------
  408. * FindStatus()
  409. *-------------------------------------------------------------------------
  410. */
  411. IMemberBase::DetailedStatus CZoneSquadImpl::FindStatus(IZonePlayer & player)
  412. {
  413. XMembershipsIt it(m_listMemberships);
  414. while(!it.End())
  415. {
  416. if (it.Value()->GetPlayer() == &player)
  417. {
  418. return it.Value()->GetStatus();
  419. }
  420. it.Next();
  421. }
  422. return IMemberBase::DSTAT_NO_ASSOCIATION;
  423. }
  424. /*-------------------------------------------------------------------------
  425. * AddPlayer()
  426. *-------------------------------------------------------------------------
  427. * Note: consider using SquadScreen::AddPlayerToSquad() instead of this
  428. * to ensure proper screen refresh
  429. */
  430. void CZoneSquadImpl::SetStatus(IZonePlayer & player, IMemberBase::DetailedStatus detailedstatus)
  431. {
  432. TRef<CMembership> pMembership = FindMembership(player);
  433. if(!pMembership)
  434. {
  435. pMembership = new CMembership;
  436. m_listMemberships.PushEnd(pMembership);
  437. pMembership->SetPlayer(&player);
  438. }
  439. if (detailedstatus == IMemberBase::DSTAT_LEADER)
  440. SetOwner(&player);
  441. else
  442. pMembership->SetStatus(detailedstatus);
  443. }
  444. /*-------------------------------------------------------------------------
  445. * QuitPlayer()
  446. *-------------------------------------------------------------------------
  447. * Note: consider using SquadScreen::RemovePlayerFromSquad() instead of this
  448. * to ensure proper screen refresh
  449. */
  450. void CZoneSquadImpl::QuitPlayer(IZonePlayer & player)
  451. {
  452. TRef<CMembership> pMembership = FindMembership(player);
  453. if(pMembership)
  454. {
  455. if (m_pOwner == &player)
  456. {
  457. m_pOwner = NULL; // for now set to nobody
  458. //
  459. // Server will update who is owner later
  460. //
  461. }
  462. m_listMemberships.Remove(pMembership);
  463. }
  464. }
  465. /*-------------------------------------------------------------------------
  466. * AddJoinRequest()
  467. *-------------------------------------------------------------------------
  468. */
  469. void CZoneSquadImpl::AddJoinRequest(IZonePlayer & player)
  470. {
  471. SetStatus(player, IZonePlayer::DSTAT_PENDING);
  472. }
  473. /*-------------------------------------------------------------------------
  474. * SetEmpty() -- Clears all membership
  475. *-------------------------------------------------------------------------
  476. */
  477. void CZoneSquadImpl::SetEmpty()
  478. {
  479. m_listMemberships.SetEmpty();
  480. }
  481. /*-------------------------------------------------------------------------
  482. * RejectPlayer()
  483. *-------------------------------------------------------------------------
  484. *
  485. * Used to reject
  486. *
  487. */
  488. void CZoneSquadImpl::RejectPlayer(IZonePlayer & player)
  489. {
  490. QuitPlayer(player);
  491. }
  492. /*-------------------------------------------------------------------------
  493. * BootPlayer()
  494. *-------------------------------------------------------------------------
  495. *
  496. * Used to boot
  497. *
  498. */
  499. void CZoneSquadImpl::BootPlayer(IZonePlayer & player)
  500. {
  501. QuitPlayer(player);
  502. }
  503. /*-------------------------------------------------------------------------
  504. * CancelJoinRequest()
  505. *-------------------------------------------------------------------------
  506. */
  507. void CZoneSquadImpl::CancelJoinRequest(IZonePlayer & player)
  508. {
  509. QuitPlayer(player);
  510. }
  511. /*-------------------------------------------------------------------------
  512. * SetOwner()
  513. *-------------------------------------------------------------------------
  514. */
  515. void CZoneSquadImpl::SetOwner(TRef<IZonePlayer> pOwner)
  516. {
  517. // demote existing owner
  518. if (m_pOwner && m_pOwner->IsMemberOf(*this))
  519. m_pOwner->SetPosition(*this, IZonePlayer::POS_MEMBER);
  520. // promote new guy
  521. m_pOwner = pOwner;
  522. m_pOwner->SetPosition(*this, IZonePlayer::POS_LEADER);
  523. }
  524. TRef<IZonePlayer> CreateZonePlayer(const char *szName, int nID)
  525. {
  526. return new CZonePlayerImpl(szName, nID);
  527. }