public GameStatus GetGameStatus(int gameID, string brief) { // Checks if gameID is valid if (!games.ContainsKey(gameID)) { SetStatus(Forbidden); return(null); } // else return status and update game else { Game thisGame = games[gameID]; GameStatus status = new GameStatus(); // if game is pending if (thisGame.GameState == "pending") { SetStatus(OK); return(new GameStatus() { GameState = "pending" }); } // if game is active or completed and "Brief=yes" was a parameter if ((thisGame.GameState == "active" || thisGame.GameState == "complete") && brief == "yes") { status = new GameStatus() { GameState = thisGame.GameState, TimeLeft = thisGame.TimeLeft, Player1 = new Player() { Score = thisGame.Player1Score }, Player2 = new Player() { Score = thisGame.Player2Score } }; } // if game is active and "Brief=yes" was not a parameter else if (thisGame.GameState == "active" && brief != "yes") { status = new GameStatus() { GameState = thisGame.GameState, Board = thisGame.GameBoard, TimeLimit = thisGame.TimeLimit, TimeLeft = thisGame.TimeLeft, Player1 = new Player() { Nickname = users[thisGame.Player1Token].Nickname, Score = thisGame.Player1Score }, Player2 = new Player() { Nickname = users[thisGame.Player2Token].Nickname, Score = thisGame.Player2Score } }; } // if game is complete and user did not specify brief else if (thisGame.GameState == "completed" && brief != "yes") { var Player1Scores = new HashSet <WordScore>(); foreach (KeyValuePair <string, int> kv in thisGame.Player1WordScores) { Player1Scores.Add(new WordScore() { Word = kv.Key, Score = kv.Value }); } var Player2Scores = new HashSet <WordScore>(); foreach (KeyValuePair <string, int> kv in thisGame.Player2WordScores) { Player2Scores.Add(new WordScore() { Word = kv.Key, Score = kv.Value }); } status = new GameStatus() { GameState = thisGame.GameState, Board = thisGame.GameBoard, TimeLimit = thisGame.TimeLimit, TimeLeft = thisGame.TimeLeft, Player1 = new Player() { Nickname = users[thisGame.Player1Token].Nickname, Score = thisGame.Player1Score, WordsPlayed = Player1Scores }, Player2 = new Player() { Nickname = users[thisGame.Player2Token].Nickname, Score = thisGame.Player2Score, WordsPlayed = Player2Scores } }; } // update game games[gameID].TimeLeft -= (DateTime.Now - thisGame.StartTime).Seconds; if (games[gameID].TimeLeft <= 0) { games[gameID].GameState = "complete"; games[gameID].TimeLeft = 0; } SetStatus(OK); return(status); } }
/// <summary> /// Processes content as it is received /// </summary> private void ContentReceived(string s, Exception e, object payload) { string httpstatus; Console.WriteLine("s: " + s); Console.WriteLine("payload: " + ((string[])payload)[0]); Console.WriteLine("payload: " + ((string[])payload)[1]); if (s != null) { //where to store a serialized object for beginsend string result = ""; ���������������� //string representing the request type ����������������string request = ((string[])payload)[0]; //(will this throw a null exception??) ���������������� //string containing the url ����������������string url = ((string[])payload)[1]; //(will this throw a null exception??) ���������������� //switch statement based upon result of request to server passed through "payload" param (ie request) ����������������switch(request) { case "createuser": //deserialize json parameters in s to pass to appropriate method in BS UserInfo userinfo = JsonConvert.DeserializeObject <UserInfo>(s); //Check that object has required fields else bad request //TODO ������������������������//store the return object of the method ������������������������userinfo = boggleservice.CreateUser(userinfo); httpstatus = userinfo.HttpStatus; //remove httpstatus userinfo.HttpStatus = null; ������������������������//serialize the return object ������������������������result = JsonConvert.SerializeObject(userinfo, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); Console.WriteLine(result); ������������������������//get the http status?����������������������� ������������������������break; case "joingame": ������������������������//deserialize json parameters in s to pass to appropriate method in BS ������������������������userinfo = JsonConvert.DeserializeObject <UserInfo>(s); //Check that object has required fields else bad request //TODO //store the return object of the method userinfo = boggleservice.JoinGame(userinfo); httpstatus = userinfo.HttpStatus; //remove httpstatus userinfo.HttpStatus = null; ������������������������//serialize the return object ������������������������result = JsonConvert.SerializeObject(userinfo, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); Console.WriteLine(result); //get the http status?����������������������� ������������������������break; case "cancelgame": ������������������������//deserialize json parameters in s to pass to appropriate method in BS ������������������������userinfo = JsonConvert.DeserializeObject <UserInfo>(s); //Check that object has required fields else bad request //TODO //store the return object of the method userinfo = boggleservice.CancelJoinRequest(userinfo); httpstatus = userinfo.HttpStatus; //remove httpstatus userinfo.HttpStatus = null; ������������������������//serialize the return object ������������������������result = JsonConvert.SerializeObject(userinfo, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); ������������������������//get the http status?����������������������� ������������������������break; case "playword": ������������������������//deserialize json parameters in s to pass to appropriate method in BS ������������������������userinfo = JsonConvert.DeserializeObject <UserInfo>(s); //Check that object has required fields else bad request //TODO //extract gameid from URL string gameid = ((string[])payload)[2]; //store the return object of the method userinfo = boggleservice.PlayWord(gameid, userinfo); httpstatus = userinfo.HttpStatus; //remove httpstatus userinfo.HttpStatus = null; //serialize the return object result = JsonConvert.SerializeObject(userinfo, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); ������������������������//get the http status?����������������������� ������������������������break; case "gamestatus": ������������������������//extract data from the url ������������������������string brief = ""; if (url.Contains("yes")) { brief = "yes"; } //save gameid gameid = ((string[])payload)[2]; ������������������������//serialize the return object ������������������������GameStatus gamestatus = boggleservice.GameStatus(gameid, brief); httpstatus = gamestatus.HttpStatus; //remove httpstatus gamestatus.HttpStatus = null; result = JsonConvert.SerializeObject(gamestatus, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); ������������������������//get the http status? ������������������������break; default: httpstatus = "400 Bad Request"; break; } Console.WriteLine(httpstatus); ss.BeginSend("HTTP/1.1 " + httpstatus + "\r\n", Ignore, null); ss.BeginSend("Content-Type: application/json\r\n", Ignore, null); ss.BeginSend("Content-Length: " + result.Length + "\r\n", Ignore, null); ss.BeginSend("\r\n", Ignore, null); ss.BeginSend(result, (ex, py) => { ss.Shutdown(); }, null); } }