OlympiadGameTeams.java 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. /*
  2. * Copyright (C) 2004-2013 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. reason = null;
  948. for (int i = _teamTwoSize; --i >= 0;)
  949. {
  950. par = _teamTwo[i];
  951. par.updatePlayer();
  952. reason = checkDefaulted(par.getPlayer());
  953. if (reason != null)
  954. {
  955. par.setDefaulted(true);
  956. if (!_teamTwoDefaulted)
  957. {
  958. _teamTwoDefaulted = true;
  959. for (Participant t : _teamOne)
  960. {
  961. if (t.getPlayer() != null)
  962. {
  963. t.getPlayer().sendPacket(reason);
  964. }
  965. }
  966. }
  967. }
  968. }
  969. return _teamOneDefaulted || _teamTwoDefaulted;
  970. }
  971. catch (Exception e)
  972. {
  973. _log.log(Level.WARNING, "Exception on checkDefaulted(): " + e.getMessage(), e);
  974. return true;
  975. }
  976. }
  977. @Override
  978. public final void resetDamage()
  979. {
  980. _damageT1 = 0;
  981. _damageT2 = 0;
  982. }
  983. protected final boolean teamOneAllDisconnected()
  984. {
  985. for (int i = _teamOneSize; --i >= 0;)
  986. {
  987. if (!_teamOne[i].isDisconnected())
  988. {
  989. return false;
  990. }
  991. }
  992. return true;
  993. }
  994. protected final boolean teamTwoAllDisconnected()
  995. {
  996. for (int i = _teamTwoSize; --i >= 0;)
  997. {
  998. if (!_teamTwo[i].isDisconnected())
  999. {
  1000. return false;
  1001. }
  1002. }
  1003. return true;
  1004. }
  1005. }