private void DeleteGameQuery(string gToken, string uToken) { //Create a Connection MySqlConnection conn = new MySqlConnection(connectionString); TransactionQuery TQ = new TransactionQuery(conn); //Get the specific game from the DataBase TQ.addCommand("select count(*) from games where GameToken = ?gameToken"); TQ.addParms(new string[] { "gameToken" }, new string[] { gToken }); TQ.addReadParms(new string[] { "count(*)" }, new bool[] { true }); TQ.addConditions((result, command) => { if (result.Dequeue().Equals("0")) { SetResponse("gt"); return(false); } else { return(true); } }); //Check to make sure the gameStatus is waiting TQ.addCommand("select GameState, Player1Token from games where GameToken = ?gameToken"); TQ.addRepeatParm(); TQ.addReadParms(new string[] { "GameState", "Player1Token" }, new bool[] { false, true }); TQ.addConditions((result, command) => { if (!result.Dequeue().Equals("waiting")) { SetResponse("nw"); return(false); } if (result.Dequeue() != uToken) { SetResponse("player"); return(false); } Queue <string> returnQ; if (TQ.injectComand("update games set GameState = 'canceled' where GameToken = ?GameToken", new string[, ] { }, new string[] { }, false, out returnQ)) { SetResponse("delete"); } return(true); }); if (!TQ.Transaction()) { SetResponse("ISE"); return; } }
/// <summary> /// Returns the Brief game status of a game with a particular GAME ID /// </summary> public BriefGame GetBriefStatus(string gt) { if (gt == null) { //403 Misssing parameters SetResponse("NF"); return(null); } //Create a Connection and a CreateTransaction Query Object MySqlConnection conn = new MySqlConnection(connectionString); TransactionQuery TQ = new TransactionQuery(conn); //Variable for holding the status of the game, if status returns "", it means the queury didn't work string status = ""; // Query Games table for the status of the given game token. TQ.addCommand("select GameState from games where GameToken = ?gameToken"); TQ.addParms(new string[] { "gameToken" }, new string[] { gt }); TQ.addReadParms(new string[] { "GameState" }, new bool[] { true }); TQ.addConditions((result, command) => { status = result.Dequeue(); if (status == "waiting" || status == "finished") { return(false); } else { return(true); } }); //Variables for keeping duration, score1 and score2. string startTime = "", sc1 = "0", sc2 = "0"; int duration = 0; ///Query the Paried Game for GetBrief status, get the duration, score for player1 and player 2 TQ.addCommand("select StartTime, Score1, Score2 from pairedGames where GameToken = ?gameToken"); TQ.addReadParms(new string[] { "StartTime", "Score1", "Score2" }, new bool[] { false, false, true }); TQ.addRepeatParm(); TQ.addConditions((result, command) => { startTime = result.Dequeue(); sc1 = result.Dequeue(); sc2 = result.Dequeue(); GetTimeSpan(command, ref duration, ref status, startTime); return(true); }); //Proccess the transactions if (!TQ.Transaction()) { SetResponse("ISE"); return(null); } if (status == "") { SetResponse("gt"); return(null); } BriefGame bf = new BriefGame() { gameStatus = status, score1 = Convert.ToInt32(sc1), score2 = Convert.ToInt32(sc2), timeleft = duration }; appLog.WriteEntry("BoggleServer: FullGame successfully returned", EventLogEntryType.Information); return(bf); }
/// <summary> /// Returns the Full game status of a game with the particular GAME ID /// </summary> public FullGame GetStatus(string gt) { if (gt == null) { //403 Misssing parameters SetResponse("NF"); return(null); } //Create a Connection and a CreateTransaction Query Object MySqlConnection conn = new MySqlConnection(connectionString); TransactionQuery TQ = new TransactionQuery(conn); Queue <string> returnQ; //Variables for holding the strings that the query returns. string player2Token = "", player1Token = "", board = "", startTime = "", score1 = "0", score2 = "0", p1Name = "", p2Name = "", status = ""; int duration = 0; int Limit = 0; List <WordScore> p1Words = new List <WordScore>(); List <WordScore> p2Words = new List <WordScore>(); // Query Games table for the status and the Player1Token of the given game token. TQ.addCommand("select GameState, Player1Token from games where GameToken = ?gameToken"); TQ.addParms(new string[] { "gameToken" }, new string[] { gt }); TQ.addReadParms(new string[] { "GameState", "Player1Token" }, new bool[] { false, true }); TQ.addConditions((result, command) => { status = result.Dequeue(); player1Token = result.Dequeue(); TQ.injectComand("select Nickname from users where UserToken = ?player1Token", new string[, ] { { "player1Token", player1Token } }, new string[] { "Nickname" }, true, out returnQ); p1Name = returnQ.Dequeue(); if (status == "waiting" || status == "cancelled") { return(false); } if (status == "finished") { TQ.injectComand("select Word, Score from words where GameToken = ?gameToken and PlayerToken = ?player1Token", new string[, ] { }, new string[] { "Word", "Score" }, true, out returnQ); int count = returnQ.Count / 2; while (count > 0) { p1Words.Add(new WordScore() { word = returnQ.Dequeue(), score = Convert.ToInt32(returnQ.Dequeue()) }); count--; } } return(true); }); TQ.addCommand("select Player2Token, Board, StartTime, Score1, Score2 from pairedGames where GameToken = ?gameToken"); TQ.addReadParms(new string[] { "Player2Token", "Board", "StartTime", "Score1", "Score2" }, new bool[] { false, false, false, false, true }); TQ.addRepeatParm(); TQ.addConditions((result, command) => { player2Token = result.Dequeue(); board = result.Dequeue(); startTime = result.Dequeue(); score1 = result.Dequeue(); score2 = result.Dequeue(); Limit = Convert.ToInt32(TotalTime); GetTimeSpan(command, ref duration, ref status, startTime); TQ.injectComand("select Nickname from users where UserToken = ?player2Token", new string[, ] { { "player2Token", player2Token } }, new string[] { "Nickname" }, true, out returnQ); p2Name = returnQ.Dequeue(); if (status == "finished") { TQ.injectComand("select Word, Score from words where GameToken = ?gameToken and PlayerToken = ?player2Token", new string[, ] { }, new string[] { "Word", "Score" }, true, out returnQ); int count = returnQ.Count / 2; while (count > 0) { p2Words.Add(new WordScore() { word = returnQ.Dequeue(), score = Convert.ToInt32(returnQ.Dequeue()) }); count--; } } return(true); }); if (!TQ.Transaction()) { SetResponse("ISE"); return(null); } if (status == "") { SetResponse("gt"); return(null); } // Create return object from extracted data PlayerStatus ps1 = new PlayerStatus() { Score = Convert.ToInt32(score1), nickname = p1Name, wordsPlayed = p1Words }; PlayerStatus ps2 = new PlayerStatus() { Score = Convert.ToInt32(score2), nickname = p2Name, wordsPlayed = p2Words }; FullGame fg = new FullGame() { player1 = ps1, player2 = ps2, board = board, gameStatus = status, timeleft = duration, timelimit = Limit }; appLog.WriteEntry("BoggleServer: FullGame successfully returned", EventLogEntryType.Information); return(fg); }