OlympiadGameTeams.java 28 KB

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