PageRenderTime 20ms CodeModel.GetById 12ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/elements/PlayerNode.java

http://inequity.googlecode.com/
Java | 134 lines | 70 code | 15 blank | 49 comment | 2 complexity | 27199516c42c86b8d309c610039e6470 MD5 | raw file
  1package elements;
  2
  3import java.util.concurrent.PriorityBlockingQueue;
  4import network.Game;
  5import network.NetQProxy;
  6import packets.Command;
  7import packets.CommandRequest;
  8import packets.PortfolioUpdateCmd;
  9import packets.Request;
 10import packets.TriggeredEventReq;
 11
 12/**
 13 * Node to hold player game data
 14 * @author Joel Garboden
 15 */
 16public class PlayerNode
 17{
 18  private NetQProxy proxy;
 19  private PlayerPortfolio portfolio;
 20  private EventFactory eventFactory;
 21
 22  private int ID;
 23
 24  /**
 25   * Constructor
 26   * @param ID player's ID
 27   * @param proxy player proxy
 28   * @param portfolio player portfolio
 29   * @param eventFactory
 30   */
 31  public PlayerNode(int ID, NetQProxy proxy, PlayerPortfolio portfolio, EventFactory eventFactory)
 32  {
 33    this.ID = ID;
 34    this.proxy = proxy;
 35    this.portfolio = portfolio;
 36    this.eventFactory = eventFactory;
 37  }
 38
 39  /**
 40   * Mutator
 41   * @param active proxy status
 42   */
 43  public void setActive(boolean active)
 44  {
 45    proxy.setActive(active);
 46  }
 47
 48  /**
 49   * Accessor
 50   * @return proxy status
 51   */
 52  public boolean isActive()
 53  {
 54    return proxy.isActive();
 55  }
 56
 57  /**
 58   * Adds a command to the outgoing queue
 59   * @param cmd command to be sent
 60   */
 61  public void send(Command cmd)
 62  {
 63    proxy.queue.add(cmd);
 64  }
 65
 66  /**
 67   * Mutator
 68   * @param name player name
 69   */
 70  public void setName(String name)
 71  {
 72    portfolio.setName(name);
 73  }
 74
 75  /**
 76   * Accessor
 77   * @return player name
 78   */
 79  public String getName()
 80  {
 81    return portfolio.getName();
 82  }
 83
 84  /**
 85   * Accessor for the players portfolio
 86   * @return player's portfolio
 87   */
 88  public PlayerPortfolio getPortfolio()
 89  {
 90    return portfolio;
 91  }
 92
 93  /**
 94   * Mutator for portfolio
 95   * @param portfolio new player portfolio
 96   */
 97  public void setPortfolio(PlayerPortfolio portfolio)
 98  {
 99    this.portfolio = portfolio;
100  }
101
102  /**
103   * Advances player data by one week.
104   * @param gameQ game's processing queue
105   * @param game
106   */
107  public void nextWeek(PriorityBlockingQueue<CommandRequest> gameQ, Game game)
108  {
109    portfolio.nextWeek();
110    portfolio.payBills();
111    Request req = null;
112
113    if(portfolio.weekCounter % Consts.WEEKS_IN_MONTH == 0) //last week of the month == monthly event
114    {
115      //req = game.triggerEvent(portfolio);
116      req = new TriggeredEventReq(eventFactory.getAnEvent());
117      req.setID(ID);
118      gameQ.add(req);
119    }
120
121    //Weekly events happen every week
122    req = new TriggeredEventReq(eventFactory.getAnEvent());
123    req.setID(ID);
124    gameQ.add(req);
125  }
126
127  /**
128   * Sends updated portfolio to client
129   */
130  public void sendPortfolio()
131  {
132    send(new PortfolioUpdateCmd(portfolio));
133  }
134}