playerList.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //-----------------------------------------------------------------------------
  23. // Hook into the client update messages to maintain our player list and scores.
  24. //-----------------------------------------------------------------------------
  25. addMessageCallback('MsgClientJoin', handleClientJoin);
  26. addMessageCallback('MsgClientDrop', handleClientDrop);
  27. addMessageCallback('MsgClientScoreChanged', handleClientScoreChanged);
  28. function handleClientJoin(%msgType, %msgString, %clientName, %clientId, %guid,
  29. %score, %kills, %deaths, %isAI, %isAdmin, %isSuperAdmin)
  30. {
  31. PlayerListGui.update(%clientId, detag(%clientName), %isSuperAdmin, %isAdmin,
  32. %isAI, %score, %kills, %deaths);
  33. }
  34. function handleClientDrop(%msgType, %msgString, %clientName, %clientId)
  35. {
  36. PlayerListGui.remove(%clientId);
  37. }
  38. function handleClientScoreChanged(%msgType, %msgString, %score, %kills, %deaths, %clientId)
  39. {
  40. PlayerListGui.updateScore(%clientId, %score, %kills, %deaths);
  41. echo(" score:"@%score@" kills:"@%kills@" deaths:"@%deaths);
  42. }
  43. // ----------------------------------------------------------------------------
  44. // GUI methods
  45. // ----------------------------------------------------------------------------
  46. function PlayerListGui::update(%this, %clientId, %name, %isSuperAdmin, %isAdmin, %isAI, %score, %kills, %deaths)
  47. {
  48. // Build the row to display. The name can have ML control tags, including
  49. // color and font. Since we're not using an ML control here, we need to
  50. // strip them off.
  51. %tag = %isSuperAdmin ? "[Super]" :
  52. (%isAdmin ? "[Admin]" :
  53. (%isAI ? "[Bot]" :
  54. ""));
  55. %text = StripMLControlChars(%name) SPC %tag TAB %score TAB %kills TAB %deaths;
  56. // Update or add the player to the control
  57. if (PlayerListGuiList.getRowNumById(%clientId) == -1)
  58. PlayerListGuiList.addRow(%clientId, %text);
  59. else
  60. PlayerListGuiList.setRowById(%clientId, %text);
  61. // Sorts by score
  62. PlayerListGuiList.sortNumerical(1, false);
  63. PlayerListGuiList.clearSelection();
  64. }
  65. function PlayerListGui::updateScore(%this, %clientId, %score, %kills, %deaths)
  66. {
  67. %text = PlayerListGuiList.getRowTextById(%clientId);
  68. %text = setField(%text, 1, %score);
  69. %text = setField(%text, 2, %kills);
  70. %text = setField(%text, 3, %deaths);
  71. PlayerListGuiList.setRowById(%clientId, %text);
  72. PlayerListGuiList.sortNumerical(1, false);
  73. PlayerListGuiList.clearSelection();
  74. }
  75. function PlayerListGui::remove(%this, %clientId)
  76. {
  77. PlayerListGuiList.removeRowById(%clientId);
  78. }
  79. function PlayerListGui::toggle(%this)
  80. {
  81. if (%this.isAwake())
  82. Canvas.popDialog(%this);
  83. else
  84. Canvas.pushDialog(%this);
  85. }
  86. function PlayerListGui::clear(%this)
  87. {
  88. // Override to clear the list.
  89. PlayerListGuiList.clear();
  90. }
  91. function PlayerListGui::zeroScores(%this)
  92. {
  93. for (%i = 0; %i < PlayerListGuiList.rowCount(); %i++)
  94. {
  95. %text = PlayerListGuiList.getRowText(%i);
  96. %text = setField(%text, 1, "0");
  97. %text = setField(%text, 2, "0");
  98. %text = setField(%text, 3, "0");
  99. PlayerListGuiList.setRowById(PlayerListGuiList.getRowId(%i), %text);
  100. }
  101. PlayerListGuiList.clearSelection();
  102. }