OlympiadGameNormal.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.SQLException;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.logging.Level;
  26. import java.util.logging.LogRecord;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  29. import com.l2jserver.gameserver.model.L2World;
  30. import com.l2jserver.gameserver.model.Location;
  31. import com.l2jserver.gameserver.model.actor.L2Character;
  32. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  33. import com.l2jserver.gameserver.model.events.EventDispatcher;
  34. import com.l2jserver.gameserver.model.events.impl.olympiad.OnOlympiadMatchResult;
  35. import com.l2jserver.gameserver.model.zone.type.L2OlympiadStadiumZone;
  36. import com.l2jserver.gameserver.network.SystemMessageId;
  37. import com.l2jserver.gameserver.network.serverpackets.ExOlympiadMatchResult;
  38. import com.l2jserver.gameserver.network.serverpackets.ExOlympiadUserInfo;
  39. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  40. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  41. import com.l2jserver.util.Rnd;
  42. /**
  43. * @author GodKratos, Pere, DS
  44. */
  45. public abstract class OlympiadGameNormal extends AbstractOlympiadGame
  46. {
  47. protected int _damageP1 = 0;
  48. protected int _damageP2 = 0;
  49. protected Participant _playerOne;
  50. protected Participant _playerTwo;
  51. protected OlympiadGameNormal(int id, Participant[] opponents)
  52. {
  53. super(id);
  54. _playerOne = opponents[0];
  55. _playerTwo = opponents[1];
  56. _playerOne.getPlayer().setOlympiadGameId(id);
  57. _playerTwo.getPlayer().setOlympiadGameId(id);
  58. }
  59. protected static final Participant[] createListOfParticipants(List<Integer> list)
  60. {
  61. if ((list == null) || list.isEmpty() || (list.size() < 2))
  62. {
  63. return null;
  64. }
  65. int playerOneObjectId = 0;
  66. L2PcInstance playerOne = null;
  67. L2PcInstance playerTwo = null;
  68. while (list.size() > 1)
  69. {
  70. playerOneObjectId = list.remove(Rnd.nextInt(list.size()));
  71. playerOne = L2World.getInstance().getPlayer(playerOneObjectId);
  72. if ((playerOne == null) || !playerOne.isOnline())
  73. {
  74. continue;
  75. }
  76. playerTwo = L2World.getInstance().getPlayer(list.remove(Rnd.nextInt(list.size())));
  77. if ((playerTwo == null) || !playerTwo.isOnline())
  78. {
  79. list.add(playerOneObjectId);
  80. continue;
  81. }
  82. Participant[] result = new Participant[2];
  83. result[0] = new Participant(playerOne, 1);
  84. result[1] = new Participant(playerTwo, 2);
  85. return result;
  86. }
  87. return null;
  88. }
  89. @Override
  90. public final boolean containsParticipant(int playerId)
  91. {
  92. return ((_playerOne != null) && (_playerOne.getObjectId() == playerId)) || ((_playerTwo != null) && (_playerTwo.getObjectId() == playerId));
  93. }
  94. @Override
  95. public final void sendOlympiadInfo(L2Character player)
  96. {
  97. player.sendPacket(new ExOlympiadUserInfo(_playerOne));
  98. player.sendPacket(new ExOlympiadUserInfo(_playerTwo));
  99. }
  100. @Override
  101. public final void broadcastOlympiadInfo(L2OlympiadStadiumZone stadium)
  102. {
  103. stadium.broadcastPacket(new ExOlympiadUserInfo(_playerOne));
  104. stadium.broadcastPacket(new ExOlympiadUserInfo(_playerTwo));
  105. }
  106. @Override
  107. protected final void broadcastPacket(L2GameServerPacket packet)
  108. {
  109. if (_playerOne.updatePlayer())
  110. {
  111. _playerOne.getPlayer().sendPacket(packet);
  112. }
  113. if (_playerTwo.updatePlayer())
  114. {
  115. _playerTwo.getPlayer().sendPacket(packet);
  116. }
  117. }
  118. @Override
  119. protected final boolean portPlayersToArena(List<Location> spawns)
  120. {
  121. boolean result = true;
  122. try
  123. {
  124. result &= portPlayerToArena(_playerOne, spawns.get(0), _stadiumID);
  125. result &= portPlayerToArena(_playerTwo, spawns.get(spawns.size() / 2), _stadiumID);
  126. }
  127. catch (Exception e)
  128. {
  129. _log.log(Level.WARNING, "", e);
  130. return false;
  131. }
  132. return result;
  133. }
  134. @Override
  135. protected boolean needBuffers()
  136. {
  137. return true;
  138. }
  139. @Override
  140. protected final void removals()
  141. {
  142. if (_aborted)
  143. {
  144. return;
  145. }
  146. removals(_playerOne.getPlayer(), true);
  147. removals(_playerTwo.getPlayer(), true);
  148. }
  149. @Override
  150. protected final boolean makeCompetitionStart()
  151. {
  152. if (!super.makeCompetitionStart())
  153. {
  154. return false;
  155. }
  156. if ((_playerOne.getPlayer() == null) || (_playerTwo.getPlayer() == null))
  157. {
  158. return false;
  159. }
  160. _playerOne.getPlayer().setIsOlympiadStart(true);
  161. _playerOne.getPlayer().updateEffectIcons();
  162. _playerTwo.getPlayer().setIsOlympiadStart(true);
  163. _playerTwo.getPlayer().updateEffectIcons();
  164. return true;
  165. }
  166. @Override
  167. protected final void cleanEffects()
  168. {
  169. if ((_playerOne.getPlayer() != null) && !_playerOne.isDefaulted() && !_playerOne.isDisconnected() && (_playerOne.getPlayer().getOlympiadGameId() == _stadiumID))
  170. {
  171. cleanEffects(_playerOne.getPlayer());
  172. }
  173. if ((_playerTwo.getPlayer() != null) && !_playerTwo.isDefaulted() && !_playerTwo.isDisconnected() && (_playerTwo.getPlayer().getOlympiadGameId() == _stadiumID))
  174. {
  175. cleanEffects(_playerTwo.getPlayer());
  176. }
  177. }
  178. @Override
  179. protected final void portPlayersBack()
  180. {
  181. if ((_playerOne.getPlayer() != null) && !_playerOne.isDefaulted() && !_playerOne.isDisconnected())
  182. {
  183. portPlayerBack(_playerOne.getPlayer());
  184. }
  185. if ((_playerTwo.getPlayer() != null) && !_playerTwo.isDefaulted() && !_playerTwo.isDisconnected())
  186. {
  187. portPlayerBack(_playerTwo.getPlayer());
  188. }
  189. }
  190. @Override
  191. protected final void playersStatusBack()
  192. {
  193. if ((_playerOne.getPlayer() != null) && !_playerOne.isDefaulted() && !_playerOne.isDisconnected() && (_playerOne.getPlayer().getOlympiadGameId() == _stadiumID))
  194. {
  195. playerStatusBack(_playerOne.getPlayer());
  196. }
  197. if ((_playerTwo.getPlayer() != null) && !_playerTwo.isDefaulted() && !_playerTwo.isDisconnected() && (_playerTwo.getPlayer().getOlympiadGameId() == _stadiumID))
  198. {
  199. playerStatusBack(_playerTwo.getPlayer());
  200. }
  201. }
  202. @Override
  203. protected final void clearPlayers()
  204. {
  205. _playerOne.setPlayer(null);
  206. _playerOne = null;
  207. _playerTwo.setPlayer(null);
  208. _playerTwo = null;
  209. }
  210. @Override
  211. protected final void handleDisconnect(L2PcInstance player)
  212. {
  213. if (player.getObjectId() == _playerOne.getObjectId())
  214. {
  215. _playerOne.setDisconnected(true);
  216. }
  217. else if (player.getObjectId() == _playerTwo.getObjectId())
  218. {
  219. _playerTwo.setDisconnected(true);
  220. }
  221. }
  222. @Override
  223. protected final boolean checkBattleStatus()
  224. {
  225. if (_aborted)
  226. {
  227. return false;
  228. }
  229. if ((_playerOne.getPlayer() == null) || _playerOne.isDisconnected())
  230. {
  231. return false;
  232. }
  233. if ((_playerTwo.getPlayer() == null) || _playerTwo.isDisconnected())
  234. {
  235. return false;
  236. }
  237. return true;
  238. }
  239. @Override
  240. protected final boolean haveWinner()
  241. {
  242. if (!checkBattleStatus())
  243. {
  244. return true;
  245. }
  246. boolean playerOneLost = true;
  247. try
  248. {
  249. if (_playerOne.getPlayer().getOlympiadGameId() == _stadiumID)
  250. {
  251. playerOneLost = _playerOne.getPlayer().isDead();
  252. }
  253. }
  254. catch (Exception e)
  255. {
  256. playerOneLost = true;
  257. }
  258. boolean playerTwoLost = true;
  259. try
  260. {
  261. if (_playerTwo.getPlayer().getOlympiadGameId() == _stadiumID)
  262. {
  263. playerTwoLost = _playerTwo.getPlayer().isDead();
  264. }
  265. }
  266. catch (Exception e)
  267. {
  268. playerTwoLost = true;
  269. }
  270. return playerOneLost || playerTwoLost;
  271. }
  272. @Override
  273. protected void validateWinner(L2OlympiadStadiumZone stadium)
  274. {
  275. if (_aborted)
  276. {
  277. return;
  278. }
  279. ExOlympiadMatchResult result = null;
  280. boolean tie = false;
  281. int winside = 0;
  282. List<OlympiadInfo> list1 = new ArrayList<>(1);
  283. List<OlympiadInfo> list2 = new ArrayList<>(1);
  284. final boolean _pOneCrash = ((_playerOne.getPlayer() == null) || _playerOne.isDisconnected());
  285. final boolean _pTwoCrash = ((_playerTwo.getPlayer() == null) || _playerTwo.isDisconnected());
  286. final int playerOnePoints = _playerOne.getStats().getInt(POINTS);
  287. final int playerTwoPoints = _playerTwo.getStats().getInt(POINTS);
  288. int pointDiff = Math.min(playerOnePoints, playerTwoPoints) / getDivider();
  289. if (pointDiff <= 0)
  290. {
  291. pointDiff = 1;
  292. }
  293. else if (pointDiff > Config.ALT_OLY_MAX_POINTS)
  294. {
  295. pointDiff = Config.ALT_OLY_MAX_POINTS;
  296. }
  297. int points;
  298. SystemMessage sm;
  299. // Check for if a player defaulted before battle started
  300. if (_playerOne.isDefaulted() || _playerTwo.isDefaulted())
  301. {
  302. try
  303. {
  304. if (_playerOne.isDefaulted())
  305. {
  306. try
  307. {
  308. points = Math.min(playerOnePoints / 3, Config.ALT_OLY_MAX_POINTS);
  309. removePointsFromParticipant(_playerOne, points);
  310. list1.add(new OlympiadInfo(_playerOne.getName(), _playerOne.getClanName(), _playerOne.getClanId(), _playerOne.getBaseClass(), _damageP1, playerOnePoints - points, -points));
  311. winside = 2;
  312. if (Config.ALT_OLY_LOG_FIGHTS)
  313. {
  314. LogRecord record = new LogRecord(Level.INFO, _playerOne.getName() + " default");
  315. record.setParameters(new Object[]
  316. {
  317. _playerOne.getName(),
  318. _playerTwo.getName(),
  319. 0,
  320. 0,
  321. 0,
  322. 0,
  323. points,
  324. getType().toString()
  325. });
  326. _logResults.log(record);
  327. }
  328. }
  329. catch (Exception e)
  330. {
  331. _log.log(Level.WARNING, "Exception on validateWinner(): " + e.getMessage(), e);
  332. }
  333. }
  334. if (_playerTwo.isDefaulted())
  335. {
  336. try
  337. {
  338. points = Math.min(playerTwoPoints / 3, Config.ALT_OLY_MAX_POINTS);
  339. removePointsFromParticipant(_playerTwo, points);
  340. list2.add(new OlympiadInfo(_playerTwo.getName(), _playerTwo.getClanName(), _playerTwo.getClanId(), _playerTwo.getBaseClass(), _damageP2, playerTwoPoints - points, -points));
  341. if (winside == 2)
  342. {
  343. tie = true;
  344. }
  345. else
  346. {
  347. winside = 1;
  348. }
  349. if (Config.ALT_OLY_LOG_FIGHTS)
  350. {
  351. LogRecord record = new LogRecord(Level.INFO, _playerTwo.getName() + " default");
  352. record.setParameters(new Object[]
  353. {
  354. _playerOne.getName(),
  355. _playerTwo.getName(),
  356. 0,
  357. 0,
  358. 0,
  359. 0,
  360. points,
  361. getType().toString()
  362. });
  363. _logResults.log(record);
  364. }
  365. }
  366. catch (Exception e)
  367. {
  368. _log.log(Level.WARNING, "Exception on validateWinner(): " + e.getMessage(), e);
  369. }
  370. }
  371. if (winside == 1)
  372. {
  373. result = new ExOlympiadMatchResult(tie, winside, list1, list2);
  374. }
  375. else
  376. {
  377. result = new ExOlympiadMatchResult(tie, winside, list2, list1);
  378. }
  379. stadium.broadcastPacket(result);
  380. return;
  381. }
  382. catch (Exception e)
  383. {
  384. _log.log(Level.WARNING, "Exception on validateWinner(): " + e.getMessage(), e);
  385. return;
  386. }
  387. }
  388. // Create results for players if a player crashed
  389. if (_pOneCrash || _pTwoCrash)
  390. {
  391. try
  392. {
  393. if (_pTwoCrash && !_pOneCrash)
  394. {
  395. sm = SystemMessage.getSystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  396. sm.addString(_playerOne.getName());
  397. stadium.broadcastPacket(sm);
  398. _playerOne.updateStat(COMP_WON, 1);
  399. addPointsToParticipant(_playerOne, pointDiff);
  400. list1.add(new OlympiadInfo(_playerOne.getName(), _playerOne.getClanName(), _playerOne.getClanId(), _playerOne.getBaseClass(), _damageP1, playerOnePoints + pointDiff, pointDiff));
  401. _playerTwo.updateStat(COMP_LOST, 1);
  402. removePointsFromParticipant(_playerTwo, pointDiff);
  403. list2.add(new OlympiadInfo(_playerTwo.getName(), _playerTwo.getClanName(), _playerTwo.getClanId(), _playerTwo.getBaseClass(), _damageP2, playerTwoPoints - pointDiff, -pointDiff));
  404. winside = 1;
  405. rewardParticipant(_playerOne.getPlayer(), getReward());
  406. if (Config.ALT_OLY_LOG_FIGHTS)
  407. {
  408. LogRecord record = new LogRecord(Level.INFO, _playerTwo.getName() + " crash");
  409. record.setParameters(new Object[]
  410. {
  411. _playerOne.getName(),
  412. _playerTwo.getName(),
  413. 0,
  414. 0,
  415. 0,
  416. 0,
  417. pointDiff,
  418. getType().toString()
  419. });
  420. _logResults.log(record);
  421. }
  422. // Notify to scripts
  423. EventDispatcher.getInstance().notifyEventAsync(new OnOlympiadMatchResult(_playerOne, _playerTwo, getType()), Olympiad.getInstance());
  424. }
  425. else if (_pOneCrash && !_pTwoCrash)
  426. {
  427. sm = SystemMessage.getSystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  428. sm.addString(_playerTwo.getName());
  429. stadium.broadcastPacket(sm);
  430. _playerTwo.updateStat(COMP_WON, 1);
  431. addPointsToParticipant(_playerTwo, pointDiff);
  432. list2.add(new OlympiadInfo(_playerTwo.getName(), _playerTwo.getClanName(), _playerTwo.getClanId(), _playerTwo.getBaseClass(), _damageP2, playerTwoPoints + pointDiff, pointDiff));
  433. _playerOne.updateStat(COMP_LOST, 1);
  434. removePointsFromParticipant(_playerOne, pointDiff);
  435. list1.add(new OlympiadInfo(_playerOne.getName(), _playerOne.getClanName(), _playerOne.getClanId(), _playerOne.getBaseClass(), _damageP1, playerOnePoints - pointDiff, -pointDiff));
  436. winside = 2;
  437. rewardParticipant(_playerTwo.getPlayer(), getReward());
  438. if (Config.ALT_OLY_LOG_FIGHTS)
  439. {
  440. LogRecord record = new LogRecord(Level.INFO, _playerOne.getName() + " crash");
  441. record.setParameters(new Object[]
  442. {
  443. _playerOne.getName(),
  444. _playerTwo.getName(),
  445. 0,
  446. 0,
  447. 0,
  448. 0,
  449. pointDiff,
  450. getType().toString()
  451. });
  452. _logResults.log(record);
  453. }
  454. // Notify to scripts
  455. EventDispatcher.getInstance().notifyEventAsync(new OnOlympiadMatchResult(_playerTwo, _playerOne, getType()), Olympiad.getInstance());
  456. }
  457. else if (_pOneCrash && _pTwoCrash)
  458. {
  459. stadium.broadcastPacket(SystemMessage.getSystemMessage(SystemMessageId.THE_GAME_ENDED_IN_A_TIE));
  460. _playerOne.updateStat(COMP_LOST, 1);
  461. removePointsFromParticipant(_playerOne, pointDiff);
  462. list1.add(new OlympiadInfo(_playerOne.getName(), _playerOne.getClanName(), _playerOne.getClanId(), _playerOne.getBaseClass(), _damageP1, playerOnePoints - pointDiff, -pointDiff));
  463. _playerTwo.updateStat(COMP_LOST, 1);
  464. removePointsFromParticipant(_playerTwo, pointDiff);
  465. list2.add(new OlympiadInfo(_playerTwo.getName(), _playerTwo.getClanName(), _playerTwo.getClanId(), _playerTwo.getBaseClass(), _damageP2, playerTwoPoints - pointDiff, -pointDiff));
  466. tie = true;
  467. if (Config.ALT_OLY_LOG_FIGHTS)
  468. {
  469. LogRecord record = new LogRecord(Level.INFO, "both crash");
  470. record.setParameters(new Object[]
  471. {
  472. _playerOne.getName(),
  473. _playerTwo.getName(),
  474. 0,
  475. 0,
  476. 0,
  477. 0,
  478. pointDiff,
  479. getType().toString()
  480. });
  481. _logResults.log(record);
  482. }
  483. }
  484. _playerOne.updateStat(COMP_DONE, 1);
  485. _playerTwo.updateStat(COMP_DONE, 1);
  486. _playerOne.updateStat(COMP_DONE_WEEK, 1);
  487. _playerTwo.updateStat(COMP_DONE_WEEK, 1);
  488. _playerOne.updateStat(getWeeklyMatchType(), 1);
  489. _playerTwo.updateStat(getWeeklyMatchType(), 1);
  490. if (winside == 1)
  491. {
  492. result = new ExOlympiadMatchResult(tie, winside, list1, list2);
  493. }
  494. else
  495. {
  496. result = new ExOlympiadMatchResult(tie, winside, list2, list1);
  497. }
  498. stadium.broadcastPacket(result);
  499. // Notify to scripts
  500. EventDispatcher.getInstance().notifyEventAsync(new OnOlympiadMatchResult(null, _playerOne, getType()), Olympiad.getInstance());
  501. EventDispatcher.getInstance().notifyEventAsync(new OnOlympiadMatchResult(null, _playerTwo, getType()), Olympiad.getInstance());
  502. return;
  503. }
  504. catch (Exception e)
  505. {
  506. _log.log(Level.WARNING, "Exception on validateWinner(): " + e.getMessage(), e);
  507. return;
  508. }
  509. }
  510. try
  511. {
  512. String winner = "draw";
  513. // Calculate Fight time
  514. long _fightTime = (System.currentTimeMillis() - _startTime);
  515. double playerOneHp = 0;
  516. if ((_playerOne.getPlayer() != null) && !_playerOne.getPlayer().isDead())
  517. {
  518. playerOneHp = _playerOne.getPlayer().getCurrentHp() + _playerOne.getPlayer().getCurrentCp();
  519. if (playerOneHp < 0.5)
  520. {
  521. playerOneHp = 0;
  522. }
  523. }
  524. double playerTwoHp = 0;
  525. if ((_playerTwo.getPlayer() != null) && !_playerTwo.getPlayer().isDead())
  526. {
  527. playerTwoHp = _playerTwo.getPlayer().getCurrentHp() + _playerTwo.getPlayer().getCurrentCp();
  528. if (playerTwoHp < 0.5)
  529. {
  530. playerTwoHp = 0;
  531. }
  532. }
  533. // if players crashed, search if they've relogged
  534. _playerOne.updatePlayer();
  535. _playerTwo.updatePlayer();
  536. if (((_playerOne.getPlayer() == null) || !_playerOne.getPlayer().isOnline()) && ((_playerTwo.getPlayer() == null) || !_playerTwo.getPlayer().isOnline()))
  537. {
  538. _playerOne.updateStat(COMP_DRAWN, 1);
  539. _playerTwo.updateStat(COMP_DRAWN, 1);
  540. sm = SystemMessage.getSystemMessage(SystemMessageId.THE_GAME_ENDED_IN_A_TIE);
  541. stadium.broadcastPacket(sm);
  542. }
  543. else if ((_playerTwo.getPlayer() == null) || !_playerTwo.getPlayer().isOnline() || ((playerTwoHp == 0) && (playerOneHp != 0)) || ((_damageP1 > _damageP2) && (playerTwoHp != 0) && (playerOneHp != 0)))
  544. {
  545. sm = SystemMessage.getSystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  546. sm.addString(_playerOne.getName());
  547. stadium.broadcastPacket(sm);
  548. _playerOne.updateStat(COMP_WON, 1);
  549. _playerTwo.updateStat(COMP_LOST, 1);
  550. addPointsToParticipant(_playerOne, pointDiff);
  551. list1.add(new OlympiadInfo(_playerOne.getName(), _playerOne.getClanName(), _playerOne.getClanId(), _playerOne.getBaseClass(), _damageP1, playerOnePoints + pointDiff, pointDiff));
  552. removePointsFromParticipant(_playerTwo, pointDiff);
  553. list2.add(new OlympiadInfo(_playerTwo.getName(), _playerTwo.getClanName(), _playerTwo.getClanId(), _playerTwo.getBaseClass(), _damageP2, playerTwoPoints - pointDiff, -pointDiff));
  554. winner = _playerOne.getName() + " won";
  555. winside = 1;
  556. // Save Fight Result
  557. saveResults(_playerOne, _playerTwo, 1, _startTime, _fightTime, getType());
  558. rewardParticipant(_playerOne.getPlayer(), getReward());
  559. // Notify to scripts
  560. EventDispatcher.getInstance().notifyEventAsync(new OnOlympiadMatchResult(_playerOne, _playerTwo, getType()), Olympiad.getInstance());
  561. }
  562. else if ((_playerOne.getPlayer() == null) || !_playerOne.getPlayer().isOnline() || ((playerOneHp == 0) && (playerTwoHp != 0)) || ((_damageP2 > _damageP1) && (playerOneHp != 0) && (playerTwoHp != 0)))
  563. {
  564. sm = SystemMessage.getSystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  565. sm.addString(_playerTwo.getName());
  566. stadium.broadcastPacket(sm);
  567. _playerTwo.updateStat(COMP_WON, 1);
  568. _playerOne.updateStat(COMP_LOST, 1);
  569. addPointsToParticipant(_playerTwo, pointDiff);
  570. list2.add(new OlympiadInfo(_playerTwo.getName(), _playerTwo.getClanName(), _playerTwo.getClanId(), _playerTwo.getBaseClass(), _damageP2, playerTwoPoints + pointDiff, pointDiff));
  571. removePointsFromParticipant(_playerOne, pointDiff);
  572. list1.add(new OlympiadInfo(_playerOne.getName(), _playerOne.getClanName(), _playerOne.getClanId(), _playerOne.getBaseClass(), _damageP1, playerOnePoints - pointDiff, -pointDiff));
  573. winner = _playerTwo.getName() + " won";
  574. winside = 2;
  575. // Save Fight Result
  576. saveResults(_playerOne, _playerTwo, 2, _startTime, _fightTime, getType());
  577. rewardParticipant(_playerTwo.getPlayer(), getReward());
  578. // Notify to scripts
  579. EventDispatcher.getInstance().notifyEventAsync(new OnOlympiadMatchResult(_playerTwo, _playerOne, getType()), Olympiad.getInstance());
  580. }
  581. else
  582. {
  583. // Save Fight Result
  584. saveResults(_playerOne, _playerTwo, 0, _startTime, _fightTime, getType());
  585. sm = SystemMessage.getSystemMessage(SystemMessageId.THE_GAME_ENDED_IN_A_TIE);
  586. stadium.broadcastPacket(sm);
  587. int value = Math.min(playerOnePoints / getDivider(), Config.ALT_OLY_MAX_POINTS);
  588. removePointsFromParticipant(_playerOne, value);
  589. list1.add(new OlympiadInfo(_playerOne.getName(), _playerOne.getClanName(), _playerOne.getClanId(), _playerOne.getBaseClass(), _damageP1, playerOnePoints - value, -value));
  590. value = Math.min(playerTwoPoints / getDivider(), Config.ALT_OLY_MAX_POINTS);
  591. removePointsFromParticipant(_playerTwo, value);
  592. list2.add(new OlympiadInfo(_playerTwo.getName(), _playerTwo.getClanName(), _playerTwo.getClanId(), _playerTwo.getBaseClass(), _damageP2, playerTwoPoints - value, -value));
  593. tie = true;
  594. }
  595. _playerOne.updateStat(COMP_DONE, 1);
  596. _playerTwo.updateStat(COMP_DONE, 1);
  597. _playerOne.updateStat(COMP_DONE_WEEK, 1);
  598. _playerTwo.updateStat(COMP_DONE_WEEK, 1);
  599. _playerOne.updateStat(getWeeklyMatchType(), 1);
  600. _playerTwo.updateStat(getWeeklyMatchType(), 1);
  601. if (winside == 1)
  602. {
  603. result = new ExOlympiadMatchResult(tie, winside, list1, list2);
  604. }
  605. else
  606. {
  607. result = new ExOlympiadMatchResult(tie, winside, list2, list1);
  608. }
  609. stadium.broadcastPacket(result);
  610. if (Config.ALT_OLY_LOG_FIGHTS)
  611. {
  612. LogRecord record = new LogRecord(Level.INFO, winner);
  613. record.setParameters(new Object[]
  614. {
  615. _playerOne.getName(),
  616. _playerTwo.getName(),
  617. playerOneHp,
  618. playerTwoHp,
  619. _damageP1,
  620. _damageP2,
  621. pointDiff,
  622. getType().toString()
  623. });
  624. _logResults.log(record);
  625. }
  626. }
  627. catch (Exception e)
  628. {
  629. _log.log(Level.WARNING, "Exception on validateWinner(): " + e.getMessage(), e);
  630. }
  631. }
  632. @Override
  633. protected final void addDamage(L2PcInstance player, int damage)
  634. {
  635. if ((_playerOne.getPlayer() == null) || (_playerTwo.getPlayer() == null))
  636. {
  637. return;
  638. }
  639. if (player == _playerOne.getPlayer())
  640. {
  641. _damageP1 += damage;
  642. }
  643. else if (player == _playerTwo.getPlayer())
  644. {
  645. _damageP2 += damage;
  646. }
  647. }
  648. @Override
  649. public final String[] getPlayerNames()
  650. {
  651. return new String[]
  652. {
  653. _playerOne.getName(),
  654. _playerTwo.getName()
  655. };
  656. }
  657. @Override
  658. public boolean checkDefaulted()
  659. {
  660. SystemMessage reason;
  661. _playerOne.updatePlayer();
  662. _playerTwo.updatePlayer();
  663. reason = checkDefaulted(_playerOne.getPlayer());
  664. if (reason != null)
  665. {
  666. _playerOne.setDefaulted(true);
  667. if (_playerTwo.getPlayer() != null)
  668. {
  669. _playerTwo.getPlayer().sendPacket(reason);
  670. }
  671. }
  672. reason = checkDefaulted(_playerTwo.getPlayer());
  673. if (reason != null)
  674. {
  675. _playerTwo.setDefaulted(true);
  676. if (_playerOne.getPlayer() != null)
  677. {
  678. _playerOne.getPlayer().sendPacket(reason);
  679. }
  680. }
  681. return _playerOne.isDefaulted() || _playerTwo.isDefaulted();
  682. }
  683. @Override
  684. public final void resetDamage()
  685. {
  686. _damageP1 = 0;
  687. _damageP2 = 0;
  688. }
  689. protected static final void saveResults(Participant one, Participant two, int winner, long startTime, long fightTime, CompetitionType type)
  690. {
  691. try (Connection con = ConnectionFactory.getInstance().getConnection();
  692. PreparedStatement ps = con.prepareStatement("INSERT INTO olympiad_fights (charOneId, charTwoId, charOneClass, charTwoClass, winner, start, time, classed) values(?,?,?,?,?,?,?,?)"))
  693. {
  694. ps.setInt(1, one.getObjectId());
  695. ps.setInt(2, two.getObjectId());
  696. ps.setInt(3, one.getBaseClass());
  697. ps.setInt(4, two.getBaseClass());
  698. ps.setInt(5, winner);
  699. ps.setLong(6, startTime);
  700. ps.setLong(7, fightTime);
  701. ps.setInt(8, (type == CompetitionType.CLASSED ? 1 : 0));
  702. ps.execute();
  703. }
  704. catch (SQLException e)
  705. {
  706. if (_log.isLoggable(Level.SEVERE))
  707. {
  708. _log.log(Level.SEVERE, "SQL exception while saving olympiad fight.", e);
  709. }
  710. }
  711. }
  712. }