1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Score Sender Coding Help?

Discussion in 'Code Snippets and Tutorials' started by dreamlorde, May 20, 2011.

  1. dreamlorde

    dreamlorde Level III

    Joined:
    Feb 17, 2007
    Messages:
    470
    Likes Received:
    32
    Location:
    Tijuana
    I'm not asking for anyone to explain all about it, I'm just looking for sample source code, snippets, tutorials, or anything that would help. I'm certain there must be some here but I've searched the forum a few times & I just can't find anything. :(
     
  2. tharoux

    tharoux Level IV

    Joined:
    Dec 30, 2006
    Messages:
    2,733
    Likes Received:
    126
    Location:
    In front of my PC, Montreal
    I have this java code sleeping in a dusty folder...
    Don't know if it's still accurate but this should help you understand what's going on everytime you send a score.

    Code (Text):
    1. import java.text.DecimalFormat;
    2. import java.util.ArrayList;
    3. import java.util.regex.Pattern;
    4. import java.util.regex.Matcher;
    5. import java.io.BufferedReader;
    6. import java.io.FileReader;
    7. import java.io.File;
    8. //import java.io.BufferedWriter;
    9. //import java.io.FileWriter;
    10.  
    11. /**
    12.  * A class to encode and decode Neopets.com flash game scores.
    13.  * @author unlimitedorb
    14.  * @since 4/20/2008
    15.  */
    16. public class Crypt {
    17.     private static final String scoreBase = "http://www.neopets.com/high_scores/process_flash_score.phtml";
    18.     private String username;
    19.     private String sessionHash;
    20.     private String sessionKey;
    21.     private int gameID;
    22.     private int wait;
    23.     private int modifiedGameID;
    24.     private String bin;
    25.     private int score;
    26.     private int dailyChallenge; //Setting it isn't implemented yet. (Don't do Daily Challenges!)
    27.     private int averageFrameRate = 24; //Isn't currently implemented.
    28.     private int multiple; //Confirm later! Probably determines whether it was multiplayer or not.
    29.    
    30.     public Crypt(String username, String sh, String sk, int gameID, int wait, int score) {
    31.         this.username = username;
    32.         this.sessionHash = sh;
    33.         this.sessionKey  = sk;
    34.         this.gameID = gameID;
    35.         this.wait   = wait;
    36.         this.score  = score;
    37.         this.dailyChallenge = -1;
    38.         this.bin = sh + sk;
    39.         modifiedGameID = 300 * gameID;
    40.     }
    41.    
    42.     /**
    43.      * The class isn't meant to be used as a standalone, but this is provided just in case/for testing
    44.      * purposes. It's true purpose is as a utility to be used in a bot.
    45.      */
    46.     public static void main(String[] args) {
    47.         //Crypt crypt = new Crypt("username", "4c82431c320b55797868", "fcb375decc3f7e1e83c7", 500, 500, 500);
    48.         String request = crypt.encode();
    49.         System.out.println(request);
    50.     }
    51.    
    52.     /**
    53.      * Method to decode a Neopets.com flash game score. Useful for development.
    54.      * Hungarian notation in the original swf variables is stripped out.
    55.      * @param encryptedScore The score to be decoded.
    56.      */
    57.     public static String decode(String encryptedScore, String hash, String key) {
    58.         String res = reverseEscapeString(encryptedScore);
    59.         String result = reverseStringToHex(res, hash, key);
    60.         return result;
    61.     }
    62.    
    63.     /**
    64.      * A method to encode sensitive data.
    65.      * @return An encoded String.
    66.      */
    67.     public String encode() {
    68.         //There might be extra parameters added, but that's game specific.
    69.         StringBuilder data = new StringBuilder(scoreBase + "?cn=" + modifiedGameID + "&gd=" + wait);
    70.         //Format the parameter r in the style of ActionScript's Math.random()
    71.         DecimalFormat rFormat = new DecimalFormat("0.000000000000000000");
    72.         data.append("&r=" + rFormat.format(Math.random()));
    73.         String toEncrypt = "ssnhsh=" + sessionHash + "&ssnky=" + sessionKey + "&gmd=" + gameID + "&scr=" + this.score + "&frmrt=" + averageFrameRate + "&chllng=" + (dailyChallenge == -1? "": dailyChallenge) + "&gmdrtn=" + wait;
    74.         String eScore = addSlashes(toEncrypt);
    75.         data.append("&gmd_g=" + this.gameID + "&mltpl_g=" + multiple + "&gmdt_g=" + eScore + "&sh_g=" + sessionHash + "&sk_g=" + sessionKey + "&usrnm_g=" + username + "&dc_g=" + (dailyChallenge == -1? 0: dailyChallenge));
    76.         String result = data.toString();
    77.         return result;
    78.     }
    79.    
    80.     /**
    81.      * Scrambles information.
    82.      * @param data The information to be scrambled.
    83.      * @return Scrambled information.
    84.      */
    85.     public String addSlashes(String data) {
    86.         return this.escapeString(this.stringToHex(data));
    87.     }
    88.    
    89.     public String stringToHex(String data){
    90.         ArrayList<String> keys = getKeys(new File("keys.txt"));
    91.         String result = "";
    92.         int randomKey = (int) (Math.random() * keys.size());
    93.         int index = 0;
    94.         int count = 0;
    95.         String key = keys.get(randomKey);
    96.         while(index < data.length()) {
    97.             if (count >= this.bin.length()) {
    98.                 count = 0;
    99.             }
    100.             int current = key.indexOf(data.charAt(index));
    101.             if (current >= 0) {
    102.                 current = (current + key.indexOf(this.bin.charAt(count))) % key.length();
    103.                 result += key.charAt(current);
    104.             } else {
    105.                 result += data.charAt(index);
    106.             }
    107.             ++count;
    108.             ++index;
    109.         }
    110.         if (randomKey >= 10) {
    111.             result += String.valueOf(randomKey);
    112.             return result;
    113.         }
    114.         result += String.valueOf(0) + String.valueOf(randomKey);
    115.         return result;
    116.     }
    117.    
    118.     public static String reverseStringToHex(String data, String sHash, String sKey) {
    119.         int k = Integer.parseInt(data.substring(data.length() - 2));
    120.         ArrayList<String> keys = getKeys(new File("keys.txt"));
    121.         String key = keys.get(k);
    122.         data = data.substring(0, data.length() - 2);
    123.         String bin = sHash + sKey;
    124.         String result = "";
    125.         int count = 0;
    126.         int current = 0;
    127.         for(int i = 0; i < data.length(); i++) {
    128.             if(count >= bin.length()) {
    129.                 count = 0;
    130.             }
    131.             current = bin.charAt(count);
    132.             current = key.indexOf(data.charAt(i)) - key.indexOf(current);
    133.             if(current < 0) {
    134.                 result += key.charAt(key.length() + current);
    135.             } else {
    136.                 result += key.charAt(current);
    137.             }
    138.             count++;
    139.         }
    140.         return result;
    141.     }
    142.    
    143.     /**
    144.      * Generates a String of ASCII code points with leading 0's for decoding purposes.
    145.      * @param data The String to escape.
    146.      * @return An escaped String.
    147.      */
    148.     public String escapeString(String data) {
    149.         String result = "";
    150.         String current = "";
    151.         int index = 0;
    152.         int count = 0;
    153.         while(index < data.length()) {
    154.             current = String.valueOf((int) data.charAt(index));
    155.             //None of the key lengths are <= 1
    156.             count = 3 - current.length();
    157.             while(count > 0) {
    158.                 current = "0" + current;
    159.                 --count;
    160.             }
    161.             result += current;
    162.             ++index;
    163.         }
    164.         return result;
    165.     }
    166.    
    167.     /**
    168.      * Reverses the escapeString process.
    169.      * @param data The string to reverse.
    170.      * @return An unescaped String.
    171.      */
    172.     public static String reverseEscapeString(String data) {
    173.         String result = "";
    174.         for(int i = 0; i < data.length(); i += 3) {
    175.             String current = data.substring(i, i + 3);
    176.             if(current.startsWith("0")) {
    177.                 current = current.substring(1, current.length());
    178.                 result += (char) Integer.parseInt(current);
    179.             } else {
    180.                 result += (char) Integer.parseInt(current);
    181.             }
    182.         }
    183.         return result;
    184.     }
    185.    
    186.     /**
    187.      * Retrieves keys from the text file "keys.txt"
    188.      * @param file The File to read the keys from.
    189.      * @return An ArrayList of Neopets.com flash game encryption keys.
    190.      */
    191.     public static ArrayList<String> getKeys(File file) {
    192.         ArrayList<String> keys = new ArrayList<String>();
    193.         keys.ensureCapacity(20);
    194.         try {
    195.             BufferedReader stream = new BufferedReader(new FileReader(file));
    196.             String line;
    197.             //BufferedWriter out = new BufferedWriter(new FileWriter(new File("reversedkeys.txt")));
    198.             while((line = stream.readLine()) != null) {
    199.                 String expression = "\\d+";
    200.                 Pattern pattern = Pattern.compile(expression);
    201.                 Matcher matcher = pattern.matcher(line);
    202.                 String eLine = new String("");
    203.                 while(matcher.find()) {
    204.                     eLine += (char) Integer.parseInt(line.substring(matcher.start(), matcher.end()));
    205.                 }
    206.                 keys.add(eLine);
    207.                 //out.write(eLine + "\n");
    208.                 //out.flush();
    209.             }
    210.             stream.close();
    211.             //out.close();
    212.         } catch(Exception e) {
    213.             e.printStackTrace();
    214.         }
    215.         return keys;
    216.     }
    217.    
    218.     /**
    219.      * @return the score
    220.      */
    221.     public int getScore() {
    222.         return this.score;
    223.     }
    224.    
    225.     public void setScore(int score) {
    226.         this.score = score;
    227.     }
    228. }
    229.  
     
    Kaden likes this.