/// <summary> /// helper method to start a game after 2 players have joind the samed game /// </summary> /// <param name="g"></param> /// <param name="item"></param> /// <param name="gUID"></param> /// <param name="time"></param> /// <returns></returns> private GameInfo startGame(GameInfo g, PostGame item, int gUID, int time) { GameInfo newGame = new GameInfo(); newGame.Board = new BoggleBoard().ToString(); newGame.GameState = "active"; newGame.GameID = gUID.ToString(); newGame.Player1 = g.Player1; newGame.Player2 = g.Player2; newGame.Player1.Score = 0; newGame.Player2.Score = 0; newGame.Player1.Nickname = g.Player1.Nickname.ToString(); newGame.Player2.Nickname = GetUserInfo(item); newGame.Player1.WordsPlayed = new HashSet <Words>(); newGame.Player2.WordsPlayed = new HashSet <Words>(); newGame.TimeLimit = getTimeLimit(g.TimeLimit, time); newGame.TimeLeft = newGame.TimeLimit; newGame.startTime = DateTime.Now; //for testing boards if (newGame.Player2.Nickname.Equals("TestPlayer")) { newGame.Board = new BoggleBoard("NAMEGAMEPAINGAIN").ToString(); } return(newGame); }
public void TestMethod10() { lock (sync) { UserInfo u = new UserInfo(); u.Nickname = "jon"; Response r = client.DoPostAsync("users", u).Result; PostGame pg1 = new PostGame(); pg1.UserToken = r.Data["UserToken"]; pg1.TimeLimit = 30; Response pgr1 = client.DoPostAsync("games", pg1).Result; Assert.AreEqual(Created, pgr1.Status); UserInfo u2 = new UserInfo(); u2.Nickname = "TestPlayer"; Response r2 = client.DoPostAsync("users", u2).Result; PostGame pg2 = new PostGame(); pg2.UserToken = r2.Data["UserToken"]; pg2.TimeLimit = 45; Response pgr2 = client.DoPostAsync("games", pg2).Result; Assert.AreEqual(Accepted, pgr2.Status); Token t = new Token(); t.UserToken = r2.Data["UserToken"]; Response r3 = client.DoPutAsync(t, "games").Result; Assert.AreEqual(OK, r3.Status); } }
/// <summary> /// gets the users nickname /// </summary> /// <param name="item"></param> /// <returns></returns> private string GetUserInfo(PostGame item) { Token t = new Token(); t.UserToken = item.UserToken; users.TryGetValue(t.UserToken, out UserInfo user); return(user.Nickname); }
public void TestMethod7() { lock (sync) { UserInfo u = new UserInfo(); u.Nickname = "jon"; Response r = client.DoPostAsync("users", u).Result; PostGame pg1 = new PostGame(); pg1.UserToken = r.Data["UserToken"]; pg1.TimeLimit = 3; Response pgr1 = client.DoPostAsync("games", pg1).Result; Assert.AreEqual(Forbidden, pgr1.Status); } }
public void TestMethod9() { lock (sync) { UserInfo u = new UserInfo(); u.Nickname = "jon"; Response r = client.DoPostAsync("users", u).Result; PostGame pg1 = new PostGame(); pg1.UserToken = r.Data["UserToken"]; pg1.TimeLimit = 30; Response pgr1 = client.DoPostAsync("games", pg1).Result; Assert.AreEqual(Accepted, pgr1.Status); Token t = new Token(); t.UserToken = ""; Response r2 = client.DoPutAsync(t, "games").Result; Assert.AreEqual(Forbidden, r2.Status); } }
public void TestMethod12() { lock (sync) { UserInfo u = new UserInfo(); u.Nickname = "jon"; Response r = client.DoPostAsync("users", u).Result; PostGame pg1 = new PostGame(); pg1.UserToken = r.Data["UserToken"]; pg1.TimeLimit = 10; Response pgr1 = client.DoPostAsync("games", pg1).Result; Assert.AreEqual(Accepted, pgr1.Status); string GameID = pgr1.Data["GameID"]; Response res = client.DoGetAsync("games/{0}?Brief={1}", GameID, "yes").Result; Assert.AreEqual(OK, res.Status); } }
public void TestMethod5() { lock (sync) { UserInfo u = new UserInfo(); u.Nickname = "jon"; Response r = client.DoPostAsync("users", u).Result; PostGame pg1 = new PostGame(); pg1.UserToken = r.Data["UserToken"]; pg1.TimeLimit = 30; Response pgr1 = client.DoPostAsync("games", pg1).Result; Assert.AreEqual(Accepted, pgr1.Status); UserInfo u2 = new UserInfo(); u2.Nickname = "TestPlayer"; Response r2 = client.DoPostAsync("users", u2).Result; PostGame pg2 = new PostGame(); pg2.UserToken = r2.Data["UserToken"]; pg2.TimeLimit = 45; Response pgr2 = client.DoPostAsync("games", pg2).Result; Assert.AreEqual(Created, pgr2.Status); string GameID = pgr2.Data["GameID"]; PlayedWord word = new PlayedWord(); word.UserToken = r.Data["UserToken"]; word.Word = ""; string url = String.Format("games/{0}", GameID); Response g = client.DoPutAsync(word, url).Result; Assert.AreEqual(Forbidden, g.Status); } }
/// <summary> /// server response for join game /// </summary> /// <param name="body"></param> private void JoinGame(string body) { HttpStatusCode httpStatus; PostGame post = JsonConvert.DeserializeObject <PostGame>(body); gameId game = service.JoinGame(post, out httpStatus); string res = ("HTTP/1.1 " + (int)httpStatus + " " + httpStatus + "\r\n"); if ((int)httpStatus / 100 == 2) { string result = JsonConvert.SerializeObject(game); res += ("Content-Type: application/json\r\n"); res += ("Content-Length: " + Encoding.UTF8.GetByteCount(result) + "\r\n"); res += "\r\n"; res += result; } else { res += "\r\n"; } socket.BeginSend(res, (x, y) => { socket.Shutdown(SocketShutdown.Both); }, null); }
public void TestMethod11() { lock (sync) { UserInfo u = new UserInfo(); u.Nickname = "jon"; Response r = client.DoPostAsync("users", u).Result; PostGame pg1 = new PostGame(); pg1.UserToken = r.Data["UserToken"]; pg1.TimeLimit = 10; Response pgr1 = client.DoPostAsync("games", pg1).Result; Assert.AreEqual(Accepted, pgr1.Status); UserInfo u2 = new UserInfo(); u2.Nickname = "TestPlayer"; Response r2 = client.DoPostAsync("users", u2).Result; PostGame pg2 = new PostGame(); pg2.UserToken = r2.Data["UserToken"]; pg2.TimeLimit = 10; Response pgr2 = client.DoPostAsync("games", pg2).Result; Assert.AreEqual(Created, pgr2.Status); string GameID = pgr2.Data["GameID"]; PlayedWord word = new PlayedWord(); word.UserToken = r.Data["UserToken"]; word.Word = "name"; string url = String.Format("games/{0}", GameID); Response g = client.DoPutAsync(word, url).Result; Assert.AreEqual(OK, g.Status); PlayedWord word2 = new PlayedWord(); word2.UserToken = r.Data["UserToken"]; word2.Word = "lane"; Response g2 = client.DoPutAsync(word2, url).Result; Assert.AreEqual(OK, g.Status); PlayedWord word3 = new PlayedWord(); word3.UserToken = r.Data["UserToken"]; word3.Word = "name"; Response g3 = client.DoPutAsync(word3, url).Result; Assert.AreEqual(OK, g.Status); PlayedWord word4 = new PlayedWord(); word4.UserToken = r2.Data["UserToken"]; word4.Word = "name"; Response g4 = client.DoPutAsync(word4, url).Result; Assert.AreEqual(OK, g.Status); PlayedWord word5 = new PlayedWord(); word5.UserToken = r2.Data["UserToken"]; word5.Word = "name"; Response g5 = client.DoPutAsync(word5, url).Result; Assert.AreEqual(OK, g.Status); Response res = client.DoGetAsync("games/{0}?Brief={1}", GameID, "yes").Result; Assert.AreEqual(OK, res.Status); Response res2 = client.DoGetAsync("games/{0}?Brief={1}", GameID, "no").Result; Assert.AreEqual(OK, res.Status); System.Threading.Thread.Sleep(10000); Response res3 = client.DoGetAsync("games/{0}?Brief={1}", GameID, "yes").Result; Assert.AreEqual(OK, res.Status); Response res4 = client.DoGetAsync("games/{0}?Brief={1}", GameID, "no").Result; Assert.AreEqual(OK, res.Status); } }
/// <summary> /// Join a boggle game /// </summary> /// <param name="item"></param> /// <returns></returns> public gameId JoinGame(PostGame item, out HttpStatusCode httpStatus) { httpStatus = OK; lock (sync) { gameId ID = new gameId(); if (item.UserToken == null || item.TimeLimit < 0) { httpStatus = Forbidden; return(null); } if (item.TimeLimit < 5 || item.TimeLimit > 120 || !users.ContainsKey(item.UserToken)) { httpStatus = Forbidden; return(null); } if (pendingGames.Count == 0) { GameInfo g = new GameInfo(); g.GameState = "pending"; g.GameID = "" + GUID; g.Player1 = new UserInfo(); g.Player2 = new UserInfo(); pendingGames.Add(g.GameID, g); GUID++; } foreach (GameInfo g in pendingGames.Values) { if (g.Player1.UserToken != null) { if (g.Player1.UserToken == item.UserToken) { httpStatus = Conflict; return(null); } } } foreach (GameInfo g in pendingGames.Values) { if (g.Player1.UserToken == null) { g.Player1.UserToken = item.UserToken; g.TimeLimit = item.TimeLimit; g.Player1.Nickname = GetUserInfo(item); //g.GameID = GUID+""; ID.GameID = g.GameID; httpStatus = Accepted; break; } else if (g.Player2.UserToken == null) { g.Player2.UserToken = item.UserToken; ID.GameID = g.GameID; httpStatus = Created; } if (g.Player1.UserToken != null && g.Player2.UserToken != null) { int.TryParse(ID.GameID, out int i); GameInfo game = startGame(g, item, i, item.TimeLimit); games.Add(g.GameID, game); pendingGames.Remove(i.ToString()); break; } } // GUID++; return(ID); } }