Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.77 KB | None | 0 0
  1. import java.io.FileInputStream;
  2. import java.math.BigInteger;
  3. import java.io.File;
  4. import java.sql.*;
  5.  
  6. public class WurmSkills {
  7.     private static final int iCR = 13;
  8.     private static final int iLF = 10;
  9.     private static final int iSP = 32;
  10.     private static final int iCO = 58;
  11.  
  12.     private static final int deityFo = 1;
  13.     private static final int deityMag = 2;
  14.     private static final int deityVin = 3;
  15.     private static final int deityLib = 4;
  16.  
  17.     private static final int serverID = 12247;
  18.  
  19.     private static int currNextSkill = -1;
  20.     private static Connection cPlayers = null;
  21.     private static Connection cLogins = null;
  22.     private static long lastused = -1;
  23.  
  24.     private static String[][] Skills = {
  25.             {"Religion","1026"},
  26.             {"Prayer","10066"},
  27.             {"Channeling","10067"},
  28.             {"Preaching","10065"},
  29.             {"Exorcism","10068"},
  30.             {"Artifacts","10069"},
  31.             {"Hammers","1027"},
  32.             {"Warhammer","10070"},
  33.             {"Healing","1024"},
  34.             {"First aid","10056"},
  35.             {"Clubs","1025"},
  36.             {"Huge club","10064"},
  37.             {"Archery","1030"},
  38.             {"Short bow","10079"},
  39.             {"Medium bow","10080"},
  40.             {"Long bow","10081"},
  41.             {"Thievery","1028"},
  42.             {"Stealing","10075"},
  43.             {"Lock picking","10076"},
  44.             {"Traps","10084"},
  45.             {"War machines","1029"},
  46.             {"Catapults","10077"},
  47.             {"Trebuchets","10094"},
  48.             {"Turrets","10095"},
  49.             {"Ballistae","10093"},
  50.             {"Polearms","1033"},
  51.             {"Staff","10090"},
  52.             {"Long spear","10088"},
  53.             {"Halberd","10089"},
  54.             {"Prospecting","10032"},
  55.             {"Coal-making","10036"},
  56.             {"Milling","10040"},
  57.             {"Tracking","10018"},
  58.             {"Paving","10031"},
  59.             {"Climbing","10073"},
  60.             {"Thatching","10092"},
  61.             {"Firemaking","1010"},
  62.             {"Pottery","1011"},
  63.             {"Mining","1008"},
  64.             {"Digging","1009"},
  65.             {"Ropemaking","1014"},
  66.             {"Smithing","1015"},
  67.             {"Blacksmithing","10015"},
  68.             {"Locksmithing","10034"},
  69.             {"Jewelry smithing","10043"},
  70.             {"Metallurgy","10041"},
  71.             {"Weapon smithing","1016"},
  72.             {"Blades smithing","10010"},
  73.             {"Weapon heads smithing","10011"},
  74.             {"Armour smithing","1017"},
  75.             {"Shield smithing","10014"},
  76.             {"Chain armour smithing","10012"},
  77.             {"Plate armour smithing","10013"},
  78.             {"Tailoring","1012"},
  79.             {"Cloth tailoring","10016"},
  80.             {"Leatherworking","10017"},
  81.             {"Masonry","1013"},
  82.             {"Stone cutting","10074"},
  83.             {"Cooking","1018"},
  84.             {"Hot food cooking","10038"},
  85.             {"Baking","10039"},
  86.             {"Dairy food making","10037"},
  87.             {"Butchering","10059"},
  88.             {"Beverages","10083"},
  89.             {"Nature","1019"},
  90.             {"Fishing","10033"},
  91.             {"Gardening","10045"},
  92.             {"Foraging","10071"},
  93.             {"Botanizing","10072"},
  94.             {"Animal taming","10078"},
  95.             {"Forestry","10048"},
  96.             {"Farming","10049"},
  97.             {"Milking","10060"},
  98.             {"Meditating","10086"},
  99.             {"Animal husbandry","10085"},
  100.             {"Papyrusmaking","10091"},
  101.             {"Toys","1022"},
  102.             {"Yoyo","10050"},
  103.             {"Puppeteering","10087"},
  104.             {"Fighting","1023"},
  105.             {"Defensive fighting","10054"},
  106.             {"Normal fighting","10055"},
  107.             {"Weaponless fighting","10052"},
  108.             {"Aggressive fighting","10053"},
  109.             {"Shield bashing","10058"},
  110.             {"Taunting","10057"},
  111.             {"Miscellaneous items","1020"},
  112.             {"Shovel","10002"},
  113.             {"Rake","10004"},
  114.             {"Saw","10008"},
  115.             {"Pickaxe","10009"},
  116.             {"Repairing","10035"},
  117.             {"Sickle","10046"},
  118.             {"Scythe","10047"},
  119.             {"Hammer","10026"},
  120.             {"Stone chisel","10030"},
  121.             {"Alchemy","1021"},
  122.             {"Natural substances","10042"},
  123.             {"Shields","1002"},
  124.             {"Medium metal shield","10006"},
  125.             {"Small wooden shield","10019"},
  126.             {"Small metal shield","10022"},
  127.             {"Large metal shield","10023"},
  128.             {"Medium wooden shield","10020"},
  129.             {"Large wooden shield","10021"},
  130.             {"Axes","1003"},
  131.             {"Hatchet","10003"},
  132.             {"Small Axe","10001"},
  133.             {"Large axe","10024"},
  134.             {"Huge axe","10025"},
  135.             {"Swords","1000"},
  136.             {"Longsword","10005"},
  137.             {"Shortsword","10027"},
  138.             {"Two handed sword","10028"},
  139.             {"Knives","1001"},
  140.             {"Carving knife","10007"},
  141.             {"Butchering knife","10029"},
  142.             {"Woodcutting","1007"},
  143.             {"Mauls","1004"},
  144.             {"Medium maul","10062"},
  145.             {"Small maul","10063"},
  146.             {"Large maul","10061"},
  147.             {"Carpentry","1005"},
  148.             {"Bowyery","1031"},
  149.             {"Fletching","1032"},
  150.             {"Fine carpentry","10044"},
  151.             {"Toy making","10051"},
  152.             {"Ship building","10082"},
  153.             {"Mind","2"},
  154.             {"Mind logic","100"},
  155.             {"Mind speed","101"},
  156.             {"Soul","3"},
  157.             {"Soul depth","106"},
  158.             {"Soul strength","105"},
  159.             {"Body","1"},
  160.             {"Body strength","102"},
  161.             {"Body stamina","103"},
  162.             {"Body control","104"},
  163.             {"Faith","-1"},         // Update on Player
  164.             {"Favor","-2"},         // Update on Player
  165.             {"Alignment","-3"}      // Update on player
  166.     };
  167.  
  168.     private static String searchSkill(String skillname) {
  169.         String skillvalue = "";
  170.         int skillcount = Skills.length;
  171.         int c = 0;
  172.  
  173.         for (c = 0; !skillname.equals(Skills[c][0]); c++) ;
  174.  
  175.         if (c < skillcount) skillvalue = Skills[c][1];
  176.         else if (skillname.equals(Skills[c][0])) skillvalue = Skills[c][1];
  177.  
  178.         return skillvalue;
  179.     }
  180.  
  181.     private static String getID(String charname) {
  182.         String sID = "";
  183.         ResultSet resultSet = null;  
  184.         PreparedStatement pStatement = null;
  185.  
  186.         try {
  187.             pStatement = cPlayers.prepareStatement("Select WURMID from PLAYERS where NAME = ?");
  188.             pStatement.setString(1, charname);
  189.  
  190.             resultSet = pStatement.executeQuery();
  191.  
  192.             while (resultSet.next()) {
  193.                 sID = resultSet.getString(1);
  194.             }
  195.         } catch (Exception e) {
  196.             System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  197.         }
  198.         return sID;
  199.     }
  200.  
  201.     private static void getMaxSkill() {
  202.         ResultSet resultSet = null;
  203.         PreparedStatement pStatement = null;
  204.  
  205.         try {
  206.             pStatement = cLogins.prepareStatement("Select PLAYERSKILLIDS from IDS where SERVER = ?");
  207.             pStatement.setLong(1, serverID);
  208.  
  209.             resultSet = pStatement.executeQuery();
  210.  
  211.             while (resultSet.next()) {
  212.                 currNextSkill = resultSet.getInt(1);
  213.             }
  214.         } catch (Exception e) {
  215.             System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  216.         }
  217.  
  218.     }
  219.  
  220.     private static void saveMaxSkill() {
  221.         PreparedStatement pStatement = null;
  222.  
  223.         try {
  224.             pStatement = cLogins.prepareStatement("update IDS set PLAYERSKILLIDS = ? where SERVER = ?");
  225.             pStatement.setLong(1, currNextSkill);
  226.             pStatement.setLong(2, serverID);
  227.  
  228.             pStatement.executeUpdate();
  229.         } catch (Exception e) {
  230.             System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  231.         }
  232.  
  233.     }
  234.  
  235.     private static int updateSpecial(String charID, int type, String value) {
  236.         ResultSet resultSet = null;  
  237.         PreparedStatement pStatement = null;
  238.         int retval = 0;
  239.         int deityVal = 0;
  240.         String attVal = "";
  241.  
  242.         try {
  243.             // check if has diety
  244.             StringBuilder statement = new StringBuilder("Select DEITY,");
  245.  
  246.             switch (type) {
  247.             case -1 : { statement.append("FAITH"); break; }
  248.             case -2 : { statement.append("FAVOR"); break; }
  249.             case -3 : { statement.append("ALIGNMENT"); break; }
  250.             default : return -1; // should never happen
  251.             }
  252.             statement.append(" from PLAYERS where WURMID = ?");
  253.             pStatement = cPlayers.prepareStatement(statement.toString());
  254.             pStatement.setString(1, charID);
  255.  
  256.             resultSet = pStatement.executeQuery();
  257.  
  258.             while (resultSet.next()) {
  259.                 deityVal = resultSet.getInt(1);
  260.                 attVal = resultSet.getString(2);
  261.  
  262.                 if ((deityVal < deityFo || deityVal > deityLib) && type != -3) return -2; // no valid deity for faith/favor
  263.                 if (attVal.equals(value)) return -1;    // no change
  264.                 if (Double.valueOf(attVal) > Double.valueOf(value)) return 0;// why decrease a skill
  265.             }
  266.  
  267.             resultSet.close();
  268.             pStatement.close();
  269.  
  270.             statement.setLength(0);
  271.             statement.append("Update PLAYERS set ");
  272.             switch (type) {
  273.             case -1 : { statement.append("FAITH"); break; }
  274.             case -2 : { statement.append("FAVOR"); break; }
  275.             case -3 : { statement.append("ALIGNMENT"); break; }
  276.             default : return -1; // should never happen
  277.             }
  278.             statement.append(" = ? where WURMID = ?");
  279.  
  280.             pStatement = cPlayers.prepareStatement(statement.toString());
  281.             pStatement.setString(1, value);
  282.             pStatement.setString(2, charID);
  283.  
  284.             pStatement.executeUpdate();
  285.             retval = 1;
  286.  
  287.         } catch (Exception e) {
  288.             System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  289.         }
  290.  
  291.  
  292.         return retval;
  293.     }
  294.  
  295.     @SuppressWarnings("resource")
  296.     private static int updateSkill(String charID, String skillID, String value) {
  297.         ResultSet resultSet = null;  
  298.         PreparedStatement pStatement = null;
  299.         boolean bExists = false;
  300.         int retval = 0;
  301.         String curValue = "";
  302.  
  303.         try {
  304.             // check if exists
  305.             pStatement = cPlayers.prepareStatement("Select VALUE from SKILLS where OWNER = ? and NUMBER = ?");
  306.             pStatement.setString(1, charID);
  307.             pStatement.setString(2, skillID);
  308.  
  309.             resultSet = pStatement.executeQuery();
  310.  
  311.             while (resultSet.next()) {
  312.                 curValue = resultSet.getString(1);
  313.  
  314.                 if (curValue.equals(value)) return -1; // no change, we're done
  315.                 if (Double.valueOf(curValue) > Double.valueOf(value)) return 0; // why decrease a skill
  316.                 bExists = true;
  317.             }
  318.  
  319.             resultSet.close();
  320.             pStatement.close();
  321.  
  322.             if (bExists) {
  323.                 pStatement = cPlayers.prepareStatement("Update SKILLS set VALUE = ?, LASTUSED = ? where OWNER = ? and NUMBER = ?");
  324.                 pStatement.setString(1, value);
  325.                 pStatement.setLong(2, lastused);
  326.                 pStatement.setString(3, charID);
  327.                 pStatement.setString(4, skillID);
  328.  
  329.                 pStatement.executeUpdate();
  330.                 retval = 1;
  331.             } else {
  332.                 pStatement = cPlayers.prepareStatement("Insert into SKILLS (ID,OWNER, NUMBER, VALUE, MINVALUE,LASTUSED) Values (?,?,?,?,?,?)");
  333.                 pStatement.setLong(1, getNextSkill());
  334.                 pStatement.setString(2, charID);
  335.                 pStatement.setString(3, skillID);
  336.                 pStatement.setString(4, value);
  337.                 pStatement.setDouble(5, ((Double.valueOf(value)>10)?Double.valueOf(value):10));
  338.                 pStatement.setLong(6, lastused);
  339.                 pStatement.executeUpdate();
  340.                 retval = 2;
  341.             }
  342.  
  343.         } catch (Exception e) {
  344.             System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  345.         }
  346.  
  347.         return retval;
  348.     }
  349.     private static long getNextSkill() {
  350.         currNextSkill++;
  351.         return BigInteger.valueOf(currNextSkill).shiftLeft(24).longValue() + (long)(serverID << 8) + 10L;
  352.     }
  353.  
  354.     public static void main(String[] args) {
  355.         int len = 0;
  356.         String sDatapath = "";
  357.         String sDatafile = "";
  358.         String sCharname = "";
  359.         String sCharID = "";
  360.  
  361.         lastused = System.currentTimeMillis();
  362.  
  363.         try {
  364.               Class.forName("org.sqlite.JDBC");
  365.               System.out.print("Database path: ");
  366.               byte[] bDatapath = new byte[256];
  367.               len = System.in.read(bDatapath);
  368.  
  369.               if (len >0) {
  370.                   sDatapath = new String(bDatapath,0,len-2);
  371.                   sDatafile = sDatapath + "\\wurmplayers.db";
  372.                   cPlayers = DriverManager.getConnection("jdbc:sqlite:"+sDatafile);
  373.                   sDatafile = sDatapath + "\\wurmlogin.db";
  374.                   cLogins =  DriverManager.getConnection("jdbc:sqlite:"+sDatafile);
  375.                   getMaxSkill();
  376.               }
  377.  
  378.         } catch ( Exception e ) {
  379.               System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  380.               System.exit(0);
  381.         }
  382.         System.out.println("Opened database successfully");
  383.         try {      
  384.             System.out.print("Character Name: ");
  385.             // validate name
  386.             byte[] bCharname = new byte[64];
  387.             len = System.in.read(bCharname);
  388.             if (len > 0) {
  389.                 sCharname = new String(bCharname,0,len-2);
  390.                 sCharID = getID(sCharname);
  391.  
  392.                 if (sCharID.isEmpty()) {
  393.                     System.out.println("No character "+sCharname+" exists");
  394.                     System.exit(-1);
  395.                 }
  396.             } else {
  397.                 System.out.println("No character name entered.");
  398.                 System.exit(-1);
  399.             }
  400.  
  401.             System.out.print("Import file: ");
  402.             byte[] bFilepath = new byte[256];
  403.  
  404.             len = System.in.read(bFilepath);
  405.  
  406.  
  407.             if (len > 0) {
  408.                 String sFilepath = new String(bFilepath,0,len-2);
  409.                 File skillFile = new File(sFilepath);
  410.  
  411.                 if (skillFile.exists()) {
  412.                     int iChar;
  413.                     boolean bLineStart = true;
  414.                     boolean bFoundSkill = false;
  415.                     boolean bReadingSkill = false;
  416.                     boolean bReadingValue = false;
  417.                     boolean bDoBreak = false;
  418.                     String curSkill = "";
  419.                     String curValue = "";
  420.                     FileInputStream fis = new FileInputStream(skillFile);
  421.  
  422.                     while ((iChar = fis.read()) !=  -1) {
  423.                         // Process the chars
  424.                         if (bLineStart && (iChar != iSP)) {
  425.                             // Ignore any line that doesn't start with SPACE
  426.                             while (fis.read() != iCR) ;
  427.                             fis.read(); // get the LF too.
  428.                             continue;
  429.                         } else bLineStart = false;
  430.  
  431.                         switch (iChar) {
  432.                         case iSP:
  433.                             if (bReadingSkill)
  434.                                 curSkill = curSkill + (char)iChar;
  435.                             else if (bReadingValue) {
  436.                                 int iTrash;
  437.                                 bReadingValue = false;
  438.  
  439.                                 while ((iTrash=fis.read()) != iCR)
  440.                                     if (iTrash == -1) {
  441.                                         bDoBreak = true;
  442.                                         break;
  443.                                     }
  444.  
  445.                                 // Process each skill name and first value it finds
  446.                                 // search for skill number in array and insert number/value/userid if doesn't exist, else update
  447.                                 String curSkillVal = searchSkill(curSkill);
  448.                                 int retval = 0;
  449.  
  450.                                 if (Integer.parseInt(curSkillVal.trim()) > 0) {
  451.                                     retval = updateSkill(sCharID,curSkillVal,curValue);
  452.                                 } else
  453.                                     retval = updateSpecial(sCharID, Integer.parseInt(curSkillVal.trim()),curValue);                                
  454.  
  455.                                 System.out.print("Current Skill: "+curSkill+" Current Value: "+curValue+" ");
  456.                                 switch (retval) {
  457.                                 case 1: { System.out.println("Updated"); break; }
  458.                                 case 2: { System.out.println("Inserted"); break; }
  459.                                 case 0: { System.out.println("Saved higher"); break; }
  460.                                 case -1: { System.out.println("No change"); break; }
  461.                                 case -2: System.out.println("Need deity first");
  462.                                 }
  463.  
  464.                                 if (bDoBreak) break;
  465.                                 iTrash = fis.read(); // get the LF too.
  466.                                 // Reset for next line
  467.                                 curSkill = "";
  468.                                 curValue = "";
  469.                                 bLineStart = true;
  470.                                 bFoundSkill = false;
  471.                             }
  472.                             continue; //ignore char
  473.                         case iCO:
  474.                             bFoundSkill = true;
  475.                             bReadingSkill = false;
  476.                             continue; //ignore, set flag to grab value
  477.                         default:
  478.                             if (!bFoundSkill) {
  479.                                 bReadingSkill = true;
  480.                                 curSkill = curSkill + (char)iChar;
  481.                             } else {
  482.                                 bReadingValue = true;
  483.                                 curValue = curValue + (char)iChar;
  484.                             }
  485.                         }
  486.  
  487.                         if (bDoBreak) break;
  488.                     }
  489.  
  490.                     fis.close();
  491.                     saveMaxSkill();
  492.                 } else {
  493.                     System.out.println("File '"+sFilepath+ "' does not exist.");
  494.                 }
  495.             } else {
  496.                 System.out.println("No filename entered.");
  497.             }
  498.         } catch (Exception e) {
  499.             System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  500.             System.exit(0);
  501.         }
  502.     }
  503.  
  504. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement