/// <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); }
/// <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); } }