Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.87 KB | None | 0 0
  1. public void saveToDB() {
  2. Connection con = DatabaseConnection.getConnection();
  3. try {
  4. con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
  5. con.setAutoCommit(false);
  6. PreparedStatement ps;
  7. ps = con.prepareStatement("UPDATE characters SET level = ?, fame = ?, str = ?, dex = ?, luk = ?, `int` = ?, exp = ?, gachaexp = ?, hp = ?, mp = ?, maxhp = ?, maxmp = ?, sp = ?, ap = ?, gm = ?, skincolor = ?, gender = ?, job = ?, hair = ?, face = ?, map = ?, meso = ?, hpMpUsed = ?, spawnpoint = ?, party = ?, buddyCapacity = ?, messengerid = ?, messengerposition = ?, mountlevel = ?, mountexp = ?, mounttiredness= ?, equipslots = ?, useslots = ?, setupslots = ?, etcslots = ?, monsterbookcover = ?, vanquisherStage = ?, dojoPoints = ?, lastDojoStage = ?, finishedDojoTutorial = ?, vanquisherKills = ?, matchcardwins = ?, matchcardlosses = ?, matchcardties = ?, omokwins = ?, omoklosses = ?, omokties = ?, eventpoints = ?, participationpoints = ?, jqpoints = ?, reborns = ?, rebornpoints = ?, autorb = ?, itemvac = ? WHERE id = ?", Statement.RETURN_GENERATED_KEYS);
  8. if (gmLevel < 1 && level > 199) {
  9. ps.setInt(1, 200);
  10. } else {
  11. ps.setInt(1, level);
  12. }
  13. ps.setInt(2, fame);
  14. ps.setInt(3, str);
  15. ps.setInt(4, dex);
  16. ps.setInt(5, luk);
  17. ps.setInt(6, int_);
  18. ps.setInt(7, Math.abs(exp.get()));
  19. ps.setInt(8, Math.abs(gachaexp.get()));
  20. ps.setInt(9, hp);
  21. ps.setInt(10, mp);
  22. ps.setInt(11, maxhp);
  23. ps.setInt(12, maxmp);
  24. ps.setInt(13, remainingSp);
  25. ps.setInt(14, remainingAp);
  26. ps.setInt(15, gmLevel);
  27. ps.setInt(16, skinColor.getId());
  28. ps.setInt(17, gender);
  29. ps.setInt(18, job.getId());
  30. ps.setInt(19, hair);
  31. ps.setInt(20, face);
  32. if (map == null || (cashshop != null && cashshop.isOpened())) {
  33. ps.setInt(21, mapid);
  34. } else {
  35. if (map.getForcedReturnId() != 999999999) {
  36. ps.setInt(21, map.getForcedReturnId());
  37. } else {
  38. ps.setInt(21, getHp() < 1 ? map.getReturnMapId() : map.getId());
  39. }
  40. }
  41. ps.setInt(22, meso.get());
  42. ps.setInt(23, hpMpApUsed);
  43. if (map == null || map.getId() == 610020000 || map.getId() == 610020001) {
  44. ps.setInt(24, 0);
  45. } else {
  46. MaplePortal closest = map.findClosestSpawnpoint(getPosition());
  47. if (closest != null) {
  48. ps.setInt(24, closest.getId());
  49. } else {
  50. ps.setInt(24, 0);
  51. }
  52. }
  53. if (party != null) {
  54. ps.setInt(25, party.getId());
  55. } else {
  56. ps.setInt(25, -1);
  57. }
  58. ps.setInt(26, buddylist.getCapacity());
  59. if (messenger != null) {
  60. ps.setInt(27, messenger.getId());
  61. ps.setInt(28, messengerposition);
  62. } else {
  63. ps.setInt(27, 0);
  64. ps.setInt(28, 4);
  65. }
  66. if (maplemount != null) {
  67. ps.setInt(29, maplemount.getLevel());
  68. ps.setInt(30, maplemount.getExp());
  69. ps.setInt(31, maplemount.getTiredness());
  70. } else {
  71. ps.setInt(29, 1);
  72. ps.setInt(30, 0);
  73. ps.setInt(31, 0);
  74. }
  75. for (int i = 1; i < 5; i++) {
  76. ps.setInt(i + 31, getSlots(i));
  77. }
  78.  
  79. monsterbook.saveCards(getId());
  80.  
  81. ps.setInt(36, bookCover);
  82. ps.setInt(37, vanquisherStage);
  83. ps.setInt(38, dojoPoints);
  84. ps.setInt(39, dojoStage);
  85. ps.setInt(40, finishedDojoTutorial ? 1 : 0);
  86. ps.setInt(41, vanquisherKills);
  87. ps.setInt(42, matchcardwins);
  88. ps.setInt(43, matchcardlosses);
  89. ps.setInt(44, matchcardties);
  90. ps.setInt(45, omokwins);
  91. ps.setInt(46, omoklosses);
  92. ps.setInt(47, omokties);
  93. ps.setInt(48, eventpoints);
  94. ps.setInt(49, participationpoints);
  95. ps.setInt(50, jqpoints);
  96. ps.setInt(51, reborns);
  97. ps.setInt(52, rebornpoints);
  98. ps.setByte(53, autorb);
  99. ps.setByte(54, itemvac);
  100. ps.setInt(55, id);
  101.  
  102. int updateRows = ps.executeUpdate();
  103. if (updateRows < 1) {
  104. throw new RuntimeException("Character not in database (" + id + ")");
  105. }
  106. for (int i = 0; i < 3; i++) {
  107. if (pets[i] != null) {
  108. pets[i].saveToDb();
  109. }
  110. }
  111. deleteWhereCharacterId(con, "DELETE FROM keymap WHERE characterid = ?");
  112. ps = con.prepareStatement("INSERT INTO keymap (characterid, `key`, `type`, `action`) VALUES (?, ?, ?, ?)");
  113. ps.setInt(1, id);
  114. for (Entry<Integer, MapleKeyBinding> keybinding : keymap.entrySet()) {
  115. ps.setInt(2, keybinding.getKey().intValue());
  116. ps.setInt(3, keybinding.getValue().getType());
  117. ps.setInt(4, keybinding.getValue().getAction());
  118. ps.addBatch();
  119. }
  120. ps.executeBatch();
  121. deleteWhereCharacterId(con, "DELETE FROM skillmacros WHERE characterid = ?");
  122. ps = con.prepareStatement("INSERT INTO skillmacros (characterid, skill1, skill2, skill3, name, shout, position) VALUES (?, ?, ?, ?, ?, ?, ?)");
  123. ps.setInt(1, getId());
  124. for (int i = 0; i < 5; i++) {
  125. SkillMacro macro = skillMacros[i];
  126. if (macro != null) {
  127. ps.setInt(2, macro.getSkill1());
  128. ps.setInt(3, macro.getSkill2());
  129. ps.setInt(4, macro.getSkill3());
  130. ps.setString(5, macro.getName());
  131. ps.setInt(6, macro.getShout());
  132. ps.setInt(7, i);
  133. ps.addBatch();
  134. }
  135. }
  136. ps.executeBatch();
  137. List<Pair<Item, MapleInventoryType>> itemsWithType = new ArrayList<>();
  138.  
  139. for (MapleInventory iv : inventory) {
  140. for (Item item : iv.list()) {
  141. itemsWithType.add(new Pair<>(item, iv.getType()));
  142. }
  143. }
  144.  
  145. ItemFactory.INVENTORY.saveItems(itemsWithType, id);
  146. deleteWhereCharacterId(con, "DELETE FROM skills WHERE characterid = ?");
  147. ps = con.prepareStatement("INSERT INTO skills (characterid, skillid, skilllevel, masterlevel, expiration) VALUES (?, ?, ?, ?, ?)");
  148. ps.setInt(1, id);
  149. for (Entry<Skill, SkillEntry> skill : skills.entrySet()) {
  150. ps.setInt(2, skill.getKey().getId());
  151. ps.setInt(3, skill.getValue().skillevel);
  152. ps.setInt(4, skill.getValue().masterlevel);
  153. ps.setLong(5, skill.getValue().expiration);
  154. ps.addBatch();
  155. }
  156. ps.executeBatch();
  157. deleteWhereCharacterId(con, "DELETE FROM savedlocations WHERE characterid = ?");
  158. ps = con.prepareStatement("INSERT INTO savedlocations (characterid, `locationtype`, `map`, `portal`) VALUES (?, ?, ?, ?)");
  159. ps.setInt(1, id);
  160. for (SavedLocationType savedLocationType : SavedLocationType.values()) {
  161. if (savedLocations[savedLocationType.ordinal()] != null) {
  162. ps.setString(2, savedLocationType.name());
  163. ps.setInt(3, savedLocations[savedLocationType.ordinal()].getMapId());
  164. ps.setInt(4, savedLocations[savedLocationType.ordinal()].getPortal());
  165. ps.addBatch();
  166. }
  167. }
  168. ps.executeBatch();
  169. deleteWhereCharacterId(con, "DELETE FROM trocklocations WHERE characterid = ?");
  170. ps = con.prepareStatement("INSERT INTO trocklocations(characterid, mapid, vip) VALUES (?, ?, 0)");
  171. for (int i = 0; i < getTrockSize(); i++) {
  172. if (trockmaps[i] != 999999999) {
  173. ps.setInt(1, getId());
  174. ps.setInt(2, trockmaps[i]);
  175. ps.addBatch();
  176. }
  177. }
  178. ps.executeBatch();
  179. ps = con.prepareStatement("INSERT INTO trocklocations(characterid, mapid, vip) VALUES (?, ?, 1)");
  180. for (int i = 0; i < getVipTrockSize(); i++) {
  181. if (viptrockmaps[i] != 999999999) {
  182. ps.setInt(1, getId());
  183. ps.setInt(2, viptrockmaps[i]);
  184. ps.addBatch();
  185. }
  186. }
  187. ps.executeBatch();
  188. deleteWhereCharacterId(con, "DELETE FROM buddies WHERE characterid = ? AND pending = 0");
  189. ps = con.prepareStatement("INSERT INTO buddies (characterid, `buddyid`, `pending`, `group`) VALUES (?, ?, 0, ?)");
  190. ps.setInt(1, id);
  191. for (BuddylistEntry entry : buddylist.getBuddies()) {
  192. if (entry.isVisible()) {
  193. ps.setInt(2, entry.getCharacterId());
  194. ps.setString(3, entry.getGroup());
  195. ps.addBatch();
  196. }
  197. }
  198. ps.executeBatch();
  199. deleteWhereCharacterId(con, "DELETE FROM area_info WHERE charid = ?");
  200. ps = con.prepareStatement("INSERT INTO area_info (id, charid, area, info) VALUES (DEFAULT, ?, ?, ?)");
  201. ps.setInt(1, id);
  202. for (Entry<Short, String> area : area_info.entrySet()) {
  203. ps.setInt(2, area.getKey());
  204. ps.setString(3, area.getValue());
  205. ps.addBatch();
  206. }
  207. ps.executeBatch();
  208. deleteWhereCharacterId(con, "DELETE FROM eventstats WHERE characterid = ?");
  209. deleteWhereCharacterId(con, "DELETE FROM queststatus WHERE characterid = ?");
  210. ps = con.prepareStatement("INSERT INTO queststatus (`queststatusid`, `characterid`, `quest`, `status`, `time`, `forfeited`) VALUES (DEFAULT, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
  211. PreparedStatement psf;
  212. try (PreparedStatement pse = con.prepareStatement("INSERT INTO questprogress VALUES (DEFAULT, ?, ?, ?)")) {
  213. psf = con.prepareStatement("INSERT INTO medalmaps VALUES (DEFAULT, ?, ?)");
  214. ps.setInt(1, id);
  215. for (MapleQuestStatus q : quests.values()) {
  216. ps.setInt(2, q.getQuest().getId());
  217. ps.setInt(3, q.getStatus().getId());
  218. ps.setInt(4, (int) (q.getCompletionTime() / 1000));
  219. ps.setInt(5, q.getForfeited());
  220. ps.executeUpdate();
  221. try (ResultSet rs = ps.getGeneratedKeys()) {
  222. rs.next();
  223. for (int mob : q.getProgress().keySet()) {
  224. pse.setInt(1, rs.getInt(1));
  225. pse.setInt(2, mob);
  226. pse.setString(3, q.getProgress(mob));
  227. pse.addBatch();
  228. }
  229. for (int i = 0; i < q.getMedalMaps().size(); i++) {
  230. psf.setInt(1, rs.getInt(1));
  231. psf.setInt(2, q.getMedalMaps().get(i));
  232. psf.addBatch();
  233. }
  234. pse.executeBatch();
  235. psf.executeBatch();
  236. }
  237. }
  238. }
  239. psf.close();
  240. ps = con.prepareStatement("UPDATE accounts SET gm = ? WHERE id = ?");
  241. ps.setInt(1, gmLevel);
  242. ps.setInt(2, client.getAccID());
  243. ps.executeUpdate();
  244. if (cashshop != null) {
  245. cashshop.save();
  246. }
  247. if (storage != null) {
  248. storage.saveToDB();
  249. }
  250. ps.close();
  251. con.commit();
  252. } catch (SQLException | RuntimeException t) {
  253. FilePrinter.printError(FilePrinter.SAVE_CHAR, t, "Error saving " + name + " Level: " + level + " Job: " + job.getId());
  254. try {
  255. con.rollback();
  256. } catch (SQLException se) {
  257. FilePrinter.printError(FilePrinter.SAVE_CHAR, se, "Error trying to rollback " + name);
  258. }
  259. } finally {
  260. try {
  261. con.setAutoCommit(true);
  262. con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
  263. } catch (Exception e) {
  264. }
  265. }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement