PlayerRegister.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "PlayerRegister.h"
  2. #include <Datei.h>
  3. Framework::Text randomSecret()
  4. {
  5. int len = 1024 + (int)(((double)rand() / RAND_MAX - 0.5) * 512);
  6. char* key = new char[len + 1];
  7. for (int i = 0; i < len; i++)
  8. key[i] = (char)((((double)rand() / RAND_MAX) * 254) + 1);
  9. key[len] = 0;
  10. Framework::Text secret = key;
  11. delete[] key;
  12. return secret;
  13. }
  14. RegisteredPlayer::RegisteredPlayer(int id, Framework::Text name)
  15. : Framework::ReferenceCounter(),
  16. id(id),
  17. name(name),
  18. secret(randomSecret())
  19. {}
  20. RegisteredPlayer::RegisteredPlayer(Framework::StreamReader* zReader)
  21. : Framework::ReferenceCounter()
  22. {
  23. zReader->lese((char*)&id, 4);
  24. char len;
  25. zReader->lese(&len, 1);
  26. char* buffer = new char[len + 1];
  27. zReader->lese(buffer, len);
  28. buffer[(int)len] = 0;
  29. name = buffer;
  30. delete[] buffer;
  31. short sLen;
  32. zReader->lese((char*)&sLen, 2);
  33. buffer = new char[sLen + 1];
  34. zReader->lese(buffer, sLen);
  35. buffer[sLen] = 0;
  36. secret = buffer;
  37. delete[] buffer;
  38. }
  39. int RegisteredPlayer::getId() const {
  40. return id;
  41. }
  42. Framework::Text RegisteredPlayer::getName() const {
  43. return name;
  44. }
  45. Framework::Text RegisteredPlayer::getSecret() const {
  46. return secret;
  47. }
  48. void RegisteredPlayer::save(Framework::StreamWriter* zWriter) const
  49. {
  50. zWriter->schreibe((char*)&id, 4);
  51. char len = (char)name.getLength();
  52. zWriter->schreibe(&len, 1);
  53. zWriter->schreibe(name.getText(), len);
  54. short sLen = (short)secret.getLength();
  55. zWriter->schreibe((char*)&sLen, 2);
  56. zWriter->schreibe(secret.getText(), sLen);
  57. }
  58. PlayerRegister::PlayerRegister(Framework::Text worldPath)
  59. : Framework::ReferenceCounter(),
  60. path(worldPath + "/player/players.reg")
  61. {
  62. Framework::Datei regDat;
  63. regDat.setDatei(path);
  64. if (!regDat.existiert())
  65. {
  66. regDat.erstellen();
  67. }
  68. regDat.open(Framework::Datei::Style::lesen);
  69. while (!regDat.istEnde())
  70. {
  71. RegisteredPlayer* p = new RegisteredPlayer(&regDat);
  72. players.add(p);
  73. }
  74. regDat.close();
  75. }
  76. Framework::Text PlayerRegister::addPlayer(Framework::Text name) {
  77. cs.lock();
  78. int nextId = 0;
  79. for (RegisteredPlayer* player : players)
  80. {
  81. nextId = MAX(nextId, player->getId() + 1);
  82. if (player->getName().istGleich(name))
  83. {
  84. cs.unlock();
  85. return "";
  86. }
  87. }
  88. RegisteredPlayer* p = new RegisteredPlayer(nextId, name);
  89. players.add(p);
  90. cs.unlock();
  91. return p->getSecret();
  92. }
  93. bool PlayerRegister::hasPlayer(Framework::Text name)
  94. {
  95. cs.lock();
  96. for (RegisteredPlayer* player : players)
  97. {
  98. if (player->getName().istGleich(name))
  99. {
  100. cs.unlock();
  101. return 1;
  102. }
  103. }
  104. cs.unlock();
  105. return 0;
  106. }
  107. int PlayerRegister::getPlayerId(Framework::Text name) {
  108. cs.lock();
  109. for (RegisteredPlayer* player : players)
  110. {
  111. if (player->getName().istGleich(name))
  112. {
  113. cs.unlock();
  114. return player->getId();
  115. }
  116. }
  117. cs.unlock();
  118. return -1;
  119. }
  120. bool PlayerRegister::checkSecret(Framework::Text name, Framework::Text secret)
  121. {
  122. cs.lock();
  123. for (RegisteredPlayer* player : players)
  124. {
  125. if (player->getName().istGleich(name))
  126. {
  127. cs.unlock();
  128. return player->getSecret().istGleich(secret);
  129. }
  130. }
  131. cs.unlock();
  132. return secret.getLength() == 0; // new player
  133. }
  134. void PlayerRegister::save()
  135. {
  136. cs.lock();
  137. Framework::Datei regDat;
  138. regDat.setDatei(path);
  139. if (!regDat.existiert())
  140. {
  141. regDat.erstellen();
  142. }
  143. regDat.open(Framework::Datei::Style::schreiben);
  144. for (RegisteredPlayer* player : players)
  145. {
  146. player->save(&regDat);
  147. }
  148. regDat.close();
  149. cs.unlock();
  150. }