Пример #1
0
        /// <summary>
        /// method for play word
        /// </summary>
        /// <param name="user">the UserToek and the word that this player typied. </param>
        /// <param name="GameID">the GameID of this player in . </param>
        /// <returnsplayWordsReturn>return the score of the word that the user type. </returns>
        public playWordsReturn PlayWords(playGameInfo user, String GameID)
        {
            //get gameID
            int  number;
            bool result = Int32.TryParse(GameID, out number);

            //check user info
            if (user == null || user.Word == null || user.Word.Trim().Length == 0 || GameID == null ||
                user.UserToken == null || !result)
            {
                SetStatus(Forbidden);
                return(null);
            }
            //check game status
            statudReturn4 check = gameStatus(GameID, "");

            if (check.GameState != "active")
            {
                SetStatus(Conflict);
                return(null);
            }
            //
            if (checkmatching(GameID, user.UserToken) == false)
            {
                SetStatus(Forbidden);
                return(null);
            }
            using (SqlConnection conn = new SqlConnection(BoggleDB))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    BoggleBoard NewGame     = new BoggleBoard(check.Board);
                    int         scoreResult = 0;
                    if (user.Word.Length < 3)
                    {
                        scoreResult = 0;
                    }
                    else
                    if (NewGame.CanBeFormed(user.Word) && File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory.ToString() + "dictionary.txt").Contains(user.Word))
                    {
                        scoreResult = CalculateScore(user.Word);
                    }
                    else
                    {
                        scoreResult--;
                    }
                    if (checkWordPlay(GameID, user.UserToken, user.Word) == false)
                    {
                        using (SqlCommand cmd = new SqlCommand(getPath("updatePlayWords"), conn, trans))
                        {
                            cmd.Parameters.AddWithValue("@Word", user.Word);
                            cmd.Parameters.AddWithValue("@Score", scoreResult);
                            cmd.Parameters.AddWithValue("@GameID", GameID);
                            cmd.Parameters.AddWithValue("@Player", user.UserToken);
                            cmd.ExecuteNonQuery();
                            trans.Commit();
                        }
                    }
                    else
                    {
                        scoreResult = 0;
                    }
                    playWordsReturn info = new playWordsReturn();
                    info.Score = scoreResult;
                    SetStatus(OK);
                    return(info);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// method for play word
        /// </summary>
        /// <param name="user">the UserToek and the word that this player typied. </param>
        /// <param name="GameID">the GameID of this player in . </param>
        /// <returnsplayWordsReturn>return the score of the word that the user type. </returns>
        public playWordsReturn PlayWords(playGameInfo user, String GameID)
        {
            lock (sync)
            {
                int  number;
                bool result = Int32.TryParse(GameID, out number);

                if (user == null || user.Word == null || user.Word.Trim().Length == 0 || GameID == null ||
                    user.UserToken == null || !result || games.ContainsKey(GameID) == false)
                {
                    SetStatus(Forbidden);
                    return(null);
                }

                Game game = games[GameID];
                gameStatus(GameID, "");
                //when the GameState is not active
                if (game.GameState != "active")
                {
                    SetStatus(Conflict);
                    return(null);
                }
                if (game.player1 == null || game.player2 == null || (game.player1.UserToken != user.UserToken && game.player2.UserToken != user.UserToken))
                {
                    SetStatus(Forbidden);
                    return(null);
                }
                //UserToken in the game identified by GameID.
                else
                {
                    int scoreResult = 0;
                    if (game.boggleboard.CanBeFormed(user.Word) && File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory.ToString() + "dictionary.txt").Contains(user.Word))
                    {
                        scoreResult = CalculateScore(user.Word);
                    }
                    else
                    {
                        scoreResult--;
                    }
                    if (game.player1.UserToken == user.UserToken)
                    {
                        WordsPlayed word = new WordsPlayed();
                        word.Word  = user.Word;
                        word.Score = scoreResult;
                        helpAdd(game.player1, word);
                    }
                    else
                    {
                        WordsPlayed word = new WordsPlayed();
                        word.Word  = user.Word;
                        word.Score = scoreResult;
                        helpAdd(game.player2, word);
                    }


                    //user.Words.Add(user.Word);
                    playWordsReturn info = new playWordsReturn();
                    info.Score = scoreResult;
                    SetStatus(OK);
                    return(info);
                }
            }
        }