/// Play a word in a game.
        /// If Word is null or empty or longer than 30 characters when trimmed, or if GameID or UserToken is invalid, or if UserToken is not a player in the
        /// game identified by GameID, responds with response code 403 (Forbidden).
        /// Otherwise, if the game state is anything other than "active", responds with response code 409 (Conflict).
        /// Otherwise, records the trimmed Word as being played by UserToken in the game identified by GameID. Returns the score for Word in the context of the
        /// game (e.g. if Word has been played before the score is zero). Responds with status 200 (OK). Note: The word is not case sensitive.
        public WordScore PlayWord(WordToPlay w, string gameID)
        {
            lock (sync)
            {
                if (w.Word == null || w.Word.Trim().Length > 30 || w.UserToken == null || !players.ContainsKey(w.UserToken) || gameID == null || !(games.TryGetValue(gameID, out GameStatus temp) || (temp.Player1.UserToken != w.UserToken || temp.Player2.UserToken != w.UserToken)))
                {
                    SetStatus(Forbidden);
                    return(null);
                }
                else
                {
                    games.TryGetValue(gameID, out GameStatus g);
                    if (!g.GameState.Equals("active"))
                    {
                        SetStatus(Conflict);
                        return(null);
                    }
                    else
                    {
                        updateTime(gameID);

                        Words wordPlay = new Words();
                        wordPlay.Word = w.Word;

                        // Generate boggle board
                        board = new BoggleBoard(g.Board);
                        players.TryGetValue(w.UserToken, out Player p);

                        // Score 0 if the word is less than 3 characters or -1 if it doesn't exist in dic.
                        wordPlay.Score = board.CanBeFormed(wordPlay.Word) ? GetScore(wordPlay.Word) : -1;
                        if (wordPlay.Word.Length < 3)
                        {
                            wordPlay.Score = 0;
                        }
                        else
                        {
                            foreach (Words word in p.WordsPlayed)
                            {
                                if (word.Word.ToUpper().Equals(wordPlay.Word.ToUpper()))
                                {
                                    wordPlay.Score = 0;
                                }
                            }
                        }

                        // Set the appropriate score for each word
                        WordScore scoreWord = new WordScore();
                        scoreWord.Score = wordPlay.Score;

                        // Update the players' scores
                        p.Score += wordPlay.Score;
                        p.WordsPlayed.Add(wordPlay);

                        SetStatus(OK);
                        return(scoreWord);
                    }
                }
            }
        }
Exemple #2
0
        /// Play a word in a game.
        /// If Word is null or empty or longer than 30 characters when trimmed, or if GameID or UserToken is invalid, or if UserToken is not a player in the
        /// game identified by GameID, responds with response code 403 (Forbidden).
        /// Otherwise, if the game state is anything other than "active", responds with response code 409 (Conflict).
        /// Otherwise, records the trimmed Word as being played by UserToken in the game identified by GameID. Returns the score for Word in the context of the
        /// game (e.g. if Word has been played before the score is zero). Responds with status 200 (OK). Note: The word is not case sensitive.
        public WordScore PlayWord(WordToPlay w, string gameID)
        {
            int timeLeft;

            if (w.Word == null || w.Word.Equals("") || w.Word.Trim().Length > 30 || w.UserToken == null || !tryGetPlayer(w.UserToken, out Player p) || gameID == null || !int.TryParse(gameID, out int GameID) || !((timeLeft = getTimeLeft(GameID)) is int) || !tryGetGame(GameID, out GameStatus g) || (!w.UserToken.Equals(g.Player1.UserToken) && !w.UserToken.Equals(g.Player2.UserToken)))
            {
                SetStatus(Forbidden);
                return(null);
            }
            else
            {
                if (!g.GameState.Equals("active"))
                {
                    SetStatus(Conflict);
                    return(null);
                }
                else
                {
                    Words wordPlay = new Words();
                    wordPlay.Word = w.Word;

                    // Generate boggle board
                    BoggleBoard board = new BoggleBoard(g.Board);

                    // Score 0 if the word is less than 3 characters or -1 if it doesn't exist in dic.
                    wordPlay.Score = board.CanBeFormed(wordPlay.Word) ? GetScore(wordPlay.Word) : -1;

                    if (wordPlay.Word.Length < 3)
                    {
                        wordPlay.Score = 0;
                    }
                    else
                    {
                        foreach (Words word in getWordsPlayed(w.UserToken, GameID))
                        {
                            if (word.Word.ToUpper().Equals(wordPlay.Word.ToUpper()))
                            {
                                wordPlay.Score = 0;
                            }
                        }
                    }

                    // Set the appropriate score for each word
                    WordScore scoreWord = new WordScore();
                    scoreWord.Score = wordPlay.Score;

                    // Update the players' played words
                    addWordToList(w, GameID, scoreWord.Score);

                    SetStatus(OK);
                    return(scoreWord);
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Private helper method to insert words to word table
        /// </summary>
        /// <param name="Word"></param>
        /// <param name="GameID"></param>
        /// <param name="Score"></param>
        private void addWordToList(WordToPlay Word, int GameID, int Score)
        {
            using (SqlConnection conn = new SqlConnection(BoggleDB))
            {
                conn.Open();

                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    using (SqlCommand command = new SqlCommand("insert into Words (Word, GameID, Player, Score) output inserted.Id values(@Word, @GameID, @Player, @Score)", conn, trans))
                    {
                        command.Parameters.AddWithValue("@Word", Word.Word);
                        command.Parameters.AddWithValue("@Player", Word.UserToken);
                        command.Parameters.AddWithValue("@GameID", GameID);
                        command.Parameters.AddWithValue("@Score", Score);

                        command.ExecuteScalar().ToString();

                        trans.Commit();
                        return;
                    }
                }
            }
        }