simplechatgame.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "simplechatgame.h"
  2. #include "chatcommand.h"
  3. #include "Debug.h"
  4. simplechatgame::simplechatgame()
  5. {
  6. // Constructor.
  7. gameid = nullptr;
  8. gamedata = nullptr;
  9. }
  10. simplechatgame::~simplechatgame()
  11. {
  12. // Destructor.
  13. simplechatgameuser *item;
  14. users.deleteallitems();
  15. users.clear(true);
  16. idisposable::dereference((idisposable **)&gameid);
  17. idisposable::dereference((idisposable **)&gamedata);
  18. }
  19. bool simplechatgame::add_client(chatclient *client,datastring username)
  20. {
  21. // Adds a client. Returns true if the client was added.
  22. simplechatgameuser *newuser = new simplechatgameuser();
  23. if (newuser == nullptr) {
  24. return false;
  25. }
  26. newuser->client = client;
  27. newuser->username = new datablock(username);
  28. if (!users.add_to_end(newuser)) {
  29. delete newuser;
  30. return false;
  31. }
  32. return true;
  33. }
  34. void simplechatgame::send_message_to_clients(message *message,chatclient *me)
  35. {
  36. DEBUG_FUNCTION
  37. biglist_iterator<simplechatgameuser *>clientloop(&users);
  38. DEBUG_LINE
  39. chatclient *client;
  40. DEBUG_LINE
  41. while(!clientloop.eof()) {
  42. DEBUG_LINE
  43. client = clientloop.item->client;
  44. DEBUG_LINE
  45. if ((client != me) && (client != nullptr)) {
  46. DEBUG_LINE
  47. client->add_message(message);
  48. client->callback_on_writable();
  49. DEBUG_LINE
  50. }
  51. DEBUG_LINE
  52. clientloop.movenext();
  53. }
  54. DEBUG_LINE
  55. return;
  56. }
  57. simplechatgame *simplechatgame::clone()
  58. {
  59. usage++;
  60. return this;
  61. }
  62. void simplechatgame::send_user_list_to_clients()
  63. {
  64. DEBUG_FUNCTION
  65. message *user_list_message;
  66. DEBUG_LINE
  67. user_list_message = chatcommand::simplegameusers(0,*gameid,&users);
  68. DEBUG_LINE
  69. send_message_to_clients(user_list_message,nullptr);
  70. DEBUG_LINE
  71. if (--user_list_message->usage <= 0) {
  72. DEBUG_LINE
  73. delete user_list_message;
  74. }
  75. //idisposable::dereference((idisposable**)user_list_message);
  76. DEBUG_LINE
  77. }
  78. int simplechatgame::usercount(bool withclient)
  79. {
  80. // Returns the number of users. If withclient is true then only count users that are connected.
  81. DEBUG_FUNCTION
  82. int count = 0;
  83. DEBUG_LINE
  84. biglist_iterator<simplechatgameuser *> loop(&users);
  85. DEBUG_LINE
  86. while (!loop.eof()) {
  87. DEBUG_LINE
  88. if ((!withclient) || (loop.item->client != nullptr)) {
  89. count++;
  90. }
  91. DEBUG_LINE
  92. loop.movenext();
  93. DEBUG_LINE
  94. }
  95. DEBUG_LINE
  96. return count;
  97. }
  98. biglist_item<simplechatgameuser *> *simplechatgame::add_user(chatclient *client,datastring username)
  99. {
  100. // Adds a user to users. Returns the added item if successful.
  101. biglist_item<simplechatgameuser *> *item = nullptr;
  102. simplechatgameuser *user;
  103. user = new simplechatgameuser();
  104. if (user != nullptr) {
  105. user->client = client;
  106. user->username = new datablock(username);
  107. if (user->username == nullptr) {
  108. delete user;
  109. } else {
  110. item = users.add(user);
  111. if (item == nullptr) {
  112. delete user;
  113. }
  114. }
  115. }
  116. return item;
  117. }
  118. biglist_item<simplechatgameuser *> *simplechatgame::getuser(chatclient *client)
  119. {
  120. // Search user by client.
  121. biglist_iterator<simplechatgameuser *> loop(&users);
  122. while (!loop.eof()) {
  123. if (loop.item->client == client) {
  124. return loop.row;
  125. }
  126. loop.movenext();
  127. }
  128. return nullptr;
  129. }
  130. biglist_item<simplechatgameuser *> *simplechatgame::getuser(datastring username)
  131. {
  132. // Search user by username.
  133. biglist_iterator<simplechatgameuser *> loop(&users);
  134. while (!loop.eof()) {
  135. if (username == *(loop.item->username)) {
  136. return loop.row;
  137. }
  138. loop.movenext();
  139. }
  140. return nullptr;
  141. }