ServerSelection.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. #include "ServerSelection.h"
  2. #include <AlphaFeld.h>
  3. #include <AsynchronCall.h>
  4. #include <Base64.h>
  5. #include <Bild.h>
  6. #include <Datei.h>
  7. #include <DateiSystem.h>
  8. #include <JSON.h>
  9. #include <Knopf.h>
  10. #include <Schrift.h>
  11. #include "FactoryClient.h"
  12. #include "Globals.h"
  13. #include "Initialisierung.h"
  14. using namespace Framework;
  15. ServerStatus::ServerStatus(Framework::Text name,
  16. Framework::Text ip,
  17. unsigned short sslPort,
  18. unsigned short port,
  19. Framework::HashMap<Framework::Text, Framework::Text>* secrets)
  20. : ZeichnungHintergrund(),
  21. name(name),
  22. ip(ip),
  23. sslPort(sslPort),
  24. port(port),
  25. playerName(""),
  26. ping(-1),
  27. status("..."),
  28. statusId(0),
  29. requestId(0),
  30. secrets(secrets),
  31. join(initKnopf(0, 0, 55, 20, Knopf::Style::Normal, "join"))
  32. {
  33. setMausEreignis(_ret1ME);
  34. setStyle(Style::Erlaubt | Style::Sichtbar | Style::Rahmen);
  35. setRahmenBreite(1);
  36. setRahmenFarbe(0xFF3d3d3d);
  37. join->setMausEreignis([this](void* p, void* o, MausEreignis me) {
  38. if (me.id == ME_RLinks)
  39. {
  40. FactoryClient* client = new FactoryClient();
  41. if (!client->connect(getIp(), getSSLPort()))
  42. {
  43. lockZeichnung();
  44. status = "The Server is currently not reachable";
  45. statusId = 404;
  46. ping = -1;
  47. rend = 1;
  48. unlockZeichnung();
  49. client->release();
  50. }
  51. else
  52. {
  53. Text secret = "";
  54. if (this->secrets->has(playerName))
  55. secret = this->secrets->get(playerName);
  56. if (statusId == 203) secret = "";
  57. bool isNew = secret.getLength() == 0;
  58. int stId = client->join(playerName, secret, getPort());
  59. lockZeichnung();
  60. statusId = stId;
  61. if (statusId == 403) status = "The name is already in use";
  62. if (statusId == 500) status = "Unknown Server message received";
  63. if (statusId == 201) status = "Please try again";
  64. unlockZeichnung();
  65. if (statusId == 200 || statusId == 201)
  66. {
  67. if (statusId == 200)
  68. ((ServerSelectionMenu*)(Menu*)menuRegister->get(
  69. "serverSelection"))
  70. ->hide();
  71. if (isNew)
  72. {
  73. this->secrets->put(playerName, secret);
  74. ((ServerSelectionMenu*)(Menu*)menuRegister->get(
  75. "serverSelection"))
  76. ->saveServers();
  77. }
  78. if (statusId == 200)
  79. {
  80. World::INSTANCE = new World(
  81. dynamic_cast<Bildschirm3D*>(window->zBildschirm()),
  82. client);
  83. ((ServerSelectionMenu*)(Menu*)menuRegister->get("game"))
  84. ->show();
  85. }
  86. }
  87. else
  88. client->release();
  89. }
  90. }
  91. return 1;
  92. });
  93. LTDBDatei iconsDat;
  94. iconsDat.setDatei(new Text("data/images/gui_icons.ltdb"));
  95. iconsDat.leseDaten(0);
  96. removeButton = initKnopf(0,
  97. 0,
  98. 20,
  99. 20,
  100. Framework::Knopf::Style::HBild | Framework::Knopf::Style::HAlpha
  101. | Framework::Knopf::Style::Hintergrund,
  102. "");
  103. removeButton->setRahmenFarbe(0xFF9C0A0A);
  104. removeButton->setAlphaFeldFarbe(0x5F9C0A0A);
  105. removeButton->setToolTipText("Remove player from list of ignored players",
  106. uiFactory.initParam.bildschirm,
  107. uiFactory.initParam.schrift);
  108. removeButton->setHintergrundBildZ(
  109. dynamic_cast<Bild*>(iconsDat.laden(0, new Text("trash.png"))));
  110. removeButton->setMausEreignis([this](void* p, void* o, MausEreignis me) {
  111. if (me.id == ME_RLinks)
  112. {
  113. ((ServerSelectionMenu*)(Menu*)menuRegister->get("serverSelection"))
  114. ->removeServer(this->name);
  115. }
  116. return 1;
  117. });
  118. }
  119. ServerStatus::~ServerStatus()
  120. {
  121. secrets->release();
  122. removeButton->release();
  123. join->release();
  124. }
  125. void ServerStatus::updatePlayerName(Framework::Text playerName)
  126. {
  127. lockZeichnung();
  128. status = "";
  129. this->playerName = playerName;
  130. ServerStatus* tmp = dynamic_cast<ServerStatus*>(getThis());
  131. int id = ++requestId;
  132. unlockZeichnung();
  133. new AsynchronCall([tmp, id]() {
  134. FactoryClient* client = new FactoryClient();
  135. if (!client->connect(tmp->getIp(), tmp->getSSLPort()))
  136. {
  137. tmp->lockZeichnung();
  138. if (tmp->requestId == id)
  139. {
  140. tmp->status = "The Server is currently not reachable";
  141. tmp->statusId = 404;
  142. tmp->ping = -1;
  143. tmp->rend = 1;
  144. }
  145. tmp->unlockZeichnung();
  146. }
  147. else
  148. {
  149. tmp->lockZeichnung();
  150. if (tmp->requestId == id)
  151. {
  152. Text secret = "";
  153. if (tmp->secrets->has(tmp->playerName))
  154. secret = tmp->secrets->get(tmp->playerName);
  155. tmp->unlockZeichnung();
  156. int ping = client->ping();
  157. int statusId = client->status(tmp->playerName, secret);
  158. if (statusId == 403)
  159. {
  160. int secondStatus = client->status(tmp->playerName, "");
  161. if (secondStatus == 200) statusId = 203;
  162. }
  163. if (secret.istGleich("") && statusId == 200)
  164. {
  165. statusId = 203;
  166. }
  167. tmp->lockZeichnung();
  168. if (tmp->requestId == id)
  169. {
  170. tmp->ping = ping;
  171. tmp->statusId = statusId;
  172. if (tmp->statusId == 200)
  173. tmp->status = "The Server is reachable";
  174. if (statusId == 203) tmp->status = "Create a new player";
  175. if (tmp->statusId == 403)
  176. tmp->status = "The name is already in use";
  177. tmp->rend = 1;
  178. }
  179. }
  180. tmp->unlockZeichnung();
  181. }
  182. client->release();
  183. tmp->release();
  184. });
  185. }
  186. void ServerStatus::doMausEreignis(Framework::MausEreignis& me, bool userRet)
  187. {
  188. removeButton->doPublicMausEreignis(me);
  189. if (canConnect()) join->doPublicMausEreignis(me);
  190. }
  191. bool ServerStatus::tick(double time)
  192. {
  193. join->setPosition(gr.x - 60, gr.y - 25);
  194. removeButton->setPosition(gr.x - 20, 0);
  195. return ZeichnungHintergrund::tick(time) || join->tick(time)
  196. || removeButton->tick(time);
  197. }
  198. void ServerStatus::render(Framework::Bild& rObj)
  199. {
  200. ZeichnungHintergrund::render(rObj);
  201. if (rObj.setDrawOptions(pos, gr))
  202. {
  203. TextRenderer tr(
  204. dynamic_cast<Schrift*>(uiFactory.initParam.schrift->getThis()));
  205. tr.setSchriftSize(12);
  206. tr.renderText(5, 5, name, rObj, 0xFFFFFFFF);
  207. tr.renderText(
  208. 5, 25, ip + ":" + sslPort + " (" + port + ")", rObj, 0xFF808080);
  209. if (requestId && requestId != 404)
  210. {
  211. int tbr = tr.getTextBreite(Text("ping: ") + ping);
  212. if (ping >= 0)
  213. {
  214. tr.renderText(getBreite() - 25 - tbr,
  215. 5,
  216. Text("ping: ") + ping,
  217. rObj,
  218. 0xFFFFFFFF);
  219. }
  220. }
  221. if (statusId == 404 || statusId == 500)
  222. tr.renderText(5, 45, status, rObj, 0xFFFF0000);
  223. if (statusId == 403 || statusId == 201 || statusId == 203)
  224. tr.renderText(5, 45, status, rObj, 0xFFFFA500);
  225. if (statusId == 200) tr.renderText(5, 45, status, rObj, 0xFF00FF00);
  226. removeButton->render(rObj);
  227. if (canConnect()) join->render(rObj);
  228. rObj.releaseDrawOptions();
  229. }
  230. }
  231. Framework::Text ServerStatus::getName() const
  232. {
  233. return name;
  234. }
  235. Framework::Text ServerStatus::getIp() const
  236. {
  237. return ip;
  238. }
  239. unsigned short ServerStatus::getSSLPort() const
  240. {
  241. return sslPort;
  242. }
  243. unsigned short ServerStatus::getPort() const
  244. {
  245. return port;
  246. }
  247. Framework::HashMap<Framework::Text, Framework::Text>*
  248. ServerStatus::zSecrets() const
  249. {
  250. return secrets;
  251. }
  252. bool ServerStatus::canConnect() const
  253. {
  254. return statusId == 200 || statusId == 201 || statusId == 203;
  255. }
  256. ServerSelectionMenu::ServerSelectionMenu(Bildschirm* zScreen)
  257. : Menu(zScreen)
  258. {
  259. playerNameLabel = initTextFeld(zScreen->getBackBufferSize().x / 2 - 150,
  260. zScreen->getBackBufferSize().y / 2 - 300,
  261. 85,
  262. 20,
  263. TextFeld::Style::Text | TextFeld::Style::VCenter,
  264. "Player Name:");
  265. elements.add(playerNameLabel);
  266. playerName = initTextFeld(zScreen->getBackBufferSize().x / 2 - 150 + 85,
  267. zScreen->getBackBufferSize().y / 2 - 300,
  268. 215,
  269. 20,
  270. TextFeld::Style::TextFeld,
  271. "");
  272. playerName->setNTastaturEreignis(
  273. [this](void* p, void* o, TastaturEreignis te) {
  274. // set player names of each server
  275. for (int i = 0; i < serverList->getEintragAnzahl(); i++)
  276. ((ServerStatus*)serverList->zEintrag(i))
  277. ->updatePlayerName(playerName->zText()->getText());
  278. saveServers();
  279. return 1;
  280. });
  281. elements.add(playerName);
  282. serverLabel = initTextFeld(zScreen->getBackBufferSize().x / 2 - 150,
  283. zScreen->getBackBufferSize().y / 2 - 275,
  284. 100,
  285. 20,
  286. TextFeld::Style::Text | TextFeld::Style::VCenter,
  287. "Server:");
  288. elements.add(serverLabel);
  289. refresh = initKnopf(zScreen->getBackBufferSize().x / 2 + 80,
  290. zScreen->getBackBufferSize().y / 2 - 275,
  291. 70,
  292. 20,
  293. Knopf::Style::Normal,
  294. "Refresh");
  295. refresh->setMausEreignis(
  296. [this](void* p, void* o, Framework::MausEreignis me) {
  297. if (me.id == ME_RLinks)
  298. {
  299. if (playerName->zText()->getLength() > 0)
  300. {
  301. for (int i = 0; i < serverList->getEintragAnzahl(); i++)
  302. ((ServerStatus*)serverList->zEintrag(i))
  303. ->updatePlayerName(playerName->zText()->getText());
  304. }
  305. }
  306. return 1;
  307. });
  308. elements.add(refresh);
  309. serverList = new ZListe();
  310. serverList->setSize(300, 480);
  311. serverList->setRahmenBreite(1);
  312. serverList->setRahmenFarbe(0xFF6d6d6d);
  313. serverList->setPosition(zScreen->getBackBufferSize().x / 2 - 150,
  314. zScreen->getBackBufferSize().y / 2 - 250);
  315. serverList->setStyle(ZListe::Style::Normal | ZListe::Style::VScroll);
  316. serverList->setMausEreignis(_ret1ME);
  317. serverList->setVertikalScrollPos(0);
  318. serverList->setEntrySeperatorSize(1);
  319. serverList->setEntrySeperatorColor(0xFF6d6d6d);
  320. elements.add(serverList);
  321. add = initKnopf(zScreen->getBackBufferSize().x / 2 - 50,
  322. zScreen->getBackBufferSize().y / 2 + 235,
  323. 100,
  324. 20,
  325. Knopf::Style::Normal,
  326. "Add Server");
  327. elements.add(add);
  328. add->setMausEreignis([this](void* p, void* o, Framework::MausEreignis me) {
  329. if (me.id == ME_RLinks)
  330. {
  331. hide();
  332. menuRegister->get("addServer")->show();
  333. }
  334. return 1;
  335. });
  336. exit = initKnopf(zScreen->getBackBufferSize().x / 2 - 50,
  337. zScreen->getBackBufferSize().y / 2 + 285,
  338. 100,
  339. 20,
  340. Knopf::Style::Normal,
  341. "Beenden");
  342. elements.add(exit);
  343. exit->setRahmenFarbe(0xFF9C0A0A);
  344. exit->setAlphaFeldStrength(10);
  345. exit->setAlphaFeldFarbe(0x5F9C0A0A);
  346. exit->setMausEreignis([this](void* p, void* o, Framework::MausEreignis me) {
  347. if (me.id == ME_RLinks)
  348. {
  349. hide();
  350. StopNachrichtenSchleife(0);
  351. }
  352. return 1;
  353. });
  354. // load server json
  355. JSON::JSONValue* json = JSON::loadJSONFromFile("data/server.json");
  356. if (json)
  357. {
  358. // build validator
  359. JSON::Validator::JSONValidator* validator
  360. = JSON::Validator::JSONValidator::buildForObject()
  361. ->withRequiredString("playerName")
  362. ->withDefault("")
  363. ->finishString()
  364. ->withRequiredAttribute("server",
  365. JSON::Validator::JSONValidator::buildForArray()
  366. ->withDefault(new JSON::JSONArray())
  367. ->removeInvalidEntries()
  368. ->addAcceptedTypeInArray(
  369. JSON::Validator::JSONValidator::buildForObject()
  370. ->withRequiredString("name")
  371. ->finishString()
  372. ->withRequiredString("ip")
  373. ->finishString()
  374. ->withRequiredNumber("sslPort")
  375. ->whichIsGreaterOrEqual(0)
  376. ->whichIsLessOrEqual((double)0xFFFF)
  377. ->finishNumber()
  378. ->withRequiredNumber("port")
  379. ->whichIsGreaterOrEqual(0)
  380. ->whichIsLessOrEqual((double)0xFFFF)
  381. ->finishNumber()
  382. ->withRequiredAttribute("secrets",
  383. JSON::Validator::JSONValidator::
  384. buildForArray()
  385. ->addAcceptedObjectInArray()
  386. ->withRequiredString("secret")
  387. ->finishString()
  388. ->withRequiredString("playerName")
  389. ->finishString()
  390. ->finishObject()
  391. ->finishArray())
  392. ->finishObject())
  393. ->finishArray())
  394. ->finishObject();
  395. RCArray<JSON::Validator::JSONValidationResult> removedParts;
  396. JSON::JSONValue* validJson
  397. = validator->getValidParts(json, &removedParts);
  398. json->release();
  399. if (validJson)
  400. {
  401. JSON::JSONArray* arr
  402. = validJson->asObject()->zValue("server")->asArray();
  403. for (int i = 0; i < arr->getLength(); i++)
  404. {
  405. JSON::JSONObject* obj = arr->zValue(i)->asObject();
  406. Framework::HashMap<Framework::Text, Framework::Text>* secrets
  407. = new Framework::HashMap<Framework::Text, Framework::Text>(
  408. 10, [](Text t) { return t.hashCode(); });
  409. JSON::JSONArray* secretsJson
  410. = obj->zValue("secrets")->asArray();
  411. for (int j = 0; j < secretsJson->getLength(); j++)
  412. {
  413. JSON::JSONObject* obj2 = secretsJson->zValue(j)->asObject();
  414. // decode base64 secret
  415. char* secretBuffer = 0;
  416. int length;
  417. Framework::base64Decode(
  418. obj2->zValue("secret")->asString()->getString(),
  419. &secretBuffer,
  420. &length);
  421. secrets->put(
  422. obj2->zValue("playerName")->asString()->getString(),
  423. Framework::Text(secretBuffer));
  424. delete[] secretBuffer;
  425. }
  426. ServerStatus* s = new ServerStatus(
  427. obj->zValue("name")->asString()->getString(),
  428. obj->zValue("ip")->asString()->getString(),
  429. (unsigned short)obj->zValue("sslPort")
  430. ->asNumber()
  431. ->getNumber(),
  432. (unsigned short)obj->zValue("port")
  433. ->asNumber()
  434. ->getNumber(),
  435. secrets);
  436. s->setSize(300, 65);
  437. serverList->addEintrag(s);
  438. }
  439. playerName->setText(validJson->asObject()
  440. ->zValue("playerName")
  441. ->asString()
  442. ->getString());
  443. if (playerName->zText()->getLength() > 0)
  444. {
  445. for (int i = 0; i < serverList->getEintragAnzahl(); i++)
  446. ((ServerStatus*)serverList->zEintrag(i))
  447. ->updatePlayerName(playerName->zText()->getText());
  448. }
  449. serverList->updateVScroll();
  450. validJson->release();
  451. }
  452. validator->release();
  453. }
  454. }
  455. void ServerSelectionMenu::addServer(Framework::Text name,
  456. Framework::Text ip,
  457. unsigned short sslPort,
  458. unsigned short port)
  459. {
  460. ServerStatus* tmp = new ServerStatus(name,
  461. ip,
  462. sslPort,
  463. port,
  464. new HashMap<Framework::Text, Framework::Text>(
  465. 10, [](Text t) { return t.hashCode(); }));
  466. tmp->setSize(300, 65);
  467. serverList->addEintrag(tmp);
  468. tmp->updatePlayerName(playerName->zText()->getText());
  469. serverList->updateVScroll();
  470. saveServers();
  471. }
  472. bool ServerSelectionMenu::hasServer(Framework::Text name) const
  473. {
  474. for (int i = 0; i < serverList->getEintragAnzahl(); i++)
  475. {
  476. if (((ServerStatus*)serverList->zEintrag(i))->getName() == name)
  477. return 1;
  478. }
  479. return 0;
  480. }
  481. void ServerSelectionMenu::removeServer(Framework::Text name) const
  482. {
  483. for (int i = 0; i < serverList->getEintragAnzahl(); i++)
  484. {
  485. if (((ServerStatus*)serverList->zEintrag(i))->getName() == name)
  486. serverList->removeEintrag(i);
  487. }
  488. serverList->updateVScroll();
  489. saveServers();
  490. }
  491. void ServerSelectionMenu::saveServers() const
  492. {
  493. Datei file;
  494. file.setDatei("data/server.json");
  495. file.open(Datei::Style::schreiben);
  496. JSON::JSONObject root;
  497. JSON::JSONArray* servers = new JSON::JSONArray();
  498. for (int i = 0; i < serverList->getEintragAnzahl(); i++)
  499. {
  500. JSON::JSONObject* server = new JSON::JSONObject();
  501. server->addValue("name",
  502. new JSON::JSONString(
  503. ((ServerStatus*)serverList->zEintrag(i))->getName()));
  504. server->addValue("ip",
  505. new JSON::JSONString(
  506. ((ServerStatus*)serverList->zEintrag(i))->getIp()));
  507. server->addValue("sslPort",
  508. new JSON::JSONNumber(
  509. ((ServerStatus*)serverList->zEintrag(i))->getSSLPort()));
  510. server->addValue("port",
  511. new JSON::JSONNumber(
  512. ((ServerStatus*)serverList->zEintrag(i))->getPort()));
  513. JSON::JSONArray* secrets = new JSON::JSONArray();
  514. for (auto secretEntry :
  515. *((ServerStatus*)serverList->zEintrag(i))->zSecrets())
  516. {
  517. JSON::JSONObject* secret = new JSON::JSONObject();
  518. secret->addValue(
  519. "playerName", new JSON::JSONString(secretEntry.getKey()));
  520. // base64 encode secret
  521. Framework::Text secretBase64 = Framework::base64Encode(
  522. secretEntry.getValue(), secretEntry.getValue().getLength());
  523. secret->addValue("secret", new JSON::JSONString(secretBase64));
  524. secrets->addValue(secret);
  525. }
  526. server->addValue("secrets", secrets);
  527. servers->addValue(server);
  528. }
  529. root.addValue("server", servers);
  530. root.addValue(
  531. "playerName", new JSON::JSONString(playerName->zText()->getText()));
  532. Framework::Text json = root.toString();
  533. file.schreibe(json, json.getLength());
  534. file.close();
  535. }