⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamerecord.java

📁 手机应用程序之蓝牙编程源代码。。蓝牙编程爱好者课用
💻 JAVA
字号:
// Copyright (c) 2005 Sony Ericsson Mobile Communications AB
//
// This software is provided "AS IS," without a warranty of any kind. 
// ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, 
// INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A 
// PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. 
//
// THIS SOFTWARE IS COMPLEMENTARY OF JAYWAY AB (www.jayway.se)

package bluegammon.logic;

import java.util.Date;
import java.util.Vector;

import bluegammon.Bluegammon;
import bluegammon.RmsFacade;

/**
 * <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 Peter Andersson
 */
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(Bluegammon.GAMEREC_OP_ID + m_gameRecordIndex,
        m_opponentId);
    RmsFacade.setChars(Bluegammon.GAMEREC_OP_NAME + m_gameRecordIndex,
        m_opponentName);
    RmsFacade.setInt(Bluegammon.GAMEREC_MY_SCORE + m_gameRecordIndex,
        m_playerScore);
    RmsFacade.setInt(Bluegammon.GAMEREC_OP_SCORE + m_gameRecordIndex,
        m_opponentScore);
    RmsFacade.setInt(Bluegammon.GAMEREC_GAME_COUNT + m_gameRecordIndex,
        m_gameCount);
    RmsFacade.setLong(Bluegammon.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(Bluegammon.GAMEREC_OP_ID + index);
    m_opponentName = RmsFacade.getChars(Bluegammon.GAMEREC_OP_NAME + index);
    m_playerScore = RmsFacade.getInt(Bluegammon.GAMEREC_MY_SCORE + index);
    m_opponentScore = RmsFacade.getInt(Bluegammon.GAMEREC_OP_SCORE + index);
    m_gameCount = RmsFacade.getInt(Bluegammon.GAMEREC_GAME_COUNT + index);
    m_timestamp = RmsFacade.getLong(Bluegammon.GAMEREC_TIMESTAMP + index);
    m_gameRecordIndex = index;
  }

  /**
   * Creates a new game record to be added to rms.
   * @param opponent	The opponent player.
   */
  protected GameRecord(Player opponent)
  {
    m_opponentId = opponent.getId();
    m_opponentName = opponent.getName();
    m_playerScore = 0;
    m_opponentScore = 0;
    m_gameCount = 0;
  }

  // Static methods
  
  /**
   * 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(Bluegammon.GAMEREC_SAVED_GAME_DATA + index);
    }
    return data;
  }
  
  /**
   * Saves the state of an ongoing game against specified opponents.
   * @param opponent	The opponent player.
   * @param savedGame	The saved game data.
   */
  public static void saveGame(Player opponent, byte[] savedGame)
  {
    GameRecord rec = getRecordForce(opponent);
    RmsFacade.set(Bluegammon.GAMEREC_SAVED_GAME_DATA + rec.getIndex(), savedGame);
    rec.save();
  }
  
  /**
   * 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 < Bluegammon.GAMERECORDS_SIZE; index++)
    {
      int id = RmsFacade.getInt(Bluegammon.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 < Bluegammon.GAMERECORDS_SIZE; index++)
    {
      int id = RmsFacade.getInt(Bluegammon.GAMEREC_OP_ID + index);
      if (id != 0)
      {
        GameRecord record = new GameRecord(index);
        res.addElement(record);
      }
    }
    return res;
  }
  
  /**
   * Returns record for a specific opponent. If the player has
   * not played against this opponent before, null is returned.
   * @param opponent	The opponent.
   * @return	The <code>GameRecord</code> for this opponent, or null
   * 			if it does not exist.
   */
  public static GameRecord getRecord(Player opponent)
  {
    int refId = opponent.getId();
    int index = getIndexForOpponentId(refId); 
    return index >= 0 ? new GameRecord(index) : null;
  }
  
  /**
   * Updates the game results with a new game.  
   * @param opponent	The opponent player.
   * @param localWinner	If this game was won by local player.
   * @param score		The score.
   */
  public static void updateGameResult(Player opponent, boolean localWinner, int score)
  {
    GameRecord rec = getRecordForce(opponent);
    rec.registerGame(localWinner, score);
    rec.save();
  }
  
  /**
   * Returns a game record for specified opponent. If no such
   * game record exists, it is created.
   * @param opponent	The opponent player.
   * @return A game record for opponent. Creates a record if needed.
   */
  protected static GameRecord getRecordForce(Player opponent)
  {
    int index = 0;
    GameRecord rec = getRecord(opponent);
    if (rec == null)
    {
      // New game record
      rec = new GameRecord(opponent);
      int size = countRecords();
      Vector records = getAllRecords();
      if (size >= Bluegammon.GAMERECORDS_SIZE)
      {
        // The rms is full, need to remove an old game record
        long minTime = Long.MAX_VALUE;
        // Find oldest game record
        for (int i = 0; i < size; i++)
        {
          long rTime = ((GameRecord)records.elementAt(i)).getTimestamp();
          if (minTime > rTime)
          {
            index = ((GameRecord)records.elementAt(i)).getIndex();
            minTime = rTime;
          }
        }
      }
      else
      {
        for (index = 0; index < Bluegammon.GAMERECORDS_SIZE; index++)
        {
          int id = RmsFacade.getInt(Bluegammon.GAMEREC_OP_ID + index);
          if (id == 0) break;
        }
        System.out.println("                              got index " + index);
      }
    }
    else
    {
      // Existing game record, update
      index = rec.getIndex();
      System.out.println("                              existing record, index " + index);
    }
    rec.setIndex(index);
    return rec;
  }
  
  /**
   * 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 < Bluegammon.GAMERECORDS_SIZE; index++)
    {
      int id = RmsFacade.getInt(Bluegammon.GAMEREC_OP_ID + index);
      if (id == refId)
      {
        res = index;
        break;
      }
    }
    return res;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -