AI2 Component  (Version nb184)
GameInstance.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime.util;
8 
9 import java.util.ArrayList;
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13 
20 public class GameInstance {
21  private String instanceId;
22  private String leader;
23 
24  // players in the current game
25  private List<String> players;
26 
27  // Use this to store the most recent time stamp of each message type received.
28  private Map<String, String> messageTimes;
29 
30 
42  public GameInstance(String instanceId) {
43  players = new ArrayList<String>(0);
44  messageTimes = new HashMap<String, String>();
45  this.instanceId = instanceId;
46  this.leader = "";
47  }
48 
53  public String getInstanceId() {
54  return instanceId;
55  }
56 
61  public String getLeader() {
62  return leader;
63  }
64 
69  public void setLeader(String leader) {
70  this.leader = leader;
71  }
72 
84  public PlayerListDelta setPlayers(List<String> newPlayersList) {
85  if (newPlayersList.equals(players)) {
87  }
88  List<String> removed = players;
89  List<String> added = new ArrayList<String>(newPlayersList);
90  players = new ArrayList<String>(newPlayersList);
91 
92  added.removeAll(removed);
93  removed.removeAll(newPlayersList);
94  // This happens if the players list is the same but the ordering
95  // has changed for some reason.
96  if (added.size() == 0 && removed.size() == 0) {
98  }
99 
100  return new PlayerListDelta(removed, added);
101  }
102 
108  public List<String> getPlayers() {
109  return players;
110  }
111 
123  public String getMessageTime(String type) {
124  if (messageTimes.containsKey(type)) {
125  return messageTimes.get(type);
126  }
127  return "";
128  }
129 
142  public void putMessageTime(String type, String time) {
143  messageTimes.put(type, time);
144  }
145 }
com.google.appinventor.components.runtime.util.PlayerListDelta
Definition: PlayerListDelta.java:17
com.google.appinventor.components.runtime.util.GameInstance
Definition: GameInstance.java:20
com.google.appinventor.components.runtime.util.GameInstance.putMessageTime
void putMessageTime(String type, String time)
Definition: GameInstance.java:142
com.google.appinventor.components.runtime.util.GameInstance.setPlayers
PlayerListDelta setPlayers(List< String > newPlayersList)
Definition: GameInstance.java:84
com.google.appinventor.components.runtime.util.GameInstance.getLeader
String getLeader()
Definition: GameInstance.java:61
com.google.appinventor.components.runtime.util.GameInstance.getInstanceId
String getInstanceId()
Definition: GameInstance.java:53
com.google.appinventor.components.runtime.util.GameInstance.GameInstance
GameInstance(String instanceId)
Definition: GameInstance.java:42
com.google.appinventor.components.runtime.Map< String, String >
com.google.appinventor.components.runtime.util.PlayerListDelta.NO_CHANGE
static PlayerListDelta NO_CHANGE
Definition: PlayerListDelta.java:22
com.google.appinventor.components.runtime.util.GameInstance.setLeader
void setLeader(String leader)
Definition: GameInstance.java:69
com.google.appinventor.components.runtime.util.GameInstance.getPlayers
List< String > getPlayers()
Definition: GameInstance.java:108
com.google.appinventor.components.runtime.util.GameInstance.getMessageTime
String getMessageTime(String type)
Definition: GameInstance.java:123