home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / enxle1f6 / src / games / battle / shared / comm / playerinfo.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  10.2 KB  |  414 lines

  1. /*
  2.  * @(#)PlayerInfo.java
  3.  */
  4. package games.Battle.shared.comm;
  5.  
  6. import games.Rating.Rating;
  7.  
  8. import java.io.*;
  9. import java.util.*;
  10.  
  11. /**
  12.  * PlayerInfo records all of the information about a particular player for
  13.  * the client and the server. Since the PlayerInfo structure contains a 
  14.  * password, the server must be careful to only send you copies of your
  15.  * own PlayerInfo structure: the derived WhoInfo is used to transmit info
  16.  * about other players.
  17.  *
  18.  * @version 1.00
  19.  * @author Alex Nicolaou
  20.  * @author Jay Steele
  21.  */
  22.  
  23. public class PlayerInfo extends BattlePacket
  24. {
  25.     /**
  26.      * A separator used for storing this packet as a string, and for parsing
  27.      * the string representation.
  28.      */
  29.     final static String FIELDSEP = "|";
  30.  
  31.     /**
  32.      * The user's login ID, which may be any unique login id they choose.
  33.      */
  34.     String handle; 
  35.     /**
  36.      * The user's real name, which hopefully is actually their real name.
  37.      */
  38.     String name;
  39.     /**
  40.      * The user's email address (optional).
  41.      */
  42.     String mail;
  43.     /**
  44.      * The user's password. This should be transmitted and stored in encrypted
  45.      * forms, but encryption was forbidden for use for the Java Cup 
  46.      * International, no doubt a side effect of the US Governments' definition
  47.      * of encryption algorithms as munitions of war. Next time Canada invades
  48.      * the US you can bet we won't be able to understand each other which will
  49.      * hopefully prolong the war, as any good encryption algorithm should be 
  50.      * able to do, proving that encryption must be secret. Which way to the
  51.      * border again?
  52.      */
  53.     String pass;
  54.     /**
  55.      * A wizard bit for the player. Wizards are allowed to shutdown the server
  56.      * cleanly, but have no other interesting powers yet.
  57.      */
  58.     boolean wizard;
  59.     /**
  60.      * The player's rating.
  61.      */
  62.     Rating rating;
  63.     /**
  64.      * The player's preference for whether the client should use sound or not.
  65.      */
  66.     boolean sound;
  67.     /**
  68.      * The player's preference for whether the client should use 3D style
  69.      * graphics or not.
  70.      */
  71.     boolean arcade;
  72.  
  73.     /**
  74.      * Constructs a player info from the information usually given on the
  75.      * registration screen.
  76.      */
  77.     public PlayerInfo(String handle, String name, String mail, String pass) {
  78.         this.handle = handle;
  79.         this.name = name;
  80.         this.mail = mail;
  81.         this.pass = pass;
  82.         rating = new Rating();
  83.         sound = true;
  84.         arcade = true;
  85.     }
  86.  
  87.     /**
  88.      * Constructs a player info object from complete information on the
  89.      * player.
  90.      */
  91.     public PlayerInfo(String handle, String name,String mail, String pass, boolean wizard, Rating rating, boolean sound, boolean arcade) {
  92.         this.handle = handle;
  93.         this.name = name;
  94.         this.mail = mail;
  95.         this.pass = pass;
  96.         this.wizard = wizard;
  97.         this.rating = rating;
  98.         this.sound = sound;
  99.         this.arcade = arcade;
  100.     }
  101.  
  102.     /**
  103.      * Constructs a PlayerInfo object from a string representation that was
  104.      * obtained by calling toString() on a previous PlayerInfo object. 
  105.      * Currently convenient for writing and reading the player file.
  106.      * @param stringRep the string representation of this user
  107.      */
  108.     public PlayerInfo(String stringRep) {
  109.         StringTokenizer data = new StringTokenizer(stringRep, FIELDSEP, true); 
  110.  
  111.         handle = data.nextToken();
  112.         data.nextToken();
  113.  
  114.         name = data.nextToken();
  115.         data.nextToken();
  116.  
  117.         mail = data.nextToken();
  118.         if (mail.equals("|"))
  119.             mail = "";
  120.         else
  121.             data.nextToken();
  122.         pass = data.nextToken();
  123.         data.nextToken();
  124.  
  125.         String wiz = data.nextToken();
  126.         if (wiz.charAt(0) == 't')
  127.             wizard = true;
  128.         else
  129.             wizard = false;
  130.         data.nextToken();
  131.  
  132.         String rstring = data.nextToken();
  133.         rating = new Rating(rstring);
  134.         data.nextToken();
  135.  
  136.         String snd = data.nextToken();
  137.         if (snd.charAt(0) == 't')
  138.             sound = true;
  139.         else
  140.             sound = false;
  141.         data.nextToken();
  142.  
  143.         String arc = data.nextToken();
  144.         if (arc.charAt(0) == 't')
  145.             arcade = true;
  146.         else
  147.             arcade = false;
  148.     }
  149.  
  150.     /**
  151.      * Construct an empty player info object, ready to be read from an input
  152.      * stream.
  153.      */
  154.     public PlayerInfo() {
  155.     }
  156.  
  157.     /**
  158.      * Converts this PlayerInfo to an array of bytes for
  159.      * transmission.
  160.      */
  161.     protected byte[] makeBytes(String s) {
  162.         // java.lang.String doesn't give us a way to know how many bytes
  163.         // we're going to get back; kludge: use knowledge of internal
  164.         // implementation to make the correct size array
  165.         // as an aside, it really ought to give us two bytes per char? 
  166.         byte bytes[] = new byte[s.length() + 1];
  167.  
  168.         int stringLength = s.length();
  169.         // max we allow is 254 characters 
  170.         if (stringLength > 255)
  171.             stringLength = 254;
  172.  
  173.         bytes[0] = (byte)stringLength;
  174.  
  175.         s.getBytes(0, stringLength, bytes, 1);
  176.  
  177.         return bytes;
  178.     }
  179.  
  180.     /**
  181.      * catBytes takes a vector of byte arrays and concatenates them into
  182.      * one long byte aray which is then returned to the caller.
  183.      * @param strings the vector of byte arrays to be glued together
  184.      */
  185.     protected byte[] catBytes(Vector strings) {
  186.         Enumeration e = strings.elements();
  187.         int totalLen = 0;
  188.  
  189.         while (e.hasMoreElements()) {
  190.             byte b[] = (byte[])e.nextElement();
  191.             totalLen += b.length;
  192.         }
  193.  
  194.         byte b[] = new byte[totalLen];
  195.         int pos = 0;
  196.  
  197.         e = strings.elements();
  198.         while (e.hasMoreElements()) {
  199.             byte subarray[] = (byte[])e.nextElement();
  200.             for (int i = 0; i < subarray.length; i++) {
  201.                 b[pos] = subarray[i];
  202.                 pos++;
  203.             }
  204.         }
  205.  
  206.         return b;
  207.     }
  208.  
  209.     /**
  210.      * return a byte array that represents this packet.
  211.      */
  212.     protected byte[] toBytes() {
  213.         Vector strings = new Vector(4);
  214.         strings.addElement(makeBytes(handle));
  215.         strings.addElement(makeBytes(name));
  216.         strings.addElement(makeBytes(mail));
  217.         strings.addElement(makeBytes(pass));
  218.         byte[] data = new byte[6];
  219.         data[0] = (byte)255;
  220.         
  221.         int r = (int)rating.getRating();
  222.         int rd = (int)rating.getRatingDeviation();
  223.         data[1] = (byte)((r & 0xff00)>>8);
  224.         data[2] = (byte)((r & 0x00ff));
  225.         data[3] = (byte)((rd & 0xff00)>>8);
  226.         data[4] = (byte)((rd & 0x00ff));
  227.  
  228.         data[5] = 0;
  229.  
  230.         if (wizard)
  231.             data[5] |= 0x01;
  232.         if (sound)
  233.             data[5] |= 0x02;
  234.         if (arcade)
  235.             data[5] |= 0x04;
  236.  
  237.         strings.addElement(data);
  238.  
  239.         return catBytes(strings);
  240.     }
  241.  
  242.     /**
  243.      * Takes in a concatenated list of byte arrays and produces a vector
  244.      * of strings from it. A sentinel is used to distinguish the byte 
  245.      * arrays from the rest of the PlayerInfo packet.
  246.      * @param data the byte array that contains data for this packet
  247.      */
  248.     protected Vector getStrings(byte[] data) {
  249.         int pos = 0;
  250.         Vector strings = new Vector();
  251.         while (pos < data.length) {
  252.             int stringLength = data[pos];
  253.  
  254.             // check for 255 because on IRIX this test fails.
  255.             // perhaps netscape compiles with signed chars on IRIX
  256.             // but unsigned chars on other platforms?
  257.             if (stringLength == -1 || stringLength == 255) {
  258.                 return strings;
  259.             }
  260.  
  261.             pos++;
  262.  
  263.             String s = new String(data, 0, pos, stringLength);
  264.             strings.addElement(s);
  265.             pos += stringLength;
  266.         }
  267.  
  268.         return strings;
  269.     }
  270.  
  271.     /**
  272.      * Converts the given array of bytes into a particular
  273.      * PlayerInfo.
  274.      * @param data the array of data to be converted
  275.      */
  276.     protected void fromBytes(byte[] data) {
  277.         Vector strings = getStrings(data);
  278.         Enumeration e = strings.elements();
  279.         handle = (String)e.nextElement();
  280.         name = (String)e.nextElement();
  281.         mail = (String)e.nextElement();
  282.         pass = (String)e.nextElement();
  283.  
  284.         wizard = sound = arcade = false;
  285.  
  286.         if ((data[data.length-1]&0x01) != 0)
  287.             wizard = true;
  288.         if ((data[data.length-1]&0x02) != 0)
  289.             sound = true;
  290.         if ((data[data.length-1]&0x04) != 0)
  291.             arcade = true;
  292.  
  293.         int r = makeInt(data[data.length-4], data[data.length-5]);
  294.         int rd = makeInt(data[data.length-2], data[data.length-3]);
  295.         rating = new Rating(r, rd);
  296.     }
  297.  
  298.     /**
  299.      * takes two bytes and produces an integer from them. Sure, two bytes
  300.      * are only long enough for a short, but we want an integer anyway.
  301.      */
  302.     int makeInt(byte b0, byte b1) {
  303.         int i0 = (int)b0 & 0x00ff;
  304.         int i1 = (int)b1 & 0x00ff;
  305.         return (i1<<8)|i0;
  306.     }
  307.  
  308.     /**
  309.      * return true if the ID's are identical ignoring case.
  310.      */
  311.     public boolean sameId(PlayerInfo other) {
  312.         return handle.equalsIgnoreCase(other.handle);
  313.     }
  314.  
  315.     /**
  316.      * return true if the passwords are identical.
  317.      */
  318.     public boolean samePass(PlayerInfo other) {
  319.         return pass.equals(other.pass);
  320.     }
  321.  
  322.     /**
  323.      * produce text output for this PlayerInfo
  324.      */
  325.     public void asciiDump() {
  326.         System.out.println("handle: " + handle);
  327.         System.out.println("name: " + name);
  328.         System.out.println("mail: " + mail);
  329.         System.out.println("pass: " + /* pass */ "<SECRET>");
  330.     }
  331.  
  332.     /**
  333.      * return the handle of this player.
  334.      */
  335.     public String getHandle() { return handle; }
  336.     /**
  337.      * return the password of this player.
  338.      */
  339.     public String getPassword() { return pass; }
  340.     /**
  341.      * return the real name of this player.
  342.      */
  343.     public String getName() { return name; }
  344.     /**
  345.      * return the email address of this player.
  346.      */
  347.     public String getMail() { return mail; }
  348.  
  349.     /**
  350.      * set the password of this player.
  351.      */
  352.     public void setPassword(String newpass) { pass = newpass; }
  353.     /**
  354.      * set the real name of this player.
  355.      */
  356.     public void setName(String newname) { name = newname; }
  357.     /**
  358.      * set the email address of this player.
  359.      */
  360.     public void setMail(String newmail) { mail = newmail; }
  361.  
  362.     /**
  363.      * convert to a string representation where all the data is in fields
  364.      * separated by the FIELDSEP character.
  365.      */
  366.     public String toString() {
  367.         return handle + FIELDSEP + name + FIELDSEP + mail + FIELDSEP + pass 
  368.                       + FIELDSEP + wizard + FIELDSEP + rating + FIELDSEP 
  369.                       + sound + FIELDSEP + arcade;
  370.     }
  371.  
  372.     /**
  373.      * return the player's rating.
  374.      */
  375.     public Rating getRating() {
  376.         return rating;
  377.     }
  378.  
  379.     /**
  380.      * returns true if the player is a wizard (administrator)
  381.      */
  382.     public boolean isWizard() {
  383.         return wizard;
  384.     }
  385.  
  386.     /**
  387.      * returns true if the player wants the client to use sound.
  388.      */
  389.     public boolean useSound() {
  390.         return sound;
  391.     }
  392.  
  393.     /**
  394.      * returns true if the player wants the client to be arcade-style (3D).
  395.      */
  396.     public boolean useArcade() {
  397.         return arcade;
  398.     }
  399.  
  400.     /**
  401.      * sets the user's sound preference (on or off)
  402.      */
  403.     public void setSound(boolean b) {
  404.         sound = b;
  405.     }
  406.  
  407.     /**
  408.      * sets the user's arcade (3D look) preference (on or off)
  409.      */
  410.     public void setArcade(boolean b) {
  411.         arcade = b;
  412.     }
  413. }
  414.