OlympiadGameTeams.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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.ExOlympiadUserInfo;
  28. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  29. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  30. import com.l2jserver.util.Rnd;
  31. /**
  32. * @author Pere, DS
  33. */
  34. public class OlympiadGameTeams extends AbstractOlympiadGame
  35. {
  36. public static final int MAX_TEAM_SIZE = 3;
  37. protected boolean _teamOneDefaulted;
  38. protected boolean _teamTwoDefaulted;
  39. protected int _damageT1 = 0;
  40. protected int _damageT2 = 0;
  41. protected final int _teamOneSize;
  42. protected final int _teamTwoSize;
  43. protected final Participant[] _teamOne;
  44. protected final Participant[] _teamTwo;
  45. protected OlympiadGameTeams(int id, Participant[] teamOne, Participant[] teamTwo)
  46. {
  47. super(id);
  48. _teamOneSize = Math.min(teamOne.length, MAX_TEAM_SIZE);
  49. _teamTwoSize = Math.min(teamTwo.length, MAX_TEAM_SIZE);
  50. _teamOne = new Participant[MAX_TEAM_SIZE];
  51. _teamTwo = new Participant[MAX_TEAM_SIZE];
  52. Participant par;
  53. for (int i = 0; i < MAX_TEAM_SIZE; i++)
  54. {
  55. if (i < _teamOneSize)
  56. {
  57. par = teamOne[i];
  58. _teamOne[i] = par;
  59. if (par.getPlayer() != null)
  60. par.getPlayer().setOlympiadGameId(id);
  61. }
  62. else
  63. _teamOne[i] = new Participant(IdFactory.getInstance().getNextId(), 1);
  64. if (i < _teamTwoSize)
  65. {
  66. par = teamTwo[i];
  67. _teamTwo[i] = par;
  68. if (par.getPlayer() != null)
  69. par.getPlayer().setOlympiadGameId(id);
  70. }
  71. else
  72. _teamTwo[i] = new Participant(IdFactory.getInstance().getNextId(), 2);
  73. }
  74. }
  75. protected static final Participant[][] createListOfParticipants(List<List<Integer>> list)
  76. {
  77. if (list == null || list.isEmpty() || list.size() < 2)
  78. return null;
  79. List<Integer> teamOne = null;
  80. List<Integer> teamTwo = null;
  81. L2PcInstance player;
  82. List<L2PcInstance> teamOnePlayers = new ArrayList<L2PcInstance>(MAX_TEAM_SIZE);
  83. List<L2PcInstance> teamTwoPlayers = new ArrayList<L2PcInstance>(MAX_TEAM_SIZE);
  84. while (list.size() > 1)
  85. {
  86. teamOne = list.remove(Rnd.nextInt(list.size()));
  87. if ((teamOne == null || teamOne.isEmpty()))
  88. continue;
  89. for (int objectId : teamOne)
  90. {
  91. player = L2World.getInstance().getPlayer(objectId);
  92. if (player == null || !player.isOnline())
  93. {
  94. teamOnePlayers.clear();
  95. break;
  96. }
  97. teamOnePlayers.add(player);
  98. }
  99. if (teamOnePlayers.isEmpty())
  100. continue;
  101. teamTwo = list.remove(Rnd.nextInt(list.size()));
  102. if (teamTwo == null || teamTwo.isEmpty())
  103. {
  104. list.add(teamOne);
  105. teamOnePlayers.clear();
  106. continue;
  107. }
  108. for (int objectId : teamTwo)
  109. {
  110. player = L2World.getInstance().getPlayer(objectId);
  111. if (player == null || !player.isOnline())
  112. {
  113. teamTwoPlayers.clear();
  114. break;
  115. }
  116. teamTwoPlayers.add(player);
  117. }
  118. if (teamTwoPlayers.isEmpty())
  119. {
  120. list.add(teamOne);
  121. teamOnePlayers.clear();
  122. continue;
  123. }
  124. Participant[] t1 = new Participant[teamOnePlayers.size()];
  125. Participant[] t2 = new Participant[teamTwoPlayers.size()];
  126. Participant[][] result = new Participant[2][];
  127. for (int i = 0; i < t1.length; i++)
  128. t1[i] = new Participant(teamOnePlayers.get(i), 1);
  129. for (int i = 0; i < t2.length; i++)
  130. t2[i] = new Participant(teamTwoPlayers.get(i), 2);
  131. result[0] = t1;
  132. result[1] = t2;
  133. return result;
  134. }
  135. return null;
  136. }
  137. protected static OlympiadGameTeams createGame(int id, List<List<Integer>> list)
  138. {
  139. final Participant[][] teams = createListOfParticipants(list);
  140. if (teams == null)
  141. return null;
  142. return new OlympiadGameTeams(id, teams[0], teams[1]);
  143. }
  144. @Override
  145. public CompetitionType getType()
  146. {
  147. return CompetitionType.TEAMS;
  148. }
  149. @Override
  150. protected int getDivider()
  151. {
  152. return 5;
  153. }
  154. @Override
  155. protected int[][] getReward()
  156. {
  157. return Config.ALT_OLY_TEAM_REWARD;
  158. }
  159. @Override
  160. protected final String getWeeklyMatchType()
  161. {
  162. return COMP_DONE_WEEK_TEAM;
  163. }
  164. @Override
  165. public final boolean containsParticipant(int playerId)
  166. {
  167. for (int i = _teamOneSize; --i >= 0;)
  168. {
  169. if (_teamOne[i].getObjectId() == playerId)
  170. return true;
  171. }
  172. for (int i = _teamTwoSize; --i >= 0;)
  173. {
  174. if (_teamTwo[i].getObjectId() == playerId)
  175. return true;
  176. }
  177. return false;
  178. }
  179. @Override
  180. public final void sendOlympiadInfo(L2Character player)
  181. {
  182. for (int i = 0; i < MAX_TEAM_SIZE; i++)
  183. player.sendPacket(new ExOlympiadUserInfo(_teamOne[i]));
  184. for (int i = 0; i < MAX_TEAM_SIZE; i++)
  185. player.sendPacket(new ExOlympiadUserInfo(_teamTwo[i]));
  186. }
  187. @Override
  188. public final void broadcastOlympiadInfo(L2OlympiadStadiumZone stadium)
  189. {
  190. for (int i = 0; i < MAX_TEAM_SIZE; i++)
  191. stadium.broadcastPacket(new ExOlympiadUserInfo(_teamOne[i]));
  192. for (int i = 0; i < MAX_TEAM_SIZE; i++)
  193. stadium.broadcastPacket(new ExOlympiadUserInfo(_teamTwo[i]));
  194. }
  195. @Override
  196. protected final void broadcastPacket(L2GameServerPacket packet)
  197. {
  198. Participant par;
  199. for (int i = 0; i < _teamOneSize; i++)
  200. {
  201. par = _teamOne[i];
  202. if (par.updatePlayer())
  203. {
  204. par.getPlayer().sendPacket(packet);
  205. }
  206. }
  207. for (int i = 0; i < _teamTwoSize; i++)
  208. {
  209. par = _teamTwo[i];
  210. par.
  211. updatePlayer();
  212. if (par.getPlayer() != null)
  213. par.getPlayer().sendPacket(packet);
  214. }
  215. }
  216. @Override
  217. protected boolean needBuffers()
  218. {
  219. return false;
  220. }
  221. @Override
  222. protected final boolean portPlayersToArena(List<Location> spawns)
  223. {
  224. boolean result = true;
  225. try
  226. {
  227. for (int i = 0; i < _teamOneSize; i++)
  228. result &= portPlayerToArena(_teamOne[i], spawns.get(i), _stadiumID);
  229. final int offset = spawns.size() / 2;
  230. for (int i = 0; i < _teamTwoSize; i++)
  231. result &= portPlayerToArena(_teamTwo[i], spawns.get(i + offset), _stadiumID);
  232. }
  233. catch (Exception e)
  234. {
  235. _log.log(Level.WARNING, "", e);
  236. return false;
  237. }
  238. return result;
  239. }
  240. @Override
  241. protected final void removals()
  242. {
  243. for (int i = _teamOneSize; --i >= 0;)
  244. removals(_teamOne[i].getPlayer(), false);
  245. for (int i = _teamTwoSize; --i >= 0;)
  246. removals(_teamTwo[i].getPlayer(), false);
  247. }
  248. @Override
  249. protected final boolean makeCompetitionStart()
  250. {
  251. if (!super.makeCompetitionStart())
  252. return false;
  253. Participant par;
  254. for (int i = 0; i < _teamOneSize; i++)
  255. {
  256. par = _teamOne[i];
  257. if (par.getPlayer() == null)
  258. return false;
  259. par.getPlayer().setIsOlympiadStart(true);
  260. par.getPlayer().updateEffectIcons();
  261. }
  262. for (int i = 0; i < _teamTwoSize; i++)
  263. {
  264. par = _teamTwo[i];
  265. if (par.getPlayer() == null)
  266. return false;
  267. par.getPlayer().setIsOlympiadStart(true);
  268. par.getPlayer().updateEffectIcons();
  269. }
  270. return true;
  271. }
  272. @Override
  273. protected final void cleanEffects()
  274. {
  275. Participant par;
  276. for (int i = _teamOneSize; --i >= 0;)
  277. {
  278. par = _teamOne[i];
  279. if (par.getPlayer() != null
  280. && !par.isDefaulted()
  281. && !par.isDisconnected()
  282. && par.getPlayer().getOlympiadGameId() == _stadiumID)
  283. cleanEffects(par.getPlayer());
  284. }
  285. for (int i = _teamTwoSize; --i >= 0;)
  286. {
  287. par = _teamTwo[i];
  288. if (par.getPlayer() != null
  289. && !par.isDefaulted()
  290. && !par.isDisconnected()
  291. && par.getPlayer().getOlympiadGameId() == _stadiumID)
  292. cleanEffects(par.getPlayer());
  293. }
  294. }
  295. @Override
  296. protected final void portPlayersBack()
  297. {
  298. Participant par;
  299. for (int i = _teamOneSize; --i >= 0;)
  300. {
  301. par = _teamOne[i];
  302. if (par.getPlayer() != null
  303. && !par.isDefaulted()
  304. && !par.isDisconnected())
  305. portPlayerBack(par.getPlayer());
  306. }
  307. for (int i = _teamTwoSize; --i >= 0;)
  308. {
  309. par = _teamTwo[i];
  310. if (par.getPlayer() != null
  311. && !par.isDefaulted()
  312. && !par.isDisconnected())
  313. portPlayerBack(par.getPlayer());
  314. }
  315. }
  316. @Override
  317. protected final void playersStatusBack()
  318. {
  319. Participant par;
  320. for (int i = _teamOneSize; --i >= 0;)
  321. {
  322. par = _teamOne[i];
  323. if (par.getPlayer() != null
  324. && !par.isDefaulted()
  325. && !par.isDisconnected()
  326. && par.getPlayer().getOlympiadGameId() == _stadiumID)
  327. playerStatusBack(par.getPlayer());
  328. }
  329. for (int i = _teamTwoSize; --i >= 0;)
  330. {
  331. par = _teamTwo[i];
  332. if (par.getPlayer() != null
  333. && !par.isDefaulted()
  334. && !par.isDisconnected()
  335. && par.getPlayer().getOlympiadGameId() == _stadiumID)
  336. playerStatusBack(par.getPlayer());
  337. }
  338. }
  339. @Override
  340. protected final void clearPlayers()
  341. {
  342. for (int i = 0; i < MAX_TEAM_SIZE; i++)
  343. {
  344. if (i < _teamOneSize)
  345. _teamOne[i].setPlayer(null);
  346. else
  347. IdFactory.getInstance().releaseId(_teamOne[i].getObjectId());
  348. if (i < _teamTwoSize)
  349. _teamTwo[i].setPlayer(null);
  350. else
  351. IdFactory.getInstance().releaseId(_teamTwo[i].getObjectId());
  352. _teamOne[i] = null;
  353. _teamTwo[i] = null;
  354. }
  355. }
  356. @Override
  357. protected final void handleDisconnect(L2PcInstance player)
  358. {
  359. Participant par;
  360. for (int i = _teamOneSize; --i >= 0;)
  361. {
  362. par = _teamOne[i];
  363. if (par.getObjectId() == player.getObjectId())
  364. {
  365. par.setDisconnected(true);
  366. return;
  367. }
  368. }
  369. for (int i = _teamTwoSize; --i >= 0;)
  370. {
  371. par = _teamTwo[i];
  372. if (par.getObjectId() == player.getObjectId())
  373. {
  374. par.setDisconnected(true);
  375. return;
  376. }
  377. }
  378. }
  379. @Override
  380. protected final boolean haveWinner()
  381. {
  382. if (!checkBattleStatus())
  383. return true;
  384. boolean teamOneLost = true;
  385. boolean teamTwoLost = true;
  386. Participant par;
  387. for (int i = _teamOneSize; --i >= 0;)
  388. {
  389. par = _teamOne[i];
  390. if (!par.isDisconnected())
  391. {
  392. if (par.getPlayer() != null && par.getPlayer().getOlympiadGameId() == _stadiumID)
  393. teamOneLost &= par.getPlayer().isDead();
  394. }
  395. }
  396. for (int i = _teamTwoSize; --i >= 0;)
  397. {
  398. par = _teamTwo[i];
  399. if (!par.isDisconnected())
  400. {
  401. if (par.getPlayer() != null && par.getPlayer().getOlympiadGameId() == _stadiumID)
  402. teamTwoLost &= par.getPlayer().isDead();
  403. }
  404. }
  405. return teamOneLost || teamTwoLost;
  406. }
  407. @Override
  408. protected final boolean checkBattleStatus()
  409. {
  410. if (_aborted)
  411. return false;
  412. if (teamOneAllDisconnected())
  413. return false;
  414. if (teamTwoAllDisconnected())
  415. return false;
  416. return true;
  417. }
  418. @Override
  419. protected void validateWinner(L2OlympiadStadiumZone stadium)
  420. {
  421. if (_aborted)
  422. return;
  423. final boolean tOneCrash = teamOneAllDisconnected();
  424. final boolean tTwoCrash = teamTwoAllDisconnected();
  425. Participant par;
  426. SystemMessage sm;
  427. int points;
  428. // Check for if a team defaulted before battle started
  429. if (_teamOneDefaulted || _teamTwoDefaulted)
  430. {
  431. try
  432. {
  433. if (_teamOneDefaulted)
  434. {
  435. for (int i = _teamOneSize; --i >= 0;)
  436. {
  437. par = _teamOne[i];
  438. removePointsFromParticipant(par, Math.min(par.getStats().getInteger(POINTS) / 3, Config.ALT_OLY_MAX_POINTS));
  439. }
  440. }
  441. if (_teamTwoDefaulted)
  442. {
  443. for (int i = _teamTwoSize; --i >= 0;)
  444. {
  445. par = _teamTwo[i];
  446. removePointsFromParticipant(par, Math.min(par.getStats().getInteger(POINTS) / 3, Config.ALT_OLY_MAX_POINTS));
  447. }
  448. }
  449. }
  450. catch (Exception e)
  451. {
  452. _log.log(Level.WARNING, "Exception on validateWinner(): " + e.getMessage(), e);
  453. }
  454. return;
  455. }
  456. // points to be dedicted in case of losing
  457. final int[] pointsTeamOne = new int[_teamOneSize];
  458. final int[] pointsTeamTwo = new int[_teamTwoSize];
  459. final int[] maxPointsTeamOne = new int[_teamOneSize];
  460. final int[] maxPointsTeamTwo = new int[_teamTwoSize];
  461. int totalPointsTeamOne = 0;
  462. int totalPointsTeamTwo = 0;
  463. for (int i = 0; i < _teamOneSize; i++)
  464. {
  465. points = _teamOne[i].getStats().getInteger(POINTS) / getDivider();
  466. if (points <= 0)
  467. points = 1;
  468. else if (points > Config.ALT_OLY_MAX_POINTS)
  469. points = Config.ALT_OLY_MAX_POINTS;
  470. totalPointsTeamOne += points;
  471. pointsTeamOne[i] = points;
  472. maxPointsTeamOne[i] = points;
  473. }
  474. for (int i = _teamTwoSize; --i >= 0;)
  475. {
  476. points = _teamTwo[i].getStats().getInteger(POINTS) / getDivider();
  477. if (points <= 0)
  478. points = 1;
  479. else if (points > Config.ALT_OLY_MAX_POINTS)
  480. points = Config.ALT_OLY_MAX_POINTS;
  481. totalPointsTeamTwo += points;
  482. pointsTeamTwo[i] = points;
  483. maxPointsTeamTwo[i] = points;
  484. }
  485. // Choose minimum sum
  486. int min = Math.min(totalPointsTeamOne, totalPointsTeamTwo);
  487. // make sure all team members got same number of the points: round down to 3x
  488. min = (min / MAX_TEAM_SIZE) * MAX_TEAM_SIZE;
  489. // calculating coefficients and trying to correct total number of points for each team
  490. // due to rounding errors total points after correction will always be lower or equal
  491. // than needed minimal sum
  492. final double dividerOne = (double)totalPointsTeamOne / min;
  493. final double dividerTwo = (double)totalPointsTeamTwo / min;
  494. totalPointsTeamOne = min;
  495. totalPointsTeamTwo = min;
  496. for (int i = 0; i < _teamOneSize; i++)
  497. {
  498. points = Math.max((int)(pointsTeamOne[i] / dividerOne), 1);
  499. pointsTeamOne[i] = points;
  500. totalPointsTeamOne -= points;
  501. }
  502. for (int i = _teamTwoSize; --i >= 0;)
  503. {
  504. points = Math.max((int)(pointsTeamTwo[i] / dividerTwo), 1);
  505. pointsTeamTwo[i] = points;
  506. totalPointsTeamTwo -= points;
  507. }
  508. // compensating remaining points, first team from begin to end, second from end to begin
  509. for (int i = 0; totalPointsTeamOne > 0 && i < _teamOneSize; i++)
  510. {
  511. if (pointsTeamOne[i] < maxPointsTeamOne[i])
  512. {
  513. pointsTeamOne[i]++;
  514. totalPointsTeamOne--;
  515. }
  516. }
  517. for (int i = _teamTwoSize; totalPointsTeamTwo > 0 && --i >= 0;)
  518. {
  519. if (pointsTeamTwo[i] < maxPointsTeamTwo[i])
  520. {
  521. pointsTeamTwo[i]++;
  522. totalPointsTeamTwo--;
  523. }
  524. }
  525. // Create results for players if a team crashed
  526. if (tOneCrash || tTwoCrash)
  527. {
  528. try
  529. {
  530. if (tTwoCrash && !tOneCrash)
  531. {
  532. sm = SystemMessage.getSystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  533. sm.addString(_teamOne[0].getName());
  534. stadium.broadcastPacket(sm);
  535. for (int i = 0; i < _teamTwoSize; i++)
  536. {
  537. par = _teamTwo[i];
  538. par.updateStat(COMP_LOST, 1);
  539. points = pointsTeamTwo[i];
  540. removePointsFromParticipant(par, points);
  541. }
  542. points = min / MAX_TEAM_SIZE;
  543. for (int i = 0; i < _teamOneSize; i++)
  544. {
  545. par = _teamOne[i];
  546. par.updateStat(COMP_WON, 1);
  547. addPointsToParticipant(par, points);
  548. }
  549. for (int i = 0; i < _teamOneSize; i++)
  550. rewardParticipant(_teamOne[i].getPlayer(), getReward());
  551. }
  552. else if (tOneCrash && !tTwoCrash)
  553. {
  554. sm = SystemMessage.getSystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  555. sm.addString(_teamTwo[0].getName());
  556. stadium.broadcastPacket(sm);
  557. for (int i = 0; i < _teamOneSize; i++)
  558. {
  559. par = _teamOne[i];
  560. par.updateStat(COMP_LOST, 1);
  561. points = pointsTeamOne[i];
  562. removePointsFromParticipant(par, points);
  563. }
  564. points = min / MAX_TEAM_SIZE;
  565. for (int i = 0; i < _teamTwoSize; i++)
  566. {
  567. par = _teamTwo[i];
  568. par.updateStat(COMP_WON, 1);
  569. addPointsToParticipant(par, points);
  570. }
  571. for (int i = 0; i < _teamTwoSize; i++)
  572. rewardParticipant(_teamTwo[i].getPlayer(), getReward());
  573. }
  574. else if (tOneCrash && tTwoCrash)
  575. {
  576. stadium.broadcastPacket(SystemMessage.getSystemMessage(SystemMessageId.THE_GAME_ENDED_IN_A_TIE));
  577. for (int i = _teamOneSize; --i >= 0;)
  578. {
  579. par = _teamOne[i];
  580. par.updateStat(COMP_LOST, 1);
  581. removePointsFromParticipant(par, pointsTeamOne[i]);
  582. }
  583. for (int i = _teamTwoSize; --i >= 0;)
  584. {
  585. par = _teamTwo[i];
  586. par.updateStat(COMP_LOST, 1);
  587. removePointsFromParticipant(par, pointsTeamTwo[i]);
  588. }
  589. }
  590. for (int i = _teamOneSize; --i >= 0;)
  591. {
  592. par = _teamOne[i];
  593. par.updateStat(COMP_DONE, 1);
  594. par.updateStat(COMP_DONE_WEEK, 1);
  595. par.updateStat(getWeeklyMatchType(), 1);
  596. }
  597. for (int i = _teamTwoSize; --i >= 0;)
  598. {
  599. par = _teamTwo[i];
  600. par.updateStat(COMP_DONE, 1);
  601. par.updateStat(COMP_DONE_WEEK, 1);
  602. par.updateStat(getWeeklyMatchType(), 1);
  603. }
  604. }
  605. catch (Exception e)
  606. {
  607. _log.log(Level.WARNING, "Exception on validateWinner(): " + e.getMessage(), e);
  608. }
  609. return;
  610. }
  611. try
  612. {
  613. double hp;
  614. double teamOneHp = 0;
  615. double teamTwoHp = 0;
  616. for (int i = _teamOneSize; --i >= 0;)
  617. {
  618. par = _teamOne[i];
  619. if (!par.isDisconnected()
  620. && par.getPlayer() != null
  621. && !par.getPlayer().isDead())
  622. {
  623. hp = par.getPlayer().getCurrentHp() + par.getPlayer().getCurrentCp();
  624. if (hp >= 0.5)
  625. teamOneHp += hp;
  626. }
  627. par.updatePlayer();
  628. }
  629. for (int i = _teamTwoSize; --i >= 0;)
  630. {
  631. par = _teamTwo[i];
  632. if (!par.isDisconnected()
  633. && par.getPlayer() != null
  634. && !par.getPlayer().isDead())
  635. {
  636. hp = par.getPlayer().getCurrentHp() + par.getPlayer().getCurrentCp();
  637. if (hp >= 0.5)
  638. teamTwoHp += hp;
  639. }
  640. par.updatePlayer();
  641. }
  642. if ((teamTwoHp == 0 && teamOneHp != 0)
  643. || (_damageT1 > _damageT2 && teamTwoHp != 0 && teamOneHp != 0))
  644. {
  645. sm = SystemMessage.getSystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  646. sm.addString(_teamOne[0].getName());
  647. stadium.broadcastPacket(sm);
  648. for (int i = 0; i < _teamTwoSize; i++)
  649. {
  650. par = _teamTwo[i];
  651. par.updateStat(COMP_LOST, 1);
  652. points = pointsTeamTwo[i];
  653. removePointsFromParticipant(par, points);
  654. }
  655. points = min / MAX_TEAM_SIZE;
  656. for (int i = 0; i < _teamOneSize; i++)
  657. {
  658. par = _teamOne[i];
  659. par.updateStat(COMP_WON, 1);
  660. addPointsToParticipant(par, points);
  661. }
  662. for (int i = 0; i < _teamOneSize; i++)
  663. rewardParticipant(_teamOne[i].getPlayer(), getReward());
  664. }
  665. else if ((teamOneHp == 0 && teamTwoHp != 0)
  666. || (_damageT2 > _damageT1 && teamOneHp != 0 && teamTwoHp != 0))
  667. {
  668. sm = SystemMessage.getSystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  669. sm.addString(_teamTwo[0].getName());
  670. stadium.broadcastPacket(sm);
  671. for (int i = 0; i < _teamOneSize; i++)
  672. {
  673. par = _teamOne[i];
  674. par.updateStat(COMP_LOST, 1);
  675. points = pointsTeamOne[i];
  676. removePointsFromParticipant(par, points);
  677. }
  678. points = min / MAX_TEAM_SIZE;
  679. for (int i = 0; i < _teamTwoSize; i++)
  680. {
  681. par = _teamTwo[i];
  682. par.updateStat(COMP_WON, 1);
  683. addPointsToParticipant(par, points);
  684. }
  685. for (int i = 0; i < _teamTwoSize; i++)
  686. rewardParticipant(_teamTwo[i].getPlayer(), getReward());
  687. }
  688. else
  689. {
  690. stadium.broadcastPacket(SystemMessage.getSystemMessage(SystemMessageId.THE_GAME_ENDED_IN_A_TIE));
  691. for (int i = 0; i < _teamOneSize; i++)
  692. {
  693. par = _teamOne[i];
  694. par.updateStat(COMP_DRAWN, 1);
  695. points = Math.min(par.getStats().getInteger(POINTS) / getDivider(), Config.ALT_OLY_MAX_POINTS);
  696. removePointsFromParticipant(par, points);
  697. }
  698. for (int i = 0; i < _teamTwoSize; i++)
  699. {
  700. par = _teamTwo[i];
  701. par.updateStat(COMP_DRAWN, 1);
  702. points = Math.min(par.getStats().getInteger(POINTS) / getDivider(), Config.ALT_OLY_MAX_POINTS);
  703. removePointsFromParticipant(par, points);
  704. }
  705. }
  706. for (int i = _teamOneSize; --i >= 0;)
  707. {
  708. par = _teamOne[i];
  709. par.updateStat(COMP_DONE, 1);
  710. par.updateStat(COMP_DONE_WEEK, 1);
  711. par.updateStat(getWeeklyMatchType(), 1);
  712. }
  713. for (int i = _teamTwoSize; --i >= 0;)
  714. {
  715. par = _teamTwo[i];
  716. par.updateStat(COMP_DONE, 1);
  717. par.updateStat(COMP_DONE_WEEK, 1);
  718. par.updateStat(getWeeklyMatchType(), 1);
  719. }
  720. }
  721. catch (Exception e)
  722. {
  723. _log.log(Level.WARNING, "Exception on validateWinner(): " + e.getMessage(), e);
  724. }
  725. }
  726. @Override
  727. protected final void addDamage(L2PcInstance player, int damage)
  728. {
  729. Participant par;
  730. for (int i = _teamOneSize; --i >= 0;)
  731. {
  732. par = _teamOne[i];
  733. if (par.getObjectId() == player.getObjectId())
  734. {
  735. if (!par.isDisconnected())
  736. _damageT1 += damage;
  737. return;
  738. }
  739. }
  740. for (int i = _teamTwoSize; --i >= 0;)
  741. {
  742. par = _teamTwo[i];
  743. if (par.getObjectId() == player.getObjectId())
  744. {
  745. if (!par.isDisconnected())
  746. _damageT2 += damage;
  747. return;
  748. }
  749. }
  750. }
  751. @Override
  752. public final String[] getPlayerNames()
  753. {
  754. return new String[] {_teamOne[0].getName(), _teamTwo[0].getName()};
  755. }
  756. @Override
  757. public final boolean checkDefaulted()
  758. {
  759. try
  760. {
  761. SystemMessage reason = null;
  762. Participant par;
  763. for (int i = _teamOneSize; --i >= 0;)
  764. {
  765. par = _teamOne[i];
  766. par.updatePlayer();
  767. reason = checkDefaulted(par.getPlayer());
  768. if (reason != null)
  769. {
  770. par.setDefaulted(true);
  771. if (!_teamOneDefaulted)
  772. {
  773. _teamOneDefaulted = true;
  774. for (Participant t : _teamTwo)
  775. {
  776. if (t.getPlayer() != null)
  777. t.getPlayer().sendPacket(reason);
  778. }
  779. }
  780. }
  781. }
  782. reason = null;
  783. for (int i = _teamTwoSize; --i >= 0;)
  784. {
  785. par = _teamTwo[i];
  786. par.updatePlayer();
  787. reason = checkDefaulted(par.getPlayer());
  788. if (reason != null)
  789. {
  790. par.setDefaulted(true);
  791. if (!_teamTwoDefaulted)
  792. {
  793. _teamTwoDefaulted = true;
  794. for (Participant t : _teamOne)
  795. {
  796. if (t.getPlayer() != null)
  797. t.getPlayer().sendPacket(reason);
  798. }
  799. }
  800. }
  801. }
  802. return _teamOneDefaulted || _teamTwoDefaulted;
  803. }
  804. catch (Exception e)
  805. {
  806. _log.log(Level.WARNING, "Exception on checkDefaulted(): " + e.getMessage(), e);
  807. return true;
  808. }
  809. }
  810. @Override
  811. public final void resetDamage()
  812. {
  813. _damageT1 = 0;
  814. _damageT2 = 0;
  815. }
  816. protected final boolean teamOneAllDisconnected()
  817. {
  818. for (int i = _teamOneSize; --i >= 0;)
  819. {
  820. if (!_teamOne[i].isDisconnected())
  821. return false;
  822. }
  823. return true;
  824. }
  825. protected final boolean teamTwoAllDisconnected()
  826. {
  827. for (int i = _teamTwoSize; --i >= 0;)
  828. {
  829. if (!_teamTwo[i].isDisconnected())
  830. return false;
  831. }
  832. return true;
  833. }
  834. }