OlympiadGameTeams.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.model.olympiad;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import java.util.logging.Level;
  19. import com.l2jserver.Config;
  20. import com.l2jserver.gameserver.idfactory.IdFactory;
  21. import com.l2jserver.gameserver.model.L2World;
  22. import com.l2jserver.gameserver.model.Location;
  23. import com.l2jserver.gameserver.model.actor.L2Character;
  24. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  25. import com.l2jserver.gameserver.model.zone.type.L2OlympiadStadiumZone;
  26. import com.l2jserver.gameserver.network.SystemMessageId;
  27. import com.l2jserver.gameserver.network.serverpackets.ExOlympiadMatchResult;
  28. import com.l2jserver.gameserver.network.serverpackets.ExOlympiadUserInfo;
  29. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  30. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  31. import com.l2jserver.util.Rnd;
  32. /**
  33. * @author Pere, DS
  34. */
  35. public class OlympiadGameTeams extends AbstractOlympiadGame
  36. {
  37. public static final int MAX_TEAM_SIZE = 3;
  38. protected boolean _teamOneDefaulted;
  39. protected boolean _teamTwoDefaulted;
  40. protected int _damageT1 = 0;
  41. protected int _damageT2 = 0;
  42. protected final int _teamOneSize;
  43. protected final int _teamTwoSize;
  44. protected final Participant[] _teamOne;
  45. protected final Participant[] _teamTwo;
  46. protected OlympiadGameTeams(int id, Participant[] teamOne, Participant[] teamTwo)
  47. {
  48. super(id);
  49. _teamOneSize = Math.min(teamOne.length, MAX_TEAM_SIZE);
  50. _teamTwoSize = Math.min(teamTwo.length, MAX_TEAM_SIZE);
  51. _teamOne = new Participant[MAX_TEAM_SIZE];
  52. _teamTwo = new Participant[MAX_TEAM_SIZE];
  53. Participant par;
  54. for (int i = 0; i < MAX_TEAM_SIZE; i++)
  55. {
  56. if (i < _teamOneSize)
  57. {
  58. par = teamOne[i];
  59. _teamOne[i] = par;
  60. if (par.getPlayer() != null)
  61. {
  62. par.getPlayer().setOlympiadGameId(id);
  63. }
  64. }
  65. else
  66. {
  67. _teamOne[i] = new Participant(IdFactory.getInstance().getNextId(), 1);
  68. }
  69. if (i < _teamTwoSize)
  70. {
  71. par = teamTwo[i];
  72. _teamTwo[i] = par;
  73. if (par.getPlayer() != null)
  74. {
  75. par.getPlayer().setOlympiadGameId(id);
  76. }
  77. }
  78. else
  79. {
  80. _teamTwo[i] = new Participant(IdFactory.getInstance().getNextId(), 2);
  81. }
  82. }
  83. }
  84. protected static final Participant[][] createListOfParticipants(List<List<Integer>> list)
  85. {
  86. if ((list == null) || list.isEmpty() || (list.size() < 2))
  87. {
  88. return null;
  89. }
  90. List<Integer> teamOne = null;
  91. List<Integer> teamTwo = null;
  92. L2PcInstance player;
  93. List<L2PcInstance> teamOnePlayers = new ArrayList<>(MAX_TEAM_SIZE);
  94. List<L2PcInstance> teamTwoPlayers = new ArrayList<>(MAX_TEAM_SIZE);
  95. while (list.size() > 1)
  96. {
  97. teamOne = list.remove(Rnd.nextInt(list.size()));
  98. if (((teamOne == null) || teamOne.isEmpty()))
  99. {
  100. continue;
  101. }
  102. for (int objectId : teamOne)
  103. {
  104. player = L2World.getInstance().getPlayer(objectId);
  105. if ((player == null) || !player.isOnline())
  106. {
  107. teamOnePlayers.clear();
  108. break;
  109. }
  110. teamOnePlayers.add(player);
  111. }
  112. if (teamOnePlayers.isEmpty())
  113. {
  114. continue;
  115. }
  116. teamTwo = list.remove(Rnd.nextInt(list.size()));
  117. if ((teamTwo == null) || teamTwo.isEmpty())
  118. {
  119. list.add(teamOne);
  120. teamOnePlayers.clear();
  121. continue;
  122. }
  123. for (int objectId : teamTwo)
  124. {
  125. player = L2World.getInstance().getPlayer(objectId);
  126. if ((player == null) || !player.isOnline())
  127. {
  128. teamTwoPlayers.clear();
  129. break;
  130. }
  131. teamTwoPlayers.add(player);
  132. }
  133. if (teamTwoPlayers.isEmpty())
  134. {
  135. list.add(teamOne);
  136. teamOnePlayers.clear();
  137. continue;
  138. }
  139. Participant[] t1 = new Participant[teamOnePlayers.size()];
  140. Participant[] t2 = new Participant[teamTwoPlayers.size()];
  141. Participant[][] result = new Participant[2][];
  142. for (int i = 0; i < t1.length; i++)
  143. {
  144. t1[i] = new Participant(teamOnePlayers.get(i), 1);
  145. }
  146. for (int i = 0; i < t2.length; i++)
  147. {
  148. t2[i] = new Participant(teamTwoPlayers.get(i), 2);
  149. }
  150. result[0] = t1;
  151. result[1] = t2;
  152. return result;
  153. }
  154. return null;
  155. }
  156. protected static OlympiadGameTeams createGame(int id, List<List<Integer>> list)
  157. {
  158. final Participant[][] teams = createListOfParticipants(list);
  159. if (teams == null)
  160. {
  161. return null;
  162. }
  163. return new OlympiadGameTeams(id, teams[0], teams[1]);
  164. }
  165. @Override
  166. public CompetitionType getType()
  167. {
  168. return CompetitionType.TEAMS;
  169. }
  170. @Override
  171. protected int getDivider()
  172. {
  173. return 5;
  174. }
  175. @Override
  176. protected int[][] getReward()
  177. {
  178. return Config.ALT_OLY_TEAM_REWARD;
  179. }
  180. @Override
  181. protected final String getWeeklyMatchType()
  182. {
  183. return COMP_DONE_WEEK_TEAM;
  184. }
  185. @Override
  186. public final boolean containsParticipant(int playerId)
  187. {
  188. for (int i = _teamOneSize; --i >= 0;)
  189. {
  190. if (_teamOne[i].getObjectId() == playerId)
  191. {
  192. return true;
  193. }
  194. }
  195. for (int i = _teamTwoSize; --i >= 0;)
  196. {
  197. if (_teamTwo[i].getObjectId() == playerId)
  198. {
  199. return true;
  200. }
  201. }
  202. return false;
  203. }
  204. @Override
  205. public final void sendOlympiadInfo(L2Character player)
  206. {
  207. for (int i = 0; i < MAX_TEAM_SIZE; i++)
  208. {
  209. player.sendPacket(new ExOlympiadUserInfo(_teamOne[i]));
  210. }
  211. for (int i = 0; i < MAX_TEAM_SIZE; i++)
  212. {
  213. player.sendPacket(new ExOlympiadUserInfo(_teamTwo[i]));
  214. }
  215. }
  216. @Override
  217. public final void broadcastOlympiadInfo(L2OlympiadStadiumZone stadium)
  218. {
  219. for (int i = 0; i < MAX_TEAM_SIZE; i++)
  220. {
  221. stadium.broadcastPacket(new ExOlympiadUserInfo(_teamOne[i]));
  222. }
  223. for (int i = 0; i < MAX_TEAM_SIZE; i++)
  224. {
  225. stadium.broadcastPacket(new ExOlympiadUserInfo(_teamTwo[i]));
  226. }
  227. }
  228. @Override
  229. protected final void broadcastPacket(L2GameServerPacket packet)
  230. {
  231. Participant par;
  232. for (int i = 0; i < _teamOneSize; i++)
  233. {
  234. par = _teamOne[i];
  235. if (par.updatePlayer())
  236. {
  237. par.getPlayer().sendPacket(packet);
  238. }
  239. }
  240. for (int i = 0; i < _teamTwoSize; i++)
  241. {
  242. par = _teamTwo[i];
  243. par.updatePlayer();
  244. if (par.getPlayer() != null)
  245. {
  246. par.getPlayer().sendPacket(packet);
  247. }
  248. }
  249. }
  250. /**
  251. * UnAfraid: FIXME: Sometimes buffers appear on arena 3v3 match where it shouldn't or they don't get unspawned when match start.
  252. */
  253. @Override
  254. protected boolean needBuffers()
  255. {
  256. return false;
  257. }
  258. @Override
  259. protected final boolean portPlayersToArena(List<Location> spawns)
  260. {
  261. boolean result = true;
  262. try
  263. {
  264. for (int i = 0; i < _teamOneSize; i++)
  265. {
  266. result &= portPlayerToArena(_teamOne[i], spawns.get(i), _stadiumID);
  267. }
  268. final int offset = spawns.size() / 2;
  269. for (int i = 0; i < _teamTwoSize; i++)
  270. {
  271. result &= portPlayerToArena(_teamTwo[i], spawns.get(i + offset), _stadiumID);
  272. }
  273. }
  274. catch (Exception e)
  275. {
  276. _log.log(Level.WARNING, "", e);
  277. return false;
  278. }
  279. return result;
  280. }
  281. @Override
  282. protected final void removals()
  283. {
  284. for (int i = _teamOneSize; --i >= 0;)
  285. {
  286. removals(_teamOne[i].getPlayer(), false);
  287. }
  288. for (int i = _teamTwoSize; --i >= 0;)
  289. {
  290. removals(_teamTwo[i].getPlayer(), false);
  291. }
  292. }
  293. @Override
  294. protected final boolean makeCompetitionStart()
  295. {
  296. if (!super.makeCompetitionStart())
  297. {
  298. return false;
  299. }
  300. Participant par;
  301. for (int i = 0; i < _teamOneSize; i++)
  302. {
  303. par = _teamOne[i];
  304. if (par.getPlayer() == null)
  305. {
  306. return false;
  307. }
  308. par.getPlayer().setIsOlympiadStart(true);
  309. par.getPlayer().updateEffectIcons();
  310. }
  311. for (int i = 0; i < _teamTwoSize; i++)
  312. {
  313. par = _teamTwo[i];
  314. if (par.getPlayer() == null)
  315. {
  316. return false;
  317. }
  318. par.getPlayer().setIsOlympiadStart(true);
  319. par.getPlayer().updateEffectIcons();
  320. }
  321. return true;
  322. }
  323. @Override
  324. protected final void cleanEffects()
  325. {
  326. Participant par;
  327. for (int i = _teamOneSize; --i >= 0;)
  328. {
  329. par = _teamOne[i];
  330. if ((par.getPlayer() != null) && !par.isDefaulted() && !par.isDisconnected() && (par.getPlayer().getOlympiadGameId() == _stadiumID))
  331. {
  332. cleanEffects(par.getPlayer());
  333. }
  334. }
  335. for (int i = _teamTwoSize; --i >= 0;)
  336. {
  337. par = _teamTwo[i];
  338. if ((par.getPlayer() != null) && !par.isDefaulted() && !par.isDisconnected() && (par.getPlayer().getOlympiadGameId() == _stadiumID))
  339. {
  340. cleanEffects(par.getPlayer());
  341. }
  342. }
  343. }
  344. @Override
  345. protected final void portPlayersBack()
  346. {
  347. Participant par;
  348. for (int i = _teamOneSize; --i >= 0;)
  349. {
  350. par = _teamOne[i];
  351. if ((par.getPlayer() != null) && !par.isDefaulted() && !par.isDisconnected())
  352. {
  353. portPlayerBack(par.getPlayer());
  354. }
  355. }
  356. for (int i = _teamTwoSize; --i >= 0;)
  357. {
  358. par = _teamTwo[i];
  359. if ((par.getPlayer() != null) && !par.isDefaulted() && !par.isDisconnected())
  360. {
  361. portPlayerBack(par.getPlayer());
  362. }
  363. }
  364. }
  365. @Override
  366. protected final void playersStatusBack()
  367. {
  368. Participant par;
  369. for (int i = _teamOneSize; --i >= 0;)
  370. {
  371. par = _teamOne[i];
  372. if ((par.getPlayer() != null) && !par.isDefaulted() && !par.isDisconnected() && (par.getPlayer().getOlympiadGameId() == _stadiumID))
  373. {
  374. playerStatusBack(par.getPlayer());
  375. }
  376. }
  377. for (int i = _teamTwoSize; --i >= 0;)
  378. {
  379. par = _teamTwo[i];
  380. if ((par.getPlayer() != null) && !par.isDefaulted() && !par.isDisconnected() && (par.getPlayer().getOlympiadGameId() == _stadiumID))
  381. {
  382. playerStatusBack(par.getPlayer());
  383. }
  384. }
  385. }
  386. @Override
  387. protected final void clearPlayers()
  388. {
  389. for (int i = 0; i < MAX_TEAM_SIZE; i++)
  390. {
  391. if (i < _teamOneSize)
  392. {
  393. _teamOne[i].setPlayer(null);
  394. }
  395. else
  396. {
  397. IdFactory.getInstance().releaseId(_teamOne[i].getObjectId());
  398. }
  399. if (i < _teamTwoSize)
  400. {
  401. _teamTwo[i].setPlayer(null);
  402. }
  403. else
  404. {
  405. IdFactory.getInstance().releaseId(_teamTwo[i].getObjectId());
  406. }
  407. _teamOne[i] = null;
  408. _teamTwo[i] = null;
  409. }
  410. }
  411. @Override
  412. protected final void handleDisconnect(L2PcInstance player)
  413. {
  414. Participant par;
  415. for (int i = _teamOneSize; --i >= 0;)
  416. {
  417. par = _teamOne[i];
  418. if (par.getObjectId() == player.getObjectId())
  419. {
  420. par.setDisconnected(true);
  421. return;
  422. }
  423. }
  424. for (int i = _teamTwoSize; --i >= 0;)
  425. {
  426. par = _teamTwo[i];
  427. if (par.getObjectId() == player.getObjectId())
  428. {
  429. par.setDisconnected(true);
  430. return;
  431. }
  432. }
  433. }
  434. @Override
  435. protected final boolean haveWinner()
  436. {
  437. if (!checkBattleStatus())
  438. {
  439. return true;
  440. }
  441. boolean teamOneLost = true;
  442. boolean teamTwoLost = true;
  443. Participant par;
  444. for (int i = _teamOneSize; --i >= 0;)
  445. {
  446. par = _teamOne[i];
  447. if (!par.isDisconnected())
  448. {
  449. if ((par.getPlayer() != null) && (par.getPlayer().getOlympiadGameId() == _stadiumID))
  450. {
  451. teamOneLost &= par.getPlayer().isDead();
  452. }
  453. }
  454. }
  455. for (int i = _teamTwoSize; --i >= 0;)
  456. {
  457. par = _teamTwo[i];
  458. if (!par.isDisconnected())
  459. {
  460. if ((par.getPlayer() != null) && (par.getPlayer().getOlympiadGameId() == _stadiumID))
  461. {
  462. teamTwoLost &= par.getPlayer().isDead();
  463. }
  464. }
  465. }
  466. return teamOneLost || teamTwoLost;
  467. }
  468. @Override
  469. protected final boolean checkBattleStatus()
  470. {
  471. if (_aborted)
  472. {
  473. return false;
  474. }
  475. if (teamOneAllDisconnected())
  476. {
  477. return false;
  478. }
  479. if (teamTwoAllDisconnected())
  480. {
  481. return false;
  482. }
  483. return true;
  484. }
  485. @Override
  486. protected void validateWinner(L2OlympiadStadiumZone stadium)
  487. {
  488. if (_aborted)
  489. {
  490. return;
  491. }
  492. ExOlympiadMatchResult result = null;
  493. boolean tie = false;
  494. int winside = 0;
  495. List<OlympiadInfo> list1 = new ArrayList<>(3);
  496. List<OlympiadInfo> list2 = new ArrayList<>(3);
  497. final boolean tOneCrash = teamOneAllDisconnected();
  498. final boolean tTwoCrash = teamTwoAllDisconnected();
  499. Participant par;
  500. SystemMessage sm;
  501. int points;
  502. // Check for if a team defaulted before battle started
  503. if (_teamOneDefaulted || _teamTwoDefaulted)
  504. {
  505. try
  506. {
  507. if (_teamOneDefaulted)
  508. {
  509. for (int i = _teamOneSize; --i >= 0;)
  510. {
  511. par = _teamOne[i];
  512. points = par.getStats().getInteger(POINTS) / getDivider();
  513. int val = Math.min(par.getStats().getInteger(POINTS) / 3, Config.ALT_OLY_MAX_POINTS);
  514. removePointsFromParticipant(par, val);
  515. list1.add(new OlympiadInfo(par.getName(), par.getClanName(), par.getClanId(), par.getBaseClass(), _damageT1, points - val, -val));
  516. }
  517. winside = 2;
  518. }
  519. if (_teamTwoDefaulted)
  520. {
  521. for (int i = _teamTwoSize; --i >= 0;)
  522. {
  523. par = _teamTwo[i];
  524. points = par.getStats().getInteger(POINTS) / getDivider();
  525. int val = Math.min(par.getStats().getInteger(POINTS) / 3, Config.ALT_OLY_MAX_POINTS);
  526. removePointsFromParticipant(par, val);
  527. list2.add(new OlympiadInfo(par.getName(), par.getClanName(), par.getClanId(), par.getBaseClass(), _damageT2, points - val, -val));
  528. }
  529. if (winside == 2)
  530. {
  531. tie = true;
  532. }
  533. else
  534. {
  535. winside = 1;
  536. }
  537. }
  538. if (winside == 1)
  539. {
  540. result = new ExOlympiadMatchResult(tie, winside, list1, list2);
  541. }
  542. else
  543. {
  544. result = new ExOlympiadMatchResult(tie, winside, list2, list1);
  545. }
  546. stadium.broadcastPacket(result);
  547. }
  548. catch (Exception e)
  549. {
  550. _log.log(Level.WARNING, "Exception on validateWinner(): " + e.getMessage(), e);
  551. }
  552. return;
  553. }
  554. // points to be dedicted in case of losing
  555. final int[] pointsTeamOne = new int[_teamOneSize];
  556. final int[] pointsTeamTwo = new int[_teamTwoSize];
  557. final int[] maxPointsTeamOne = new int[_teamOneSize];
  558. final int[] maxPointsTeamTwo = new int[_teamTwoSize];
  559. int totalPointsTeamOne = 0;
  560. int totalPointsTeamTwo = 0;
  561. for (int i = 0; i < _teamOneSize; i++)
  562. {
  563. points = _teamOne[i].getStats().getInteger(POINTS) / getDivider();
  564. if (points <= 0)
  565. {
  566. points = 1;
  567. }
  568. else if (points > Config.ALT_OLY_MAX_POINTS)
  569. {
  570. points = Config.ALT_OLY_MAX_POINTS;
  571. }
  572. totalPointsTeamOne += points;
  573. pointsTeamOne[i] = points;
  574. maxPointsTeamOne[i] = points;
  575. }
  576. for (int i = _teamTwoSize; --i >= 0;)
  577. {
  578. points = _teamTwo[i].getStats().getInteger(POINTS) / getDivider();
  579. if (points <= 0)
  580. {
  581. points = 1;
  582. }
  583. else if (points > Config.ALT_OLY_MAX_POINTS)
  584. {
  585. points = Config.ALT_OLY_MAX_POINTS;
  586. }
  587. totalPointsTeamTwo += points;
  588. pointsTeamTwo[i] = points;
  589. maxPointsTeamTwo[i] = points;
  590. }
  591. // Choose minimum sum
  592. int min = Math.min(totalPointsTeamOne, totalPointsTeamTwo);
  593. // make sure all team members got same number of the points: round down to 3x
  594. min = (min / MAX_TEAM_SIZE) * MAX_TEAM_SIZE;
  595. // calculating coefficients and trying to correct total number of points for each team
  596. // due to rounding errors total points after correction will always be lower or equal
  597. // than needed minimal sum
  598. final double dividerOne = (double) totalPointsTeamOne / min;
  599. final double dividerTwo = (double) totalPointsTeamTwo / min;
  600. totalPointsTeamOne = min;
  601. totalPointsTeamTwo = min;
  602. for (int i = 0; i < _teamOneSize; i++)
  603. {
  604. points = Math.max((int) (pointsTeamOne[i] / dividerOne), 1);
  605. pointsTeamOne[i] = points;
  606. totalPointsTeamOne -= points;
  607. }
  608. for (int i = _teamTwoSize; --i >= 0;)
  609. {
  610. points = Math.max((int) (pointsTeamTwo[i] / dividerTwo), 1);
  611. pointsTeamTwo[i] = points;
  612. totalPointsTeamTwo -= points;
  613. }
  614. // compensating remaining points, first team from begin to end, second from end to begin
  615. for (int i = 0; (totalPointsTeamOne > 0) && (i < _teamOneSize); i++)
  616. {
  617. if (pointsTeamOne[i] < maxPointsTeamOne[i])
  618. {
  619. pointsTeamOne[i]++;
  620. totalPointsTeamOne--;
  621. }
  622. }
  623. for (int i = _teamTwoSize; (totalPointsTeamTwo > 0) && (--i >= 0);)
  624. {
  625. if (pointsTeamTwo[i] < maxPointsTeamTwo[i])
  626. {
  627. pointsTeamTwo[i]++;
  628. totalPointsTeamTwo--;
  629. }
  630. }
  631. // Create results for players if a team crashed
  632. if (tOneCrash || tTwoCrash)
  633. {
  634. try
  635. {
  636. if (tTwoCrash && !tOneCrash)
  637. {
  638. sm = SystemMessage.getSystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  639. sm.addString(_teamOne[0].getName());
  640. stadium.broadcastPacket(sm);
  641. for (int i = 0; i < _teamTwoSize; i++)
  642. {
  643. par = _teamTwo[i];
  644. par.updateStat(COMP_LOST, 1);
  645. points = pointsTeamTwo[i];
  646. removePointsFromParticipant(par, points);
  647. list2.add(new OlympiadInfo(par.getName(), par.getClanName(), par.getClanId(), par.getBaseClass(), _damageT2, par.getStats().getInteger(POINTS) - points, -points));
  648. }
  649. points = min / MAX_TEAM_SIZE;
  650. for (int i = 0; i < _teamOneSize; i++)
  651. {
  652. par = _teamOne[i];
  653. par.updateStat(COMP_WON, 1);
  654. addPointsToParticipant(par, points);
  655. list1.add(new OlympiadInfo(par.getName(), par.getClanName(), par.getClanId(), par.getBaseClass(), _damageT1, par.getStats().getInteger(POINTS) + points, points));
  656. }
  657. for (int i = 0; i < _teamOneSize; i++)
  658. {
  659. rewardParticipant(_teamOne[i].getPlayer(), getReward());
  660. }
  661. winside = 1;
  662. }
  663. else if (tOneCrash && !tTwoCrash)
  664. {
  665. sm = SystemMessage.getSystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  666. sm.addString(_teamTwo[0].getName());
  667. stadium.broadcastPacket(sm);
  668. for (int i = 0; i < _teamOneSize; i++)
  669. {
  670. par = _teamOne[i];
  671. par.updateStat(COMP_LOST, 1);
  672. points = pointsTeamOne[i];
  673. removePointsFromParticipant(par, points);
  674. list1.add(new OlympiadInfo(par.getName(), par.getClanName(), par.getClanId(), par.getBaseClass(), _damageT1, par.getStats().getInteger(POINTS) - points, -points));
  675. }
  676. points = min / MAX_TEAM_SIZE;
  677. for (int i = 0; i < _teamTwoSize; i++)
  678. {
  679. par = _teamTwo[i];
  680. par.updateStat(COMP_WON, 1);
  681. addPointsToParticipant(par, points);
  682. list2.add(new OlympiadInfo(par.getName(), par.getClanName(), par.getClanId(), par.getBaseClass(), _damageT2, par.getStats().getInteger(POINTS) + points, points));
  683. }
  684. winside = 2;
  685. for (int i = 0; i < _teamTwoSize; i++)
  686. {
  687. rewardParticipant(_teamTwo[i].getPlayer(), getReward());
  688. }
  689. }
  690. else if (tOneCrash && tTwoCrash)
  691. {
  692. stadium.broadcastPacket(SystemMessage.getSystemMessage(SystemMessageId.THE_GAME_ENDED_IN_A_TIE));
  693. for (int i = _teamOneSize; --i >= 0;)
  694. {
  695. par = _teamOne[i];
  696. par.updateStat(COMP_LOST, 1);
  697. removePointsFromParticipant(par, pointsTeamOne[i]);
  698. list1.add(new OlympiadInfo(par.getName(), par.getClanName(), par.getClanId(), par.getBaseClass(), _damageT1, par.getStats().getInteger(POINTS) - pointsTeamOne[i], -pointsTeamOne[i]));
  699. }
  700. for (int i = _teamTwoSize; --i >= 0;)
  701. {
  702. par = _teamTwo[i];
  703. par.updateStat(COMP_LOST, 1);
  704. removePointsFromParticipant(par, pointsTeamTwo[i]);
  705. list2.add(new OlympiadInfo(par.getName(), par.getClanName(), par.getClanId(), par.getBaseClass(), _damageT2, par.getStats().getInteger(POINTS) - pointsTeamOne[i], -pointsTeamOne[i]));
  706. }
  707. tie = true;
  708. }
  709. for (int i = _teamOneSize; --i >= 0;)
  710. {
  711. par = _teamOne[i];
  712. par.updateStat(COMP_DONE, 1);
  713. par.updateStat(COMP_DONE_WEEK, 1);
  714. par.updateStat(getWeeklyMatchType(), 1);
  715. }
  716. for (int i = _teamTwoSize; --i >= 0;)
  717. {
  718. par = _teamTwo[i];
  719. par.updateStat(COMP_DONE, 1);
  720. par.updateStat(COMP_DONE_WEEK, 1);
  721. par.updateStat(getWeeklyMatchType(), 1);
  722. }
  723. }
  724. catch (Exception e)
  725. {
  726. _log.log(Level.WARNING, "Exception on validateWinner(): " + e.getMessage(), e);
  727. }
  728. if (winside == 1)
  729. {
  730. result = new ExOlympiadMatchResult(tie, winside, list1, list2);
  731. }
  732. else
  733. {
  734. result = new ExOlympiadMatchResult(tie, winside, list2, list1);
  735. }
  736. stadium.broadcastPacket(result);
  737. return;
  738. }
  739. try
  740. {
  741. double hp;
  742. double teamOneHp = 0;
  743. double teamTwoHp = 0;
  744. for (int i = _teamOneSize; --i >= 0;)
  745. {
  746. par = _teamOne[i];
  747. if (!par.isDisconnected() && (par.getPlayer() != null) && !par.getPlayer().isDead())
  748. {
  749. hp = par.getPlayer().getCurrentHp() + par.getPlayer().getCurrentCp();
  750. if (hp >= 0.5)
  751. {
  752. teamOneHp += hp;
  753. }
  754. }
  755. par.updatePlayer();
  756. }
  757. for (int i = _teamTwoSize; --i >= 0;)
  758. {
  759. par = _teamTwo[i];
  760. if (!par.isDisconnected() && (par.getPlayer() != null) && !par.getPlayer().isDead())
  761. {
  762. hp = par.getPlayer().getCurrentHp() + par.getPlayer().getCurrentCp();
  763. if (hp >= 0.5)
  764. {
  765. teamTwoHp += hp;
  766. }
  767. }
  768. par.updatePlayer();
  769. }
  770. if (((teamTwoHp == 0) && (teamOneHp != 0)) || ((_damageT1 > _damageT2) && (teamTwoHp != 0) && (teamOneHp != 0)))
  771. {
  772. sm = SystemMessage.getSystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  773. sm.addString(_teamOne[0].getName());
  774. stadium.broadcastPacket(sm);
  775. for (int i = 0; i < _teamTwoSize; i++)
  776. {
  777. par = _teamTwo[i];
  778. par.updateStat(COMP_LOST, 1);
  779. points = pointsTeamTwo[i];
  780. removePointsFromParticipant(par, points);
  781. list2.add(new OlympiadInfo(par.getName(), par.getClanName(), par.getClanId(), par.getBaseClass(), _damageT2, par.getStats().getInteger(POINTS) - points, -points));
  782. }
  783. points = min / MAX_TEAM_SIZE;
  784. for (int i = 0; i < _teamOneSize; i++)
  785. {
  786. par = _teamOne[i];
  787. par.updateStat(COMP_WON, 1);
  788. addPointsToParticipant(par, points);
  789. list1.add(new OlympiadInfo(par.getName(), par.getClanName(), par.getClanId(), par.getBaseClass(), _damageT1, par.getStats().getInteger(POINTS) + points, points));
  790. }
  791. winside = 1;
  792. for (int i = 0; i < _teamOneSize; i++)
  793. {
  794. rewardParticipant(_teamOne[i].getPlayer(), getReward());
  795. }
  796. }
  797. else if (((teamOneHp == 0) && (teamTwoHp != 0)) || ((_damageT2 > _damageT1) && (teamOneHp != 0) && (teamTwoHp != 0)))
  798. {
  799. sm = SystemMessage.getSystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  800. sm.addString(_teamTwo[0].getName());
  801. stadium.broadcastPacket(sm);
  802. for (int i = 0; i < _teamOneSize; i++)
  803. {
  804. par = _teamOne[i];
  805. par.updateStat(COMP_LOST, 1);
  806. points = pointsTeamOne[i];
  807. removePointsFromParticipant(par, points);
  808. list1.add(new OlympiadInfo(par.getName(), par.getClanName(), par.getClanId(), par.getBaseClass(), _damageT1, par.getStats().getInteger(POINTS) - points, -points));
  809. }
  810. points = min / MAX_TEAM_SIZE;
  811. for (int i = 0; i < _teamTwoSize; i++)
  812. {
  813. par = _teamTwo[i];
  814. par.updateStat(COMP_WON, 1);
  815. addPointsToParticipant(par, points);
  816. list2.add(new OlympiadInfo(par.getName(), par.getClanName(), par.getClanId(), par.getBaseClass(), _damageT2, par.getStats().getInteger(POINTS) + points, points));
  817. }
  818. winside = 2;
  819. for (int i = 0; i < _teamTwoSize; i++)
  820. {
  821. rewardParticipant(_teamTwo[i].getPlayer(), getReward());
  822. }
  823. }
  824. else
  825. {
  826. stadium.broadcastPacket(SystemMessage.getSystemMessage(SystemMessageId.THE_GAME_ENDED_IN_A_TIE));
  827. for (int i = 0; i < _teamOneSize; i++)
  828. {
  829. par = _teamOne[i];
  830. par.updateStat(COMP_DRAWN, 1);
  831. points = Math.min(par.getStats().getInteger(POINTS) / getDivider(), Config.ALT_OLY_MAX_POINTS);
  832. removePointsFromParticipant(par, points);
  833. list1.add(new OlympiadInfo(par.getName(), par.getClanName(), par.getClanId(), par.getBaseClass(), _damageT1, par.getStats().getInteger(POINTS) - points, -points));
  834. }
  835. for (int i = 0; i < _teamTwoSize; i++)
  836. {
  837. par = _teamTwo[i];
  838. par.updateStat(COMP_DRAWN, 1);
  839. points = Math.min(par.getStats().getInteger(POINTS) / getDivider(), Config.ALT_OLY_MAX_POINTS);
  840. removePointsFromParticipant(par, points);
  841. list2.add(new OlympiadInfo(par.getName(), par.getClanName(), par.getClanId(), par.getBaseClass(), _damageT2, par.getStats().getInteger(POINTS) - points, -points));
  842. }
  843. tie = true;
  844. }
  845. for (int i = _teamOneSize; --i >= 0;)
  846. {
  847. par = _teamOne[i];
  848. par.updateStat(COMP_DONE, 1);
  849. par.updateStat(COMP_DONE_WEEK, 1);
  850. par.updateStat(getWeeklyMatchType(), 1);
  851. }
  852. for (int i = _teamTwoSize; --i >= 0;)
  853. {
  854. par = _teamTwo[i];
  855. par.updateStat(COMP_DONE, 1);
  856. par.updateStat(COMP_DONE_WEEK, 1);
  857. par.updateStat(getWeeklyMatchType(), 1);
  858. }
  859. if (winside == 1)
  860. {
  861. result = new ExOlympiadMatchResult(tie, winside, list1, list2);
  862. }
  863. else
  864. {
  865. result = new ExOlympiadMatchResult(tie, winside, list2, list1);
  866. }
  867. stadium.broadcastPacket(result);
  868. }
  869. catch (Exception e)
  870. {
  871. _log.log(Level.WARNING, "Exception on validateWinner(): " + e.getMessage(), e);
  872. }
  873. }
  874. /**
  875. * UnAfraid: TODO: We should calculate the damage in array separately for each player so we can display it on ExOlympiadMatchResult correctly.
  876. */
  877. @Override
  878. protected final void addDamage(L2PcInstance player, int damage)
  879. {
  880. Participant par;
  881. for (int i = _teamOneSize; --i >= 0;)
  882. {
  883. par = _teamOne[i];
  884. if (par.getObjectId() == player.getObjectId())
  885. {
  886. if (!par.isDisconnected())
  887. {
  888. _damageT1 += damage;
  889. }
  890. return;
  891. }
  892. }
  893. for (int i = _teamTwoSize; --i >= 0;)
  894. {
  895. par = _teamTwo[i];
  896. if (par.getObjectId() == player.getObjectId())
  897. {
  898. if (!par.isDisconnected())
  899. {
  900. _damageT2 += damage;
  901. }
  902. return;
  903. }
  904. }
  905. }
  906. @Override
  907. public final String[] getPlayerNames()
  908. {
  909. return new String[]
  910. {
  911. _teamOne[0].getName(),
  912. _teamTwo[0].getName()
  913. };
  914. }
  915. @Override
  916. public final boolean checkDefaulted()
  917. {
  918. try
  919. {
  920. SystemMessage reason = null;
  921. Participant par;
  922. for (int i = _teamOneSize; --i >= 0;)
  923. {
  924. par = _teamOne[i];
  925. par.updatePlayer();
  926. reason = checkDefaulted(par.getPlayer());
  927. if (reason != null)
  928. {
  929. par.setDefaulted(true);
  930. if (!_teamOneDefaulted)
  931. {
  932. _teamOneDefaulted = true;
  933. for (Participant t : _teamTwo)
  934. {
  935. if (t.getPlayer() != null)
  936. {
  937. t.getPlayer().sendPacket(reason);
  938. }
  939. }
  940. }
  941. }
  942. }
  943. reason = null;
  944. for (int i = _teamTwoSize; --i >= 0;)
  945. {
  946. par = _teamTwo[i];
  947. par.updatePlayer();
  948. reason = checkDefaulted(par.getPlayer());
  949. if (reason != null)
  950. {
  951. par.setDefaulted(true);
  952. if (!_teamTwoDefaulted)
  953. {
  954. _teamTwoDefaulted = true;
  955. for (Participant t : _teamOne)
  956. {
  957. if (t.getPlayer() != null)
  958. {
  959. t.getPlayer().sendPacket(reason);
  960. }
  961. }
  962. }
  963. }
  964. }
  965. return _teamOneDefaulted || _teamTwoDefaulted;
  966. }
  967. catch (Exception e)
  968. {
  969. _log.log(Level.WARNING, "Exception on checkDefaulted(): " + e.getMessage(), e);
  970. return true;
  971. }
  972. }
  973. @Override
  974. public final void resetDamage()
  975. {
  976. _damageT1 = 0;
  977. _damageT2 = 0;
  978. }
  979. protected final boolean teamOneAllDisconnected()
  980. {
  981. for (int i = _teamOneSize; --i >= 0;)
  982. {
  983. if (!_teamOne[i].isDisconnected())
  984. {
  985. return false;
  986. }
  987. }
  988. return true;
  989. }
  990. protected final boolean teamTwoAllDisconnected()
  991. {
  992. for (int i = _teamTwoSize; --i >= 0;)
  993. {
  994. if (!_teamTwo[i].isDisconnected())
  995. {
  996. return false;
  997. }
  998. }
  999. return true;
  1000. }
  1001. }