OlympiadGameTeams.java 23 KB

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