Advertisement
scotty92

Untitled

Oct 17th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.87 KB | None | 0 0
  1. package varekd.io;
  2.  
  3. import java.net.InetSocketAddress;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. import varekd.model.Inventory;
  13. import varekd.model.Point;
  14. import varekd.model.Player;
  15. import varekd.model.InvItem;
  16. import varekd.model.PlayerAppearance;
  17. import varekd.model.World;
  18. import varekd.util.Logger;
  19. import varekd.model.BankItem;
  20. import varekd.Server;
  21. import varekd.model.Bank;
  22.  
  23. /**
  24. * Coded at 1AM by a very pissed off varek.
  25. * So don't expect miracles.
  26. * WARNING: NOT THREAD SAFE.
  27. */
  28. public class DBPlayerLoader implements PlayerLoader {
  29. // Statements for saving/loading playerz.
  30. private PreparedStatement queryPlayer, queryInv, queryBank, querySettings, queryStat, queryList, updatePlayer, updateInv, updateBank, updateStat, clearList, saveList, updateAppearance, updateSettings, insertSuspicious;
  31. // This shit is whack. Blocks people who try and log in too fast.
  32. private Map<String, BadPassword> attempts = new HashMap<String, BadPassword>();
  33.  
  34. public DBPlayerLoader() throws SQLException, ClassNotFoundException {
  35. loadStatements();
  36. }
  37.  
  38. public void loadStatements() throws SQLException {
  39. Connection c = DBConnection.getConnection("playerpersistence");
  40. queryPlayer = c.prepareStatement("SELECT * FROM player INNER JOIN appearance ON appearance.playerid = player.id " +
  41. "WHERE player.username=? AND player.password=SHA1(?)");
  42. queryInv = c.prepareStatement("SELECT itemid, amount, wielded FROM invitem WHERE playerid=? " +
  43. "ORDER BY position");
  44. queryBank = c.prepareStatement("SELECT itemid, amount FROM bankitem WHERE playerid=? " +
  45. "ORDER BY position");
  46. querySettings = c.prepareStatement("SELECT * FROM setting WHERE playerid=? AND type=?");
  47. queryStat = c.prepareStatement("SELECT num, cur, exp FROM stat WHERE playerid=? " +
  48. "ORDER BY num");
  49. queryList = c.prepareStatement("SELECT playerhash, type FROM playerlist WHERE playerid=?");
  50. updatePlayer = c.prepareStatement("UPDATE player SET " +
  51. "x=?, y=?, fightmode=?, lastlogin=?, lastip=?, lastskulled=?, " +
  52. "changingappearance=?, male=?, combat=? WHERE id=?");
  53. updateInv = c.prepareStatement("UPDATE invitem SET itemid=?, amount=?, wielded=? " +
  54. "WHERE playerid=? AND position=?");
  55. updateBank = c.prepareStatement("UPDATE bankitem SET itemid=?, amount=? " +
  56. "WHERE playerid=? AND position=?");
  57. updateStat = c.prepareStatement("UPDATE stat SET cur=?, exp=? " +
  58. "WHERE playerid=? AND num=?");
  59. clearList = c.prepareStatement("DELETE FROM playerlist WHERE playerid=?");
  60. saveList = c.prepareStatement("INSERT INTO playerlist (playerid, playerhash, type) " +
  61. "VALUES (?, ?, ?)");
  62. updateAppearance = c.prepareStatement("UPDATE appearance SET " +
  63. "haircolour=?, topcolour=?, trousercolour=?, skincolour=?, head=?, body=? " +
  64. "WHERE playerid=?");
  65. updateSettings = c.prepareStatement("UPDATE setting SET " +
  66. "bool1=?, bool2=?, bool3=?, bool4=? WHERE type=? AND playerid=?");
  67. insertSuspicious = c.prepareStatement("INSERT INTO suspiciousevent (playerid, value) VALUES (?, ?)");
  68. }
  69.  
  70. /**
  71. * This is a bit of a hack. In case the connections are somehow closed, this method will check that they're okay
  72. * and if they're not okay it re-loads them.
  73. */
  74. private static boolean checkStatements(PreparedStatement... stmnts) {
  75. for (PreparedStatement ps : stmnts) {
  76. try {
  77. if (ps == null || ps.getConnection().isClosed()) {
  78. return false;
  79. }
  80. } catch (SQLException sqe) {
  81. return false;
  82. }
  83. }
  84. return true;
  85. }
  86.  
  87. public byte load(Player p) {
  88. try {
  89. return load0(p);
  90. } catch (Exception e) {
  91. Logger.err("Error loading " + p.getUsername());
  92. Logger.err(e);
  93. }
  94. return (byte) 17;
  95. }
  96.  
  97. public synchronized byte load0(Player p) throws SQLException {
  98. String ip = ((InetSocketAddress) p.getSession().getRemoteAddress()).getAddress().getHostAddress();
  99. Server.getServer().getThrottleFilter().connectionOk(p.getSession());
  100. if (attempts.containsKey(ip)) {
  101. BadPassword bp = attempts.get(ip);
  102. if (bp.nextAttempt > 0) {
  103. if (System.currentTimeMillis() > bp.nextAttempt) {
  104. attempts.remove(ip);
  105. } else {
  106. return (byte) 7;
  107. }
  108. }
  109. }
  110. if (!checkStatements(queryPlayer, queryInv, queryBank, querySettings, queryStat, queryList)) {
  111. loadStatements();
  112. }
  113. queryPlayer.setString(1, p.getUsername());
  114. queryPlayer.setString(2, p.getPassword());
  115. ResultSet resultPlayer = queryPlayer.executeQuery();
  116. if (!resultPlayer.next()) {
  117. BadPassword bp = attempts.get(ip);
  118. if (bp == null || System.currentTimeMillis() - 10 * 60 * 1000 > bp.lastAttempt) {
  119. bp = new BadPassword();
  120. attempts.put(ip, bp);
  121. }
  122. bp.attempt++;
  123. bp.lastAttempt = System.currentTimeMillis();
  124. if (bp.attempt >= Server.getServer().getConf().getInt("security.badpasswordcount")) {
  125. bp.nextAttempt = System.currentTimeMillis() + Server.getServer().getConf().getInt("security.badpassworddelay");
  126. }
  127. return 0x3; //no such user/password
  128. } else {
  129. //p.setGroup(resultPlayer.getByte("group"));
  130. if (World.getWorld().getPlayer(p.getUsernameHash()) != null) {
  131. return 0x4; //user already logged in
  132. }
  133. p.setId(resultPlayer.getInt("id"));
  134. p.setLocation(Point.location(resultPlayer.getInt("x"), resultPlayer.getInt("y")), true);
  135. p.setFightMode(resultPlayer.getInt("fightmode"));
  136. p.setLastLogin(System.currentTimeMillis());
  137. p.setLastIP(resultPlayer.getString("lastip"));
  138. p.setLastSkulled(resultPlayer.getLong("lastskulled"));
  139. p.setChangingAppearance(resultPlayer.getBoolean("changingappearance"));
  140. p.setMale(resultPlayer.getBoolean("male"));
  141.  
  142. //Inventory
  143. Inventory i = new Inventory(p);
  144. queryInv.setInt(1, resultPlayer.getInt("id"));
  145. ResultSet resultInv = queryInv.executeQuery();
  146. while (resultInv.next()) {
  147. int id = resultInv.getInt(1);
  148. int amount = resultInv.getInt(2);
  149. if (id == -1) {
  150. break;
  151. }
  152. InvItem it = new InvItem(id, amount);
  153. it.setWield(resultInv.getBoolean(3));
  154. i.add(it);
  155. }
  156. p.setInventory(i);
  157.  
  158. //Bank
  159. Bank b = new Bank();
  160. queryBank.setInt(1, p.getId());
  161. ResultSet resultBank = queryBank.executeQuery();
  162. while (resultBank.next()) {
  163. int id = resultBank.getInt(1);
  164. int amount = resultBank.getInt(2);
  165. if (id == -1) {
  166. break;
  167. }
  168. BankItem it = new BankItem(id, amount);
  169. b.addItem(it);
  170. }
  171. p.setBank(b);
  172.  
  173. //Settings
  174. querySettings.setInt(1, p.getId());
  175. querySettings.setString(2, "game");
  176. ResultSet gameSettings = querySettings.executeQuery();
  177. gameSettings.next();
  178. p.setGameSetting(0, gameSettings.getBoolean("bool1"));
  179. p.setGameSetting(1, gameSettings.getBoolean("bool2"));
  180. p.setGameSetting(2, gameSettings.getBoolean("bool3"));
  181. p.setGameSetting(3, gameSettings.getBoolean("bool4"));
  182. querySettings.setString(2, "privacy");
  183. ResultSet privacySettings = querySettings.executeQuery();
  184. privacySettings.next();
  185. p.setPrivacySetting(0, privacySettings.getBoolean("bool1"));
  186. p.setPrivacySetting(1, privacySettings.getBoolean("bool2"));
  187. p.setPrivacySetting(2, privacySettings.getBoolean("bool3"));
  188. p.setPrivacySetting(3, privacySettings.getBoolean("bool4"));
  189.  
  190. //Stats
  191. queryStat.setInt(1, p.getId());
  192. ResultSet resultStat = queryStat.executeQuery();
  193. while (resultStat.next()) {
  194. int stat = resultStat.getInt(1);
  195. p.setCurStat(stat, resultStat.getInt(2));
  196. p.setExp(stat, resultStat.getLong(3));
  197. }
  198.  
  199. queryList.setInt(1, p.getId());
  200. List<Long> friendsList = new ArrayList<Long>(), ignoreList = new ArrayList<Long>();
  201. ResultSet resultList = queryList.executeQuery();
  202. while (resultList.next()) {
  203. long playerHash = resultList.getLong(1);
  204. String type = resultList.getString(2);
  205. if (type.equals("ignore")) {
  206. ignoreList.add(playerHash);
  207. } else if (type.equals("friend")) {
  208. friendsList.add(playerHash);
  209. }
  210. }
  211. p.setFriendList(friendsList);
  212. p.setIgnoreList(ignoreList);
  213.  
  214. //Appearance
  215. PlayerAppearance pa = new PlayerAppearance(resultPlayer.getInt("haircolour"), resultPlayer.getInt("topcolour"),
  216. resultPlayer.getInt("trousercolour"), resultPlayer.getInt("skincolour"), resultPlayer.getInt("head"),
  217. resultPlayer.getInt("body"));
  218. p.setWornItems(pa.getSprites());
  219. p.setAppearance(pa);
  220.  
  221. Server.getServer().getThrottleFilter().acceptedLogin(p.getSession());
  222. return (p.getGroup() == 2 ? (byte) 25 : (p.getGroup() == 1 ? (byte) 24 : 0x00));
  223. }
  224. }
  225.  
  226. public void save(Player p) {
  227. try {
  228. save0(p);
  229. } catch (Exception e) {
  230. Logger.err("Error loading " + p.getUsername());
  231. Logger.err(e);
  232. }
  233. }
  234.  
  235. public synchronized void save0(Player p) throws SQLException {
  236. int i = 0, x = 0, y = 0;
  237. try {
  238. x = p.getLocation().getX();
  239. y = p.getLocation().getY();
  240. } catch (Exception e) {
  241. }
  242. if (!checkStatements(updatePlayer, updateInv, updateBank, updateStat, clearList, saveList, updateAppearance, updateSettings)) {
  243. loadStatements();
  244. }
  245. updatePlayer.setInt(++i, x);
  246. updatePlayer.setInt(++i, y);
  247. updatePlayer.setInt(++i, p.getFightMode());
  248. updatePlayer.setLong(++i, p.getLastLogin());
  249. updatePlayer.setString(++i, p.getLastIP());
  250. updatePlayer.setLong(++i, p.getLastSkulled());
  251. updatePlayer.setBoolean(++i, p.isChangingAppearance());
  252. updatePlayer.setBoolean(++i, p.isMale());
  253. updatePlayer.setInt(++i, p.getCombatLevel());
  254. updatePlayer.setInt(++i, p.getId());
  255. updatePlayer.executeUpdate();
  256.  
  257. updateInv.setInt(4, p.getId());
  258. for (int slot = 0; slot < 30; slot++) {
  259. InvItem it = p.getInventory().get(slot);
  260. updateInv.setInt(1, it == null ? -1 : it.getId());
  261. updateInv.setInt(2, it == null ? -1 : it.getAmount());
  262. updateInv.setBoolean(3, it == null ? false : it.isWielded());
  263. updateInv.setInt(5, slot);
  264. updateInv.addBatch();
  265. }
  266. updateInv.executeBatch();
  267.  
  268. updateBank.setInt(3, p.getId());
  269. for (int slot = 0; slot < 256; slot++) {
  270. BankItem it = p.getBank().getItem(slot);
  271. updateBank.setInt(1, it == null ? -1 : it.getId());
  272. updateBank.setInt(2, it == null ? -1 : it.getAmount());
  273. updateBank.setInt(4, slot);
  274. updateBank.addBatch();
  275. }
  276. updateBank.executeBatch();
  277.  
  278. updateStat.setInt(3, p.getId());
  279. for (int i1 = 0; i1 < 18; i1++) {
  280. updateStat.setInt(1, p.getCurStat(i1));
  281. updateStat.setLong(2, p.getExp(i1));
  282. updateStat.setInt(4, i1);
  283. updateStat.addBatch();
  284. }
  285. updateStat.executeBatch();
  286.  
  287. clearList.setInt(1, p.getId());
  288. clearList.executeUpdate();
  289.  
  290. saveList.setInt(1, p.getId());
  291. for (long friend : p.getFriendList()) {
  292. saveList.setLong(2, friend);
  293. saveList.setString(3, "friend");
  294. saveList.addBatch();
  295. }
  296. saveList.executeBatch();
  297. for (long ignore : p.getIgnoreList()) {
  298. saveList.setLong(2, ignore);
  299. saveList.setString(3, "ignore");
  300. saveList.addBatch();
  301. }
  302. saveList.executeBatch();
  303.  
  304. PlayerAppearance pa = p.getAppearance();
  305. updateAppearance.setInt(1, pa.getHairColour());
  306. updateAppearance.setInt(2, pa.getTopColour());
  307. updateAppearance.setInt(3, pa.getTrouserColour());
  308. updateAppearance.setInt(4, pa.getSkinColour());
  309. updateAppearance.setInt(5, pa.getHead());
  310. updateAppearance.setInt(6, pa.getBody());
  311. updateAppearance.setInt(7, p.getId());
  312. updateAppearance.executeUpdate();
  313.  
  314. updateSettings.setInt(6, p.getId());
  315. updateSettings.setString(5, "game");
  316. for (int n = 0; n < 4; n++) {
  317. updateSettings.setBoolean(n + 1, p.getGameSetting(n));
  318. }
  319. updateSettings.addBatch();
  320. updateSettings.setString(5, "privacy");
  321. for (int n = 0; n < 4; n++) {
  322. updateSettings.setBoolean(n + 1, p.getPrivacySetting(n));
  323. }
  324. updateSettings.addBatch();
  325. updateSettings.executeBatch();
  326. }
  327.  
  328. static class BadPassword {
  329.  
  330. int attempt = 0;
  331. long nextAttempt, lastAttempt;
  332. }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement