Guest User

Untitled

a guest
May 10th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 38.63 KB | None | 0 0
  1. /*
  2. This file is part of the OdinMS Maple Story Server
  3. Copyright (C) 2008 Patrick Huy <[email protected]>
  4. Matthias Butz <[email protected]>
  5. Jan Christian Meyer <[email protected]>
  6.  
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU Affero General Public License as
  9. published by the Free Software Foundation version 3 as published by
  10. the Free Software Foundation. You may not use, modify or distribute
  11. this program under any other version of the GNU Affero General Public
  12. License.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU Affero General Public License for more details.
  18.  
  19. You should have received a copy of the GNU Affero General Public License
  20. along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. package client;
  23.  
  24. import gm.server.GMServer;
  25.  
  26. import java.io.UnsupportedEncodingException;
  27. import java.security.MessageDigest;
  28. import java.security.NoSuchAlgorithmException;
  29. import java.sql.Connection;
  30. import java.sql.PreparedStatement;
  31. import java.sql.ResultSet;
  32. import java.sql.SQLException;
  33. import java.util.ArrayList;
  34. import java.util.Arrays;
  35. import java.util.Calendar;
  36. import java.util.Collections;
  37. import java.util.HashMap;
  38. import java.util.HashSet;
  39. import java.util.Iterator;
  40. import java.util.LinkedList;
  41. import java.util.List;
  42. import java.util.Map;
  43. import java.util.Set;
  44. import java.util.concurrent.ScheduledFuture;
  45. import java.util.concurrent.locks.Lock;
  46. import java.util.concurrent.locks.ReentrantLock;
  47.  
  48. import javax.script.ScriptEngine;
  49.  
  50. import net.server.Server;
  51. import net.server.channel.Channel;
  52. import net.server.guild.MapleGuildCharacter;
  53. import net.server.world.MapleMessengerCharacter;
  54. import net.server.world.MapleParty;
  55. import net.server.world.MaplePartyCharacter;
  56. import net.server.world.PartyOperation;
  57. import net.server.world.World;
  58.  
  59. import org.apache.mina.core.session.IoSession;
  60.  
  61. import scripting.npc.NPCConversationManager;
  62. import scripting.npc.NPCScriptManager;
  63. import scripting.quest.QuestActionManager;
  64. import scripting.quest.QuestScriptManager;
  65. import server.MapleMiniGame;
  66. import server.MaplePlayerShop;
  67. import server.MapleTrade;
  68. import server.TimerManager;
  69. import server.maps.HiredMerchant;
  70. import server.maps.MapleMap;
  71. import tools.DatabaseConnection;
  72. import tools.FilePrinter;
  73. import tools.HexTool;
  74. import tools.MapleAESOFB;
  75. import tools.MaplePacketCreator;
  76.  
  77. public class MapleClient {
  78.  
  79. public static final int LOGIN_NOTLOGGEDIN = 0;
  80. public static final int LOGIN_SERVER_TRANSITION = 1;
  81. public static final int LOGIN_LOGGEDIN = 2;
  82. public static final String CLIENT_KEY = "CLIENT";
  83. private final MapleAESOFB send;
  84. private final MapleAESOFB receive;
  85. private final IoSession session;
  86. private MapleCharacter player;
  87. private int channel = 1;
  88. private int accId = 1;
  89. private boolean loggedIn = false;
  90. private boolean serverTransition = false;
  91. private Calendar birthday = null;
  92. private String accountName = null;
  93. private int world;
  94. private long lastPong;
  95. private int gmlevel;
  96. private final Set<String> macs = new HashSet<>();
  97. private final Map<String, ScriptEngine> engines = new HashMap<>();
  98. private ScheduledFuture<?> idleTask = null;
  99. private byte characterSlots = 3;
  100. private byte loginattempt = 0;
  101. private String pin = null;
  102. private int pinattempt = 0;
  103. private String pic = null;
  104. private int picattempt = 0;
  105. private byte gender = -1;
  106. private boolean disconnecting = false;
  107. private final Lock mutex = new ReentrantLock(true);
  108. private boolean message;
  109. private int votingRecords = 0;
  110. private int donatorPoints = 0;
  111.  
  112. public MapleClient(MapleAESOFB send, MapleAESOFB receive, IoSession session) {
  113. this.send = send;
  114. this.receive = receive;
  115. this.session = session;
  116. }
  117.  
  118. public synchronized MapleAESOFB getReceiveCrypto() {
  119. return this.receive;
  120. }
  121.  
  122. public synchronized MapleAESOFB getSendCrypto() {
  123. return this.send;
  124. }
  125.  
  126. public synchronized IoSession getSession() {
  127. return this.session;
  128. }
  129.  
  130. public MapleCharacter getPlayer() {
  131. return this.player;
  132. }
  133.  
  134. public void setPlayer(MapleCharacter player) {
  135. this.player = player;
  136. }
  137.  
  138. public void sendCharList(int server) {
  139. this.session.write(MaplePacketCreator.getCharList(this, server));
  140. }
  141.  
  142. public List<MapleCharacter> loadCharacters(int serverId) {
  143. final List<MapleCharacter> chars = new ArrayList<>(15);
  144. try {
  145. for (final CharNameAndId cni : this
  146. .loadCharactersInternal(serverId)) {
  147. chars.add(MapleCharacter.loadCharFromDB(cni.id, this, false));
  148. }
  149. } catch (final Exception e) {
  150. }
  151. return chars;
  152. }
  153.  
  154. public List<String> loadCharacterNames(int serverId) {
  155. final List<String> chars = new ArrayList<>(15);
  156. for (final CharNameAndId cni : this.loadCharactersInternal(serverId)) {
  157. chars.add(cni.name);
  158. }
  159. return chars;
  160. }
  161.  
  162. private List<CharNameAndId> loadCharactersInternal(int serverId) {
  163. PreparedStatement ps;
  164. final List<CharNameAndId> chars = new ArrayList<>(15);
  165. try {
  166. ps = DatabaseConnection
  167. .getConnection()
  168. .prepareStatement(
  169. "SELECT id, name FROM characters WHERE accountid = ? AND world = ?");
  170. ps.setInt(1, this.getAccID());
  171. ps.setInt(2, serverId);
  172. try (ResultSet rs = ps.executeQuery()) {
  173. while (rs.next()) {
  174. chars.add(new CharNameAndId(rs.getString("name"), rs
  175. .getInt("id")));
  176. }
  177. }
  178. ps.close();
  179. } catch (final SQLException e) {
  180. e.printStackTrace();
  181. }
  182. return chars;
  183. }
  184.  
  185. public boolean isLoggedIn() {
  186. return this.loggedIn;
  187. }
  188.  
  189. public boolean messageOn() {
  190. PreparedStatement ps;
  191. try {
  192. ps = DatabaseConnection.getConnection().prepareStatement("SELECT message FROM accounts WHERE id = ?");
  193. ps.setInt(1, this.getAccID());
  194. ResultSet rs = ps.executeQuery();
  195. while (rs.next()) {
  196. if (rs.getInt("message") == 0) {
  197. message = false;
  198. } else {
  199. message = true;
  200. }
  201. }
  202. rs.close();
  203. ps.close();
  204. } catch (Exception e) {
  205. System.out.println("message error");
  206. }
  207. return message;
  208. }
  209.  
  210. public boolean hasBannedIP() {
  211. boolean ret = false;
  212. try {
  213. try (PreparedStatement ps = DatabaseConnection
  214. .getConnection()
  215. .prepareStatement(
  216. "SELECT COUNT(*) FROM ipbans WHERE ? LIKE CONCAT(ip, '%')")) {
  217. ps.setString(1, this.session.getRemoteAddress().toString());
  218. try (ResultSet rs = ps.executeQuery()) {
  219. rs.next();
  220. if (rs.getInt(1) > 0) {
  221. ret = true;
  222. }
  223. }
  224. }
  225. } catch (final SQLException e) {
  226. }
  227. return ret;
  228. }
  229.  
  230. public boolean hasBannedMac() {
  231. if (this.macs.isEmpty()) {
  232. return false;
  233. }
  234. boolean ret = false;
  235. int i;
  236. try {
  237. final StringBuilder sql = new StringBuilder(
  238. "SELECT COUNT(*) FROM macbans WHERE mac IN (");
  239. for (i = 0; i < this.macs.size(); i++) {
  240. sql.append("?");
  241. if (i != (this.macs.size() - 1)) {
  242. sql.append(", ");
  243. }
  244. }
  245. sql.append(")");
  246. try (PreparedStatement ps = DatabaseConnection.getConnection()
  247. .prepareStatement(sql.toString())) {
  248. i = 0;
  249. for (final String mac : this.macs) {
  250. i++;
  251. ps.setString(i, mac);
  252. }
  253. try (ResultSet rs = ps.executeQuery()) {
  254. rs.next();
  255. if (rs.getInt(1) > 0) {
  256. ret = true;
  257. }
  258. }
  259. }
  260. } catch (final Exception e) {
  261. }
  262. return ret;
  263. }
  264.  
  265. private void loadMacsIfNescessary() throws SQLException {
  266. if (this.macs.isEmpty()) {
  267. try (PreparedStatement ps = DatabaseConnection.getConnection()
  268. .prepareStatement("SELECT macs FROM accounts WHERE id = ?")) {
  269. ps.setInt(1, this.accId);
  270. try (ResultSet rs = ps.executeQuery()) {
  271. if (rs.next()) {
  272. for (final String mac : rs.getString("macs")
  273. .split(", ")) {
  274. if (!mac.equals("")) {
  275. this.macs.add(mac);
  276. }
  277. }
  278. }
  279. }
  280. }
  281. }
  282. }
  283.  
  284. public void banMacs() {
  285. final Connection con = DatabaseConnection.getConnection();
  286. try {
  287. this.loadMacsIfNescessary();
  288. final List<String> filtered = new LinkedList<>();
  289. try (PreparedStatement ps = con
  290. .prepareStatement("SELECT filter FROM macfilters");
  291. ResultSet rs = ps.executeQuery()) {
  292. while (rs.next()) {
  293. filtered.add(rs.getString("filter"));
  294. }
  295. }
  296. try (PreparedStatement ps = con
  297. .prepareStatement("INSERT INTO macbans (mac) VALUES (?)")) {
  298. for (final String mac : this.macs) {
  299. boolean matched = false;
  300. for (final String filter : filtered) {
  301. if (mac.matches(filter)) {
  302. matched = true;
  303. break;
  304. }
  305. }
  306. if (!matched) {
  307. ps.setString(1, mac);
  308. ps.executeUpdate();
  309. }
  310. }
  311. }
  312. } catch (final SQLException e) {
  313. }
  314. }
  315.  
  316. public int finishLogin() {
  317. synchronized (MapleClient.class) {
  318. if (this.getLoginState() > LOGIN_NOTLOGGEDIN) {
  319. this.loggedIn = false;
  320. return 7;
  321. }
  322. this.updateLoginState(LOGIN_LOGGEDIN);
  323. }
  324. return 0;
  325. }
  326.  
  327. public void setPin(String pin) {
  328. this.pin = pin;
  329. try {
  330. try (PreparedStatement ps = DatabaseConnection.getConnection()
  331. .prepareStatement(
  332. "UPDATE accounts SET pin = ? WHERE id = ?")) {
  333. ps.setString(1, pin);
  334. ps.setInt(2, this.accId);
  335. ps.executeUpdate();
  336. }
  337. } catch (final SQLException e) {
  338. }
  339. }
  340.  
  341. public String getPin() {
  342. return this.pin;
  343. }
  344.  
  345. public boolean checkPin(String other) {
  346. this.pinattempt++;
  347. if (this.pinattempt > 5) {
  348. this.getSession().close(true);
  349. }
  350. if (this.pin.equals(other)) {
  351. this.pinattempt = 0;
  352. return true;
  353. }
  354. return false;
  355. }
  356.  
  357. public void setPic(String pic) {
  358. this.pic = pic;
  359. try {
  360. try (PreparedStatement ps = DatabaseConnection.getConnection()
  361. .prepareStatement(
  362. "UPDATE accounts SET pic = ? WHERE id = ?")) {
  363. ps.setString(1, pic);
  364. ps.setInt(2, this.accId);
  365. ps.executeUpdate();
  366. }
  367. } catch (final SQLException e) {
  368. }
  369. }
  370.  
  371. public String getPic() {
  372. return this.pic;
  373. }
  374.  
  375. public boolean checkPic(String other) {
  376. this.picattempt++;
  377. if (this.picattempt > 5) {
  378. this.getSession().close(true);
  379. }
  380. if (this.pic.equals(other)) {
  381. this.picattempt = 0;
  382. return true;
  383. }
  384. return false;
  385. }
  386.  
  387. public int login(String login, String pwd) {
  388. this.loginattempt++;
  389. if (this.loginattempt > 4) {
  390. this.getSession().close(true);
  391. }
  392. int loginok = 5;
  393. final Connection con = DatabaseConnection.getConnection();
  394. PreparedStatement ps = null;
  395. ResultSet rs = null;
  396. try {
  397. ps = con.prepareStatement("SELECT id, password, salt, gender, banned, gm, pin, pic, characterslots, votingrecords, donatorpoints, tos FROM accounts WHERE name = ?");
  398. ps.setString(1, login);
  399. rs = ps.executeQuery();
  400. if (rs.next()) {
  401. if (rs.getByte("banned") == 1) {
  402. return 3;
  403. }
  404. this.accId = rs.getInt("id");
  405. this.gmlevel = rs.getInt("gm");
  406. this.pin = rs.getString("pin");
  407. this.pic = rs.getString("pic");
  408. this.gender = rs.getByte("gender");
  409. this.characterSlots = rs.getByte("characterslots");
  410. final String passhash = rs.getString("password");
  411. final String salt = rs.getString("salt");
  412. this.votingRecords = rs.getInt("votingrecords");
  413. this.donatorPoints = rs.getInt("donatorpoints");
  414.  
  415. // we do not unban
  416. final byte tos = rs.getByte("tos");
  417. ps.close();
  418. rs.close();
  419. if (this.getLoginState() > LOGIN_NOTLOGGEDIN) { // already
  420. // loggedin
  421. this.loggedIn = false;
  422. loginok = 7;
  423. } else if (pwd.equals(passhash)
  424. || checkHash(passhash, "SHA-1", pwd)
  425. || checkHash(passhash, "SHA-512", pwd + salt)) {
  426. if (tos == 0) {
  427. loginok = 23;
  428. } else {
  429. loginok = 0;
  430. }
  431. } else {
  432. this.loggedIn = false;
  433. loginok = 4;
  434. }
  435.  
  436. ps = con.prepareStatement("INSERT INTO iplog (accountid, ip) VALUES (?, ?)");
  437. ps.setInt(1, this.accId);
  438. ps.setString(2, this.session.getRemoteAddress().toString());
  439. ps.executeUpdate();
  440. }
  441. } catch (final SQLException e) {
  442. e.printStackTrace();
  443. } finally {
  444. try {
  445. if ((ps != null) && !ps.isClosed()) {
  446. ps.close();
  447. }
  448. if ((rs != null) && !rs.isClosed()) {
  449. rs.close();
  450. }
  451. } catch (final SQLException e) {
  452. }
  453. }
  454.  
  455. if (loginok == 0) {
  456. this.loginattempt = 0;
  457. }
  458. return loginok;
  459. }
  460.  
  461. public Calendar getTempBanCalendar() {
  462. final Connection con = DatabaseConnection.getConnection();
  463. PreparedStatement ps = null;
  464. ResultSet rs = null;
  465. final Calendar lTempban = Calendar.getInstance();
  466. try {
  467. ps = con.prepareStatement("SELECT `tempban` FROM accounts WHERE id = ?");
  468. ps.setInt(1, this.getAccID());
  469. rs = ps.executeQuery();
  470. if (!rs.next()) {
  471. return null;
  472. }
  473. final long blubb = rs.getLong("tempban");
  474. if (blubb == 0) { // basically if timestamp in db is 0000-00-00
  475. return null;
  476. }
  477. lTempban.setTimeInMillis(rs.getTimestamp("tempban").getTime());
  478. return lTempban;
  479. } catch (final SQLException e) {
  480. } finally {
  481. try {
  482. if (ps != null) {
  483. ps.close();
  484. }
  485. if (rs != null) {
  486. rs.close();
  487. }
  488. } catch (final SQLException e) {
  489. }
  490. }
  491. return null;// why oh why!?!
  492. }
  493.  
  494. public static long dottedQuadToLong(String dottedQuad)
  495. throws RuntimeException {
  496. final String[] quads = dottedQuad.split("\\.");
  497. if (quads.length != 4) {
  498. throw new RuntimeException("Invalid IP Address format.");
  499. }
  500. long ipAddress = 0;
  501. for (int i = 0; i < 4; i++) {
  502. final int quad = Integer.parseInt(quads[i]);
  503. ipAddress += (quad % 256) * (long) Math.pow(256, 4 - i);
  504. }
  505. return ipAddress;
  506. }
  507.  
  508. public void updateMacs(String macData) {
  509. this.macs.addAll(Arrays.asList(macData.split(", ")));
  510. final StringBuilder newMacData = new StringBuilder();
  511. final Iterator<String> iter = this.macs.iterator();
  512. PreparedStatement ps = null;
  513. while (iter.hasNext()) {
  514. final String cur = iter.next();
  515. newMacData.append(cur);
  516. if (iter.hasNext()) {
  517. newMacData.append(", ");
  518. }
  519. }
  520. try {
  521. ps = DatabaseConnection.getConnection().prepareStatement(
  522. "UPDATE accounts SET macs = ?, votingrecords = ?, donatorpoints = ? WHERE id = ?");
  523. ps.setString(1, newMacData.toString());
  524. ps.setInt(2, this.votingRecords);
  525. ps.setInt(3, this.donatorPoints);
  526. ps.setInt(4, this.accId);
  527. ps.executeUpdate();
  528. ps.close();
  529. } catch (final SQLException e) {
  530. e.printStackTrace();
  531. } finally {
  532. try {
  533. if ((ps != null) && !ps.isClosed()) {
  534. ps.close();
  535. }
  536. } catch (final SQLException ex) {
  537. }
  538. }
  539. }
  540.  
  541. public void setAccID(int id) {
  542. this.accId = id;
  543. }
  544.  
  545. public int getAccID() {
  546. return this.accId;
  547. }
  548.  
  549. public void updateLoginState(int newstate) {
  550. try {
  551. final Connection con = DatabaseConnection.getConnection();
  552. try (PreparedStatement ps = con
  553. .prepareStatement("UPDATE accounts SET loggedin = ?, lastlogin = CURRENT_TIMESTAMP() WHERE id = ?")) {
  554. ps.setInt(1, newstate);
  555. ps.setInt(2, this.getAccID());
  556. ps.executeUpdate();
  557. }
  558. } catch (final SQLException e) {
  559. e.printStackTrace();
  560. }
  561. if (newstate == LOGIN_NOTLOGGEDIN) {
  562. this.loggedIn = false;
  563. this.serverTransition = false;
  564. } else {
  565. this.serverTransition = (newstate == LOGIN_SERVER_TRANSITION);
  566. this.loggedIn = !this.serverTransition;
  567. }
  568. }
  569.  
  570. public int getLoginState() {
  571. try {
  572. final Connection con = DatabaseConnection.getConnection();
  573. PreparedStatement ps = con
  574. .prepareStatement("SELECT loggedin, lastlogin, UNIX_TIMESTAMP(birthday) as birthday FROM accounts WHERE id = ?");
  575. ps.setInt(1, this.getAccID());
  576. final ResultSet rs = ps.executeQuery();
  577. if (!rs.next()) {
  578. rs.close();
  579. ps.close();
  580. throw new RuntimeException("getLoginState - MapleClient");
  581. }
  582. this.birthday = Calendar.getInstance();
  583. final long blubb = rs.getLong("birthday");
  584. if (blubb > 0) {
  585. this.birthday.setTimeInMillis(blubb * 1000);
  586. }
  587. int state = rs.getInt("loggedin");
  588. if (state == LOGIN_SERVER_TRANSITION) {
  589. if ((rs.getTimestamp("lastlogin").getTime() + 30000) < System
  590. .currentTimeMillis()) {
  591. state = LOGIN_NOTLOGGEDIN;
  592. this.updateLoginState(LOGIN_NOTLOGGEDIN);
  593. }
  594. }
  595. rs.close();
  596. ps.close();
  597. if (state == LOGIN_LOGGEDIN) {
  598. this.loggedIn = true;
  599. } else if (state == LOGIN_SERVER_TRANSITION) {
  600. ps = con.prepareStatement("UPDATE accounts SET loggedin = 0 WHERE id = ?");
  601. ps.setInt(1, this.getAccID());
  602. ps.executeUpdate();
  603. ps.close();
  604. } else {
  605. this.loggedIn = false;
  606. }
  607. return state;
  608. } catch (final SQLException e) {
  609. this.loggedIn = false;
  610. e.printStackTrace();
  611. throw new RuntimeException("login state");
  612. }
  613. }
  614.  
  615. public boolean checkBirthDate(Calendar date) {
  616. return (date.get(Calendar.YEAR) == this.birthday.get(Calendar.YEAR))
  617. && (date.get(Calendar.MONTH) == this.birthday
  618. .get(Calendar.MONTH))
  619. && (date.get(Calendar.DAY_OF_MONTH) == this.birthday
  620. .get(Calendar.DAY_OF_MONTH));
  621. }
  622.  
  623. private void removePlayer() {
  624. try {
  625. this.player.cancelAllBuffs(true);
  626. this.player.cancelAllDebuffs();
  627. final MaplePlayerShop mps = this.player.getPlayerShop();
  628. if (mps != null) {
  629. mps.removeVisitors();
  630. this.player.setPlayerShop(null);
  631. }
  632. final HiredMerchant merchant = this.player.getHiredMerchant();
  633. if (merchant != null) {
  634. if (merchant.isOwner(this.player)) {
  635. merchant.setOpen(true);
  636. } else {
  637. merchant.removeVisitor(this.player);
  638. }
  639. try {
  640. merchant.saveItems(false);
  641. } catch (final SQLException ex) {
  642. System.out
  643. .println("Error while saving Hired Merchant items.");
  644. }
  645. }
  646. this.player.setMessenger(null);
  647. final MapleMiniGame game = this.player.getMiniGame();
  648. if (game != null) {
  649. this.player.setMiniGame(null);
  650. if (game.isOwner(this.player)) {
  651. this.player.getMap().broadcastMessage(
  652. MaplePacketCreator.removeCharBox(this.player));
  653. game.broadcastToVisitor(MaplePacketCreator
  654. .getMiniGameClose());
  655. } else {
  656. game.removeVisitor(this.player);
  657. }
  658. }
  659. NPCScriptManager.getInstance().dispose(this);
  660. QuestScriptManager.getInstance().dispose(this);
  661. if (this.player.getTrade() != null) {
  662. MapleTrade.cancelTrade(this.player);
  663. }
  664. if (this.gmlevel > 0) {
  665. GMServer.removeInGame(this.player.getName());
  666. }
  667. if (this.player.getEventInstance() != null) {
  668. this.player.getEventInstance().playerDisconnected(this.player);
  669. }
  670. if (this.player.getMap() != null) {
  671. this.player.getMap().removePlayer(this.player);
  672. }
  673. } catch (final Throwable t) {
  674. FilePrinter.printError(FilePrinter.ACCOUNT_STUCK, t);
  675. }
  676. }
  677.  
  678. public final void disconnect(boolean shutdown, boolean cashshop) {// once
  679. // per
  680. // MapleClient
  681. // instance
  682. if (this.disconnecting) {
  683. return;
  684. }
  685. this.disconnecting = true;
  686. if ((this.player != null) && this.player.isLoggedin()
  687. && (this.player.getClient() != null)) {
  688. final MapleMap map = this.player.getMap();
  689. final MapleParty party = this.player.getParty();
  690. final int idz = this.player.getId(), messengerid = this.player
  691. .getMessenger() == null ? 0 : this.player.getMessenger()
  692. .getId();
  693. this.player.getFamilyId();
  694. this.player.getName();
  695. final BuddyList bl = this.player.getBuddylist();
  696. final MaplePartyCharacter chrp = new MaplePartyCharacter(
  697. this.player);
  698. final MapleMessengerCharacter chrm = new MapleMessengerCharacter(
  699. this.player);
  700. // final MapleGuildCharacter chrg = player.getMGC();
  701.  
  702. this.removePlayer();
  703. this.player.saveToDB();
  704. if ((this.channel == -1) || shutdown) {
  705. this.player = null;
  706. return;
  707. }
  708. final World worlda = this.getWorldServer();
  709. try {
  710. if (!cashshop) {
  711. if (messengerid > 0) {
  712. worlda.leaveMessenger(messengerid, chrm);
  713. }
  714.  
  715. /*
  716. * for (MapleQuestStatus status : player.getStartedQuests())
  717. * { MapleQuest quest = status.getQuest(); if
  718. * (quest.getTimeLimit() > 0) { MapleQuestStatus newStatus =
  719. * new MapleQuestStatus(quest,
  720. * MapleQuestStatus.Status.NOT_STARTED);
  721. * newStatus.setForfeited
  722. * (player.getQuest(quest).getForfeited() + 1);
  723. * player.updateQuest(newStatus); } }
  724. */
  725. if (party != null) {
  726. chrp.setOnline(false);
  727. worlda.updateParty(party.getId(),
  728. PartyOperation.LOG_ONOFF, chrp);
  729. if ((map != null) && (party.getLeader().getId() == idz)) {
  730. MaplePartyCharacter lchr = null;
  731. for (final MaplePartyCharacter pchr : party
  732. .getMembers()) {
  733. if ((pchr != null)
  734. && (map.getCharacterById(pchr.getId()) != null)
  735. && ((lchr == null) || (lchr.getLevel() < pchr
  736. .getLevel()))) {
  737. lchr = pchr;
  738. }
  739. }
  740. if (lchr != null) {
  741. worlda.updateParty(party.getId(),
  742. PartyOperation.CHANGE_LEADER, lchr);
  743. }
  744. }
  745. }
  746. if (bl != null) {
  747. if (!this.serverTransition) {
  748. worlda.loggedOff(this.player.getName(),
  749. this.player.getId(), this.channel,
  750. this.player.getBuddylist().getBuddyIds());
  751. } else {
  752. worlda.loggedOn(this.player.getName(),
  753. this.player.getId(), this.channel,
  754. this.player.getBuddylist().getBuddyIds());
  755. }
  756. }
  757. } else {
  758. if (party != null) {
  759. chrp.setOnline(false);
  760. worlda.updateParty(party.getId(),
  761. PartyOperation.LOG_ONOFF, chrp);
  762. }
  763. if (!this.serverTransition) {
  764. worlda.loggedOff(this.player.getName(), this.player
  765. .getId(), this.channel, this.player
  766. .getBuddylist().getBuddyIds());
  767. } else {
  768. worlda.loggedOn(this.player.getName(), this.player
  769. .getId(), this.channel, this.player
  770. .getBuddylist().getBuddyIds());
  771. }
  772. }
  773. } catch (final Exception e) {
  774. FilePrinter.printError(FilePrinter.ACCOUNT_STUCK, e);
  775. } finally {
  776. this.getChannelServer().removePlayer(this.player);
  777. if (!this.serverTransition) {
  778. worlda.removePlayer(this.player);
  779. if (this.player != null) {// no idea, occur :(
  780. this.player.empty(false);
  781. }
  782. this.player.logOff();
  783. }
  784. this.player = null;
  785. }
  786. }
  787. if (!this.serverTransition && this.isLoggedIn()) {
  788. this.updateLoginState(MapleClient.LOGIN_NOTLOGGEDIN);
  789. this.session.removeAttribute(MapleClient.CLIENT_KEY); // prevents
  790. // double
  791. // dcing
  792. // during
  793. // login
  794. this.session.close();
  795. }
  796. this.engines.clear();
  797. }
  798.  
  799. public int getChannel() {
  800. return this.channel;
  801. }
  802.  
  803. public Channel getChannelServer() {
  804. return Server.getInstance().getChannel(this.world, this.channel);
  805. }
  806.  
  807. public World getWorldServer() {
  808. return Server.getInstance().getWorld(this.world);
  809. }
  810.  
  811. public Channel getChannelServer(byte channel) {
  812. return Server.getInstance().getChannel(this.world, channel);
  813. }
  814.  
  815. public boolean deleteCharacter(int cid) {
  816. final Connection con = DatabaseConnection.getConnection();
  817. try {
  818. try (PreparedStatement ps = con
  819. .prepareStatement("SELECT id, guildid, guildrank, name, allianceRank FROM characters WHERE id = ? AND accountid = ?")) {
  820. ps.setInt(1, cid);
  821. ps.setInt(2, this.accId);
  822. try (ResultSet rs = ps.executeQuery()) {
  823. if (!rs.next()) {
  824. return false;
  825. }
  826. if (rs.getInt("guildid") > 0) {
  827. try {
  828. Server.getInstance().deleteGuildCharacter(
  829. new MapleGuildCharacter(cid, 0, rs
  830. .getString("name"), (byte) -1,
  831. (byte) -1, 0, rs
  832. .getInt("guildrank"), rs
  833. .getInt("guildid"), false,
  834. rs.getInt("allianceRank")));
  835. } catch (final Exception re) {
  836. return false;
  837. }
  838. }
  839. }
  840. }
  841. try (PreparedStatement ps = con
  842. .prepareStatement("DELETE FROM wishlists WHERE charid = ?")) {
  843. ps.setInt(1, cid);
  844. ps.executeUpdate();
  845. }
  846. try (PreparedStatement ps = con
  847. .prepareStatement("DELETE FROM characters WHERE id = ?")) {
  848. ps.setInt(1, cid);
  849. ps.executeUpdate();
  850. }
  851. final String[] toDel = {"famelog", "inventoryitems", "keymap",
  852. "queststatus", "savedlocations", "skillmacros", "skills",
  853. "eventstats"};
  854. for (final String s : toDel) {
  855. MapleCharacter.deleteWhereCharacterId(con, "DELETE FROM `" + s
  856. + "` WHERE characterid = ?", cid);
  857. }
  858. return true;
  859. } catch (final SQLException e) {
  860. e.printStackTrace();
  861. return false;
  862. }
  863. }
  864.  
  865. public String getAccountName() {
  866. return this.accountName;
  867. }
  868.  
  869. public void gaindonatorPoints(int amt) {
  870. donatorPoints = donatorPoints + amt;
  871. }
  872.  
  873. public int getdonatorPoints() {
  874. return donatorPoints;
  875. }
  876.  
  877. public int getvotingRecords() {
  878. return votingRecords;
  879. }
  880.  
  881. public void gainvotingRecords(int amt) {
  882. votingRecords = votingRecords + amt;
  883. }
  884.  
  885. public void removedonatorPoints(int amt) {
  886. donatorPoints = donatorPoints - amt;
  887. }
  888.  
  889. public void setdonatorPoints(int amt) {
  890. donatorPoints = amt;
  891. }
  892.  
  893. public void setvotingRecords(int amt) {
  894. votingRecords = amt;
  895. }
  896.  
  897. public void setAccountName(String a) {
  898. this.accountName = a;
  899. }
  900.  
  901. public void setChannel(int channel) {
  902. this.channel = channel;
  903. }
  904.  
  905. public int getWorld() {
  906. return this.world;
  907. }
  908.  
  909. public void setWorld(int world) {
  910. this.world = world;
  911. }
  912.  
  913. public void pongReceived() {
  914. this.lastPong = System.currentTimeMillis();
  915. }
  916.  
  917. public void sendPing() {
  918. final long then = System.currentTimeMillis();
  919. this.announce(MaplePacketCreator.getPing());
  920. TimerManager.getInstance().schedule(new Runnable() {
  921.  
  922. @Override
  923. public void run() {
  924. try {
  925. if (MapleClient.this.lastPong < then) {
  926. if ((MapleClient.this.getSession() != null)
  927. && MapleClient.this.getSession().isConnected()) {
  928. MapleClient.this.getSession().close(true);
  929. }
  930. }
  931. } catch (final NullPointerException e) {
  932. }
  933. }
  934. }, 15000);
  935. }
  936.  
  937. public Set<String> getMacs() {
  938. return Collections.unmodifiableSet(this.macs);
  939. }
  940.  
  941. public int gmLevel() {
  942. return this.gmlevel;
  943. }
  944.  
  945. public void setScriptEngine(String name, ScriptEngine e) {
  946. this.engines.put(name, e);
  947. }
  948.  
  949. public ScriptEngine getScriptEngine(String name) {
  950. return this.engines.get(name);
  951. }
  952.  
  953. public void removeScriptEngine(String name) {
  954. this.engines.remove(name);
  955. }
  956.  
  957. public ScheduledFuture<?> getIdleTask() {
  958. return this.idleTask;
  959. }
  960.  
  961. public void setIdleTask(ScheduledFuture<?> idleTask) {
  962. this.idleTask = idleTask;
  963. }
  964.  
  965. public NPCConversationManager getCM() {
  966. return NPCScriptManager.getInstance().getCM(this);
  967. }
  968.  
  969. public QuestActionManager getQM() {
  970. return QuestScriptManager.getInstance().getQM(this);
  971. }
  972.  
  973. public boolean acceptToS() {
  974. boolean disconnectForBeingAFaggot = false;
  975. if (this.accountName == null) {
  976. return true;
  977. }
  978. try {
  979. PreparedStatement ps = DatabaseConnection
  980. .getConnection()
  981. .prepareStatement("SELECT `tos` FROM accounts WHERE id = ?");
  982. ps.setInt(1, this.accId);
  983. final ResultSet rs = ps.executeQuery();
  984.  
  985. if (rs.next()) {
  986. if (rs.getByte("tos") == 1) {
  987. disconnectForBeingAFaggot = true;
  988. }
  989. }
  990. ps.close();
  991. rs.close();
  992. ps = DatabaseConnection.getConnection().prepareStatement(
  993. "UPDATE accounts SET tos = 1 WHERE id = ?");
  994. ps.setInt(1, this.accId);
  995. ps.executeUpdate();
  996. ps.close();
  997. } catch (final SQLException e) {
  998. }
  999. return disconnectForBeingAFaggot;
  1000. }
  1001.  
  1002. public final Lock getLock() {
  1003. return this.mutex;
  1004. }
  1005.  
  1006. private static class CharNameAndId {
  1007.  
  1008. public String name;
  1009. public int id;
  1010.  
  1011. public CharNameAndId(String name, int id) {
  1012. super();
  1013. this.name = name;
  1014. this.id = id;
  1015. }
  1016. }
  1017.  
  1018. public static boolean checkHash(String hash, String type, String password) {
  1019. try {
  1020. final MessageDigest digester = MessageDigest.getInstance(type);
  1021. digester.update(password.getBytes("UTF-8"), 0, password.length());
  1022. return HexTool.toString(digester.digest()).replace(" ", "")
  1023. .toLowerCase().equals(hash);
  1024. } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
  1025. throw new RuntimeException("Encoding the string failed", e);
  1026. }
  1027. }
  1028.  
  1029. public short getCharacterSlots() {
  1030. return this.characterSlots;
  1031. }
  1032.  
  1033. public boolean gainCharacterSlot() {
  1034. if (this.characterSlots < 15) {
  1035. final Connection con = DatabaseConnection.getConnection();
  1036. try {
  1037. try (PreparedStatement ps = con
  1038. .prepareStatement("UPDATE accounts SET characterslots = ? WHERE id = ?")) {
  1039. ps.setInt(1, this.characterSlots += 1);
  1040. ps.setInt(2, this.accId);
  1041. ps.executeUpdate();
  1042. }
  1043. } catch (final SQLException e) {
  1044. }
  1045. return true;
  1046. }
  1047. return false;
  1048. }
  1049.  
  1050. public final byte getGReason() {
  1051. final Connection con = DatabaseConnection.getConnection();
  1052. PreparedStatement ps = null;
  1053. ResultSet rs = null;
  1054. try {
  1055. ps = con.prepareStatement("SELECT `greason` FROM `accounts` WHERE id = ?");
  1056. ps.setInt(1, this.accId);
  1057. rs = ps.executeQuery();
  1058. if (rs.next()) {
  1059. return rs.getByte("greason");
  1060. }
  1061. } catch (final SQLException e) {
  1062. e.printStackTrace();
  1063. } finally {
  1064. try {
  1065. if (ps != null) {
  1066. ps.close();
  1067. }
  1068. if (rs != null) {
  1069. rs.close();
  1070. }
  1071. } catch (final SQLException e) {
  1072. }
  1073. }
  1074. return 0;
  1075. }
  1076.  
  1077. public byte getGender() {
  1078. return this.gender;
  1079. }
  1080.  
  1081. public void setGender(byte m) {
  1082. this.gender = m;
  1083. try {
  1084. try (PreparedStatement ps = DatabaseConnection.getConnection()
  1085. .prepareStatement(
  1086. "UPDATE accounts SET gender = ? WHERE id = ?")) {
  1087. ps.setByte(1, this.gender);
  1088. ps.setInt(2, this.accId);
  1089. ps.executeUpdate();
  1090. }
  1091. } catch (final SQLException e) {
  1092. }
  1093. }
  1094.  
  1095. public synchronized void announce(final byte[] packet) {// MINA CORE IS A
  1096. // FUCKING BITCH AND
  1097. // I HATE IT <3
  1098. this.session.write(packet);
  1099. }
  1100. }
Advertisement
Add Comment
Please, Sign In to add comment