Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void saveToDB(boolean update) {
- if((update) && this.trade != null)
- return; //t
- Connection con = DatabaseConnection.getConnection();
- try {
- con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
- con.setAutoCommit(false);
- PreparedStatement ps;
- if (update) {
- 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 = ?");
- } else {
- 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
- }
- if (gmLevel < 1 && level > 199) {
- ps.setInt(1, isCygnus() ? 120 : 200);
- } else {
- ps.setInt(1, level);
- }
- ps.setInt(2, fame);
- ps.setInt(3, str);
- ps.setInt(4, dex);
- ps.setInt(5, luk);
- ps.setInt(6, int_);
- ps.setInt(7, exp.get());
- ps.setInt(8, hp);
- ps.setInt(9, mp);
- ps.setInt(10, maxhp);
- ps.setInt(11, maxmp);
- ps.setInt(12, remainingSp);
- ps.setInt(13, remainingAp);
- ps.setInt(14, gmLevel);
- ps.setInt(15, skinColor.getId());
- ps.setInt(16, gender);
- ps.setInt(17, job.getId());
- ps.setInt(18, hair);
- ps.setInt(19, face);
- if ((map == null) && (mapid == -1)) { //ie no data for map (shouldn't happen)
- ps.setInt(20, 0);
- } else if (map == null)//ie mapid set at createchar
- {
- ps.setInt(20, mapid);
- } else if (map.getForcedReturnId() != 999999999) {
- ps.setInt(20, map.getForcedReturnId());
- } else {
- ps.setInt(20, map.getId());
- }
- ps.setInt(21, meso.get());
- ps.setInt(22, hpMpApUsed);
- if (map == null || map.getId() == 610020000 || map.getId() == 610020001) {
- ps.setInt(23, 0);
- } else {
- MaplePortal closest = map.findClosestSpawnpoint(getPosition());
- if (closest != null) {
- ps.setInt(23, closest.getId());
- } else {
- ps.setInt(23, 0);
- }
- }
- ps.setInt(24, party != null ? party.getId() : -1);
- ps.setInt(25, buddylist.getCapacity());
- if (messenger != null) {
- ps.setInt(26, messenger.getId());
- ps.setInt(27, messengerposition);
- } else {
- ps.setInt(26, 0);
- ps.setInt(27, 4);
- }
- if (maplemount != null) {
- ps.setInt(28, maplemount.getLevel());
- ps.setInt(29, maplemount.getExp());
- ps.setInt(30, maplemount.getTiredness());
- } else {
- ps.setInt(28, 1);
- ps.setInt(29, 0);
- ps.setInt(30, 0);
- }
- for (int i = 31; i < 35; i++) {
- ps.setInt(i, getInventory(MapleInventoryType.getByType((byte) (i - 30))).getSlotLimit());
- }
- if (update) {
- monsterbook.saveCards(getId());
- try {
- getFamily().save();
- } catch (NullPointerException npe) {
- }
- }
- ps.setInt(35, bookCover);
- ps.setInt(36, watchedCygnusIntro ? 1 : 0);
- ps.setInt(37, vanquisherStage);
- ps.setInt(38, dojoPoints);
- ps.setInt(39, dojoStage);
- ps.setInt(40, finishedDojoTutorial ? 1 : 0);
- ps.setInt(41, vanquisherKills);
- ps.setInt(42, matchcardwins);
- ps.setInt(43, matchcardlosses);
- ps.setInt(44, matchcardties);
- ps.setInt(45, omokwins);
- ps.setInt(46, omoklosses);
- ps.setInt(47, omokties);
- ps.setInt(48, givenRiceCakes);
- ps.setString(49, partyquestitems);
- ps.setInt(50, jailtime);
- if (update) {
- ps.setInt(51, id);
- } else {
- ps.setInt(51, accountid);
- ps.setString(52, name);
- ps.setInt(53, world);
- }
- int updateRows = ps.executeUpdate();
- if (!update) {
- ResultSet rs = ps.getGeneratedKeys();
- if (rs.next()) {
- this.id = rs.getInt(1);
- } else {
- throw new RuntimeException("Inserting char failed.");
- }
- rs.close();
- } else if (updateRows < 1) {
- throw new RuntimeException("Character not in database (" + id + ")");
- }
- for (int i = 0; i < 3; i++) {
- if (pets[i] != null) {
- pets[i].saveToDb();
- }
- }
- ps.close();
- deleteWhereCharacterId(con, "DELETE FROM keymap WHERE characterid = ?");
- /* ps = con.prepaffreStatement("INSERT INTO keymap (characterid, `key`, `type`, `action`) VALUES (?, ?, ?, ?)");
- ps.setInt(1, id);
- for (Entry<Integer, MapleKeyBinding> keybinding : keymap.entrySet()) {
- ps.setInt(2, keybinding.getKey().intValue());
- ps.setInt(3, keybinding.getValue().getType());
- ps.setInt(4, keybinding.getValue().getAction());
- ps.addBatch();
- }
- ps.executeBatch();*/
- if(!keymap.isEmpty())
- {
- ps = con.prepareStatement(prepareKeymapQuery());
- ps.executeUpdate();
- ps.close();
- }
- deleteWhereCharacterId(con, "DELETE FROM skillmacros WHERE characterid = ?");
- ps = con.prepareStatement("INSERT INTO skillmacros (characterid, skill1, skill2, skill3, name, shout, position) VALUES (?, ?, ?, ?, ?, ?, ?)");
- ps.setInt(1, getId());
- for (int i = 0; i < 5; i++) {
- SkillMacro macro = skillMacros[i];
- if (macro != null) {
- ps.setInt(2, macro.getSkill1());
- ps.setInt(3, macro.getSkill2());
- ps.setInt(4, macro.getSkill3());
- ps.setString(5, macro.getName());
- ps.setInt(6, macro.getShout());
- ps.setInt(7, i);
- ps.addBatch();
- }
- }
- ps.executeBatch();
- ps.close();
- deleteWhereCharacterId(con, "DELETE FROM telerockmaps WHERE characterId = ?");
- ps = con.prepareStatement("INSERT into telerockmaps (characterId, mapId, type) VALUES (?, ?, ?)");
- ps.setInt(1, id);
- for (int mapId : rockMaps) {
- ps.setInt(2, mapId);
- ps.setInt(3, 0);
- ps.addBatch();
- }
- for (int mapId : vipRockMaps) {
- ps.setInt(2, mapId);
- ps.setInt(3, 1);
- ps.addBatch();
- }
- ps.executeBatch();
- ps.close();
- List<Pair<IItem, MapleInventoryType>> itemsWithType = new ArrayList<Pair<IItem, MapleInventoryType>>();
- for (MapleInventory iv : inventory) {
- for (IItem item : iv.list())
- itemsWithType.add(new Pair<IItem, MapleInventoryType>(item, iv.getType()));
- }
- ItemFactory.INVENTORY.saveItems(itemsWithType, id);
- deleteWhereCharacterId(con, "DELETE FROM skills WHERE characterid = ?");
- // ps = con.prepareStatement("INSERT INTO skills (characterid, skillid, skilllevel, masterlevel) VALUES (?, ?, ?, ?)");
- /* ps.setInt(1, id);
- for (Entry<ISkill, SkillEntry> skill : skills.entrySet()) {
- ps.setInt(2, skill.getKey().getId());
- ps.setInt(3, skill.getValue().skillevel);
- ps.setInt(4, skill.getValue().masterlevel);
- ps.addBatch();
- }
- ps.executeBatch();*/
- if(!skills.isEmpty())
- {
- ps = con.prepareStatement(prepareSkillQuery());
- ps.executeUpdate();
- ps.close();
- }
- deleteWhereCharacterId(con, "DELETE FROM savedlocations WHERE characterid = ?");
- ps = con.prepareStatement("INSERT INTO savedlocations (characterid, `locationtype`, `map`, `portal`) VALUES (?, ?, ?, ?)");
- ps.setInt(1, id);
- for (SavedLocationType savedLocationType : SavedLocationType.values()) {
- if (savedLocations[savedLocationType.ordinal()] != null) {
- ps.setString(2, savedLocationType.name());
- ps.setInt(3, savedLocations[savedLocationType.ordinal()].getMapId());
- ps.setInt(4, savedLocations[savedLocationType.ordinal()].getPortal());
- ps.addBatch();
- }
- }
- ps.executeBatch();
- ps.close();
- deleteWhereCharacterId(con, "DELETE FROM buddies WHERE characterid = ? AND pending = 0");
- ps = con.prepareStatement("INSERT INTO buddies (characterid, `buddyid`, `pending`, `group`) VALUES (?, ?, 0, ?)");
- ps.setInt(1, id);
- for (BuddylistEntry entry : buddylist.getBuddies()) {
- if (entry.isVisible()) {
- ps.setInt(2, entry.getCharacterId());
- ps.setString(3, entry.getGroup());
- ps.addBatch();
- }
- }
- ps.executeBatch();
- ps.close();
- deleteWhereCharacterId(con, "DELETE FROM queststatus WHERE characterid = ?");
- ps = con.prepareStatement("INSERT INTO queststatus (`queststatusid`, `characterid`, `quest`, `status`, `time`, `forfeited`) VALUES (DEFAULT, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
- PreparedStatement pse = con.prepareStatement("INSERT INTO queststatusmobs VALUES (DEFAULT, ?, ?, ?)");
- ps.setInt(1, id);
- for (MapleQuestStatus q : quests.values()) {
- ps.setInt(2, q.getQuest().getId());
- ps.setInt(3, q.getStatus().getId());
- ps.setInt(4, (int) (q.getCompletionTime() / 1000));
- ps.setInt(5, q.getForfeited());
- ps.executeUpdate();
- ResultSet rs = ps.getGeneratedKeys();
- rs.next();
- for (int mob : q.getMobKills().keySet()) {
- pse.setInt(1, rs.getInt(1));
- pse.setInt(2, mob);
- pse.setInt(3, q.getMobKills(mob));
- pse.addBatch();
- }
- pse.executeBatch();
- rs.close();
- }
- pse.close();
- ps.close();
- ps = con.prepareStatement("UPDATE accounts SET `paypalNX` = ?, `mPoints` = ?, `cardNX` = ?, gm = ?, votingpoints = ? WHERE id = ?");
- ps.setInt(1, paypalnx);
- ps.setInt(2, maplepoints);
- ps.setInt(3, cardnx);
- ps.setInt(4, gmLevel);
- ps.setInt(5, votingpoints);
- ps.setInt(6, client.getAccID());
- ps.executeUpdate();
- ps.close();
- if (storage != null) {
- storage.saveToDB();
- }
- ps = con.prepareStatement("DELETE FROM achievements WHERE accountid = ?");
- ps.setInt(1, accountid);
- ps.executeUpdate();
- ps.close();
- for (Integer achid : finishedAchievements) {
- ps = con.prepareStatement("INSERT INTO achievements(charid, achievementid, accountid) VALUES(?, ?, ?)");
- ps.setInt(1, id);
- ps.setInt(2, achid);
- ps.setInt(3, accountid);
- ps.executeUpdate();
- ps.close();
- }
- ps = con.prepareStatement("DELETE FROM wishlist WHERE `charid` = ?");
- ps.setInt(1, id);
- ps.executeUpdate();
- ps.close();
- ps = con.prepareStatement("INSERT INTO wishlist(`sn`, `charid`) VALUES(?, ?)");
- for (int sn : wishList) {
- ps.setInt(1, sn);
- ps.setInt(2, id);
- ps.addBatch();
- }
- ps.executeBatch();
- ps.close();
- con.commit();
- ps = null;
- } catch (Exception e) {
- e.printStackTrace();
- try {
- con.rollback();
- } catch (SQLException se) {
- }
- } finally {
- try {
- con.setAutoCommit(true);
- con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
- } catch (Exception e) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement