2
0

OlympiadGameNormal.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.SQLException;
  19. import java.util.List;
  20. import java.util.logging.Level;
  21. import java.util.logging.LogRecord;
  22. import com.l2jserver.Config;
  23. import com.l2jserver.L2DatabaseFactory;
  24. import com.l2jserver.gameserver.model.L2World;
  25. import com.l2jserver.gameserver.model.Location;
  26. import com.l2jserver.gameserver.model.actor.L2Character;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.model.zone.type.L2OlympiadStadiumZone;
  29. import com.l2jserver.gameserver.network.SystemMessageId;
  30. import com.l2jserver.gameserver.network.serverpackets.ExOlympiadUserInfo;
  31. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  32. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  33. import com.l2jserver.util.Rnd;
  34. /**
  35. *
  36. * @author GodKratos, Pere, DS
  37. */
  38. abstract public class OlympiadGameNormal extends AbstractOlympiadGame
  39. {
  40. protected int _damageP1 = 0;
  41. protected int _damageP2 = 0;
  42. protected Participant _playerOne;
  43. protected Participant _playerTwo;
  44. protected OlympiadGameNormal(int id, Participant[] opponents)
  45. {
  46. super(id);
  47. _playerOne = opponents[0];
  48. _playerTwo = opponents[1];
  49. _playerOne.player.setOlympiadGameId(id);
  50. _playerTwo.player.setOlympiadGameId(id);
  51. }
  52. protected static final Participant[] createListOfParticipants(List<Integer> list)
  53. {
  54. if (list == null || list.isEmpty() || list.size() < 2)
  55. return null;
  56. int playerOneObjectId = 0;
  57. L2PcInstance playerOne = null;
  58. L2PcInstance playerTwo = null;
  59. while (list.size() > 1)
  60. {
  61. playerOneObjectId = list.remove(Rnd.nextInt(list.size()));
  62. playerOne = L2World.getInstance().getPlayer(playerOneObjectId);
  63. if (playerOne == null || !playerOne.isOnline())
  64. continue;
  65. playerTwo = L2World.getInstance().getPlayer(list.remove(Rnd.nextInt(list.size())));
  66. if (playerTwo == null || !playerTwo.isOnline())
  67. {
  68. list.add(playerOneObjectId);
  69. continue;
  70. }
  71. Participant[] result = new Participant[2];
  72. result[0] = new Participant(playerOne, 1);
  73. result[1] = new Participant(playerTwo, 2);
  74. return result;
  75. }
  76. return null;
  77. }
  78. @Override
  79. public final boolean containsParticipant(int playerId)
  80. {
  81. return _playerOne.objectId == playerId || _playerTwo.objectId == playerId;
  82. }
  83. @Override
  84. public final void sendOlympiadInfo(L2Character player)
  85. {
  86. player.sendPacket(new ExOlympiadUserInfo(_playerOne));
  87. player.sendPacket(new ExOlympiadUserInfo(_playerTwo));
  88. }
  89. @Override
  90. public final void broadcastOlympiadInfo(L2OlympiadStadiumZone stadium)
  91. {
  92. stadium.broadcastPacket(new ExOlympiadUserInfo(_playerOne));
  93. stadium.broadcastPacket(new ExOlympiadUserInfo(_playerTwo));
  94. }
  95. @Override
  96. protected final void broadcastPacket(L2GameServerPacket packet)
  97. {
  98. _playerOne.updatePlayer();
  99. if (_playerOne.player != null)
  100. _playerOne.player.sendPacket(packet);
  101. _playerTwo.updatePlayer();
  102. if (_playerTwo.player != null)
  103. _playerTwo.player.sendPacket(packet);
  104. }
  105. @Override
  106. protected final boolean portPlayersToArena(List<Location> spawns)
  107. {
  108. boolean result = true;
  109. try
  110. {
  111. result &= portPlayerToArena(_playerOne, spawns.get(0), _stadiumID);
  112. result &= portPlayerToArena(_playerTwo, spawns.get(spawns.size() / 2), _stadiumID);
  113. }
  114. catch (Exception e)
  115. {
  116. _log.log(Level.WARNING, "", e);
  117. return false;
  118. }
  119. return result;
  120. }
  121. @Override
  122. protected boolean needBuffers()
  123. {
  124. return true;
  125. }
  126. @Override
  127. protected final void removals()
  128. {
  129. if (_aborted)
  130. return;
  131. removals(_playerOne.player, true);
  132. removals(_playerTwo.player, true);
  133. }
  134. @Override
  135. protected final boolean makeCompetitionStart()
  136. {
  137. if (!super.makeCompetitionStart())
  138. return false;
  139. if (_playerOne.player == null || _playerTwo.player == null)
  140. return false;
  141. _playerOne.player.setIsOlympiadStart(true);
  142. _playerOne.player.updateEffectIcons();
  143. _playerTwo.player.setIsOlympiadStart(true);
  144. _playerTwo.player.updateEffectIcons();
  145. return true;
  146. }
  147. @Override
  148. protected final void cleanEffects()
  149. {
  150. if (_playerOne.player != null
  151. && !_playerOne.defaulted
  152. && !_playerOne.disconnected
  153. && _playerOne.player.getOlympiadGameId() == _stadiumID)
  154. cleanEffects(_playerOne.player);
  155. if (_playerTwo.player != null
  156. && !_playerTwo.defaulted
  157. && !_playerTwo.disconnected
  158. && _playerTwo.player.getOlympiadGameId() == _stadiumID)
  159. cleanEffects(_playerTwo.player);
  160. }
  161. @Override
  162. protected final void portPlayersBack()
  163. {
  164. if (_playerOne.player != null
  165. && !_playerOne.defaulted
  166. && !_playerOne.disconnected)
  167. portPlayerBack(_playerOne.player);
  168. if (_playerTwo.player != null
  169. && !_playerTwo.defaulted
  170. && !_playerTwo.disconnected)
  171. portPlayerBack(_playerTwo.player);
  172. }
  173. @Override
  174. protected final void playersStatusBack()
  175. {
  176. if (_playerOne.player != null
  177. && !_playerOne.defaulted
  178. && !_playerOne.disconnected
  179. && _playerOne.player.getOlympiadGameId() == _stadiumID)
  180. playerStatusBack(_playerOne.player);
  181. if (_playerTwo.player != null
  182. && !_playerTwo.defaulted
  183. && !_playerTwo.disconnected
  184. && _playerTwo.player.getOlympiadGameId() == _stadiumID)
  185. playerStatusBack(_playerTwo.player);
  186. }
  187. @Override
  188. protected final void clearPlayers()
  189. {
  190. _playerOne.player = null;
  191. _playerOne = null;
  192. _playerTwo.player = null;
  193. _playerTwo = null;
  194. }
  195. @Override
  196. protected final void handleDisconnect(L2PcInstance player)
  197. {
  198. if (player.getObjectId() == _playerOne.objectId)
  199. _playerOne.disconnected = true;
  200. else if (player.getObjectId() == _playerTwo.objectId)
  201. _playerTwo.disconnected = true;
  202. }
  203. @Override
  204. protected final boolean checkBattleStatus()
  205. {
  206. if (_aborted)
  207. return false;
  208. if (_playerOne.player == null || _playerOne.disconnected)
  209. return false;
  210. if (_playerTwo.player == null || _playerTwo.disconnected)
  211. return false;
  212. return true;
  213. }
  214. @Override
  215. protected final boolean haveWinner()
  216. {
  217. if (!checkBattleStatus())
  218. return true;
  219. boolean playerOneLost = true;
  220. try
  221. {
  222. if (_playerOne.player.getOlympiadGameId() == _stadiumID)
  223. playerOneLost = _playerOne.player.isDead();
  224. }
  225. catch (Exception e)
  226. {
  227. playerOneLost = true;
  228. }
  229. boolean playerTwoLost = true;
  230. try
  231. {
  232. if (_playerTwo.player.getOlympiadGameId() == _stadiumID)
  233. playerTwoLost = _playerTwo.player.isDead();
  234. }
  235. catch (Exception e)
  236. {
  237. playerTwoLost = true;
  238. }
  239. return playerOneLost || playerTwoLost;
  240. }
  241. @Override
  242. protected void validateWinner(L2OlympiadStadiumZone stadium)
  243. {
  244. if (_aborted)
  245. return;
  246. final boolean _pOneCrash = (_playerOne.player == null || _playerOne.disconnected);
  247. final boolean _pTwoCrash = (_playerTwo.player == null || _playerTwo.disconnected);
  248. final int playerOnePoints = _playerOne.stats.getInteger(POINTS);
  249. final int playerTwoPoints = _playerTwo.stats.getInteger(POINTS);
  250. int pointDiff = Math.min(playerOnePoints, playerTwoPoints) / getDivider();
  251. if (pointDiff <= 0)
  252. pointDiff = 1;
  253. else if (pointDiff > Config.ALT_OLY_MAX_POINTS)
  254. pointDiff = Config.ALT_OLY_MAX_POINTS;
  255. int points;
  256. SystemMessage sm;
  257. // Check for if a player defaulted before battle started
  258. if (_playerOne.defaulted || _playerTwo.defaulted)
  259. {
  260. try
  261. {
  262. if (_playerOne.defaulted)
  263. {
  264. try
  265. {
  266. points = Math.min(playerOnePoints / 3, Config.ALT_OLY_MAX_POINTS);
  267. removePointsFromParticipant(_playerOne, points);
  268. _playerOne.updateNobleStats();
  269. if (Config.ALT_OLY_LOG_FIGHTS)
  270. {
  271. LogRecord record = new LogRecord(Level.INFO, _playerOne.name+" default");
  272. record.setParameters(new Object[]{_playerOne.name, _playerTwo.name, 0, 0, 0, 0, points, getType().toString()});
  273. _logResults.log(record);
  274. }
  275. }
  276. catch (Exception e)
  277. {
  278. _log.log(Level.WARNING, "Exception on validateWinner(): " + e.getMessage(), e);
  279. }
  280. }
  281. if (_playerTwo.defaulted)
  282. {
  283. try
  284. {
  285. points = Math.min(playerTwoPoints / 3, Config.ALT_OLY_MAX_POINTS);
  286. removePointsFromParticipant(_playerTwo, points);
  287. _playerTwo.updateNobleStats();
  288. if (Config.ALT_OLY_LOG_FIGHTS)
  289. {
  290. LogRecord record = new LogRecord(Level.INFO, _playerTwo.name+" default");
  291. record.setParameters(new Object[]{_playerOne.name, _playerTwo.name, 0, 0, 0, 0, points, getType().toString()});
  292. _logResults.log(record);
  293. }
  294. }
  295. catch (Exception e)
  296. {
  297. _log.log(Level.WARNING, "Exception on validateWinner(): " + e.getMessage(), e);
  298. }
  299. }
  300. return;
  301. }
  302. catch (Exception e)
  303. {
  304. _log.log(Level.WARNING, "Exception on validateWinner(): " + e.getMessage(), e);
  305. return;
  306. }
  307. }
  308. // Create results for players if a player crashed
  309. if (_pOneCrash || _pTwoCrash)
  310. {
  311. try
  312. {
  313. if (_pTwoCrash && !_pOneCrash)
  314. {
  315. sm = SystemMessage.getSystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  316. sm.addString(_playerOne.name);
  317. stadium.broadcastPacket(sm);
  318. _playerOne.updateStat(COMP_WON, 1);
  319. addPointsToParticipant(_playerOne, pointDiff);
  320. _playerTwo.updateStat(COMP_LOST, 1);
  321. removePointsFromParticipant(_playerTwo, pointDiff);
  322. rewardParticipant(_playerOne.player, getReward());
  323. if (Config.ALT_OLY_LOG_FIGHTS)
  324. {
  325. LogRecord record = new LogRecord(Level.INFO, _playerTwo.name+" crash");
  326. record.setParameters(new Object[]{_playerOne.name, _playerTwo.name, 0, 0, 0, 0, pointDiff, getType().toString()});
  327. _logResults.log(record);
  328. }
  329. }
  330. else if (_pOneCrash && !_pTwoCrash)
  331. {
  332. sm = SystemMessage.getSystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  333. sm.addString(_playerTwo.name);
  334. stadium.broadcastPacket(sm);
  335. _playerTwo.updateStat(COMP_WON, 1);
  336. addPointsToParticipant(_playerTwo, pointDiff);
  337. _playerOne.updateStat(COMP_LOST, 1);
  338. removePointsFromParticipant(_playerOne, pointDiff);
  339. rewardParticipant(_playerTwo.player, getReward());
  340. if (Config.ALT_OLY_LOG_FIGHTS)
  341. {
  342. LogRecord record = new LogRecord(Level.INFO, _playerOne.name+" crash");
  343. record.setParameters(new Object[]{_playerOne.name, _playerTwo.name, 0, 0, 0, 0, pointDiff, getType().toString()});
  344. _logResults.log(record);
  345. }
  346. }
  347. else if (_pOneCrash && _pTwoCrash)
  348. {
  349. stadium.broadcastPacket(SystemMessage.getSystemMessage(SystemMessageId.THE_GAME_ENDED_IN_A_TIE));
  350. _playerOne.updateStat(COMP_LOST, 1);
  351. removePointsFromParticipant(_playerOne, pointDiff);
  352. _playerTwo.updateStat(COMP_LOST, 1);
  353. removePointsFromParticipant(_playerTwo, pointDiff);
  354. if (Config.ALT_OLY_LOG_FIGHTS)
  355. {
  356. LogRecord record = new LogRecord(Level.INFO, "both crash");
  357. record.setParameters(new Object[]{_playerOne.name, _playerTwo.name, 0, 0, 0, 0, pointDiff, getType().toString()});
  358. _logResults.log(record);
  359. }
  360. }
  361. _playerOne.updateStat(COMP_DONE, 1);
  362. _playerTwo.updateStat(COMP_DONE, 1);
  363. _playerOne.updateNobleStats();
  364. _playerTwo.updateNobleStats();
  365. return;
  366. }
  367. catch (Exception e)
  368. {
  369. _log.log(Level.WARNING, "Exception on validateWinner(): " + e.getMessage(), e);
  370. return;
  371. }
  372. }
  373. try
  374. {
  375. String winner = "draw";
  376. // Calculate Fight time
  377. long _fightTime = (System.currentTimeMillis() - _startTime);
  378. double playerOneHp = 0;
  379. if (_playerOne.player != null && !_playerOne.player.isDead())
  380. {
  381. playerOneHp = _playerOne.player.getCurrentHp() + _playerOne.player.getCurrentCp();
  382. if (playerOneHp < 0.5)
  383. playerOneHp = 0;
  384. }
  385. double playerTwoHp = 0;
  386. if (_playerTwo.player != null && !_playerTwo.player.isDead())
  387. {
  388. playerTwoHp = _playerTwo.player.getCurrentHp() + _playerTwo.player.getCurrentCp();
  389. if (playerTwoHp < 0.5)
  390. playerTwoHp = 0;
  391. }
  392. // if players crashed, search if they've relogged
  393. _playerOne.updatePlayer();
  394. _playerTwo.updatePlayer();
  395. if ((_playerOne.player == null || !_playerOne.player.isOnline())
  396. && (_playerTwo.player == null || !_playerTwo.player.isOnline()))
  397. {
  398. _playerOne.updateStat(COMP_DRAWN, 1);
  399. _playerTwo.updateStat(COMP_DRAWN, 1);
  400. sm = SystemMessage.getSystemMessage(SystemMessageId.THE_GAME_ENDED_IN_A_TIE);
  401. stadium.broadcastPacket(sm);
  402. }
  403. else if (_playerTwo.player == null
  404. || !_playerTwo.player.isOnline()
  405. || (playerTwoHp == 0 && playerOneHp != 0)
  406. || (_damageP1 > _damageP2 && playerTwoHp != 0 && playerOneHp != 0))
  407. {
  408. sm = SystemMessage.getSystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  409. sm.addString(_playerOne.name);
  410. stadium.broadcastPacket(sm);
  411. _playerOne.updateStat(COMP_WON, 1);
  412. _playerTwo.updateStat(COMP_LOST, 1);
  413. addPointsToParticipant(_playerOne, pointDiff);
  414. removePointsFromParticipant(_playerTwo, pointDiff);
  415. winner = _playerOne.name + " won";
  416. // Save Fight Result
  417. saveResults(_playerOne,_playerTwo,1,_startTime,_fightTime, getType());
  418. rewardParticipant(_playerOne.player, getReward());
  419. }
  420. else if (_playerOne.player == null
  421. || !_playerOne.player.isOnline()
  422. || (playerOneHp == 0 && playerTwoHp != 0)
  423. || (_damageP2 > _damageP1 && playerOneHp != 0 && playerTwoHp != 0))
  424. {
  425. sm = SystemMessage.getSystemMessage(SystemMessageId.C1_HAS_WON_THE_GAME);
  426. sm.addString(_playerTwo.name);
  427. stadium.broadcastPacket(sm);
  428. _playerTwo.updateStat(COMP_WON, 1);
  429. _playerOne.updateStat(COMP_LOST, 1);
  430. addPointsToParticipant(_playerTwo, pointDiff);
  431. removePointsFromParticipant(_playerOne, pointDiff);
  432. winner = _playerTwo.name + " won";
  433. // Save Fight Result
  434. saveResults(_playerOne,_playerTwo,2,_startTime,_fightTime,getType());
  435. rewardParticipant(_playerTwo.player, getReward());
  436. }
  437. else
  438. {
  439. // Save Fight Result
  440. saveResults(_playerOne,_playerTwo,0,_startTime,_fightTime, getType());
  441. sm = SystemMessage.getSystemMessage(SystemMessageId.THE_GAME_ENDED_IN_A_TIE);
  442. stadium.broadcastPacket(sm);
  443. removePointsFromParticipant(_playerOne, Math.min(playerOnePoints / getDivider(), Config.ALT_OLY_MAX_POINTS));
  444. removePointsFromParticipant(_playerTwo, Math.min(playerTwoPoints / getDivider(), Config.ALT_OLY_MAX_POINTS));
  445. }
  446. _playerOne.updateStat(COMP_DONE, 1);
  447. _playerTwo.updateStat(COMP_DONE, 1);
  448. _playerOne.updateNobleStats();
  449. _playerTwo.updateNobleStats();
  450. if (Config.ALT_OLY_LOG_FIGHTS)
  451. {
  452. LogRecord record = new LogRecord(Level.INFO, winner);
  453. record.setParameters(new Object[]{_playerOne.name, _playerTwo.name, playerOneHp, playerTwoHp, _damageP1, _damageP2, pointDiff, getType().toString()});
  454. _logResults.log(record);
  455. }
  456. }
  457. catch (Exception e)
  458. {
  459. _log.log(Level.WARNING, "Exception on validateWinner(): " + e.getMessage(), e);
  460. }
  461. }
  462. @Override
  463. protected final void addDamage(L2PcInstance player, int damage)
  464. {
  465. if (_playerOne.player == null || _playerTwo.player == null)
  466. return;
  467. if (player == _playerOne.player)
  468. _damageP1 += damage;
  469. else if (player == _playerTwo.player)
  470. _damageP2 += damage;
  471. }
  472. @Override
  473. public final String[] getPlayerNames()
  474. {
  475. return new String[] {_playerOne.name, _playerTwo.name};
  476. }
  477. @Override
  478. public final boolean checkDefaulted()
  479. {
  480. SystemMessage reason;
  481. _playerOne.updatePlayer();
  482. _playerTwo.updatePlayer();
  483. reason = AbstractOlympiadGame.checkDefaulted(_playerOne.player);
  484. if (reason != null)
  485. {
  486. _playerOne.defaulted = true;
  487. if (_playerTwo.player != null)
  488. _playerTwo.player.sendPacket(reason);
  489. }
  490. reason = AbstractOlympiadGame.checkDefaulted(_playerTwo.player);
  491. if (reason != null)
  492. {
  493. _playerTwo.defaulted = true;
  494. if (_playerOne.player != null)
  495. _playerOne.player.sendPacket(reason);
  496. }
  497. return _playerOne.defaulted || _playerTwo.defaulted;
  498. }
  499. @Override
  500. public final void resetDamage()
  501. {
  502. _damageP1 = 0;
  503. _damageP2 = 0;
  504. }
  505. protected static final void saveResults(Participant one, Participant two, int _winner, long _startTime, long _fightTime, CompetitionType type)
  506. {
  507. Connection con = null;
  508. try
  509. {
  510. con = L2DatabaseFactory.getInstance().getConnection();
  511. PreparedStatement statement = con.prepareStatement("INSERT INTO olympiad_fights (charOneId, charTwoId, charOneClass, charTwoClass, winner, start, time, classed) values(?,?,?,?,?,?,?,?)");
  512. statement.setInt(1, one.objectId);
  513. statement.setInt(2, two.objectId);
  514. statement.setInt(3, one.baseClass);
  515. statement.setInt(4, two.baseClass);
  516. statement.setInt(5, _winner);
  517. statement.setLong(6, _startTime);
  518. statement.setLong(7, _fightTime);
  519. statement.setInt(8, (type == CompetitionType.CLASSED ? 1 : 0));
  520. statement.execute();
  521. statement.close();
  522. }
  523. catch (SQLException e)
  524. {
  525. if(_log.isLoggable(Level.SEVERE))
  526. _log.log(Level.SEVERE, "SQL exception while saving olympiad fight.", e);
  527. }
  528. finally
  529. {
  530. L2DatabaseFactory.close(con);
  531. }
  532. }
  533. }