/**
*
* @author Daniel
*/
package server.model.minigames;
import java.util.ArrayList;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.DataOutputStream;
import java.io.DataInputStream;
public class MiniGameCore implements Runnable {
ArrayList<String> playerNames = new ArrayList<String>();
ArrayList<String> playerScores = new ArrayList<String>();
private String gameName;
private DataOutputStream out;
private DataInputStream dataIn;
private boolean read = true;
private int spot;
private String[] topScore = { "", "" };
public MiniGameCore(String name) {
gameName = name;
writeData();
loadData();
}
public void run() {
}
public void addBestScore(String player, int score) {
try {
int temp;
if(playerNames.contains(player)) {
temp = playerNames.indexOf(player);
playerScores.set(temp, Integer.toBinaryString(score));
} else {
playerNames.add(player);
playerScores.add(Integer.toBinaryString(score));
}
writeData();
} catch(Exception e) {
System.out.println(e);
}
}
public void addToScore(String player, int score) {
try {
int temp;
if(playerNames.contains(player)) {
temp = playerNames.indexOf(player);
playerScores.set(temp, "" + Integer.parseInt(playerScores.get(temp) + score));
} else {
playerNames.add(player);
playerScores.add(Integer.toBinaryString(score));
}
writeData();
} catch(Exception e) {
System.out.println(e);
}
}
public void writeData() {
try {
out = new DataOutputStream(new FileOutputStream("C:/Projects/Fallout Data/Games/" + gameName + ".fall"));
String[] names = new String[playerNames.size()];
String[] scores = new String[playerNames.size()];
playerNames.toArray(names);
playerScores.toArray(scores);
for(int i = 0; i < names.length; i++) {
out.writeUTF(names[i] + ", " + scores[i]);
}
} catch(Exception e) {
System.out.println(e);
}
}
public void loadData() {
try {
dataIn = new DataInputStream(new FileInputStream("C:/Projects/Fallout Data/Games/" + gameName + ".fall"));
while(read) {
try {
++spot;
String temp = dataIn.readUTF();
String[] temp2 = temp.split(", ");
playerNames.add(spot, temp2[0]);
playerScores.add(spot, temp2[1]);
} catch(Exception e) {
read = false;
}
}
} catch(Exception e) {
System.out.println(e);
}
}
/*
* The reason for the lack of ties is that the first person to reach the
* highest score will win the game in the event of a tie
*/
public void getBestScore() {
try {
for(int i = 0; i < playerScores.size(); i++) {
if(Integer.parseInt(playerScores.get(i))
> Integer.parseInt(topScore[1])) {
topScore[0] = playerNames.get(i);
topScore[1] = playerScores.get(i);
}
}
} catch(Exception e) {
System.out.println(e);
}
}
public String getWinner() {
return topScore[0];
}
}