public GameInfoAlt[] GetGamesAlt(GameInfo.GameStatus status = GameInfo.GameStatus.AVAILABLE) { List<GameInfo> games = Registry.Instance.GetGames(status); GameInfoAlt[] results = new GameInfoAlt[games.Count]; for (int i=0; i<games.Count; i++) { results[i] = new GameInfoAlt() { Id = games[i].Id, CommunicationEndPoint = games[i].CommunicationEndPoint.ToString(), Status = games[i].Status.ToString(), AliveTimestamp = games[i].AliveTimestamp.ToString(), Label = games[i].Label }; } return results; }
public void GameInfo_TestEverything() { GameInfo g0 = new GameInfo(); Assert.AreEqual(0, g0.Id); Assert.IsNull(g0.Label); Assert.IsNotNull(g0.AliveTimestamp); Assert.IsTrue(g0.AliveTimestamp.AddMilliseconds(3000) > DateTime.Now); Assert.IsNull(g0.CommunicationEndPoint); Assert.AreEqual(GameInfo.GameStatus.NOT_INITIAlIZED, g0.Status); EndPoint ep = new EndPoint("113.24.4.1:1325"); g0 = new GameInfo() { Id = 10, Label = "Test", CommunicationEndPoint = ep, AliveTimestamp = DateTime.Now, Status = GameInfo.GameStatus.RUNNING }; Assert.AreEqual(10, g0.Id); Assert.AreEqual("Test", g0.Label); Assert.IsNotNull(g0.AliveTimestamp); Assert.IsTrue(g0.AliveTimestamp.AddMilliseconds(1000) > DateTime.Now); Assert.AreSame(ep, g0.CommunicationEndPoint); Assert.AreEqual(GameInfo.GameStatus.RUNNING, g0.Status); g0 = new GameInfo(200, "Testing", ep, GameInfo.GameStatus.COMPLETED); Assert.AreEqual(200, g0.Id); Assert.AreEqual("Testing", g0.Label); Assert.IsNotNull(g0.AliveTimestamp); Assert.IsTrue(g0.AliveTimestamp.AddMilliseconds(1000) > DateTime.Now); Assert.AreSame(ep, g0.CommunicationEndPoint); Assert.AreEqual(GameInfo.GameStatus.COMPLETED, g0.Status); g0 = new GameInfo(300, "More Testing", ep, "1"); Assert.AreEqual(300, g0.Id); Assert.AreEqual("More Testing", g0.Label); Assert.IsNotNull(g0.AliveTimestamp); Assert.IsTrue(g0.AliveTimestamp.AddMilliseconds(1000) > DateTime.Now); Assert.AreSame(ep, g0.CommunicationEndPoint); Assert.AreEqual(GameInfo.GameStatus.AVAILABLE, g0.Status); }
private bool GamesContain(GameInfo[] games, Int16 id, string label) { bool result = false; foreach (GameInfo game in games) if (game.Id == id && game.Label == label) { result = true; break; } return result; }
public void ChangeGameStatus(int gameId, GameInfo.GameStatus status) { log.DebugFormat("In ChangeChangeStatus for gameId={0}", gameId); lock (myLock) { if (games.ContainsKey(gameId)) { log.DebugFormat("Change status to {0}", status); games[gameId].Status = status; games[gameId].AliveTimestamp = DateTime.Now; Save(); } } }
public GameInfo RegisterGame(string label, Common.EndPoint publicEP) { log.Debug("In RegisterGame"); GameInfo game = null; if (!string.IsNullOrWhiteSpace(label)) { log.DebugFormat("Register {0} at {1}", label, publicEP.ToString()); game = new GameInfo() { Label = label, CommunicationEndPoint = publicEP, AliveTimestamp = DateTime.Now }; game.Id = GetNextIdNumber(); log.DebugFormat("New game's id={0}", game.Id); lock (myLock) { games.Add(game.Id, game); } Save(); LogContents(); } return game; }
public void LoadFromFile(string filename) { log.Debug("In LoadFromFile"); if (!string.IsNullOrWhiteSpace(filename) && File.Exists(filename)) { log.DebugFormat("Load from {0}", filename); this.fileName = filename; StreamReader reader = new StreamReader(fileName); while (!reader.EndOfStream) { string entry = reader.ReadLine(); string[] fields = entry.Split(','); if (fields.Length == 4) { Int16 gameId; if (Int16.TryParse(fields[0], out gameId)) { GameInfo game = new GameInfo(gameId, fields[1], new Common.EndPoint(fields[2]), fields[3]); game.AliveTimestamp = DateTime.Now; lock (myLock) { if (games.ContainsKey(game.Id)) games[game.Id] = game; else games.Add(game.Id, game); } } } } reader.Close(); } }
public List<GameInfo> GetGames(GameInfo.GameStatus status) { log.Debug("In GetGames"); List<GameInfo> filteredGameList = new List<GameInfo>(); lock (myLock) { Dictionary<int, GameInfo>.Enumerator enumerator = games.GetEnumerator(); while (enumerator.MoveNext()) { if (enumerator.Current.Value.Status == status) filteredGameList.Add(enumerator.Current.Value); } } return filteredGameList; }
public GameInfo[] GetGames(GameInfo.GameStatus status = GameInfo.GameStatus.AVAILABLE) { return Registry.Instance.GetGames(status).ToArray(); }
public void ChangeStatus(int gameId, GameInfo.GameStatus status) { Registry.Instance.ChangeGameStatus(gameId, status); }