|
|
1b8111 |
package com.icystar.findnumber;
|
|
|
1b8111 |
|
|
|
1b8111 |
import java.io.FileInputStream;
|
|
|
1b8111 |
import java.io.FileOutputStream;
|
|
|
1b8111 |
import java.io.IOException;
|
|
|
1b8111 |
|
|
|
1b8111 |
import android.app.Activity;
|
|
|
1b8111 |
import android.graphics.Typeface;
|
|
|
1b8111 |
import android.os.Bundle;
|
|
|
1b8111 |
import android.view.Gravity;
|
|
|
1b8111 |
import android.widget.TabHost;
|
|
|
1b8111 |
import android.widget.TableLayout;
|
|
|
1b8111 |
import android.widget.TableRow;
|
|
|
1b8111 |
import android.widget.TableRow.LayoutParams;
|
|
|
1b8111 |
import android.widget.TextView;
|
|
|
1b8111 |
|
|
|
1b8111 |
public class ScoreboardActivity extends Activity {
|
|
|
1b8111 |
public static final int maxScoresCount = 10;
|
|
|
1b8111 |
public static final int[] counts = { 25, 50, 100 };
|
|
|
1b8111 |
|
|
|
1b8111 |
public static class Score {
|
|
|
1b8111 |
boolean exists = false;
|
|
|
1b8111 |
String name = "";
|
|
|
1b8111 |
int count = 0;
|
|
|
1b8111 |
int time = 0;
|
|
|
1b8111 |
float secondsPerNumber = 0;
|
|
|
1b8111 |
|
|
|
1b8111 |
public void save(FileOutputStream fos) throws IOException {
|
|
|
1b8111 |
Serializer.writeInt(fos, exists ? 1 : 0);
|
|
|
1b8111 |
Serializer.writeString(fos, name);
|
|
|
1b8111 |
Serializer.writeInt(fos, count);
|
|
|
1b8111 |
Serializer.writeInt(fos, time);
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
public void load(FileInputStream fis) throws IOException {
|
|
|
1b8111 |
exists = Serializer.readInt(fis) != 0;
|
|
|
1b8111 |
name = Serializer.readString(fis);
|
|
|
1b8111 |
count = Serializer.readInt(fis);
|
|
|
1b8111 |
time = Serializer.readInt(fis);
|
|
|
1b8111 |
secondsPerNumber = count > 0 ? (float)time/(float)count : time;
|
|
|
1b8111 |
}
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
private static Object staticHandle = new Object();
|
|
|
1b8111 |
private static Score[][] scores;
|
|
|
1b8111 |
|
|
|
1b8111 |
private static void fixScores() {
|
|
|
1b8111 |
scores = Serializer.resizeArrayIfNeed(Score[].class, scores, counts.length);
|
|
|
1b8111 |
for(int i = 0; i < scores.length; i++) {
|
|
|
1b8111 |
scores[i] = Serializer.resizeArrayIfNeed(Score.class, scores[i], maxScoresCount);
|
|
|
1b8111 |
for(int j = 0; j < scores[i].length; j++)
|
|
|
1b8111 |
if (scores[i][j] == null) scores[i][j] = new Score();
|
|
|
1b8111 |
}
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
public static void save(FileOutputStream fos) throws IOException {
|
|
|
1b8111 |
synchronized (staticHandle) {
|
|
|
1b8111 |
fixScores();
|
|
|
1b8111 |
Serializer.writeInt(fos, scores.length);
|
|
|
1b8111 |
for(Score[] ss : scores) {
|
|
|
1b8111 |
Serializer.writeInt(fos, ss.length);
|
|
|
1b8111 |
for(Score score : ss) score.save(fos);
|
|
|
1b8111 |
}
|
|
|
1b8111 |
}
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
public static void load(FileInputStream fis) throws IOException {
|
|
|
1b8111 |
synchronized (staticHandle) {
|
|
|
1b8111 |
Score[][] ss = null;
|
|
|
1b8111 |
try {
|
|
|
1b8111 |
ss = new Score[Serializer.readInt(fis)][0];
|
|
|
1b8111 |
for(int i = 0; i < ss.length; i++) {
|
|
|
1b8111 |
ss[i] = new Score[Serializer.readInt(fis)];
|
|
|
1b8111 |
for(int j = 0; j < ss[i].length; j++) {
|
|
|
1b8111 |
ss[i][j] = new Score();
|
|
|
1b8111 |
ss[i][j].load(fis);
|
|
|
1b8111 |
}
|
|
|
1b8111 |
}
|
|
|
1b8111 |
} finally {
|
|
|
1b8111 |
scores = ss;
|
|
|
1b8111 |
fixScores();
|
|
|
1b8111 |
}
|
|
|
1b8111 |
}
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
|
|
|
1b8111 |
@Override
|
|
|
1b8111 |
public void onCreate(Bundle savedInstanceState) {
|
|
|
1b8111 |
super.onCreate(savedInstanceState);
|
|
|
1b8111 |
setContentView(R.layout.activity_scoreboard);
|
|
|
1b8111 |
|
|
|
1b8111 |
TabHost tabs = (TabHost)findViewById(android.R.id.tabhost);
|
|
|
1b8111 |
|
|
|
1b8111 |
tabs.setup();
|
|
|
1b8111 |
|
|
|
1b8111 |
TabHost.TabSpec spec;
|
|
|
1b8111 |
boolean xlarge = getString(R.string.screenSize).compareTo("xlarge") == 0;
|
|
|
1b8111 |
|
|
|
1b8111 |
spec = tabs.newTabSpec("tap25");
|
|
|
1b8111 |
spec.setContent(R.id.tab25);
|
|
|
1b8111 |
spec.setIndicator(getString(R.string.radio25), xlarge ? null : getResources().getDrawable(R.drawable.ic_tab_n25));
|
|
|
1b8111 |
tabs.addTab(spec);
|
|
|
1b8111 |
|
|
|
1b8111 |
spec = tabs.newTabSpec("tag50");
|
|
|
1b8111 |
spec.setContent(R.id.tab50);
|
|
|
1b8111 |
spec.setIndicator(getString(R.string.radio50), xlarge ? null : getResources().getDrawable(R.drawable.ic_tab_n50));
|
|
|
1b8111 |
tabs.addTab(spec);
|
|
|
1b8111 |
|
|
|
1b8111 |
spec = tabs.newTabSpec("tab100");
|
|
|
1b8111 |
spec.setContent(R.id.tab100);
|
|
|
1b8111 |
spec.setIndicator(getString(R.string.radio100), xlarge ? null : getResources().getDrawable(R.drawable.ic_tab_n100));
|
|
|
1b8111 |
tabs.addTab(spec);
|
|
|
1b8111 |
|
|
|
1b8111 |
tabs.setCurrentTab( getCountMode(Game.games[0].getCount(), 0) );
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
@Override
|
|
|
1b8111 |
protected void onResume() {
|
|
|
1b8111 |
super.onResume();
|
|
|
1b8111 |
|
|
|
1b8111 |
synchronized (staticHandle) {
|
|
|
1b8111 |
int count = getIntent().getIntExtra("count", 0);
|
|
|
1b8111 |
int rank = getIntent().getIntExtra("rank", 0);
|
|
|
1b8111 |
|
|
|
1b8111 |
fixScores();
|
|
|
1b8111 |
final int[] ids = new int[] { R.id.tab25, R.id.tab50, R.id.tab100 };
|
|
|
1b8111 |
for(int i = 0; i < scores.length; i++) {
|
|
|
1b8111 |
TableLayout table = (TableLayout)findViewById(ids[i]);
|
|
|
1b8111 |
table.removeAllViews();
|
|
|
1b8111 |
|
|
|
1b8111 |
// first row - column titles
|
|
|
1b8111 |
TableRow row = new TableRow(this);
|
|
|
1b8111 |
TextView text;
|
|
|
1b8111 |
|
|
|
1b8111 |
int margin = getResources().getDimensionPixelSize(R.dimen.margin);
|
|
|
1b8111 |
TableRow.LayoutParams layoutRank = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0f);
|
|
|
1b8111 |
layoutRank.setMargins(margin, 0, margin, 0);
|
|
|
1b8111 |
TableRow.LayoutParams layoutName = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
|
|
|
1b8111 |
layoutName.setMargins(margin, 0, margin, 0);
|
|
|
1b8111 |
TableRow.LayoutParams layoutTimeCount = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0f);
|
|
|
1b8111 |
layoutTimeCount.setMargins(margin, 0, margin, 0);
|
|
|
1b8111 |
TableRow.LayoutParams layoutSecondsPerNumber = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0f);
|
|
|
1b8111 |
layoutSecondsPerNumber.setMargins(margin, 0, margin, 0);
|
|
|
1b8111 |
|
|
|
1b8111 |
text = new TextView(this);
|
|
|
1b8111 |
text.setText(R.string.scoreRank);
|
|
|
1b8111 |
text.setTypeface(Typeface.DEFAULT_BOLD);
|
|
|
1b8111 |
text.setTextSize(text.getTextSize()/2f);
|
|
|
1b8111 |
text.setSingleLine();
|
|
|
1b8111 |
text.setGravity(Gravity.RIGHT);
|
|
|
1b8111 |
row.addView(text);
|
|
|
1b8111 |
text.setLayoutParams(layoutRank);
|
|
|
1b8111 |
|
|
|
1b8111 |
text = new TextView(this);
|
|
|
1b8111 |
text.setText(R.string.scoreName);
|
|
|
1b8111 |
text.setTypeface(Typeface.DEFAULT_BOLD);
|
|
|
1b8111 |
text.setTextSize(text.getTextSize()/2f);
|
|
|
1b8111 |
text.setSingleLine();
|
|
|
1b8111 |
text.setGravity(Gravity.LEFT);
|
|
|
1b8111 |
row.addView(text);
|
|
|
1b8111 |
text.setLayoutParams(layoutName);
|
|
|
1b8111 |
|
|
|
1b8111 |
text = new TextView(this);
|
|
|
1b8111 |
text.setText(R.string.scoreTimeCount);
|
|
|
1b8111 |
text.setTypeface(Typeface.DEFAULT_BOLD);
|
|
|
1b8111 |
text.setTextSize(text.getTextSize()/2f);
|
|
|
1b8111 |
text.setSingleLine();
|
|
|
1b8111 |
text.setGravity(Gravity.RIGHT);
|
|
|
1b8111 |
row.addView(text);
|
|
|
1b8111 |
text.setLayoutParams(layoutTimeCount);
|
|
|
1b8111 |
|
|
|
1b8111 |
text = new TextView(this);
|
|
|
1b8111 |
text.setText(R.string.scoreSecondsPerNumber);
|
|
|
1b8111 |
text.setTypeface(Typeface.DEFAULT_BOLD);
|
|
|
1b8111 |
text.setTextSize(text.getTextSize()/2f);
|
|
|
1b8111 |
text.setSingleLine();
|
|
|
1b8111 |
text.setGravity(Gravity.RIGHT);
|
|
|
1b8111 |
row.addView(text);
|
|
|
1b8111 |
text.setLayoutParams(layoutSecondsPerNumber);
|
|
|
1b8111 |
|
|
|
1b8111 |
table.addView(row);
|
|
|
1b8111 |
|
|
|
1b8111 |
// rows
|
|
|
1b8111 |
for(int j = 0; j < scores[i].length; j++) {
|
|
|
1b8111 |
row = new TableRow(this);
|
|
|
1b8111 |
boolean blank = !scores[i][j].exists;
|
|
|
1b8111 |
String dummy = " -- ";
|
|
|
1b8111 |
|
|
|
1b8111 |
if (counts[i] == count && j+1 == rank)
|
|
|
1b8111 |
row.setBackgroundColor(getResources().getColor(R.color.statusbar_color));
|
|
|
1b8111 |
|
|
|
1b8111 |
text = new TextView(this);
|
|
|
1b8111 |
text.setText(String.format("%d. ", j+1));
|
|
|
1b8111 |
text.setSingleLine();
|
|
|
1b8111 |
text.setGravity(Gravity.RIGHT);
|
|
|
1b8111 |
row.addView(text);
|
|
|
1b8111 |
text.setLayoutParams(layoutRank);
|
|
|
1b8111 |
|
|
|
1b8111 |
text = new TextView(this);
|
|
|
1b8111 |
text.setText(blank ? dummy : scores[i][j].name);
|
|
|
1b8111 |
text.setSingleLine();
|
|
|
1b8111 |
text.setGravity(Gravity.LEFT);
|
|
|
1b8111 |
row.addView(text);
|
|
|
1b8111 |
text.setLayoutParams(layoutName);
|
|
|
1b8111 |
|
|
|
1b8111 |
text = new TextView(this);
|
|
|
1b8111 |
text.setText(blank ? dummy : String.format("%d/%d", scores[i][j].time, scores[i][j].count));
|
|
|
1b8111 |
text.setSingleLine();
|
|
|
1b8111 |
text.setGravity(Gravity.RIGHT);
|
|
|
1b8111 |
row.addView(text);
|
|
|
1b8111 |
text.setLayoutParams(layoutTimeCount);
|
|
|
1b8111 |
|
|
|
1b8111 |
text = new TextView(this);
|
|
|
1b8111 |
text.setText(blank ? dummy : String.format("%.3f", scores[i][j].secondsPerNumber));
|
|
|
1b8111 |
text.setSingleLine();
|
|
|
1b8111 |
text.setGravity(Gravity.RIGHT);
|
|
|
1b8111 |
row.addView(text);
|
|
|
1b8111 |
text.setLayoutParams(layoutSecondsPerNumber);
|
|
|
1b8111 |
|
|
|
1b8111 |
table.addView(row);
|
|
|
1b8111 |
}
|
|
|
1b8111 |
}
|
|
|
1b8111 |
}
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
private static int getCountMode(int count, int def) {
|
|
|
1b8111 |
for(int i = 0; i < counts.length; i++)
|
|
|
1b8111 |
if (counts[i] == count)
|
|
|
1b8111 |
return i;
|
|
|
1b8111 |
return def;
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
public static int isRecord(int count, int time) {
|
|
|
1b8111 |
synchronized (staticHandle) {
|
|
|
1b8111 |
fixScores();
|
|
|
1b8111 |
int countMode = getCountMode(count, -1);
|
|
|
1b8111 |
if (countMode >= 0)
|
|
|
1b8111 |
for(int i = 0; i < scores[countMode].length; i++)
|
|
|
1b8111 |
if (!scores[countMode][i].exists || time < scores[countMode][i].time)
|
|
|
1b8111 |
return i+1;
|
|
|
1b8111 |
}
|
|
|
1b8111 |
return 0;
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
public static void addNewRecord(int count, int time, String name) {
|
|
|
1b8111 |
int countMode = getCountMode(count, -1);
|
|
|
1b8111 |
if (name != null && countMode >= 0 && isRecord(count, time) > 0) {
|
|
|
1b8111 |
synchronized (staticHandle) {
|
|
|
1b8111 |
fixScores();
|
|
|
1b8111 |
|
|
|
1b8111 |
Score score = new Score();
|
|
|
1b8111 |
score.exists = true;
|
|
|
1b8111 |
score.name = name;
|
|
|
1b8111 |
score.count = counts[countMode];
|
|
|
1b8111 |
score.time = time;
|
|
|
1b8111 |
score.secondsPerNumber = score.count > 0 ? (float)score.time/(float)score.count : 0;
|
|
|
1b8111 |
|
|
|
1b8111 |
for(int i = 0; i < scores[countMode].length; i++) {
|
|
|
1b8111 |
if (!scores[countMode][i].exists || score.time < scores[countMode][i].time) {
|
|
|
1b8111 |
for(int j = scores[countMode].length-1; j > i ; j--)
|
|
|
1b8111 |
scores[countMode][j] = scores[countMode][j-1];
|
|
|
1b8111 |
scores[countMode][i] = score;
|
|
|
1b8111 |
break;
|
|
|
1b8111 |
}
|
|
|
1b8111 |
}
|
|
|
1b8111 |
}
|
|
|
1b8111 |
}
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
public static void updateRecord(int count, int time, String name) {
|
|
|
1b8111 |
int countMode = getCountMode(count, -1);
|
|
|
1b8111 |
if (name != null && countMode >= 0) {
|
|
|
1b8111 |
synchronized (staticHandle) {
|
|
|
1b8111 |
fixScores();
|
|
|
1b8111 |
for(int i = scores[countMode].length - 1; i >= 0 ; i--)
|
|
|
1b8111 |
if (scores[countMode][i].exists && scores[countMode][i].time == time)
|
|
|
1b8111 |
{ scores[countMode][i].name = name; break; }
|
|
|
1b8111 |
}
|
|
|
1b8111 |
}
|
|
|
1b8111 |
}
|
|
|
1b8111 |
|
|
|
1b8111 |
}
|