Guest
Public paste!

Minigame

By: a guest | Mar 21st, 2010 | Syntax: Java | Size: 3.70 KB | Hits: 57 | Expires: Never
Copy text to clipboard
  1. /**
  2.  *
  3.  * @author Daniel
  4.  */
  5.  
  6. package server.model.minigames;
  7.  
  8. import java.util.ArrayList;
  9. import java.io.FileOutputStream;
  10. import java.io.FileInputStream;
  11. import java.io.DataOutputStream;
  12. import java.io.DataInputStream;
  13.  
  14. public class MiniGameCore implements Runnable {
  15.  
  16.     ArrayList<String> playerNames = new ArrayList<String>();
  17.     ArrayList<String> playerScores = new ArrayList<String>();
  18.     private String gameName;
  19.     private DataOutputStream out;
  20.     private DataInputStream dataIn;
  21.     private boolean read = true;
  22.     private int spot;
  23.     private String[] topScore = { "", "" };
  24.  
  25.     public MiniGameCore(String name) {
  26.         gameName = name;
  27.         writeData();
  28.         loadData();
  29.     }
  30.  
  31.     public void run() {
  32.     }
  33.  
  34.     public void addBestScore(String player, int score) {
  35.         try {
  36.             int temp;
  37.             if(playerNames.contains(player)) {
  38.                 temp = playerNames.indexOf(player);
  39.                 playerScores.set(temp, Integer.toBinaryString(score));
  40.             } else {
  41.                 playerNames.add(player);
  42.                 playerScores.add(Integer.toBinaryString(score));
  43.             }
  44.             writeData();
  45.         } catch(Exception e) {
  46.             System.out.println(e);
  47.         }
  48.     }
  49.  
  50.     public void addToScore(String player, int score) {
  51.         try {
  52.             int temp;
  53.             if(playerNames.contains(player)) {
  54.                 temp = playerNames.indexOf(player);
  55.                 playerScores.set(temp, "" + Integer.parseInt(playerScores.get(temp) + score));
  56.             } else {
  57.                 playerNames.add(player);
  58.                 playerScores.add(Integer.toBinaryString(score));
  59.             }
  60.             writeData();
  61.         } catch(Exception e) {
  62.             System.out.println(e);
  63.         }
  64.     }
  65.  
  66.     public void writeData() {
  67.         try {
  68.             out = new DataOutputStream(new FileOutputStream("C:/Projects/Fallout Data/Games/" + gameName + ".fall"));
  69.             String[] names = new String[playerNames.size()];
  70.             String[] scores = new String[playerNames.size()];
  71.             playerNames.toArray(names);
  72.             playerScores.toArray(scores);
  73.             for(int i = 0; i < names.length; i++) {
  74.                  out.writeUTF(names[i] + ", " + scores[i]);
  75.             }
  76.         } catch(Exception e) {
  77.             System.out.println(e);
  78.         }
  79.     }
  80.  
  81.     public void loadData() {
  82.         try {
  83.             dataIn = new DataInputStream(new FileInputStream("C:/Projects/Fallout Data/Games/" + gameName + ".fall"));
  84.             while(read) {
  85.                 try {
  86.                     ++spot;
  87.                     String temp = dataIn.readUTF();
  88.                     String[] temp2 = temp.split(", ");
  89.                     playerNames.add(spot, temp2[0]);
  90.                     playerScores.add(spot, temp2[1]);
  91.                 } catch(Exception e) {
  92.                     read = false;
  93.                 }
  94.              }
  95.         } catch(Exception e) {
  96.             System.out.println(e);
  97.         }
  98.     }
  99.  
  100.     /*
  101.      * The reason for the lack of ties is that the first person to reach the
  102.      * highest score will win the game in the event of a tie
  103.      */
  104.     public void getBestScore() {
  105.         try {
  106.             for(int i = 0; i < playerScores.size(); i++) {
  107.                 if(Integer.parseInt(playerScores.get(i))
  108.                         > Integer.parseInt(topScore[1])) {
  109.                     topScore[0] = playerNames.get(i);
  110.                     topScore[1] = playerScores.get(i);
  111.                 }
  112.             }
  113.         } catch(Exception e) {
  114.             System.out.println(e);
  115.         }
  116.     }
  117.  
  118.     public String getWinner() {
  119.         return topScore[0];
  120.     }
  121. }