RankingWorker.java
Upload User: gwt600
Upload Date: 2021-06-03
Package Size: 704k
Code Size: 3k
Category:

Games

Development Platform:

Java

  1. /*
  2. This file is part of the OdinMS Maple Story Server
  3.     Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc> 
  4.                        Matthias Butz <matze@odinms.de>
  5.                        Jan Christian Meyer <vimes@odinms.de>
  6.     This program is free software: you can redistribute it and/or modify
  7.     it under the terms of the GNU Affero General Public License version 3
  8.     as published by the Free Software Foundation. You may not use, modify
  9.     or distribute this program under any other version of the
  10.     GNU Affero General Public License.
  11.     This program 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
  14.     GNU Affero General Public License for more details.
  15.     You should have received a copy of the GNU Affero General Public License
  16.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. package net.sf.odinms.net.login;
  19. import java.sql.Connection;
  20. import java.sql.PreparedStatement;
  21. import java.sql.ResultSet;
  22. import java.sql.SQLException;
  23. import net.sf.odinms.client.MapleJob;
  24. import net.sf.odinms.database.DatabaseConnection;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. /**
  28.  *
  29.  * @author Matze
  30.  * @author Quit
  31.  */
  32. public class RankingWorker implements Runnable {
  33. private Connection con;
  34. private long lastUpdate = System.currentTimeMillis();
  35. private static Logger log = LoggerFactory.getLogger(RankingWorker.class);
  36. public void run() {
  37. try {
  38. con = DatabaseConnection.getConnection();
  39. con.setAutoCommit(false);
  40. updateRanking(null);
  41. updateRanking(MapleJob.BEGINNER);
  42. updateRanking(MapleJob.WARRIOR);
  43. updateRanking(MapleJob.MAGICIAN);
  44. updateRanking(MapleJob.BOWMAN);
  45. updateRanking(MapleJob.THIEF);
  46.                         updateRanking(MapleJob.PIRATE);
  47. con.commit();
  48. con.setAutoCommit(true);
  49. lastUpdate = System.currentTimeMillis();
  50. } catch (SQLException ex) {
  51. try {
  52. con.rollback();
  53. con.setAutoCommit(true);
  54. log.warn("Could not update rankings", ex);
  55. } catch (SQLException ex2) {
  56. log.error("Could not rollback unfinished ranking transaction", ex2);
  57. }
  58. }
  59. }
  60. private void updateRanking(MapleJob job) throws SQLException {
  61. String sqlCharSelect = "SELECT c.id, " + (job != null ? "c.jobRank, c.jobRankMove" : "c.rank, c.rankMove") + ", a.lastlogin AS lastlogin, a.loggedin FROM characters AS c LEFT JOIN accounts AS a ON c.accountid = a.id WHERE c.gm = 0 ";
  62. if (job != null) {
  63. sqlCharSelect += "AND c.job DIV 100 = ? ";
  64. }
  65. sqlCharSelect += "ORDER BY c.reborns DESC , c.level DESC , c.exp DESC , c.fame DESC , c.meso DESC";
  66. PreparedStatement charSelect = con.prepareStatement(sqlCharSelect);
  67. if (job != null) {
  68. charSelect.setInt(1, job.getId() / 100);
  69. }
  70. ResultSet rs = charSelect.executeQuery();
  71. PreparedStatement ps = con.prepareStatement("UPDATE characters SET " + (job != null ? "jobRank = ?, jobRankMove = ? " : "rank = ?, rankMove = ? ") + "WHERE id = ?");
  72. int rank = 0;
  73. while (rs.next()) {
  74. int rankMove = 0;
  75. rank++;
  76. if (rs.getLong("lastlogin") < lastUpdate || rs.getInt("loggedin") > 0) {
  77. rankMove = rs.getInt((job != null ? "jobRankMove" : "rankMove"));
  78. }
  79. rankMove += rs.getInt((job != null ? "jobRank" : "rank")) - rank;
  80. ps.setInt(1, rank);
  81. ps.setInt(2, rankMove);
  82. ps.setInt(3, rs.getInt("id"));
  83. ps.executeUpdate();
  84. }
  85. rs.close();
  86. charSelect.close();
  87. ps.close();
  88. }
  89. }