Spiel.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. #include "Spiel.h"
  2. #include <Zeit.h>
  3. #ifdef WIN32
  4. #include "..\..\Datenbank\Datenbank.h"
  5. #include "..\SpielServer.h"
  6. #include "Reader\KartenLeser.h"
  7. #else
  8. #include "Datenbank.h"
  9. #include "SpielServer.h"
  10. #include "KartenLeser.h"
  11. #include <unistd.h>
  12. #define Sleep( x ) usleep( (x) * 1000 )
  13. #include <dlfcn.h>
  14. #define LoadLibrary( x ) dlopen( (x), RTLD_LAZY )
  15. #define GetProcAddress dlsym
  16. #define FreeLibrary dlclose
  17. #endif
  18. typedef SpielKlasse* (*DllStart)();
  19. // Inhalt der Spiel Klasse aus Spiel.h
  20. // Konstruktor
  21. Spiel::Spiel(int id, SpielServer* ss)
  22. : Thread()
  23. {
  24. this->ss = ss;
  25. this->id = id;
  26. klients = new RCArray< SSKlient >();
  27. sts = new SpielerTeamStruktur();
  28. accounts = new Array< int >();
  29. spielerNummern = new Array< int >();
  30. status = new Array< int >();
  31. prozent = new Array< int >();
  32. teamAuswahlFertig = new Array< bool >();
  33. spiel = 0;
  34. state = 0;
  35. spielerAnzahl = 0;
  36. karte = 0;
  37. spielDll = 0;
  38. statistik = 0;
  39. }
  40. // Destruktor
  41. Spiel::~Spiel()
  42. {
  43. klients->release();
  44. sts->release();
  45. accounts->release();
  46. spielerNummern->release();
  47. status->release();
  48. prozent->release();
  49. teamAuswahlFertig->release();
  50. if (spiel)
  51. spiel->release();
  52. if (statistik)
  53. statistik->release();
  54. if (spielDll)
  55. FreeLibrary(spielDll);
  56. ss->release();
  57. }
  58. int Spiel::findFreeTeam()
  59. {
  60. int free = 0;
  61. int teamIndex = 0;
  62. int teamSize = sts->teamSize->get(teamIndex);
  63. double max = 0;
  64. Array< int > teams;
  65. for (int i = 0; i < sts->spielerAnzahl; i++) // suche nach teams die prozentual am wenigsten voll sind
  66. {
  67. bool used = 0;
  68. for (int j = 0; j < spielerAnzahl; j++)
  69. {
  70. if (spielerNummern->hat(j) && spielerNummern->get(j) == i + 1)
  71. used = 1;
  72. }
  73. if (!used)
  74. free++;
  75. teamSize--;
  76. if (!teamSize)
  77. {
  78. double teamVal = (double)free / sts->teamSize->get(teamIndex);
  79. if (teamVal > max)
  80. {
  81. teams.leeren();
  82. max = teamVal;
  83. }
  84. if (teamVal >= max)
  85. teams.add(teamIndex);
  86. free = 0;
  87. if (!sts->teamSize->hat(++teamIndex))
  88. break;
  89. else
  90. teamSize = sts->teamSize->get(teamIndex);
  91. }
  92. } // suche aus den teams eines zufällig aus
  93. double randomVal = rand() / ((double)RAND_MAX + 1);
  94. double diff = 1.0 / teams.getEintragAnzahl();
  95. teamIndex = -1;
  96. while (randomVal >= 0)
  97. {
  98. randomVal -= diff;
  99. teamIndex++;
  100. }
  101. if (teams.hat(teamIndex))
  102. return teams.get(teamIndex);
  103. else
  104. return teams.get(0);
  105. }
  106. void Spiel::teamAusgleich()
  107. {
  108. for (int i = 0; i < spielerAnzahl; i++)
  109. {
  110. int sNum = spielerNummern->hat(i) ? spielerNummern->get(i) : 0;
  111. if (!sNum)
  112. {
  113. int team = findFreeTeam();
  114. for (int j = 0; j < team; j++)
  115. sNum += sts->teamSize->get(j);
  116. while (true)
  117. {
  118. sNum++;
  119. bool gefunden = 0;
  120. for (int k = 0; k < spielerAnzahl; k++)
  121. {
  122. if (spielerNummern->hat(k) && spielerNummern->get(k) == sNum)
  123. {
  124. gefunden = 1;
  125. break;
  126. }
  127. }
  128. if (!gefunden)
  129. break;
  130. }
  131. while (!spielerNummern->hat(i))
  132. spielerNummern->add(0);
  133. spielerNummern->set(sNum, i);
  134. }
  135. }
  136. }
  137. // nicht constant
  138. void Spiel::setAccounts(int anzahl, Array< int >* zAccounts)
  139. {
  140. accounts->leeren();
  141. for (int i = 0; i < anzahl; i++)
  142. accounts->add(zAccounts->hat(i) ? zAccounts->get(i) : 0);
  143. spielerAnzahl = anzahl;
  144. }
  145. void Spiel::setKlients(int anzahl, RCArray< SSKlient >* zKlients)
  146. {
  147. klients->leeren();
  148. for (int i = 0; i < anzahl; i++)
  149. klients->add(zKlients->get(i));
  150. }
  151. void Spiel::setKarteId(int karteId)
  152. {
  153. karte = karteId;
  154. KartenLeser* reader = new KartenLeser(id, karte, dynamic_cast<SpielServer*>(getThis()));
  155. reader->ladeSpielerTeamStruktur(sts);
  156. reader->release();
  157. start();
  158. }
  159. bool Spiel::klientVerbunden(SSKlient* klient)
  160. {
  161. int accId = klient->getAccountId();
  162. bool ret = 0;
  163. for (int i = 0; i < spielerAnzahl; i++)
  164. {
  165. if (accounts->hat(i) && accounts->get(i) == accId)
  166. {
  167. if (state == 1)
  168. {
  169. klient->erstellungInitialisierung(sts);
  170. for (int j = 0; j < spielerAnzahl; j++)
  171. {
  172. if (accounts->hat(j) && accounts->get(j) != accId)
  173. {
  174. SSKlient* tmp = klients->z(j);
  175. tmp->erstellungAddSpieler(accId);
  176. klient->erstellungAddSpieler(accounts->get(j));
  177. if (spielerNummern->hat(j))
  178. klient->erstellungSpielerTeam(accounts->get(j), spielerNummern->get(j));
  179. }
  180. }
  181. }
  182. if (state == 5 && spiel)
  183. spiel->klientOnline(klient->getAccountId(), klient);
  184. while (!klients->hat(i))
  185. klients->add(0);
  186. klients->set(klient, i);
  187. while (!status->hat(i))
  188. status->add(0);
  189. status->set(1, i);
  190. klient->setSpiel(this);
  191. ret = 1;
  192. break;
  193. }
  194. }
  195. if (!ret)
  196. klient->release();
  197. return ret;
  198. }
  199. bool Spiel::klientWechseltTeam(int accountId, int team)
  200. {
  201. if (state != 1)
  202. return 0;
  203. if (team == 0)
  204. {
  205. for (int i = 0; i < spielerAnzahl; i++)
  206. {
  207. if (accounts->hat(i) && accounts->get(i) == accountId)
  208. {
  209. while (!spielerNummern->hat(i))
  210. spielerNummern->add(0);
  211. spielerNummern->set(0, i);
  212. }
  213. SSKlient* tmp = klients->z(i);
  214. if (tmp)
  215. tmp->erstellungSpielerTeam(accountId, 0);
  216. }
  217. return 1;
  218. }
  219. int von = 1;
  220. for (int i = 0; i < team - 1; i++)
  221. {
  222. if (sts->teamSize->hat(i))
  223. von += sts->teamSize->get(i);
  224. }
  225. int bis = von + (sts->teamSize->hat(team - 1) ? sts->teamSize->get(team - 1) : 0);
  226. for (int i = von; i < bis; i++)
  227. {
  228. bool verwendet = 0;
  229. for (int j = 0; j < spielerAnzahl; j++)
  230. {
  231. if (spielerNummern->hat(j) && spielerNummern->get(j) == i)
  232. {
  233. verwendet = 1;
  234. break;
  235. }
  236. }
  237. if (!verwendet)
  238. {
  239. for (int j = 0; j < spielerAnzahl; j++)
  240. {
  241. if (accounts->hat(j) && accounts->get(j) == accountId)
  242. {
  243. while (!spielerNummern->hat(j))
  244. spielerNummern->add(0);
  245. spielerNummern->set(i, j);
  246. }
  247. SSKlient* tmp = klients->z(j);
  248. if (tmp)
  249. tmp->erstellungSpielerTeam(accountId, i);
  250. }
  251. return 1;
  252. }
  253. }
  254. return 0;
  255. }
  256. bool Spiel::chatNachricht(const char* nachricht)
  257. {
  258. if (state != 1)
  259. return 0;
  260. for (int i = 0; i < spielerAnzahl; i++)
  261. {
  262. SSKlient* tmp = klients->z(i);
  263. if (tmp)
  264. tmp->erstellungChatNachricht(nachricht);
  265. }
  266. return 1;
  267. }
  268. bool Spiel::klientGetrennt(SSKlient* zKlient)
  269. {
  270. int accId = zKlient->getAccountId();
  271. bool ret = 0;
  272. for (int i = 0; i < spielerAnzahl; i++)
  273. {
  274. if (accounts->hat(i) && accounts->get(i) == accId)
  275. {
  276. if (status->hat(i) && status->get(i) != 0)
  277. {
  278. if (state == 1)
  279. {
  280. for (int j = 0; j < spielerAnzahl; j++)
  281. {
  282. if (accounts->hat(j) && accounts->get(j) != accId)
  283. {
  284. SSKlient* tmp = klients->z(j);
  285. tmp->erstellungRemoveSpieler(accId);
  286. }
  287. }
  288. }
  289. if (state == 5 && spiel)
  290. spiel->klientOffline(zKlient->getAccountId());
  291. if (state == 6)
  292. {
  293. if (statistik)
  294. statistik->klientOffline(zKlient->getAccountId());
  295. }
  296. zKlient->setSpielErstellt(0);
  297. while (!spielerNummern->hat(i))
  298. spielerNummern->add(0);
  299. spielerNummern->set(0, i);
  300. while (!status->hat(i))
  301. status->add(0);
  302. status->set(0, i);
  303. while (!klients->hat(i))
  304. klients->add(0);
  305. klients->set(0, i);
  306. ret = 1;
  307. }
  308. break;
  309. }
  310. }
  311. return ret;
  312. }
  313. bool Spiel::klientTeamAuswahlFertig(int accountId)
  314. {
  315. for (int i = 0; i < spielerAnzahl; i++)
  316. {
  317. if (accounts->hat(i) && accounts->get(i) == accountId)
  318. {
  319. while (!teamAuswahlFertig->hat(i))
  320. teamAuswahlFertig->add(0);
  321. teamAuswahlFertig->set(1, i);
  322. return 1;
  323. }
  324. }
  325. return 0;
  326. }
  327. bool Spiel::klientBereitZumLaden(int accountId)
  328. {
  329. if (state != 2)
  330. return 0;
  331. for (int i = 0; i < spielerAnzahl; i++)
  332. {
  333. if (accounts->hat(i) && accounts->get(i) == accountId)
  334. {
  335. while (!status->hat(i))
  336. status->add(0);
  337. status->set(2, i);
  338. return 1;
  339. }
  340. }
  341. return 0;
  342. }
  343. bool Spiel::klientSpielLadenProzent(int accountId, int prozent)
  344. {
  345. if (state != 3 && state != 2)
  346. return 0;
  347. for (int i = 0; i < spielerAnzahl; i++)
  348. {
  349. if (accounts->hat(i) && accounts->get(i) == accountId)
  350. {
  351. while (!this->prozent->hat(i))
  352. this->prozent->add(0);
  353. this->prozent->set(prozent, i);
  354. return 1;
  355. }
  356. }
  357. return 0;
  358. }
  359. bool Spiel::klientBereitZumSpiel(int accountId)
  360. {
  361. if (state != 4)
  362. return 0;
  363. for (int i = 0; i < spielerAnzahl; i++)
  364. {
  365. if (accounts->hat(i) && accounts->get(i) == accountId)
  366. {
  367. while (!status->hat(i))
  368. status->add(0);
  369. status->set(3, i);
  370. return 1;
  371. }
  372. }
  373. return 0;
  374. }
  375. bool Spiel::spielNachricht(int accountId, int len, char* bytes)
  376. {
  377. if (state != 5)
  378. return 0;
  379. if (spiel)
  380. {
  381. spiel->nachricht(accountId, len, bytes);
  382. return 1;
  383. }
  384. return 0;
  385. }
  386. bool Spiel::statistikNachricht(int accountId, int len, char* bytes)
  387. {
  388. if (state != 6)
  389. return 0;
  390. if (statistik)
  391. {
  392. statistik->statistikAnfrage(accountId, (short)len, bytes);
  393. return 1;
  394. }
  395. return 0;
  396. }
  397. void Spiel::thread()
  398. {
  399. // Klients aktualisieren
  400. for (int i = 0; i < spielerAnzahl; i++)
  401. {
  402. SSKlient* tmp = klients->z(i);
  403. if (tmp)
  404. {
  405. tmp->setSpielErstellt(0);
  406. tmp->setSpiel(this);
  407. }
  408. }
  409. // Vortsetzung senden
  410. for (int i = 0; i < spielerAnzahl; i++)
  411. {
  412. SSKlient* tmp = klients->z(i);
  413. if (tmp)
  414. tmp->erstellungInitialisierung(sts);
  415. }
  416. for (int i = 0; i < spielerAnzahl; i++)
  417. {
  418. SSKlient* tmp = klients->z(i);
  419. if (tmp)
  420. tmp->erstellungFortsetzen();
  421. }
  422. for (int i = 0; i < spielerAnzahl; i++)
  423. {
  424. SSKlient* tmp = klients->z(i);
  425. if (tmp)
  426. {
  427. for (int j = 0; j < spielerAnzahl; j++)
  428. {
  429. if (accounts->hat(j))
  430. tmp->erstellungAddSpieler(accounts->get(j));
  431. }
  432. }
  433. }
  434. // warten bis alle ein Team gewählt haben
  435. double time = 0;
  436. state = 1;
  437. ZeitMesser* zeit = new ZeitMesser();
  438. while (time < 40)
  439. {
  440. zeit->messungStart();
  441. Sleep(100);
  442. char verbleibend = (char)(40 - (char)time);
  443. bool weiter = 1;
  444. for (int i = 0; i < spielerAnzahl; i++)
  445. weiter &= teamAuswahlFertig->hat(i) && teamAuswahlFertig->get(i);
  446. if (verbleibend < 0 || weiter)
  447. {
  448. verbleibend = 0;
  449. time = 40;
  450. }
  451. for (int i = 0; i < spielerAnzahl; i++)
  452. {
  453. SSKlient* tmp = klients->z(i);
  454. if (tmp)
  455. tmp->zeitVerbleibend(verbleibend);
  456. }
  457. zeit->messungEnde();
  458. time += zeit->getSekunden();
  459. } // Spielernummern Aufteilen
  460. zeit->release();
  461. state = 2;
  462. teamAusgleich();
  463. Array< int >* teams = new Array< int >();
  464. for (int i = 0; i < spielerAnzahl; i++)
  465. {
  466. int sNum = spielerNummern->hat(i) ? spielerNummern->get(i) : 0;
  467. int tg = 1;
  468. for (int j = 0; j < sts->teamAnzahl; j++)
  469. {
  470. tg += sts->teamSize->hat(j) ? sts->teamSize->get(j) : 0;
  471. if (sNum < tg)
  472. {
  473. while (!teams->hat(i))
  474. teams->add(0);
  475. teams->set(j, i);
  476. break;
  477. }
  478. }
  479. }
  480. ss->zDB()->spielSetTeams(id, spielerAnzahl, accounts, teams);
  481. ss->zDB()->spielSetSpielerNummern(id, spielerAnzahl, accounts, spielerNummern);
  482. teams->release();
  483. // Fortsetzung senden
  484. for (int i = 0; i < spielerAnzahl; i++)
  485. {
  486. SSKlient* tmp = klients->z(i);
  487. if (tmp)
  488. tmp->spielLadenBeginn();
  489. }
  490. // Auf bereitschaft warten
  491. while (1)
  492. {
  493. Sleep(100);
  494. bool br = 1;
  495. for (int i = 0; i < spielerAnzahl; i++)
  496. {
  497. if (klients->z(i))
  498. br = br && status->hat(i) && status->get(i) == 2;
  499. }
  500. if (br)
  501. break;
  502. } // Spieler hinzufügen
  503. for (int i = 0; i < spielerAnzahl; i++)
  504. spielerNummern->set((spielerNummern->hat(i) ? spielerNummern->get(i) : 0) - 1, i);
  505. for (int i = 0; i < spielerAnzahl; i++)
  506. {
  507. SSKlient* tmp = klients->z(i);
  508. if (tmp)
  509. {
  510. for (int j = 0; j < spielerAnzahl; j++)
  511. tmp->spielLadenSpielerAdd(accounts->hat(j) ? accounts->get(j) : 0, spielerNummern->hat(j) ? spielerNummern->get(j) : 0);
  512. }
  513. }
  514. state = 3;
  515. // Warten auf laden
  516. while (1)
  517. {
  518. Sleep(1000);
  519. bool br = 1;
  520. for (int i = 0; i < spielerAnzahl; i++)
  521. {
  522. SSKlient* tmp = klients->z(i);
  523. if (tmp)
  524. {
  525. int ping = (int)(tmp->ping() * 1000 + 0.5);
  526. for (int j = 0; j < spielerAnzahl; j++)
  527. {
  528. SSKlient* k = klients->z(j);
  529. if (k)
  530. {
  531. k->spielLadenSetSpielerPing(accounts->hat(i) ? accounts->get(i) : 0, ping);
  532. k->spielLadenSetSpielerProzent(accounts->hat(i) ? accounts->get(i) : 0, prozent->hat(i) ? prozent->get(i) : 0);
  533. }
  534. }
  535. br = br && prozent->hat(i) && prozent->get(i) == 100;
  536. }
  537. }
  538. if (br)
  539. break;
  540. }
  541. state = 4;
  542. // Sende Fortsetzung
  543. for (int i = 0; i < spielerAnzahl; i++)
  544. {
  545. SSKlient* tmp = klients->z(i);
  546. if (tmp)
  547. tmp->spielLadenFertig();
  548. }
  549. // Spiel Initialisieren
  550. Text* pfad = new Text(ss->zInit()->zWert("SpielePfad")->getText());
  551. pfad->append("/");
  552. pfad->append(ss->zDB()->getSpielArtId(karte));
  553. pfad->append("/bin/game");
  554. #ifdef WIN32
  555. pfad->append(".dll");
  556. #else
  557. pfad->append(".so");
  558. #endif
  559. spielDll = LoadLibrary(pfad->getText());
  560. if (!spielDll)
  561. {
  562. char* err = dlerror();
  563. Text tmp = ss->zInit()->zWert("TempMapPfad")->getText();
  564. tmp += "/";
  565. tmp += id;
  566. DateiRemove(tmp);
  567. for (int i = 0; i < spielerAnzahl; i++)
  568. {
  569. SSKlient* tmp = klients->z(i);
  570. if (tmp)
  571. {
  572. tmp->setSpiel(0);
  573. tmp->errorZuKlientSende(Text("Fehler beim laden des Spiels: ").operator+(err));
  574. }
  575. }
  576. ss->zDB()->setSpielStatusBeendet(id, 4);
  577. ss->removeSpiel(id); // delete this
  578. return;
  579. }
  580. pfad->release();
  581. DllStart getSpielKlasse = (DllStart)GetProcAddress(spielDll, "getSpielKlasse");
  582. if (!getSpielKlasse)
  583. {
  584. FreeLibrary(spielDll);
  585. spielDll = 0;
  586. Text tmp = ss->zInit()->zWert("TempMapPfad")->getText();
  587. tmp += "/";
  588. tmp += id;
  589. DateiRemove(tmp);
  590. for (int i = 0; i < spielerAnzahl; i++)
  591. {
  592. SSKlient* tmp = klients->z(i);
  593. if (tmp)
  594. {
  595. tmp->setSpiel(0);
  596. tmp->errorZuKlientSende("Fehler beim laden des Spiels. Der Server kann das Spiel nicht finden.");
  597. }
  598. }
  599. ss->zDB()->setSpielStatusBeendet(id, 4);
  600. ss->removeSpiel(id); // delete this
  601. return;
  602. }
  603. spiel = getSpielKlasse();
  604. spiel->setPSQLK(dynamic_cast<SSDatenbankV*>(ss->zDB()->getThis()));
  605. spiel->setSpielId(id);
  606. spiel->setKarteId(karte);
  607. Text tmp = ss->zInit()->zWert("TempMapPfad")->getText();
  608. tmp += "/";
  609. tmp += id;
  610. spiel->setTempPfad(tmp);
  611. spiel->setAccounts(spielerAnzahl, accounts);
  612. spiel->setKlients(spielerAnzahl, (RCArray< SSKlientV > *)klients);
  613. spiel->setSpielerNummern(spielerAnzahl, spielerNummern);
  614. // auf klients warten
  615. while (1)
  616. {
  617. Sleep(100);
  618. bool br = 1;
  619. for (int i = 0; i < spielerAnzahl; i++)
  620. {
  621. if (klients->z(i))
  622. br = br && status->hat(i) && status->get(i) == 3;
  623. }
  624. if (br)
  625. break;
  626. }
  627. // Spiel starten
  628. state = 5;
  629. ss->zDB()->setSpielStatusIsRunning(id);
  630. spiel->run();
  631. // Spiel ist zuende
  632. state = 6;
  633. statistik = spiel->getStatistik();
  634. spiel = (SpielKlasse*)spiel->release();
  635. statistik->run();
  636. // Statistik ist zuende
  637. state = 7;
  638. statistik = (StatistikV*)statistik->release();
  639. FreeLibrary(spielDll);
  640. spielDll = 0;
  641. DateiRemove(tmp);
  642. for (int i = 0; i < spielerAnzahl; i++)
  643. {
  644. SSKlient* tmp = klients->z(i);
  645. if (tmp)
  646. tmp->setSpiel(0);
  647. }
  648. ss->removeSpiel(id); // delete this
  649. }
  650. // constant
  651. int Spiel::getId() const
  652. {
  653. return id;
  654. }
  655. int Spiel::getSpielerNummer(int accountId) const
  656. {
  657. for (int i = 0; i < spielerAnzahl; i++)
  658. {
  659. if (accounts->hat(i) && accounts->get(i) == accountId)
  660. return spielerNummern->hat(i) ? spielerNummern->get(i) : 0;
  661. }
  662. return 0;
  663. }