scotty92

Untitled

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