Exemple #1
0
            private void PlayWordRequest(string line)
            {
                PlayWordDetails playWordDetails = JsonConvert.DeserializeObject <PlayWordDetails>(line);
                PlayWordDetails wordDetail      = new BoggleService().PlayWord(gameID, playWordDetails, out HttpStatusCode status);
                string          result          = CreateResult(wordDetail, status);

                ss.BeginSend(result, (x, y) => { ss.Shutdown(System.Net.Sockets.SocketShutdown.Both); }, null);
            }
Exemple #2
0
        public PlayWordDetails PlayWord(string GameID, PlayWordDetails PlayWordDetails, out HttpStatusCode status)
        {
            string     word       = PlayWordDetails.Word.Trim();
            PlayedWord playedWord = new PlayedWord(word);

            if (word == "" || word == null || word.Length > 30)
            {
                status = Forbidden;
                return(null);
            }

            lock (sync)
            {
                using (SqlConnection connection = new SqlConnection(BoggleDB))
                {
                    connection.Open();
                    using (SqlTransaction transaction = connection.BeginTransaction())
                    {
                        // Check if game exists
                        using (SqlCommand command = new SqlCommand(
                                   "select * from Games where Games.GameID = @GameID",
                                   connection,
                                   transaction))
                        {
                            command.Parameters.AddWithValue("@GameID", GameID);

                            using (SqlDataReader reader = command.ExecuteReader())
                            {
                                if (!reader.HasRows)
                                {
                                    status = Forbidden;
                                    reader.Close();
                                    transaction.Commit();
                                    return(null);
                                }

                                // Game exists, Pending
                                reader.Read();
                                if (reader["Player2"].ToString() == "")
                                {
                                    status = Conflict;
                                    reader.Close();
                                    transaction.Commit();
                                    return(null);
                                }

                                // completed
                                DateTime start     = (DateTime)reader["StartTime"];
                                int      timeLimit = (int)reader["TimeLimit"];
                                if ((start.AddSeconds((double)timeLimit) - DateTime.Now).TotalSeconds <= 0)
                                {
                                    status = Conflict;
                                    reader.Close();
                                    transaction.Commit();
                                    return(null);
                                }


                                // User is in this game
                                if (reader["Player1"].ToString() == PlayWordDetails.UserToken ||
                                    reader["Player2"].ToString() == PlayWordDetails.UserToken)
                                {
                                    BoggleBoard board = new BoggleBoard(reader["Board"].ToString());
                                    reader.Close();

                                    List <string> words = new List <string>();

                                    // Get all words
                                    using (SqlCommand wordCommand = new SqlCommand(
                                               "select Word from Words where Player = @Player",
                                               connection,
                                               transaction))
                                    {
                                        wordCommand.Parameters.AddWithValue("@Player", PlayWordDetails.UserToken);

                                        // Get list of words played
                                        using (SqlDataReader wordReader = wordCommand.ExecuteReader())
                                        {
                                            while (wordReader.Read())
                                            {
                                                words.Add(wordReader["Word"].ToString());
                                            }
                                            wordReader.Close();
                                        }

                                        int score = GetWordScore(PlayWordDetails.Word, board, words);

                                        // Add word to word table
                                        using (SqlCommand addWordCommand = new SqlCommand(
                                                   "insert into Words (Word, GameID, Player, Score) " +
                                                   "values(@Word, @GameID, @Player, @Score)",
                                                   connection,
                                                   transaction))
                                        {
                                            addWordCommand.Parameters.AddWithValue("@Word", PlayWordDetails.Word);
                                            addWordCommand.Parameters.AddWithValue("@GameID", GameID);
                                            addWordCommand.Parameters.AddWithValue("@Player", PlayWordDetails.UserToken);
                                            addWordCommand.Parameters.AddWithValue("@Score", score);

                                            if (addWordCommand.ExecuteNonQuery() != 1)
                                            {
                                                throw new Exception("Query failed unexpectedly");
                                            }
                                            status = OK;
                                            transaction.Commit();
                                            return(new PlayWordDetails(score));
                                        }
                                    }
                                }

                                // User is not in this game
                                else
                                {
                                    status = Forbidden;
                                    reader.Close();
                                    transaction.Commit();
                                    return(null);
                                }
                            }
                        }
                    }
                }
            }
        }