SpielErstellt.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #ifdef WIN32
  2. #include "..\SpielServer.h"
  3. #include "..\..\Datenbank\Datenbank.h"
  4. #else
  5. #include "SpielServer.h"
  6. #include "Datenbank.h"
  7. #include <unistd.h>
  8. #define Sleep( x ) usleep( (x) * 1000 )
  9. #endif
  10. #include <Zeit.h>
  11. // Inhalt der SpielErstellt Kasse aus SpielErstellt.h
  12. // Konstruktor
  13. SpielErstellt::SpielErstellt(int id, SpielServer* ss)
  14. : Thread()
  15. {
  16. this->ss = ss;
  17. this->id = id;
  18. karte = ss->zDB()->getKarteVonErstelltemSpiel(id);
  19. spielArt = ss->zDB()->getSpielArtId(karte);
  20. accounts = new Array< int >();
  21. spielerAnzahl = ss->zDB()->getSpielErstelltSpieler(id, accounts);
  22. klients = new RCArray< SSKlient >();
  23. status = new Array< int >();
  24. verbunden = 0;
  25. state = 0;
  26. abbr = 0;
  27. spielId = 0;
  28. }
  29. // Destruktor
  30. SpielErstellt::~SpielErstellt()
  31. {
  32. ss->release();
  33. accounts->release();
  34. klients->release();
  35. status->release();
  36. }
  37. // nicht constant
  38. void SpielErstellt::abbruch()
  39. {
  40. abbr = 1;
  41. }
  42. void SpielErstellt::sendeVerbindungsBefehl()
  43. {
  44. unsigned short sPort = (unsigned short)ss->getPort();
  45. const char* sIp = ss->getIp();
  46. unsigned char* sIp4 = new unsigned char[4];
  47. Text* tmpT = new Text(sIp);
  48. Text* ip1 = tmpT->getTeilText(0, tmpT->positionVon('.'));
  49. Text* ip2 = tmpT->getTeilText(tmpT->positionVon('.') + 1, tmpT->positionVon('.', 1));
  50. Text* ip3 = tmpT->getTeilText(tmpT->positionVon('.', 1) + 1, tmpT->positionVon('.', 2));
  51. Text* ip4 = tmpT->getTeilText(tmpT->positionVon('.', 2) + 1);
  52. sIp4[0] = (unsigned char)TextZuInt(ip1->getText(), 10);
  53. sIp4[1] = (unsigned char)TextZuInt(ip2->getText(), 10);
  54. sIp4[2] = (unsigned char)TextZuInt(ip3->getText(), 10);
  55. sIp4[3] = (unsigned char)TextZuInt(ip4->getText(), 10);
  56. ip1->release();
  57. ip2->release();
  58. ip3->release();
  59. ip4->release();
  60. tmpT->release();
  61. for (int i = 0; i < spielerAnzahl; i++)
  62. {
  63. int accId = accounts->hat(i) ? accounts->get(i) : 0;
  64. int port = 0;
  65. Text* ip = new Text("");
  66. if (ss->zDB()->getChatServerPortIp(accId, &port, ip))
  67. {
  68. char ret = 0;
  69. Klient* klient = new Klient();
  70. unsigned char key[20] = { 78, 20, 190, 133, 10, 175, 51, 96, 62, 1, 180, 194, 126, 50, 211, 154, 105, 227, 22, 101 };
  71. klient->setSendeKey((char*)key, 20);
  72. klient->setEmpfangKey((char*)key, 20);
  73. klient->verbinde((unsigned short)port, ip->getText());
  74. klient->sendeEncrypted("\5\x1D", 2);
  75. klient->getNachrichtEncrypted(&ret, 1);
  76. if (ret != 3)
  77. {
  78. klient->sendeEncrypted((char*)&accId, 4);
  79. klient->sendeEncrypted((char*)&sPort, 2);
  80. klient->sendeEncrypted((char*)sIp4, 4);
  81. klient->getNachrichtEncrypted(&ret, 1);
  82. }
  83. if (ret == 3)
  84. {
  85. unsigned char len = 0;
  86. klient->getNachrichtEncrypted((char*)&len, 1);
  87. char* err = new char[len];
  88. klient->getNachrichtEncrypted(err, len);
  89. delete[] err;
  90. }
  91. klient->sendeEncrypted("\3", 1);
  92. klient->getNachrichtEncrypted(&ret, 1);
  93. ss->addEmpfangen(klient->getDownloadBytes(1));
  94. ss->addGesendet(klient->getUploadBytes(1));
  95. klient->trenne();
  96. klient->release();
  97. }
  98. ip->release();
  99. }
  100. delete[] sIp4;
  101. start();
  102. }
  103. bool SpielErstellt::klientVerbunden(SSKlient* klient)
  104. {
  105. if (verbunden == spielerAnzahl)
  106. {
  107. klient->release();
  108. return 0;
  109. }
  110. int accId = klient->getAccountId();
  111. bool ret = 0;
  112. for (int i = 0; i < spielerAnzahl; i++)
  113. {
  114. if (accounts->hat(i) && accounts->get(i) == accId)
  115. {
  116. if (state == 1)
  117. klient->spielGefunden(karte);
  118. while (!klients->hat(i))
  119. klients->add(0);
  120. klients->set(klient, i);
  121. while (!status->hat(i))
  122. status->add(0);
  123. status->set(1, i);
  124. verbunden++;
  125. klient->setSpielErstellt(this);
  126. ret = 1;
  127. break;
  128. }
  129. }
  130. if (!ret)
  131. klient->release();
  132. return ret;
  133. }
  134. bool SpielErstellt::klientNimmtAn(int accountId)
  135. {
  136. if (!verbunden || state != 1)
  137. return 0;
  138. bool ret = 0;
  139. for (int i = 0; i < spielerAnzahl; i++)
  140. {
  141. if (accounts->hat(i) && accounts->get(i) == accountId)
  142. {
  143. while (!status->hat(i))
  144. status->add(0);
  145. status->set(2, i);
  146. ret = 1;
  147. break;
  148. }
  149. }
  150. return ret;
  151. }
  152. bool SpielErstellt::klientLehntAb(int accountId)
  153. {
  154. if (!verbunden || state != 1)
  155. return 0;
  156. bool ret = 0;
  157. for (int i = 0; i < spielerAnzahl; i++)
  158. {
  159. if (accounts->hat(i) && accounts->get(i) == accountId)
  160. {
  161. while (!status->hat(i))
  162. status->add(0);
  163. status->set(3, i);
  164. ret = 1;
  165. break;
  166. }
  167. }
  168. return ret;
  169. }
  170. bool SpielErstellt::klientGetrennt(SSKlient* zKlient)
  171. {
  172. if (!verbunden)
  173. return 0;
  174. int accId = zKlient->getAccountId();
  175. bool ret = 0;
  176. for (int i = 0; i < spielerAnzahl; i++)
  177. {
  178. if (accounts->hat(i) && accounts->get(i) == accId)
  179. {
  180. if (status->hat(i) && status->get(i) != 0)
  181. {
  182. zKlient->setSpielErstellt(0);
  183. while (!status->hat(i))
  184. status->add(0);
  185. status->set(0, i);
  186. verbunden--;
  187. while (!klients->hat(i))
  188. klients->add(0);
  189. klients->set(0, i);
  190. ret = 1;
  191. }
  192. break;
  193. }
  194. }
  195. return ret;
  196. }
  197. void SpielErstellt::thread()
  198. {
  199. state = 1;
  200. double time = 0;
  201. ZeitMesser* zeit = new ZeitMesser();
  202. int nachrichtSenden = 1;
  203. while (time < 10)
  204. { // warten auf annahme von allen Spielern
  205. if (abbr)
  206. break;
  207. zeit->messungStart();
  208. Sleep(100);
  209. bool br = 0;
  210. bool st2 = 1;
  211. for (int i = 0; i < spielerAnzahl; i++)
  212. {
  213. if (status->hat(i) && status->get(i) == 3)
  214. {
  215. br = 1;
  216. for (int j = 0; j < spielerAnzahl; j++)
  217. {
  218. if (j != i)
  219. {
  220. while (!status->hat(j))
  221. status->add(0);
  222. status->set(2, j);
  223. }
  224. }
  225. break;
  226. }
  227. if (st2)
  228. st2 = status->hat(i) && status->get(i) == 2;
  229. }
  230. if (st2)
  231. br = 1;
  232. if (br)
  233. {
  234. zeit->messungEnde();
  235. break;
  236. }
  237. char verbleibend = (char)(10 - (char)time);
  238. if (verbleibend < 0)
  239. verbleibend = 0;
  240. if (time > nachrichtSenden)
  241. {
  242. for (int i = 0; i < spielerAnzahl; i++)
  243. {
  244. if (status->hat(i) && status->get(i) != 0)
  245. {
  246. SSKlient* tmp = klients->z(i);
  247. if (tmp)
  248. tmp->zeitVerbleibend(verbleibend);
  249. }
  250. }
  251. nachrichtSenden++;
  252. }
  253. zeit->messungEnde();
  254. time += zeit->getSekunden();
  255. } // prüfen ob alle Spieler bereit sind
  256. state = 2;
  257. bool abbruch = verbunden != spielerAnzahl;
  258. const char* err = "Ein Spieler konnte nicht erreicht werden.";
  259. if (!abbruch)
  260. {
  261. for (int i = 0; i < spielerAnzahl; i++)
  262. {
  263. if (!status->hat(i) || status->get(i) != 2)
  264. {
  265. abbruch = 1;
  266. err = "Ein Spieler ist noch nicht bereit.";
  267. break;
  268. }
  269. }
  270. }
  271. if (abbr)
  272. {
  273. err = "Das Spiel wurde Wegen abwesenheit eines Spielers Abgebrochen.";
  274. abbruch = 1;
  275. for (int j = 0; j < spielerAnzahl; j++)
  276. {
  277. while (!status->hat(j))
  278. status->add(0);
  279. status->set(2, j);
  280. }
  281. }
  282. if (abbruch)
  283. { // Es sind nicht alle bereit Vorgang abbrechen
  284. for (int i = 0; i < spielerAnzahl; i++)
  285. {
  286. if (status->hat(i) && status->get(i) != 0)
  287. {
  288. SSKlient* tmp = klients->z(i);
  289. if (tmp)
  290. tmp->erstellungAbgebrochen((char*)err);
  291. }
  292. }
  293. Array< bool >* inWarteschlange = new Array< bool >();
  294. RCArray< Zeit >* wZeit = new RCArray< Zeit >();
  295. if (ss->zDB()->spielErstelltAbbruch(id, spielerAnzahl, accounts, status, inWarteschlange, wZeit)) // zurück in Warteschlange
  296. {
  297. for (int i = 0; i < spielerAnzahl; i++)
  298. {
  299. if (inWarteschlange->hat(i) && inWarteschlange->get(i))
  300. {
  301. SSKlient* tmp = klients->z(i);
  302. if (tmp)
  303. {
  304. Zeit* zTmp = wZeit->z(i);
  305. if (zTmp)
  306. tmp->backInWarteschlange((char)zTmp->zUhrzeit()->getStunde(), (char)zTmp->zUhrzeit()->getMinute(), (char)zTmp->zUhrzeit()->getSekunde());
  307. }
  308. }
  309. }
  310. }
  311. inWarteschlange->release();
  312. wZeit->release();
  313. for (int i = 0; i < spielerAnzahl; i++)
  314. {
  315. SSKlient* tmp = klients->z(i);
  316. if (tmp)
  317. tmp->trenne();
  318. }
  319. zeit->release();
  320. ss->removeSpielErstellt(id); // delete this
  321. return;
  322. }
  323. // alle Spieler sind bereit Vorgang fortsetzen
  324. spielId = ss->zDB()->spielErstelltFortsetzen(id);
  325. Spiel* weiter = new Spiel(spielId, dynamic_cast<SpielServer*>(getThis()));
  326. ss->addSpiel(weiter);
  327. weiter->setAccounts(spielerAnzahl, accounts);
  328. weiter->setKlients(spielerAnzahl, klients);
  329. weiter->setKarteId(karte);
  330. ss->removeSpielErstellt(id); // delete this
  331. zeit->release();
  332. }
  333. // constant
  334. int SpielErstellt::getId() const
  335. {
  336. return id;
  337. }
  338. // Inhalt der SpielFinden Klasse aus SpielErstellt.h
  339. // Konstruktor
  340. SpielFinden::SpielFinden(SpielServer* ss)
  341. : Thread()
  342. {
  343. this->ss = ss;
  344. end = 0;
  345. spielServerId = 0;
  346. }
  347. // Destruktor
  348. SpielFinden::~SpielFinden()
  349. {
  350. end = 1;
  351. warteAufThread(2000);
  352. }
  353. // nicht constant
  354. void SpielFinden::setEnde()
  355. {
  356. end = 1;
  357. }
  358. void SpielFinden::setSpielServerId(int id)
  359. {
  360. spielServerId = id;
  361. }
  362. void SpielFinden::thread()
  363. {
  364. end = 0;
  365. while (!end)
  366. {
  367. for (int i = 0; i < 10 && !end; i++)
  368. Sleep(1000);
  369. if (end)
  370. break;
  371. int ret = ss->zDB()->erstelleSpiel(spielServerId);
  372. if (!ret)
  373. { // erfolg
  374. int id = ss->zDB()->getSpielErstelltId(spielServerId);
  375. SpielErstellt* erstellt = new SpielErstellt(id, dynamic_cast<SpielServer*>(getThis()));
  376. ss->addSpielErstellt(erstellt);
  377. erstellt->sendeVerbindungsBefehl();
  378. ss->zDB()->deleteSpielErstelltNext(spielServerId);
  379. }
  380. }
  381. run = 0;
  382. end = 0;
  383. }