Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
8.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #include "pch_serverbrowser.h"
  8. bool IsReplayServer( gameserveritem_t &server );
  9. //-----------------------------------------------------------------------------
  10. // Purpose: List password column sort function
  11. //-----------------------------------------------------------------------------
  12. int __cdecl PasswordCompare(ListPanel *pPanel, const ListPanelItem &p1, const ListPanelItem &p2)
  13. {
  14. gameserveritem_t *s1 = ServerBrowserDialog().GetServer(p1.userData);
  15. gameserveritem_t *s2 = ServerBrowserDialog().GetServer(p2.userData);
  16. if ( !s1 && s2 )
  17. return -1;
  18. if ( !s2 && s1 )
  19. return 1;
  20. if ( !s1 && !s2 )
  21. return 0;
  22. if ( s1->m_bPassword < s2->m_bPassword )
  23. return 1;
  24. else if ( s1->m_bPassword > s2->m_bPassword )
  25. return -1;
  26. return 0;
  27. }
  28. //-----------------------------------------------------------------------------
  29. // Purpose: list column sort function
  30. //-----------------------------------------------------------------------------
  31. int __cdecl BotsCompare(ListPanel *pPanel, const ListPanelItem &p1, const ListPanelItem &p2)
  32. {
  33. gameserveritem_t *s1 = ServerBrowserDialog().GetServer(p1.userData);
  34. gameserveritem_t *s2 = ServerBrowserDialog().GetServer(p2.userData);
  35. if ( !s1 && s2 )
  36. return -1;
  37. if ( !s2 && s1 )
  38. return 1;
  39. if ( !s1 && !s2 )
  40. return 0;
  41. if ( s1->m_nBotPlayers < s2->m_nBotPlayers )
  42. return 1;
  43. else if ( s1->m_nBotPlayers > s2->m_nBotPlayers )
  44. return -1;
  45. return 0;
  46. }
  47. //-----------------------------------------------------------------------------
  48. // Purpose: list column sort function
  49. //-----------------------------------------------------------------------------
  50. int __cdecl SecureCompare(ListPanel *pPanel, const ListPanelItem &p1, const ListPanelItem &p2)
  51. {
  52. gameserveritem_t *s1 = ServerBrowserDialog().GetServer(p1.userData);
  53. gameserveritem_t *s2 = ServerBrowserDialog().GetServer(p2.userData);
  54. if ( !s1 && s2 )
  55. return -1;
  56. if ( !s2 && s1 )
  57. return 1;
  58. if ( !s1 && !s2 )
  59. return 0;
  60. if ( s1->m_bSecure < s2->m_bSecure )
  61. return 1;
  62. else if ( s1->m_bSecure > s2->m_bSecure )
  63. return -1;
  64. return 0;
  65. }
  66. //-----------------------------------------------------------------------------
  67. // Purpose: list column sort function
  68. //-----------------------------------------------------------------------------
  69. int __cdecl IPAddressCompare(ListPanel *pPanel, const ListPanelItem &p1, const ListPanelItem &p2)
  70. {
  71. gameserveritem_t *s1 = ServerBrowserDialog().GetServer(p1.userData);
  72. gameserveritem_t *s2 = ServerBrowserDialog().GetServer(p2.userData);
  73. if ( !s1 && s2 )
  74. return -1;
  75. if ( !s2 && s1 )
  76. return 1;
  77. if ( !s1 && !s2 )
  78. return 0;
  79. if ( s1->m_NetAdr < s2->m_NetAdr )
  80. return -1;
  81. else if ( s2->m_NetAdr < s1->m_NetAdr )
  82. return 1;
  83. return 0;
  84. }
  85. //-----------------------------------------------------------------------------
  86. // Purpose: Ping comparison function
  87. //-----------------------------------------------------------------------------
  88. int __cdecl PingCompare(ListPanel *pPanel, const ListPanelItem &p1, const ListPanelItem &p2)
  89. {
  90. gameserveritem_t *s1 = ServerBrowserDialog().GetServer(p1.userData);
  91. gameserveritem_t *s2 = ServerBrowserDialog().GetServer(p2.userData);
  92. if ( !s1 && s2 )
  93. return -1;
  94. if ( !s2 && s1 )
  95. return 1;
  96. if ( !s1 && !s2 )
  97. return 0;
  98. int ping1 = s1->m_nPing;
  99. int ping2 = s2->m_nPing;
  100. if ( ping1 < ping2 )
  101. return -1;
  102. else if ( ping1 > ping2 )
  103. return 1;
  104. return 0;
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Purpose: Map comparison function
  108. //-----------------------------------------------------------------------------
  109. int __cdecl MapCompare(ListPanel *pPanel, const ListPanelItem &p1, const ListPanelItem &p2)
  110. {
  111. gameserveritem_t *s1 = ServerBrowserDialog().GetServer(p1.userData);
  112. gameserveritem_t *s2 = ServerBrowserDialog().GetServer(p2.userData);
  113. if ( !s1 && s2 )
  114. return -1;
  115. if ( !s2 && s1 )
  116. return 1;
  117. if ( !s1 && !s2 )
  118. return 0;
  119. return Q_stricmp( s1->m_szMap, s2->m_szMap );
  120. }
  121. //-----------------------------------------------------------------------------
  122. // Purpose: Game dir comparison function
  123. //-----------------------------------------------------------------------------
  124. int __cdecl GameCompare(ListPanel *pPanel, const ListPanelItem &p1, const ListPanelItem &p2)
  125. {
  126. gameserveritem_t *s1 = ServerBrowserDialog().GetServer(p1.userData);
  127. gameserveritem_t *s2 = ServerBrowserDialog().GetServer(p2.userData);
  128. if ( !s1 && s2 )
  129. return -1;
  130. if ( !s2 && s1 )
  131. return 1;
  132. if ( !s1 && !s2 )
  133. return 0;
  134. // make sure we haven't added the same server to the list twice
  135. Assert( p1.userData != p2.userData );
  136. return Q_stricmp( s1->m_szGameDescription, s2->m_szGameDescription );
  137. }
  138. //-----------------------------------------------------------------------------
  139. // Purpose: Server name comparison function
  140. //-----------------------------------------------------------------------------
  141. int __cdecl ServerNameCompare(ListPanel *pPanel, const ListPanelItem &p1, const ListPanelItem &p2)
  142. {
  143. gameserveritem_t *s1 = ServerBrowserDialog().GetServer(p1.userData);
  144. gameserveritem_t *s2 = ServerBrowserDialog().GetServer(p2.userData);
  145. if ( !s1 && s2 )
  146. return -1;
  147. if ( !s2 && s1 )
  148. return 1;
  149. if ( !s1 && !s2 )
  150. return 0;
  151. return Q_stricmp( s1->GetName(), s2->GetName() );
  152. }
  153. //-----------------------------------------------------------------------------
  154. // Purpose: Player number comparison function
  155. //-----------------------------------------------------------------------------
  156. int __cdecl PlayersCompare(ListPanel *pPanel, const ListPanelItem &p1, const ListPanelItem &p2)
  157. {
  158. gameserveritem_t *s1 = ServerBrowserDialog().GetServer(p1.userData);
  159. gameserveritem_t *s2 = ServerBrowserDialog().GetServer(p2.userData);
  160. if ( !s1 && s2 )
  161. return -1;
  162. if ( !s2 && s1 )
  163. return 1;
  164. if ( !s1 && !s2 )
  165. return 0;
  166. int s1p = max( 0, s1->m_nPlayers - s1->m_nBotPlayers );
  167. int s1m = max( 0, s1->m_nMaxPlayers - s1->m_nBotPlayers );
  168. int s2p = max( 0, s2->m_nPlayers - s2->m_nBotPlayers );
  169. int s2m = max( 0, s2->m_nMaxPlayers - s2->m_nBotPlayers );
  170. // compare number of players
  171. if ( s1p > s2p )
  172. return -1;
  173. if ( s1p < s2p )
  174. return 1;
  175. // compare max players if number of current players is equal
  176. if ( s1m > s2m )
  177. return -1;
  178. if ( s1m < s2m )
  179. return 1;
  180. return 0;
  181. }
  182. //-----------------------------------------------------------------------------
  183. // Purpose: Player number comparison function
  184. //-----------------------------------------------------------------------------
  185. int __cdecl LastPlayedCompare(ListPanel *pPanel, const ListPanelItem &p1, const ListPanelItem &p2)
  186. {
  187. gameserveritem_t *s1 = ServerBrowserDialog().GetServer( p1.userData );
  188. gameserveritem_t *s2 = ServerBrowserDialog().GetServer( p2.userData );
  189. if ( !s1 && s2 )
  190. return -1;
  191. if ( !s2 && s1 )
  192. return 1;
  193. if ( !s1 && !s2 )
  194. return 0;
  195. // compare number of players
  196. if ( s1->m_ulTimeLastPlayed > s2->m_ulTimeLastPlayed )
  197. return -1;
  198. if ( s1->m_ulTimeLastPlayed < s2->m_ulTimeLastPlayed )
  199. return 1;
  200. return 0;
  201. }
  202. //-----------------------------------------------------------------------------
  203. // Purpose: Tag comparison function
  204. //-----------------------------------------------------------------------------
  205. int __cdecl TagsCompare(ListPanel *pPanel, const ListPanelItem &p1, const ListPanelItem &p2)
  206. {
  207. gameserveritem_t *s1 = ServerBrowserDialog().GetServer(p1.userData);
  208. gameserveritem_t *s2 = ServerBrowserDialog().GetServer(p2.userData);
  209. if ( !s1 && s2 )
  210. return -1;
  211. if ( !s2 && s1 )
  212. return 1;
  213. if ( !s1 && !s2 )
  214. return 0;
  215. return Q_stricmp( s1->m_szGameTags, s2->m_szGameTags );
  216. }
  217. //-----------------------------------------------------------------------------
  218. // Purpose: Replay comparison function
  219. //-----------------------------------------------------------------------------
  220. int __cdecl ReplayCompare(ListPanel *pPanel, const ListPanelItem &p1, const ListPanelItem &p2)
  221. {
  222. gameserveritem_t *s1 = ServerBrowserDialog().GetServer(p1.userData);
  223. gameserveritem_t *s2 = ServerBrowserDialog().GetServer(p2.userData);
  224. if ( !s1 && s2 )
  225. return -1;
  226. if ( !s2 && s1 )
  227. return 1;
  228. if ( !s1 && !s2 )
  229. return 0;
  230. bool s1_is_replay = IsReplayServer( *s1 );
  231. bool s2_is_replay = IsReplayServer( *s2 );
  232. if ( s1_is_replay < s2_is_replay )
  233. return 1;
  234. else if ( s1_is_replay > s2_is_replay )
  235. return -1;
  236. return 0;
  237. }