Scoreboard / Leaderboard: How do you manage them?

Started by
0 comments, last by tom_mai78101 11 years, 7 months ago
Currently, I have something like this planned out:

ijBls.png

A Scoreboard object consists of many Formats. Each Format object means a record of a player's session. There are many players playing, and once they are done playing, each of their sessions is recorded into a Format object, stored into a binary file somewhere, and can be compared with other Formats. The Scoreboard object, when invoked, will gather up all currently recorded Format sessions from one binary file, and gives a simple list of the Formats in an orderly fashion.

I wanted to store many of these Formats into 1 binary file. So, here's what I have in mind:

[source lang="java"]package test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Scoreboard {
public static List<Format> formats = new ArrayList<Format>();

public static void main(String arg[]) {
make();
retrieve();
}

public static void make() {
Random r = new Random();
for (int i = 0; i < r.nextInt(4) + 1; i++) {
Format f = new Format();
f.position = r.nextInt(10);
f.name = r.toString();
f.score = r.nextLong();
formats.add(f);
}
try {
FileOutputStream out = new FileOutputStream(new File("format.txt"));
ObjectOutputStream output = new ObjectOutputStream(new BufferedOutputStream(out));
output.writeObject(formats);
output.flush();
output.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
formats.clear();
}

public static void retrieve() {
FileInputStream in;
try {
in = new FileInputStream(new File("format.txt"));
ObjectInputStream input = new ObjectInputStream(new BufferedInputStream(in));
formats = (ArrayList<Format>) input.readObject();
input.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("Clear!");
}
}
[/source]

And the Format object:

[source lang="java"]package test;

import java.io.Serializable;

public class Format implements Serializable {
private static final long serialVersionUID = 1L;
public int position;
public String name;
public long score;
}
[/source]

But there is a problem. And it's a big flaw. How does one simply manage the Formats, by recording and saving them at arbitary times, and from the Scoreboard, you can invoke it to list out the Formats at arbitary times? This is something I haven't come up with yet. Probably going to need some hints on this.

It's something that I wanted to do on an Android smartphone, but I guess Java is fine anyway.
Advertisement
Update:

I was just thinking a little bit more when I thought, why not just put the ArrayList<Format> inside the Format class object?

[source lang="java"]public class Format implements Serializable {
private static final long serialVersionUID = 1L;
List<Format> formats = new ArrayList<Format>();
public int position;
public String name;
public long score;
}
[/source]
It would seems it might be the key to solving this solution: A temporary place to store a session in, and a permanent place to store lots of older sessions in an array. So, that way, when I save it, the file will consist mainly of a temporary cache session, and a large chunk of permanent data. When I read it back, it will read all of them together, and I could omit the temporary cached session and continue to do some processing.

Seems about right...

This topic is closed to new replies.

Advertisement