generals.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. #ifndef __GENERALS_H__
  19. #define __GENERALS_H__
  20. #ifdef _WIN32
  21. #include <process.h>
  22. #endif
  23. //#include <wstring.h>
  24. //#include <dictionary.h>
  25. //#include <arraylist.h>
  26. #include "matcher.h"
  27. #include "global.h"
  28. #include <string>
  29. #include <bitset>
  30. #include <vector>
  31. #include <map>
  32. #include <hash_map>
  33. typedef std::vector<bool> MapBitSet;
  34. // =====================================================================
  35. // Users
  36. // =====================================================================
  37. // Here are the states a matcher can be in:
  38. typedef enum
  39. {
  40. STATUS_INVAL = 0,
  41. STATUS_INCHANNEL, // Just entered the channel
  42. STATUS_WORKING, // Sent info, needs to be matched
  43. STATUS_MATCHED, // Been matched, but is still in the channel
  44. } UserStatus;
  45. class GeneralsUser
  46. {
  47. public:
  48. GeneralsUser(void);
  49. UserStatus status;
  50. int points, minPoints, maxPoints;
  51. int discons, maxDiscons;
  52. int country;
  53. int color;
  54. bool widened;
  55. time_t timeToWiden;
  56. time_t matchStart; // when did we request a match?
  57. // This is a ping to a designated 3rd-party server who just
  58. // responds to pings. The idea is that if the game server is
  59. // behind a firewall, the client will have a 1000ms ping to it,
  60. // even though he might be very close. To combat this, we have
  61. // clients & servers ping some 3rd-party servers & calculate a
  62. // pesudo-ping based on the sum of pings server-->3rd-->client.
  63. std::vector<int> pseudoPing;
  64. int maxPing;
  65. unsigned int IP;
  66. int NAT;
  67. MapBitSet maps;
  68. int numPlayers;
  69. };
  70. // =====================================================================
  71. // Matcher class
  72. // =====================================================================
  73. typedef std::map<std::string, GeneralsUser*> UserMap;
  74. typedef std::map<int, UserMap> LadderMap;
  75. class GeneralsMatcher : public MatcherClass
  76. {
  77. public:
  78. GeneralsMatcher();
  79. virtual ~GeneralsMatcher()
  80. {}
  81. virtual void init(void);
  82. virtual void checkMatches(void);
  83. virtual void handleDisconnect( const char *reason );
  84. virtual void handleRoomMessage( const char *nick, const char *message, MessageType messageType );
  85. virtual void handlePlayerMessage( const char *nick, const char *message, MessageType messageType );
  86. virtual void handlePlayerJoined( const char *nick );
  87. virtual void handlePlayerLeft( const char *nick );
  88. virtual void handlePlayerChangedNick( const char *oldNick, const char *newNick );
  89. virtual void handlePlayerEnum( bool success, int gameSpyIndex, const char *nick, int flags );
  90. private:
  91. LadderMap m_ladders;
  92. UserMap m_nonLadderUsers1v1;
  93. UserMap m_nonLadderUsers2v2;
  94. UserMap m_nonLadderUsers3v3;
  95. UserMap m_nonLadderUsers4v4;
  96. UserMap m_nonMatchingUsers;
  97. double computeMatchFitness(const std::string& i1, const GeneralsUser *u1, const std::string& i2, const GeneralsUser *u2);
  98. GeneralsUser* findUser(const std::string& who);
  99. GeneralsUser* findUserInLadder(const std::string& who, int ladderID);
  100. GeneralsUser* findUserInAnyLadder(const std::string& who);
  101. GeneralsUser* findNonLadderUser(const std::string& who);
  102. GeneralsUser* findNonMatchingUser(const std::string& who);
  103. void addUser(const std::string& who);
  104. void addUserInLadder(const std::string& who, int ladderID, GeneralsUser *user);
  105. void addUserInAnyLadder(const std::string& who, GeneralsUser *user);
  106. void addNonLadderUser(const std::string& who, GeneralsUser *user);
  107. void addNonMatchingUser(const std::string& who, GeneralsUser *user);
  108. bool removeUser(const std::string& who);
  109. GeneralsUser* removeUserInLadder(const std::string& who, int ladderID);
  110. GeneralsUser* removeUserInAnyLadder(const std::string& who);
  111. GeneralsUser* removeNonLadderUser(const std::string& who);
  112. GeneralsUser* removeNonMatchingUser(const std::string& who);
  113. void checkMatchesInUserMap(UserMap& userMap, int ladderID, int numPlayers, bool showPoolSize);
  114. void dumpUsers(void);
  115. void sendMatchInfo(std::string name1, std::string name2, std::string name3, std::string name4,
  116. std::string name5, std::string name6, std::string name7, std::string name8,
  117. GeneralsUser *user1, GeneralsUser *user2, GeneralsUser *user3, GeneralsUser *user4,
  118. GeneralsUser *user5, GeneralsUser *user6, GeneralsUser *user7, GeneralsUser *user8,
  119. int numPlayers, int ladderID);
  120. // Command handlers for above privmsg commands (offset is the
  121. // offset for the getToken() past the command token)
  122. bool handleUserInfo(const char *nick, const std::string& msg);
  123. bool handleUserWiden(const char *nick);
  124. // Weights for various matching parameters
  125. int weightLowPing;
  126. int weightAvgPoints;
  127. int totalWeight;
  128. time_t m_nextPoolSizeAnnouncement;
  129. int m_secondsBetweenPoolSizeAnnouncements;
  130. //typedef std::vector<std::string> StringVec;
  131. //StringVec mapFileList;
  132. }
  133. ;
  134. // =====================================================================
  135. // TEST Client Matcher class
  136. // =====================================================================
  137. class GeneralsClientMatcher : public MatcherClass
  138. {
  139. public:
  140. GeneralsClientMatcher();
  141. virtual ~GeneralsClientMatcher()
  142. {}
  143. virtual void init(void);
  144. virtual void checkMatches(void);
  145. virtual void handleDisconnect( const char *reason );
  146. virtual void handleRoomMessage( const char *nick, const char *message, MessageType messageType );
  147. virtual void handlePlayerMessage( const char *nick, const char *message, MessageType messageType );
  148. virtual void handlePlayerJoined( const char *nick );
  149. virtual void handlePlayerLeft( const char *nick );
  150. virtual void handlePlayerChangedNick( const char *oldNick, const char *newNick );
  151. virtual void handlePlayerEnum( bool success, int gameSpyIndex, const char *nick, int flags );
  152. private:
  153. }
  154. ;
  155. #endif /* __GENERALS_H__ */