Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileInputStream;
- import java.math.BigInteger;
- import java.io.File;
- import java.sql.*;
- public class WurmSkills {
- private static final int iCR = 13;
- private static final int iLF = 10;
- private static final int iSP = 32;
- private static final int iCO = 58;
- private static final int deityFo = 1;
- private static final int deityMag = 2;
- private static final int deityVin = 3;
- private static final int deityLib = 4;
- private static final int serverID = 12247;
- private static int currNextSkill = -1;
- private static Connection cPlayers = null;
- private static Connection cLogins = null;
- private static long lastused = -1;
- private static String[][] Skills = {
- {"Religion","1026"},
- {"Prayer","10066"},
- {"Channeling","10067"},
- {"Preaching","10065"},
- {"Exorcism","10068"},
- {"Artifacts","10069"},
- {"Hammers","1027"},
- {"Warhammer","10070"},
- {"Healing","1024"},
- {"First aid","10056"},
- {"Clubs","1025"},
- {"Huge club","10064"},
- {"Archery","1030"},
- {"Short bow","10079"},
- {"Medium bow","10080"},
- {"Long bow","10081"},
- {"Thievery","1028"},
- {"Stealing","10075"},
- {"Lock picking","10076"},
- {"Traps","10084"},
- {"War machines","1029"},
- {"Catapults","10077"},
- {"Trebuchets","10094"},
- {"Turrets","10095"},
- {"Ballistae","10093"},
- {"Polearms","1033"},
- {"Staff","10090"},
- {"Long spear","10088"},
- {"Halberd","10089"},
- {"Prospecting","10032"},
- {"Coal-making","10036"},
- {"Milling","10040"},
- {"Tracking","10018"},
- {"Paving","10031"},
- {"Climbing","10073"},
- {"Thatching","10092"},
- {"Firemaking","1010"},
- {"Pottery","1011"},
- {"Mining","1008"},
- {"Digging","1009"},
- {"Ropemaking","1014"},
- {"Smithing","1015"},
- {"Blacksmithing","10015"},
- {"Locksmithing","10034"},
- {"Jewelry smithing","10043"},
- {"Metallurgy","10041"},
- {"Weapon smithing","1016"},
- {"Blades smithing","10010"},
- {"Weapon heads smithing","10011"},
- {"Armour smithing","1017"},
- {"Shield smithing","10014"},
- {"Chain armour smithing","10012"},
- {"Plate armour smithing","10013"},
- {"Tailoring","1012"},
- {"Cloth tailoring","10016"},
- {"Leatherworking","10017"},
- {"Masonry","1013"},
- {"Stone cutting","10074"},
- {"Cooking","1018"},
- {"Hot food cooking","10038"},
- {"Baking","10039"},
- {"Dairy food making","10037"},
- {"Butchering","10059"},
- {"Beverages","10083"},
- {"Nature","1019"},
- {"Fishing","10033"},
- {"Gardening","10045"},
- {"Foraging","10071"},
- {"Botanizing","10072"},
- {"Animal taming","10078"},
- {"Forestry","10048"},
- {"Farming","10049"},
- {"Milking","10060"},
- {"Meditating","10086"},
- {"Animal husbandry","10085"},
- {"Papyrusmaking","10091"},
- {"Toys","1022"},
- {"Yoyo","10050"},
- {"Puppeteering","10087"},
- {"Fighting","1023"},
- {"Defensive fighting","10054"},
- {"Normal fighting","10055"},
- {"Weaponless fighting","10052"},
- {"Aggressive fighting","10053"},
- {"Shield bashing","10058"},
- {"Taunting","10057"},
- {"Miscellaneous items","1020"},
- {"Shovel","10002"},
- {"Rake","10004"},
- {"Saw","10008"},
- {"Pickaxe","10009"},
- {"Repairing","10035"},
- {"Sickle","10046"},
- {"Scythe","10047"},
- {"Hammer","10026"},
- {"Stone chisel","10030"},
- {"Alchemy","1021"},
- {"Natural substances","10042"},
- {"Shields","1002"},
- {"Medium metal shield","10006"},
- {"Small wooden shield","10019"},
- {"Small metal shield","10022"},
- {"Large metal shield","10023"},
- {"Medium wooden shield","10020"},
- {"Large wooden shield","10021"},
- {"Axes","1003"},
- {"Hatchet","10003"},
- {"Small Axe","10001"},
- {"Large axe","10024"},
- {"Huge axe","10025"},
- {"Swords","1000"},
- {"Longsword","10005"},
- {"Shortsword","10027"},
- {"Two handed sword","10028"},
- {"Knives","1001"},
- {"Carving knife","10007"},
- {"Butchering knife","10029"},
- {"Woodcutting","1007"},
- {"Mauls","1004"},
- {"Medium maul","10062"},
- {"Small maul","10063"},
- {"Large maul","10061"},
- {"Carpentry","1005"},
- {"Bowyery","1031"},
- {"Fletching","1032"},
- {"Fine carpentry","10044"},
- {"Toy making","10051"},
- {"Ship building","10082"},
- {"Mind","2"},
- {"Mind logic","100"},
- {"Mind speed","101"},
- {"Soul","3"},
- {"Soul depth","106"},
- {"Soul strength","105"},
- {"Body","1"},
- {"Body strength","102"},
- {"Body stamina","103"},
- {"Body control","104"},
- {"Faith","-1"}, // Update on Player
- {"Favor","-2"}, // Update on Player
- {"Alignment","-3"} // Update on player
- };
- private static String searchSkill(String skillname) {
- String skillvalue = "";
- int skillcount = Skills.length;
- int c = 0;
- for (c = 0; !skillname.equals(Skills[c][0]); c++) ;
- if (c < skillcount) skillvalue = Skills[c][1];
- else if (skillname.equals(Skills[c][0])) skillvalue = Skills[c][1];
- return skillvalue;
- }
- private static String getID(String charname) {
- String sID = "";
- ResultSet resultSet = null;
- PreparedStatement pStatement = null;
- try {
- pStatement = cPlayers.prepareStatement("Select WURMID from PLAYERS where NAME = ?");
- pStatement.setString(1, charname);
- resultSet = pStatement.executeQuery();
- while (resultSet.next()) {
- sID = resultSet.getString(1);
- }
- } catch (Exception e) {
- System.err.println( e.getClass().getName() + ": " + e.getMessage() );
- }
- return sID;
- }
- private static void getMaxSkill() {
- ResultSet resultSet = null;
- PreparedStatement pStatement = null;
- try {
- pStatement = cLogins.prepareStatement("Select PLAYERSKILLIDS from IDS where SERVER = ?");
- pStatement.setLong(1, serverID);
- resultSet = pStatement.executeQuery();
- while (resultSet.next()) {
- currNextSkill = resultSet.getInt(1);
- }
- } catch (Exception e) {
- System.err.println( e.getClass().getName() + ": " + e.getMessage() );
- }
- }
- private static void saveMaxSkill() {
- PreparedStatement pStatement = null;
- try {
- pStatement = cLogins.prepareStatement("update IDS set PLAYERSKILLIDS = ? where SERVER = ?");
- pStatement.setLong(1, currNextSkill);
- pStatement.setLong(2, serverID);
- pStatement.executeUpdate();
- } catch (Exception e) {
- System.err.println( e.getClass().getName() + ": " + e.getMessage() );
- }
- }
- private static int updateSpecial(String charID, int type, String value) {
- ResultSet resultSet = null;
- PreparedStatement pStatement = null;
- int retval = 0;
- int deityVal = 0;
- String attVal = "";
- try {
- // check if has diety
- StringBuilder statement = new StringBuilder("Select DEITY,");
- switch (type) {
- case -1 : { statement.append("FAITH"); break; }
- case -2 : { statement.append("FAVOR"); break; }
- case -3 : { statement.append("ALIGNMENT"); break; }
- default : return -1; // should never happen
- }
- statement.append(" from PLAYERS where WURMID = ?");
- pStatement = cPlayers.prepareStatement(statement.toString());
- pStatement.setString(1, charID);
- resultSet = pStatement.executeQuery();
- while (resultSet.next()) {
- deityVal = resultSet.getInt(1);
- attVal = resultSet.getString(2);
- if ((deityVal < deityFo || deityVal > deityLib) && type != -3) return -2; // no valid deity for faith/favor
- if (attVal.equals(value)) return -1; // no change
- if (Double.valueOf(attVal) > Double.valueOf(value)) return 0;// why decrease a skill
- }
- resultSet.close();
- pStatement.close();
- statement.setLength(0);
- statement.append("Update PLAYERS set ");
- switch (type) {
- case -1 : { statement.append("FAITH"); break; }
- case -2 : { statement.append("FAVOR"); break; }
- case -3 : { statement.append("ALIGNMENT"); break; }
- default : return -1; // should never happen
- }
- statement.append(" = ? where WURMID = ?");
- pStatement = cPlayers.prepareStatement(statement.toString());
- pStatement.setString(1, value);
- pStatement.setString(2, charID);
- pStatement.executeUpdate();
- retval = 1;
- } catch (Exception e) {
- System.err.println( e.getClass().getName() + ": " + e.getMessage() );
- }
- return retval;
- }
- @SuppressWarnings("resource")
- private static int updateSkill(String charID, String skillID, String value) {
- ResultSet resultSet = null;
- PreparedStatement pStatement = null;
- boolean bExists = false;
- int retval = 0;
- String curValue = "";
- try {
- // check if exists
- pStatement = cPlayers.prepareStatement("Select VALUE from SKILLS where OWNER = ? and NUMBER = ?");
- pStatement.setString(1, charID);
- pStatement.setString(2, skillID);
- resultSet = pStatement.executeQuery();
- while (resultSet.next()) {
- curValue = resultSet.getString(1);
- if (curValue.equals(value)) return -1; // no change, we're done
- if (Double.valueOf(curValue) > Double.valueOf(value)) return 0; // why decrease a skill
- bExists = true;
- }
- resultSet.close();
- pStatement.close();
- if (bExists) {
- pStatement = cPlayers.prepareStatement("Update SKILLS set VALUE = ?, LASTUSED = ? where OWNER = ? and NUMBER = ?");
- pStatement.setString(1, value);
- pStatement.setLong(2, lastused);
- pStatement.setString(3, charID);
- pStatement.setString(4, skillID);
- pStatement.executeUpdate();
- retval = 1;
- } else {
- pStatement = cPlayers.prepareStatement("Insert into SKILLS (ID,OWNER, NUMBER, VALUE, MINVALUE,LASTUSED) Values (?,?,?,?,?,?)");
- pStatement.setLong(1, getNextSkill());
- pStatement.setString(2, charID);
- pStatement.setString(3, skillID);
- pStatement.setString(4, value);
- pStatement.setDouble(5, ((Double.valueOf(value)>10)?Double.valueOf(value):10));
- pStatement.setLong(6, lastused);
- pStatement.executeUpdate();
- retval = 2;
- }
- } catch (Exception e) {
- System.err.println( e.getClass().getName() + ": " + e.getMessage() );
- }
- return retval;
- }
- private static long getNextSkill() {
- currNextSkill++;
- return BigInteger.valueOf(currNextSkill).shiftLeft(24).longValue() + (long)(serverID << 8) + 10L;
- }
- public static void main(String[] args) {
- int len = 0;
- String sDatapath = "";
- String sDatafile = "";
- String sCharname = "";
- String sCharID = "";
- lastused = System.currentTimeMillis();
- try {
- Class.forName("org.sqlite.JDBC");
- System.out.print("Database path: ");
- byte[] bDatapath = new byte[256];
- len = System.in.read(bDatapath);
- if (len >0) {
- sDatapath = new String(bDatapath,0,len-2);
- sDatafile = sDatapath + "\\wurmplayers.db";
- cPlayers = DriverManager.getConnection("jdbc:sqlite:"+sDatafile);
- sDatafile = sDatapath + "\\wurmlogin.db";
- cLogins = DriverManager.getConnection("jdbc:sqlite:"+sDatafile);
- getMaxSkill();
- }
- } catch ( Exception e ) {
- System.err.println( e.getClass().getName() + ": " + e.getMessage() );
- System.exit(0);
- }
- System.out.println("Opened database successfully");
- try {
- System.out.print("Character Name: ");
- // validate name
- byte[] bCharname = new byte[64];
- len = System.in.read(bCharname);
- if (len > 0) {
- sCharname = new String(bCharname,0,len-2);
- sCharID = getID(sCharname);
- if (sCharID.isEmpty()) {
- System.out.println("No character "+sCharname+" exists");
- System.exit(-1);
- }
- } else {
- System.out.println("No character name entered.");
- System.exit(-1);
- }
- System.out.print("Import file: ");
- byte[] bFilepath = new byte[256];
- len = System.in.read(bFilepath);
- if (len > 0) {
- String sFilepath = new String(bFilepath,0,len-2);
- File skillFile = new File(sFilepath);
- if (skillFile.exists()) {
- int iChar;
- boolean bLineStart = true;
- boolean bFoundSkill = false;
- boolean bReadingSkill = false;
- boolean bReadingValue = false;
- boolean bDoBreak = false;
- String curSkill = "";
- String curValue = "";
- FileInputStream fis = new FileInputStream(skillFile);
- while ((iChar = fis.read()) != -1) {
- // Process the chars
- if (bLineStart && (iChar != iSP)) {
- // Ignore any line that doesn't start with SPACE
- while (fis.read() != iCR) ;
- fis.read(); // get the LF too.
- continue;
- } else bLineStart = false;
- switch (iChar) {
- case iSP:
- if (bReadingSkill)
- curSkill = curSkill + (char)iChar;
- else if (bReadingValue) {
- int iTrash;
- bReadingValue = false;
- while ((iTrash=fis.read()) != iCR)
- if (iTrash == -1) {
- bDoBreak = true;
- break;
- }
- // Process each skill name and first value it finds
- // search for skill number in array and insert number/value/userid if doesn't exist, else update
- String curSkillVal = searchSkill(curSkill);
- int retval = 0;
- if (Integer.parseInt(curSkillVal.trim()) > 0) {
- retval = updateSkill(sCharID,curSkillVal,curValue);
- } else
- retval = updateSpecial(sCharID, Integer.parseInt(curSkillVal.trim()),curValue);
- System.out.print("Current Skill: "+curSkill+" Current Value: "+curValue+" ");
- switch (retval) {
- case 1: { System.out.println("Updated"); break; }
- case 2: { System.out.println("Inserted"); break; }
- case 0: { System.out.println("Saved higher"); break; }
- case -1: { System.out.println("No change"); break; }
- case -2: System.out.println("Need deity first");
- }
- if (bDoBreak) break;
- iTrash = fis.read(); // get the LF too.
- // Reset for next line
- curSkill = "";
- curValue = "";
- bLineStart = true;
- bFoundSkill = false;
- }
- continue; //ignore char
- case iCO:
- bFoundSkill = true;
- bReadingSkill = false;
- continue; //ignore, set flag to grab value
- default:
- if (!bFoundSkill) {
- bReadingSkill = true;
- curSkill = curSkill + (char)iChar;
- } else {
- bReadingValue = true;
- curValue = curValue + (char)iChar;
- }
- }
- if (bDoBreak) break;
- }
- fis.close();
- saveMaxSkill();
- } else {
- System.out.println("File '"+sFilepath+ "' does not exist.");
- }
- } else {
- System.out.println("No filename entered.");
- }
- } catch (Exception e) {
- System.err.println( e.getClass().getName() + ": " + e.getMessage() );
- System.exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement