Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2011
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.21 KB | None | 0 0
  1.  
  2. public void saveToDB(boolean update) {
  3. if((update) && this.trade != null)
  4. return; //t
  5. Connection con = DatabaseConnection.getConnection();
  6. try {
  7. con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
  8. con.setAutoCommit(false);
  9. PreparedStatement ps;
  10. if (update) {
  11. ps = con.prepareStatement("UPDATE characters SET level = ?, fame = ?, str = ?, dex = ?, luk = ?, `int` = ?, exp = ?, 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 = ?, watchedcygnusintro = ?, vanquisherStage = ?, dojoPoints = ?, lastDojoStage = ?, finishedDojoTutorial = ?, vanquisherKills = ?, matchcardwins = ?, matchcardlosses = ?, matchcardties = ?, omokwins = ?, omoklosses = ?, omokties = ?, givenRiceCakes = ?, partyquestitems = ?, jailtime = ? WHERE id = ?");
  12. } else {
  13. ps = con.prepareStatement("INSERT INTO characters (level, fame, str, dex, luk, `int`, exp, hp, mp, maxhp, maxmp, sp, ap, gm, skincolor, gender, job, hair, face, map, meso, hpMpUsed, spawnpoint, party, buddyCapacity, messengerid, messengerposition, mountlevel, mounttiredness, mountexp, equipslots, useslots, setupslots, etcslots, monsterbookcover, watchedcygnusintro, vanquisherStage, dojopoints, lastDojoStage, finishedDojoTutorial, vanquisherKills, matchcardwins, matchcardlosses, matchcardties, omokwins, omoklosses, omokties, givenRiceCakes, partyquestitems, jailtime, accountid, name, world) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
  14. }
  15. if (gmLevel < 1 && level > 199) {
  16. ps.setInt(1, isCygnus() ? 120 : 200);
  17. } else {
  18. ps.setInt(1, level);
  19. }
  20. ps.setInt(2, fame);
  21. ps.setInt(3, str);
  22. ps.setInt(4, dex);
  23. ps.setInt(5, luk);
  24. ps.setInt(6, int_);
  25. ps.setInt(7, exp.get());
  26. ps.setInt(8, hp);
  27. ps.setInt(9, mp);
  28. ps.setInt(10, maxhp);
  29. ps.setInt(11, maxmp);
  30. ps.setInt(12, remainingSp);
  31. ps.setInt(13, remainingAp);
  32. ps.setInt(14, gmLevel);
  33. ps.setInt(15, skinColor.getId());
  34. ps.setInt(16, gender);
  35. ps.setInt(17, job.getId());
  36. ps.setInt(18, hair);
  37. ps.setInt(19, face);
  38. if ((map == null) && (mapid == -1)) { //ie no data for map (shouldn't happen)
  39. ps.setInt(20, 0);
  40. } else if (map == null)//ie mapid set at createchar
  41. {
  42. ps.setInt(20, mapid);
  43. } else if (map.getForcedReturnId() != 999999999) {
  44. ps.setInt(20, map.getForcedReturnId());
  45. } else {
  46. ps.setInt(20, map.getId());
  47. }
  48. ps.setInt(21, meso.get());
  49. ps.setInt(22, hpMpApUsed);
  50. if (map == null || map.getId() == 610020000 || map.getId() == 610020001) {
  51. ps.setInt(23, 0);
  52. } else {
  53. MaplePortal closest = map.findClosestSpawnpoint(getPosition());
  54. if (closest != null) {
  55. ps.setInt(23, closest.getId());
  56. } else {
  57. ps.setInt(23, 0);
  58. }
  59. }
  60. ps.setInt(24, party != null ? party.getId() : -1);
  61. ps.setInt(25, buddylist.getCapacity());
  62. if (messenger != null) {
  63. ps.setInt(26, messenger.getId());
  64. ps.setInt(27, messengerposition);
  65. } else {
  66. ps.setInt(26, 0);
  67. ps.setInt(27, 4);
  68. }
  69. if (maplemount != null) {
  70. ps.setInt(28, maplemount.getLevel());
  71. ps.setInt(29, maplemount.getExp());
  72. ps.setInt(30, maplemount.getTiredness());
  73. } else {
  74. ps.setInt(28, 1);
  75. ps.setInt(29, 0);
  76. ps.setInt(30, 0);
  77. }
  78. for (int i = 31; i < 35; i++) {
  79. ps.setInt(i, getInventory(MapleInventoryType.getByType((byte) (i - 30))).getSlotLimit());
  80. }
  81. if (update) {
  82. monsterbook.saveCards(getId());
  83. try {
  84. getFamily().save();
  85. } catch (NullPointerException npe) {
  86. }
  87. }
  88. ps.setInt(35, bookCover);
  89. ps.setInt(36, watchedCygnusIntro ? 1 : 0);
  90. ps.setInt(37, vanquisherStage);
  91. ps.setInt(38, dojoPoints);
  92. ps.setInt(39, dojoStage);
  93. ps.setInt(40, finishedDojoTutorial ? 1 : 0);
  94. ps.setInt(41, vanquisherKills);
  95. ps.setInt(42, matchcardwins);
  96. ps.setInt(43, matchcardlosses);
  97. ps.setInt(44, matchcardties);
  98. ps.setInt(45, omokwins);
  99. ps.setInt(46, omoklosses);
  100. ps.setInt(47, omokties);
  101. ps.setInt(48, givenRiceCakes);
  102. ps.setString(49, partyquestitems);
  103. ps.setInt(50, jailtime);
  104. if (update) {
  105. ps.setInt(51, id);
  106. } else {
  107. ps.setInt(51, accountid);
  108. ps.setString(52, name);
  109. ps.setInt(53, world);
  110. }
  111. int updateRows = ps.executeUpdate();
  112. if (!update) {
  113. ResultSet rs = ps.getGeneratedKeys();
  114. if (rs.next()) {
  115. this.id = rs.getInt(1);
  116. } else {
  117. throw new RuntimeException("Inserting char failed.");
  118. }
  119. rs.close();
  120. } else if (updateRows < 1) {
  121. throw new RuntimeException("Character not in database (" + id + ")");
  122. }
  123. for (int i = 0; i < 3; i++) {
  124. if (pets[i] != null) {
  125. pets[i].saveToDb();
  126. }
  127. }
  128. ps.close();
  129. deleteWhereCharacterId(con, "DELETE FROM keymap WHERE characterid = ?");
  130. /* ps = con.prepaffreStatement("INSERT INTO keymap (characterid, `key`, `type`, `action`) VALUES (?, ?, ?, ?)");
  131. ps.setInt(1, id);
  132. for (Entry<Integer, MapleKeyBinding> keybinding : keymap.entrySet()) {
  133. ps.setInt(2, keybinding.getKey().intValue());
  134. ps.setInt(3, keybinding.getValue().getType());
  135. ps.setInt(4, keybinding.getValue().getAction());
  136. ps.addBatch();
  137. }
  138. ps.executeBatch();*/
  139. if(!keymap.isEmpty())
  140. {
  141. ps = con.prepareStatement(prepareKeymapQuery());
  142. ps.executeUpdate();
  143. ps.close();
  144. }
  145.  
  146. deleteWhereCharacterId(con, "DELETE FROM skillmacros WHERE characterid = ?");
  147. ps = con.prepareStatement("INSERT INTO skillmacros (characterid, skill1, skill2, skill3, name, shout, position) VALUES (?, ?, ?, ?, ?, ?, ?)");
  148. ps.setInt(1, getId());
  149. for (int i = 0; i < 5; i++) {
  150. SkillMacro macro = skillMacros[i];
  151. if (macro != null) {
  152. ps.setInt(2, macro.getSkill1());
  153. ps.setInt(3, macro.getSkill2());
  154. ps.setInt(4, macro.getSkill3());
  155. ps.setString(5, macro.getName());
  156. ps.setInt(6, macro.getShout());
  157. ps.setInt(7, i);
  158. ps.addBatch();
  159. }
  160. }
  161. ps.executeBatch();
  162. ps.close();
  163. deleteWhereCharacterId(con, "DELETE FROM telerockmaps WHERE characterId = ?");
  164. ps = con.prepareStatement("INSERT into telerockmaps (characterId, mapId, type) VALUES (?, ?, ?)");
  165. ps.setInt(1, id);
  166. for (int mapId : rockMaps) {
  167. ps.setInt(2, mapId);
  168. ps.setInt(3, 0);
  169. ps.addBatch();
  170. }
  171. for (int mapId : vipRockMaps) {
  172. ps.setInt(2, mapId);
  173. ps.setInt(3, 1);
  174. ps.addBatch();
  175. }
  176. ps.executeBatch();
  177. ps.close();
  178. List<Pair<IItem, MapleInventoryType>> itemsWithType = new ArrayList<Pair<IItem, MapleInventoryType>>();
  179.  
  180. for (MapleInventory iv : inventory) {
  181. for (IItem item : iv.list())
  182. itemsWithType.add(new Pair<IItem, MapleInventoryType>(item, iv.getType()));
  183. }
  184.  
  185. ItemFactory.INVENTORY.saveItems(itemsWithType, id);
  186. deleteWhereCharacterId(con, "DELETE FROM skills WHERE characterid = ?");
  187. // ps = con.prepareStatement("INSERT INTO skills (characterid, skillid, skilllevel, masterlevel) VALUES (?, ?, ?, ?)");
  188. /* ps.setInt(1, id);
  189. for (Entry<ISkill, SkillEntry> skill : skills.entrySet()) {
  190. ps.setInt(2, skill.getKey().getId());
  191. ps.setInt(3, skill.getValue().skillevel);
  192. ps.setInt(4, skill.getValue().masterlevel);
  193. ps.addBatch();
  194. }
  195. ps.executeBatch();*/
  196. if(!skills.isEmpty())
  197. {
  198. ps = con.prepareStatement(prepareSkillQuery());
  199. ps.executeUpdate();
  200. ps.close();
  201. }
  202. deleteWhereCharacterId(con, "DELETE FROM savedlocations WHERE characterid = ?");
  203. ps = con.prepareStatement("INSERT INTO savedlocations (characterid, `locationtype`, `map`, `portal`) VALUES (?, ?, ?, ?)");
  204. ps.setInt(1, id);
  205. for (SavedLocationType savedLocationType : SavedLocationType.values()) {
  206. if (savedLocations[savedLocationType.ordinal()] != null) {
  207. ps.setString(2, savedLocationType.name());
  208. ps.setInt(3, savedLocations[savedLocationType.ordinal()].getMapId());
  209. ps.setInt(4, savedLocations[savedLocationType.ordinal()].getPortal());
  210. ps.addBatch();
  211. }
  212. }
  213. ps.executeBatch();
  214. ps.close();
  215. deleteWhereCharacterId(con, "DELETE FROM buddies WHERE characterid = ? AND pending = 0");
  216. ps = con.prepareStatement("INSERT INTO buddies (characterid, `buddyid`, `pending`, `group`) VALUES (?, ?, 0, ?)");
  217. ps.setInt(1, id);
  218. for (BuddylistEntry entry : buddylist.getBuddies()) {
  219. if (entry.isVisible()) {
  220. ps.setInt(2, entry.getCharacterId());
  221. ps.setString(3, entry.getGroup());
  222. ps.addBatch();
  223. }
  224. }
  225. ps.executeBatch();
  226. ps.close();
  227. deleteWhereCharacterId(con, "DELETE FROM queststatus WHERE characterid = ?");
  228. ps = con.prepareStatement("INSERT INTO queststatus (`queststatusid`, `characterid`, `quest`, `status`, `time`, `forfeited`) VALUES (DEFAULT, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
  229. PreparedStatement pse = con.prepareStatement("INSERT INTO queststatusmobs VALUES (DEFAULT, ?, ?, ?)");
  230. ps.setInt(1, id);
  231. for (MapleQuestStatus q : quests.values()) {
  232. ps.setInt(2, q.getQuest().getId());
  233. ps.setInt(3, q.getStatus().getId());
  234. ps.setInt(4, (int) (q.getCompletionTime() / 1000));
  235. ps.setInt(5, q.getForfeited());
  236. ps.executeUpdate();
  237. ResultSet rs = ps.getGeneratedKeys();
  238. rs.next();
  239. for (int mob : q.getMobKills().keySet()) {
  240. pse.setInt(1, rs.getInt(1));
  241. pse.setInt(2, mob);
  242. pse.setInt(3, q.getMobKills(mob));
  243. pse.addBatch();
  244. }
  245. pse.executeBatch();
  246. rs.close();
  247. }
  248. pse.close();
  249. ps.close();
  250. ps = con.prepareStatement("UPDATE accounts SET `paypalNX` = ?, `mPoints` = ?, `cardNX` = ?, gm = ?, votingpoints = ? WHERE id = ?");
  251. ps.setInt(1, paypalnx);
  252. ps.setInt(2, maplepoints);
  253. ps.setInt(3, cardnx);
  254. ps.setInt(4, gmLevel);
  255. ps.setInt(5, votingpoints);
  256. ps.setInt(6, client.getAccID());
  257. ps.executeUpdate();
  258. ps.close();
  259. if (storage != null) {
  260. storage.saveToDB();
  261. }
  262. ps = con.prepareStatement("DELETE FROM achievements WHERE accountid = ?");
  263. ps.setInt(1, accountid);
  264. ps.executeUpdate();
  265. ps.close();
  266.  
  267. for (Integer achid : finishedAchievements) {
  268. ps = con.prepareStatement("INSERT INTO achievements(charid, achievementid, accountid) VALUES(?, ?, ?)");
  269. ps.setInt(1, id);
  270. ps.setInt(2, achid);
  271. ps.setInt(3, accountid);
  272. ps.executeUpdate();
  273. ps.close();
  274. }
  275.  
  276. ps = con.prepareStatement("DELETE FROM wishlist WHERE `charid` = ?");
  277. ps.setInt(1, id);
  278. ps.executeUpdate();
  279. ps.close();
  280. ps = con.prepareStatement("INSERT INTO wishlist(`sn`, `charid`) VALUES(?, ?)");
  281. for (int sn : wishList) {
  282. ps.setInt(1, sn);
  283. ps.setInt(2, id);
  284. ps.addBatch();
  285. }
  286. ps.executeBatch();
  287. ps.close();
  288. con.commit();
  289. ps = null;
  290. } catch (Exception e) {
  291. e.printStackTrace();
  292. try {
  293. con.rollback();
  294. } catch (SQLException se) {
  295. }
  296. } finally {
  297. try {
  298. con.setAutoCommit(true);
  299. con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
  300. } catch (Exception e) {
  301. }
  302. }
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement