📄 gamerecord.java
字号:
package myGame.main;
import java.util.Date;
import java.util.Vector;
/**
* <p>
* A game record represents one or multiple games between an opponent, keeping
* track of scores and such. <code>GameRecord</code>s are stored in the RMS.
* </p>
* <p>
* The static interface of this class provides a hash table like functionality
* and also contains logic for keeping the limit of maximum allowed game
* records.
* </p>
*
* @author YuBingxing
*/
public class GameRecord {
/** Id of the opponent belonging to this game record */
protected int m_opponentId;
/** Name of the opponent belonging to this game record */
protected char[] m_opponentName;
/** Number of games played against opponent belonging to this game record */
protected int m_gameCount;
/**
* Total score in all games played against opponent belonging to this game
* record
*/
protected int m_playerScore;
/**
* Total opponent score in all games played against opponent belonging to
* this game record
*/
protected int m_opponentScore;
/** Timestamp for latest change in this game record */
protected long m_timestamp;
/** Game record index in RmsFacade */
protected int m_gameRecordIndex;
/**
* Returns the time of the latest change to this record.
*
* @return The timestamp.
*/
public long getTimestamp() {
return m_timestamp;
}
/**
* Returns the id of the opponent represented in this record.
*
* @return The opponent id.
*/
public int getOpponentId() {
return m_opponentId;
}
/**
* Returns the name of the opponent represented in this record.
*
* @return The opponent name.
*/
public char[] getOpponentName() {
return m_opponentName;
}
/**
* Returns the score of the opponent represented in this record.
*
* @return The opponent score.
*/
public int getOpponentScore() {
return m_opponentScore;
}
/**
* Returns the score of the player represented in this record.
*
* @return The player score.
*/
public int getPlayerScore() {
return m_playerScore;
}
/**
* Returns number of games played agains opponent represented in this
* record.
*
* @return Number of games against opponent.
*/
public int getGameCount() {
return m_gameCount;
}
/**
* Returns the index in rms that this record occupies.
*
* @return The rms index.
*/
public int getIndex() {
return m_gameRecordIndex;
}
/**
* Updates this game record with data from a new game.
*
* @param localWinner
* If this game was won by local player.
* @param score
* The score.
*/
protected void registerGame(boolean localWinner, int score) {
m_playerScore += localWinner ? score : 0;
m_opponentScore += localWinner ? 0 : score;
m_gameCount++;
}
/**
* Sets the index of this game record.
*
* @param index
* The index to set.
*/
protected void setIndex(int index) {
m_gameRecordIndex = index;
}
/**
* Saves this game record to rms.
*/
protected void save() {
m_timestamp = new Date().getTime();
RmsFacade
.setInt(MyGame.GAMEREC_OP_ID + m_gameRecordIndex, m_opponentId);
RmsFacade.setChars(MyGame.GAMEREC_OP_NAME + m_gameRecordIndex,
m_opponentName);
RmsFacade.setInt(MyGame.GAMEREC_MY_SCORE + m_gameRecordIndex,
m_playerScore);
RmsFacade.setInt(MyGame.GAMEREC_OP_SCORE + m_gameRecordIndex,
m_opponentScore);
RmsFacade.setInt(MyGame.GAMEREC_GAME_COUNT + m_gameRecordIndex,
m_gameCount);
RmsFacade.setLong(MyGame.GAMEREC_TIMESTAMP + m_gameRecordIndex,
m_timestamp);
}
/**
* Creates a gamerecord from existing entry in rms.
*
* @param index
* The index of the game record in rms.
*/
protected GameRecord(int index) {
m_opponentId = RmsFacade.getInt(MyGame.GAMEREC_OP_ID + index);
m_opponentName = RmsFacade.getChars(MyGame.GAMEREC_OP_NAME + index);
m_playerScore = RmsFacade.getInt(MyGame.GAMEREC_MY_SCORE + index);
m_opponentScore = RmsFacade.getInt(MyGame.GAMEREC_OP_SCORE + index);
m_gameCount = RmsFacade.getInt(MyGame.GAMEREC_GAME_COUNT + index);
m_timestamp = RmsFacade.getLong(MyGame.GAMEREC_TIMESTAMP + index);
m_gameRecordIndex = index;
}
/**
* Returns the data of a saved game.
*
* @param id
* The opponent id.
* @return The saved game data or null if there is no saved game data for an
* opponent with specified id.
*/
public static byte[] getSavedGame(int id) {
byte[] data = null;
int index = getIndexForOpponentId(id);
if (index >= 0) {
data = RmsFacade.get(MyGame.GAMEREC_SAVED_GAME_DATA + index);
}
return data;
}
/**
* Returns number of records saved in the rms.
*
* @return Number of stored game records.
*/
public static int countRecords() {
int res = 0;
for (int index = 0; index < MyGame.GAMERECORDS_SIZE; index++) {
int id = RmsFacade.getInt(MyGame.GAMEREC_OP_ID + index);
if (id != 0)
res++;
}
return res;
}
/**
* Returns a vector of <code>GameRecord</code>s, containing all stored
* game records.
*
* @return All stored game records.
*/
public static Vector getAllRecords() {
Vector res = new Vector();
for (int index = 0; index < MyGame.GAMERECORDS_SIZE; index++) {
int id = RmsFacade.getInt(MyGame.GAMEREC_OP_ID + index);
if (id != 0) {
GameRecord record = new GameRecord(index);
res.addElement(record);
}
}
return res;
}
/**
* Returns the RMS index for specified opponent id.
*
* @param refId
* The opponent id.
* @return The index, or -1 if opponent is not saved in RMS.
*/
protected static int getIndexForOpponentId(int refId) {
int res = -1;
for (int index = 0; index < MyGame.GAMERECORDS_SIZE; index++) {
int id = RmsFacade.getInt(MyGame.GAMEREC_OP_ID + index);
if (id == refId) {
res = index;
break;
}
}
return res;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -