/// <summary> /// Adds the word to the players played word list and score and /// returns the words score /// </summary> public static async Task <WordValue> PlayWord(PlayWord w) { using (HttpClient client = CreateClient()) { StringContent content = new StringContent(JsonConvert.SerializeObject(w), Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync("/Boggle.svc/playword", content); if (response.IsSuccessStatusCode) { String result = await response.Content.ReadAsStringAsync(); return(JsonConvert.DeserializeObject <WordValue>(result)); } else { return(null); } } }
/// <summary> /// Takes a word and adds it to a players list while increasing their score in a game with a particular game ID /// Returns the words value /// </summary> public WordValue PlayWord(PlayWord w) { if (w == null) { SetResponse("NF"); //403 forbidden missing parameters return(null); } if (String.IsNullOrEmpty(w.playerToken)) { SetResponse("player"); //403 Forbidden Response(invalid player token) return(null); } if (String.IsNullOrEmpty(w.word)) { SetResponse("word"); //403 Forbidden Response(invalid player token) return(null); } if (String.IsNullOrEmpty(w.gameToken)) { SetResponse("gt"); //403 Forbidden Response (invalid game token) return(null); } return(PlayWordQuery(w)); }
/// <summary> /// Takes a word and adds it to a players list while increasing their score in a game with a particular game ID /// Returns the words value /// </summary> public int?PlayWord(PlayWord w) { if (w == null) { SetResponse("NF"); return(null); } if (String.IsNullOrEmpty(w.playerToken)) { SetResponse("player"); //403 Forbidden Response(invalid player token) return(null); } if (String.IsNullOrEmpty(w.word)) { SetResponse("word"); //403 Forbidden Response(invalid player token) return(null); } if (String.IsNullOrEmpty(w.gameToken)) { SetResponse("gt"); //403 Forbidden Response (invalid game token) return(null); } SetResponse("Success"); int value = WordLookUp(w.word); Game game; if (Games.TryGetValue(w.gameToken, out game)) //Find game { BoggleBoard bb = new BoggleBoard(game.board); if (!bb.CanBeFormed(w.word)) //TODO check to make sure its not negative points { value = 0; } if (game.Player1.userToken == w.playerToken) //Check if player is Player1 { if (value > 0 || value < 1) //If word value is greater than 0 { if (!game.Player1Words.ContainsKey(w.word)) //If Players word list does not contain the word being played { game.Player1Words.Add(w.word, value); //Add it to player word list //game.Player1.wordsPlayed.Add(word, value); //Add to word to player words played list //WordListComparer(g.gameToken); //Check if Player lists are unique and removes like words } } } else if (game.Player2.userToken == w.playerToken) //Repeat previous if the player is player two { if (value > 0 || value < 1) { if (!game.Player2Words.ContainsKey(w.word)) { game.Player2Words.Add(w.word, value); //game.Player2.wordsPlayed.Add(word, value); //Add to words to word played //WordListComparer(g.gameToken); } } } else { SetResponse("player"); //403 Forbidden Response(invalid player token) return(null); } } return(value); }
private WordValue PlayWordQuery(PlayWord w) { //Create a Connection MySqlConnection conn = new MySqlConnection(connectionString); //CreateTransaction Query Object TransactionQuery TQ = new TransactionQuery(conn); int player = 0; ///Check for games with gametoken TQ.addCommand("select count(*) from games where GameToken = ?gameToken"); TQ.addParms(new string[] { "gameToken" }, new string[] { w.gameToken }); TQ.addReadParms(new string[] { "count(*)" }, new bool[] { true }); TQ.addConditions((result, command) => { if (result.Dequeue().Equals("0")) { SetResponse("gt"); return(false); } else { Queue <string> returnQ; TQ.injectComand("select Player1Token from games where GameToken = ?gameToken", new string[, ] { }, new string[] { "Player1Token" }, true, out returnQ); if (returnQ.Dequeue() == w.playerToken) { player = 1; } return(true); } }); bool status = true; string gameState = ""; ///Check if the gamestate is playing TQ.addCommand("select GameState from games where GameToken = ?gameToken"); TQ.addParms(new string[] { }, new string[] { }); TQ.addReadParms(new string[] { "GameState" }, new bool[] { true }); TQ.addConditions((result, command) => { gameState = result.Dequeue(); if (gameState.Equals("playing")) { return(true); } else if (player == 1) { SetResponse("waitingPlay"); status = false; return(false); } else { SetResponse("wrongStatus"); status = false; return(false); } }); string board; //Get the board and see if word can be played TQ.addCommand("select Player2Token, Board from pairedGames where GameToken = ?gameToken"); TQ.addParms(new string[] { }, new string[] { }); TQ.addReadParms(new string[] { "Player2Token", "Board" }, new bool[] { false, true }); TQ.addConditions((result, command) => { if (result.Dequeue() == w.playerToken) { player = 2; } board = result.Dequeue(); BoggleBoard b = new BoggleBoard(board); points = WordLookUp(w.word, board); if (player == 0) { SetResponse("player"); return(false); } return(true); }); int totalScore = 0; bool repeat = false; ///Check if word is already played TQ.addCommand("select count(Word) from words where GameToken = ?gameToken and PlayerToken = ?playerToken"); TQ.addParms(new string[] { "playerToken" }, new string[] { w.playerToken }); TQ.addReadParms(new string[] { "Count(Word)" }, new bool[] { true }); TQ.addConditions((result, command) => { command.CommandText = "select Word, Score from words where GameToken = ?gameToken and PlayerToken = ?playerToken"; if (command.ExecuteNonQuery() == 0) { throw new Exception("GET WORDS FAILED"); } using (MySqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { if (reader.GetString("Word") == w.word) { points = 0; } totalScore += Convert.ToInt32(reader.GetString("Score")); } } if (!repeat) { totalScore += points; command.CommandText = "update pairedGames set Score" + player + "=?Score where GameToken=?gameToken"; command.Parameters.AddWithValue("Score", totalScore); if (command.ExecuteNonQuery() == 0) { throw new Exception("Update player score failed"); } command.CommandText = "insert into words (GameToken, PlayerToken, Word, Score) values (?gameToken, ?pt, ?word, ?wordscore)"; command.Parameters.AddWithValue("pt", w.playerToken); command.Parameters.AddWithValue("word", w.word); command.Parameters.AddWithValue("wordscore", points); if (command.ExecuteNonQuery() == 0) { throw new Exception("insert word score failed"); } return(true); } else { return(false); } }); if (!TQ.Transaction()) { SetResponse("ISE"); return(null); } else { if (status) { return new WordValue() { wordScore = points } } ; else { return(null); } } }
public void ResponseTest() { Player p = null; string gt; string ut = MakeUser(p).Result.userToken; if (!String.IsNullOrEmpty(ut)) { Assert.Fail("Not failing"); } gt = JoinGame(p).Result.gameToken; if (!String.IsNullOrEmpty(gt)) { Assert.Fail("Not failing"); } p = new Player() { userToken = null }; gt = JoinGame(p).Result.gameToken; if (!String.IsNullOrEmpty(gt)) { Assert.Fail("Not failing"); } p = new Player() { userToken = "0" }; gt = JoinGame(p).Result.gameToken; if (!String.IsNullOrEmpty(gt)) { Assert.Fail("Not failing"); } p = new Player() { nickname = "test" }; ut = MakeUser(p).Result.userToken; p.userToken = ut; gt = JoinGame(p).Result.gameToken; gt = JoinGame(p).Result.gameToken; if (!String.IsNullOrEmpty(gt)) { Assert.Fail("Not failing"); } //TODO these tests are not working need to figure out why??? if (DeleteGame("", null).Result) { Assert.Fail("delete game nulll user token respones not working"); } if (DeleteGame(null, "").Result) { Assert.Fail("delete game nulll user game respones not working"); } if (DeleteGame("", "").Result) { Assert.Fail("delete game couldnt find gametoken in games list response not working"); } FullGame fg = GetStatus(null).Result; if (fg != null) { Assert.Fail("full game null respones not working"); } fg = GetStatus("").Result; if (fg != null) { Assert.Fail("full game not game with game token respones not working"); } BriefGame bg = GetBriefStatus(null).Result; if (bg != null) { Assert.Fail("brief game null respones not working"); } bg = GetBriefStatus("").Result; if (bg != null) { Assert.Fail("brief game not game with game token respones not working"); } PlayWord w = null; int? pw = PlayWord(w).Result.wordScore; if (pw != null) { Assert.Fail("playword respones failing"); } w = new PlayWord() { playerToken = "" }; pw = PlayWord(w).Result.wordScore; if (pw != null) { Assert.Fail("playword respones failing"); } }
public void SimpleDataStructureTest() { BoggleService Boggle = new BoggleService(); Player[] uT = new Player[USERS]; string[] gT = new string[GAMES]; for (int i = 0; i < USERS; i++) { string nn = i.ToString(); Player p = new Player { nickname = nn }; uT[i] = new Player() { userToken = MakeUser(p).Result.userToken }; } int gc = 0; //counter int pc = 0; foreach (Player tempP in uT) { if (pc % 2 == 0) { gT[gc] = JoinGame(tempP).Result.gameToken; Assert.AreEqual(GetBriefStatus(gT[gc]).Result.gameStatus, "waiting"); Assert.AreEqual(GetStatus(gT[gc]).Result.gameStatus, "waiting"); Assert.AreEqual(GetStatus(gT[gc]).Result.player1.Score, 0); Assert.AreEqual(GetStatus(gT[gc]).Result.board, ""); gc++; } else { string gt = JoinGame(tempP).Result.gameToken; Assert.AreEqual(GetBriefStatus(gT[gc - 1]).Result.gameStatus, "playing"); Assert.AreEqual(GetStatus(gT[gc - 1]).Result.gameStatus, "playing"); } pc++; } for (int i = 0; i < 30; i++) { int uc = 0; //counter foreach (string s in gT) { if (GetBriefStatus(s).Result.gameStatus == "playing") { BoggleBoard boggleboard = new BoggleBoard(GetStatus(s).Result.board); gameboard = GetStatus(s).Result.board; PlayerStatus p1 = GetStatus(s).Result.player1; PlayWord pw = new PlayWord() { gameToken = s, playerToken = uT[uc].userToken, word = playWords1[i] }; uT[uc].Score = uT[uc].Score + (int)PlayWord(pw).Result.wordScore; } else { Assert.Fail("Game in waiting"); } Assert.AreEqual(GetStatus(s).Result.player1.Score, uT[uc].Score); Assert.AreEqual(GetBriefStatus(s).Result.score1, uT[uc].Score); uc = uc + 2; } } }