ServerSelection.cpp 14 KB

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