//Jeff Ferguson Enter for High Score 15/7/13
//che-wei wang
//csv and string split code originally by che-wei wang with help from Chrisir
//Comparable references ColinOwens and PhiLho
import java.util.*;
int Index,csvWidth,Age;
boolean returned;
boolean up, down;
String typedEntry="";
String csvLines[];
color textColor = color (0,255,0,200);
String[][] csvList;
String Info,Score;
int[] sortIndex;
int timer;
PFont font;
EntryTable myTable;
EntryScreen myEntryScreen;
VotingScreen myVotingScreen;
//All entries held here live
ArrayList <Entry> entries = new ArrayList <Entry>();
//Array for sorting, from entries ArrayList
Entry[] entriesArray;
//Array for voting, from entries ArrayList
Entry[] entriesVotingArray;
void setup(){
frameRate(15);
size(1000, 1000);
font = createFont("Helvetica",24);
textFont(font);
//csvLines = loadStrings("List.txt");
//csvWidth = 2;
//for (int i=0; i < csvLines.length; i++) {
//String [] chars = split(csvLines[i],',');
//}
//csvList = new String [csvLines.length][csvWidth];
//for (int i=0; i < csvLines.length; i++) {
//String [] temp = new String [csvLines.length];
//temp= split(csvLines[i], ',');
//for (int j=0; j < temp.length; j++){
//csvList[i][j]=temp[j];
//}
//}
//for (int i=0; i < csvList.length; i++) {
//for (int j=0; j < 2 ; j++) {
//if (j==0) {Score = (csvList[i][j]);}
//if (j==1) {Info = (csvList[i][j]);}
//if (csvList[i][j]!=null && j==1){
//Age = int (year() + nf(month(),2) + nf(day(),2) + nf(hour(),2) + nf(minute(),2) + nf(second(),2));
//entries.add(new Entry(Index, Info, int(Score), Age)); //arguments are; int count of all Nodes, int Seq, int Key, year string, information string
//Index++;
//}
//}
entries.add(new Entry(0, "Kittenszzz",22042, 0));
entries.add(new Entry(1, "ChessDarts", 0, 0));
entries.add(new Entry(2, "ShortButFun", 100, 0));
entries.add(new Entry(3, "DinosaurStllThere", 3046, 0));
entries.add(new Entry(4, "BrakesGdTyresFr", 0, 0));
entries.add(new Entry(5, "GenerationalAmnesia", 5048, 0));
entries.add(new Entry(6, "TrainerGeddon", 0, 0));
entries.add(new Entry(7, "NotEvenWrong", 5048, 0));
entries.add(new Entry(8, "FakeFake", 500, 0));
entries.add(new Entry(9, "GrayGlitter", 101, 0));
entries.add(new Entry(10, "PowerAnimal", 499, 0));
entries.add(new Entry(11, "MosquitoSwamp", 10000, 0));
entries.add(new Entry(12, "EnoughsEnough", 50, 0));
//}
myTable = new EntryTable(); //run once - just assignment/initialization?
myEntryScreen = new EntryScreen();
myVotingScreen = new VotingScreen();
}
void draw(){
background(0);
myTable.display();
myEntryScreen.display();
myVotingScreen.display();
}
void keyPressed(){
if (key == CODED) {
if (keyCode == UP) {up=true;}
if (keyCode == DOWN) {down=true;}
}
}
void keyReleased() {
if (key == CODED) {
if (keyCode == UP) {up=false;}
if (keyCode == DOWN) {down=false;}
}
if (key != CODED) {
switch(key) {
case BACKSPACE:
typedEntry = typedEntry.substring(0,max(0,typedEntry.length()-1));
break;
case ENTER:
case RETURN:
myEntryScreen.addEntry();
typedEntry="";
break;
case ESC:
case DELETE:
break;
default:
typedEntry += key;
up=false;
down=false;
}
}
}
class ComparatorIndex implements Comparator {
int compare(Object o, Object p)
{
Entry o1=(Entry)o;
Entry p1=(Entry)p;
//return o1.compareTo(p1);
if (o1.eIndex>p1.eIndex)
return -1;
if (o1.eIndex==p1.eIndex)
return 0;
else
return 1;
}
}
class ComparatorScore implements Comparator {
int compare(Object o, Object p)
{
Entry o1=(Entry)o;
Entry p1=(Entry)p;
if (o1.eScore>p1.eScore)
return -1;
if (o1.eScore==p1.eScore)
return 0;
else
return 1;
}
}
class ComparatorVoted implements Comparator {
int compare(Object o, Object p)
{
Entry o1=(Entry)o;
Entry p1=(Entry)p;
//return o1.compareTo(p1);
if (o1.voted==true)
return 1;
if (o1.voted==false)
return 0;
else
return -1;
}
}
class Entry implements Comparable{
int eIndex;
int eAge;
int eScore;
int vScore;
int eSortIndex;
String eInfo;
color textColor;
PVector pos;
PVector vPos;
boolean voted;
Entry(int Index_, String Info_, int Score_, int Age_){
eIndex = Index_;
eInfo = Info_;
eScore = Score_;
eAge = Age_;
pos = new PVector(0,0,0);
vPos = new PVector((width - (width/20)),height/6,0);
textColor = color (0,255,0,200);
}
void tableUpdate(int sortIndex_){
//translate(vPos.x,vPos.y,vPos.z);
eSortIndex = sortIndex_;
pos.x=((width/2)-150);
pos.y=(25*eSortIndex);
}
void votePosUpdate(){
pos.x=((width/2)+(width/6));
pos.y=(height/3);
}
void display(){
fill (textColor);
textAlign(LEFT, CENTER);
text(eInfo,pos.x,pos.y,pos.z);
textAlign(RIGHT, CENTER);
text(eScore,pos.x+300,pos.y,pos.z);
}
void voting(int vScore_){
vScore = vScore_;
eScore = eScore + vScore;
voted = true;
}
int getIndex() {
return eIndex;
}
int compareTo(Object anotherEntry) throws ClassCastException {
if (!(anotherEntry instanceof Entry))
throw new ClassCastException("An Entry object expected.");
int anotherEntryIndex = ((Entry) anotherEntry).getIndex();
return this.eIndex - anotherEntryIndex;
}
}
class EntryScreen{
int esAge;
int esScore;
String esInfo;
PVector esPos;
EntryScreen(){
esPos = new PVector((width/20),height/6,0);
}
void display(){
fill (textColor);
textAlign(LEFT, CENTER);
text(typedEntry,esPos.x,esPos.y,esPos.z);
}
void addEntry(){
esInfo = typedEntry;
esScore = 0;
Index++;
esAge = int (year() + nf(month(),2) + nf(day(),2) + nf(hour(),2) + nf(minute(),2) + nf(second(),2));
entries.add (new Entry (Index, esInfo, esScore, esAge));
}
}
class EntryTable{
Sorting mySorting;
boolean cScore;
EntryTable(){
mySorting = new Sorting();
}
void display(){
mySorting.display();
}
}
class Sorting{
Sorting(){
}
void update(){
}
void display(){
entriesArray = new Entry [entries.size()];
for( int i=0; i < entries.size(); i++ ){
entriesArray[i] = (Entry) entries.get(i);
}
Arrays.sort(entriesArray, new ComparatorScore());
for(int i=0; i < entriesArray.length; i++ ){
Entry mySortedEntry = (Entry) entriesArray[i];
mySortedEntry.display();
mySortedEntry.tableUpdate(i);
}
}
}
class VotingScreen{
VotingSorting myVSorting;
VotingScreen(){
myVSorting = new VotingSorting();
}
void display(){
myVSorting.display();
if (millis() - timer >= 300) {
myVSorting.update();
timer = millis();
}
}
}
class VotingSorting{
int Number;
VotingSorting(){
entriesVotingArray = new Entry [entries.size()];
for( int i=0; i < entries.size(); i++ ){
Entry myEntry = (Entry) entries.get(i);
entriesVotingArray[i] = myEntry;
Entry myNoEntry = entriesVotingArray[i];
}
}
void update(){
entriesVotingArray = new Entry [entries.size()];
for( int i=0; i < entries.size(); i++ ){
Entry myUEntry = (Entry) entries.get(i);
entriesVotingArray[i] = myUEntry;
Entry myNEntry = entriesVotingArray[i];
}
Arrays.sort(entriesVotingArray, new ComparatorIndex());
reverse(entriesVotingArray);
Arrays.sort(entriesVotingArray, new ComparatorVoted());
}
void display(){
Entry myVEntry = (Entry) entriesVotingArray[Number];
myVEntry.display();
myVEntry.votePosUpdate();
if (up==true) {myVEntry.voting(1);}
if (down==true) {myVEntry.voting(-1);}
}
}