예제 #1
0
        public WordScore PlayWord(PlayedWord data, string gameID)
        {
            lock (sync)
            {
                if (gameID == null || data.UserToken == null || !users.ContainsKey(data.UserToken) ||
                    (!games.ContainsKey(gameID) && gameID != pendingGame) || data.Word == null || data.Word.Trim().Length == 0)
                {
                    SetStatus(Forbidden);
                    return(null);
                }

                if (gameID == pendingGame)
                {
                    SetStatus(Conflict);
                    return(null);
                }

                BoggleGame game = games[gameID];
                if (game.GameState != "active")
                {
                    SetStatus(Conflict);
                    return(null);
                }

                try
                {
                    int score = game.PlayWord(data.UserToken, data.Word.Trim());
                    SetStatus(OK);
                    return(new WordScore()
                    {
                        Score = score
                    });
                }
                catch (Exception)
                {
                    SetStatus(Forbidden);
                    return(null);
                }
            }
        }
        /// <summary>
        /// server response for for play word
        /// </summary>
        /// <param name="body"></param>
        /// <param name="gameid"></param>
        private void PlayWord(string body, string gameid)
        {
            HttpStatusCode httpStatus;
            PlayedWord     word  = JsonConvert.DeserializeObject <PlayedWord>(body);
            WordScore      score = service.playWord(gameid, word, out httpStatus);

            string res = ("HTTP/1.1 " + (int)httpStatus + " " + httpStatus + "\r\n");

            if ((int)httpStatus / 100 == 2)
            {
                string result = JsonConvert.SerializeObject(score);
                res += ("Content-Type: application/json\r\n");
                res += ("Content-Length: " + Encoding.UTF8.GetByteCount(result) + "\r\n");
                res += "\r\n";
                res += result;
            }
            else
            {
                res += "\r\n";
            }
            socket.BeginSend(res, (x, y) => { socket.Shutdown(SocketShutdown.Both); }, null);
        }
        public void TestMethod5()
        {
            lock (sync)
            {
                UserInfo u = new UserInfo();
                u.Nickname = "jon";
                Response r = client.DoPostAsync("users", u).Result;

                PostGame pg1 = new PostGame();
                pg1.UserToken = r.Data["UserToken"];
                pg1.TimeLimit = 30;
                Response pgr1 = client.DoPostAsync("games", pg1).Result;
                Assert.AreEqual(Accepted, pgr1.Status);

                UserInfo u2 = new UserInfo();
                u2.Nickname = "TestPlayer";
                Response r2 = client.DoPostAsync("users", u2).Result;



                PostGame pg2 = new PostGame();
                pg2.UserToken = r2.Data["UserToken"];
                pg2.TimeLimit = 45;
                Response pgr2 = client.DoPostAsync("games", pg2).Result;
                Assert.AreEqual(Created, pgr2.Status);

                string GameID = pgr2.Data["GameID"];



                PlayedWord word = new PlayedWord();
                word.UserToken = r.Data["UserToken"];
                word.Word      = "";
                string   url = String.Format("games/{0}", GameID);
                Response g   = client.DoPutAsync(word, url).Result;
                Assert.AreEqual(Forbidden, g.Status);
            }
        }
        public void TestMethod11()
        {
            lock (sync)
            {
                UserInfo u = new UserInfo();
                u.Nickname = "jon";
                Response r = client.DoPostAsync("users", u).Result;

                PostGame pg1 = new PostGame();
                pg1.UserToken = r.Data["UserToken"];
                pg1.TimeLimit = 10;
                Response pgr1 = client.DoPostAsync("games", pg1).Result;
                Assert.AreEqual(Accepted, pgr1.Status);

                UserInfo u2 = new UserInfo();
                u2.Nickname = "TestPlayer";
                Response r2 = client.DoPostAsync("users", u2).Result;



                PostGame pg2 = new PostGame();
                pg2.UserToken = r2.Data["UserToken"];
                pg2.TimeLimit = 10;
                Response pgr2 = client.DoPostAsync("games", pg2).Result;
                Assert.AreEqual(Created, pgr2.Status);

                string GameID = pgr2.Data["GameID"];



                PlayedWord word = new PlayedWord();
                word.UserToken = r.Data["UserToken"];
                word.Word      = "name";
                string   url = String.Format("games/{0}", GameID);
                Response g   = client.DoPutAsync(word, url).Result;
                Assert.AreEqual(OK, g.Status);

                PlayedWord word2 = new PlayedWord();
                word2.UserToken = r.Data["UserToken"];
                word2.Word      = "lane";
                Response g2 = client.DoPutAsync(word2, url).Result;
                Assert.AreEqual(OK, g.Status);

                PlayedWord word3 = new PlayedWord();
                word3.UserToken = r.Data["UserToken"];
                word3.Word      = "name";
                Response g3 = client.DoPutAsync(word3, url).Result;
                Assert.AreEqual(OK, g.Status);

                PlayedWord word4 = new PlayedWord();
                word4.UserToken = r2.Data["UserToken"];
                word4.Word      = "name";
                Response g4 = client.DoPutAsync(word4, url).Result;
                Assert.AreEqual(OK, g.Status);

                PlayedWord word5 = new PlayedWord();
                word5.UserToken = r2.Data["UserToken"];
                word5.Word      = "name";
                Response g5 = client.DoPutAsync(word5, url).Result;
                Assert.AreEqual(OK, g.Status);


                Response res = client.DoGetAsync("games/{0}?Brief={1}", GameID, "yes").Result;
                Assert.AreEqual(OK, res.Status);

                Response res2 = client.DoGetAsync("games/{0}?Brief={1}", GameID, "no").Result;
                Assert.AreEqual(OK, res.Status);

                System.Threading.Thread.Sleep(10000);

                Response res3 = client.DoGetAsync("games/{0}?Brief={1}", GameID, "yes").Result;
                Assert.AreEqual(OK, res.Status);

                Response res4 = client.DoGetAsync("games/{0}?Brief={1}", GameID, "no").Result;
                Assert.AreEqual(OK, res.Status);
            }
        }
        /// <summary>
        /// play a word given the GameID and the word object
        /// </summary>
        /// <param name="GameID"></param>
        /// <param name="word"></param>
        /// <returns></returns>
        public WordScore playWord(string GameID, PlayedWord word, out HttpStatusCode httpStatus)
        {
            httpStatus = OK;
            Words     newWord = new Words();
            WordScore pw      = new WordScore();


            GameInfo pg = new GameInfo();
            GameInfo g  = new GameInfo();

            if (String.IsNullOrEmpty(word.Word) || String.IsNullOrEmpty(GameID) || word.Word.Trim().Length > 30)
            {
                httpStatus = Forbidden;
                pw.Score   = -1;
                return(pw);
            }

            try
            {
                pendingGames.TryGetValue(GameID.ToString(), out pg);
                if (pg.GameState.Equals("pending"))
                {
                    httpStatus = Conflict;
                    pw.Score   = -1;
                    return(pw);
                }
            }
            catch (Exception)
            {
            }



            games.TryGetValue(GameID.ToString(), out g);


            if (!g.GameState.Equals("active"))
            {
                httpStatus = Conflict;
                pw.Score   = -1;
                return(pw);
            }

            if (getTimeLeft(g) <= 0)
            {
                httpStatus = Conflict;
                pw.Score   = 0;
                return(pw);
            }

            if (!word.UserToken.Equals(g.Player1.UserToken.ToString()) && !word.UserToken.Equals(g.Player2.UserToken.ToString()))
            {
                httpStatus = Forbidden;
                pw.Score   = 0;
                return(pw);
            }

            if (new BoggleBoard(g.Board).CanBeFormed(word.Word.Trim()))
            {
                if (word.UserToken.Equals(g.Player1.UserToken))
                {
                    foreach (var w in g.Player1.WordsPlayed)
                    {
                        if (w.Word.Equals(word.Word.Trim()))
                        {
                            newWord.Score = 0;
                            newWord.Word  = word.Word.Trim();
                            g.Player1.WordsPlayed.Add(newWord);
                            httpStatus = OK;
                            pw.Score   = newWord.Score;
                            return(pw);
                        }
                    }
                    if (WordValid(word.Word))
                    {
                        newWord.Score = ScoreWord(word.Word);
                        newWord.Word  = word.Word.Trim();
                        g.Player1.WordsPlayed.Add(newWord);
                        g.Player1.Score += newWord.Score;
                        httpStatus       = OK;
                        pw.Score         = newWord.Score;
                        return(pw);
                    }
                }

                else
                {
                    foreach (var w in g.Player2.WordsPlayed)
                    {
                        if (w.Word.Equals(word.Word.Trim()))
                        {
                            newWord.Score = 0;
                            newWord.Word  = word.Word.Trim();
                            g.Player2.WordsPlayed.Add(newWord);
                            httpStatus = OK;
                            pw.Score   = newWord.Score;
                            return(pw);
                        }
                    }
                    if (WordValid(word.Word))
                    {
                        newWord.Score = ScoreWord(word.Word);
                        newWord.Word  = word.Word.Trim();
                        g.Player2.WordsPlayed.Add(newWord);
                        g.Player2.Score += newWord.Score;
                        httpStatus       = OK;
                        pw.Score         = newWord.Score;
                        return(pw);
                    }
                }
            }
            else
            {
                if (word.UserToken.Equals(g.Player1.UserToken))
                {
                    newWord.Score = ScoreWord(word.Word);
                    newWord.Word  = word.Word.Trim();
                    g.Player1.WordsPlayed.Add(newWord);
                    g.Player1.Score += newWord.Score;
                    httpStatus       = OK;
                    pw.Score         = newWord.Score;
                    return(pw);
                }
                else
                {
                    newWord.Score = ScoreWord(word.Word);
                    newWord.Word  = word.Word.Trim();
                    g.Player2.WordsPlayed.Add(newWord);
                    g.Player2.Score += newWord.Score;
                    httpStatus       = OK;
                    pw.Score         = newWord.Score;
                    return(pw);
                }
            }


            return(pw);
        }
예제 #6
0
        public WordScore PlayWord(PlayedWord data, string game)
        {
            // Validate incoming parameters
            if (game == null || data.UserToken == null || data.Word == null || data.Word.Trim().Length == 0)
            {
                SetStatus(Forbidden);
                return(null);
            }

            // The GameID must be an integer
            long gameID;

            if (!Int64.TryParse(game, out gameID))
            {
                SetStatus(Forbidden);
                return(null);
            }

            using (SqlConnection conn = new SqlConnection(BoggleDB))
            {
                conn.Open();
                SqlTransaction trans = conn.BeginTransaction();

                try
                {
                    // Get information about the game in which the word is to be played
                    SqlCommand command = new SqlCommand("select GameID, Player1, Player2, Board, TimeLimit, StartTime from Games where GameID=@GameID and (Player1 = @Player or Player2 = @Player)", conn, trans);
                    command.Parameters.AddWithValue("@GameID", gameID);
                    command.Parameters.AddWithValue("@Player", data.UserToken);

                    //string player;
                    string   board;
                    int      timeLimit;
                    DateTime startTime;

                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            //player = (string)(((string)reader["Player1"] == data.UserToken) ? reader["Player1"] : reader["Player2"]);
                            if (reader["Player2"] is DBNull)
                            {
                                SetStatus(Conflict);
                                return(null);
                            }
                            board     = (string)reader["Board"];
                            timeLimit = (int)reader["TimeLimit"];
                            startTime = (DateTime)reader["StartTime"];
                            if (reader["Player2"] is DBNull)
                            {
                                SetStatus(Conflict);
                                return(null);
                            }
                        }
                        else
                        {
                            SetStatus(Forbidden);
                            return(null);
                        }
                    }

                    if (BoggleGame.GetTimeLeft(startTime, timeLimit) <= 0)
                    {
                        SetStatus(Conflict);
                        return(null);
                    }

                    int score = BoggleGame.WordValue(data.Word.Trim(), board, dictionary);
                    if (score == -2)
                    {
                        SetStatus(Forbidden);
                        return(null);
                    }

                    command = new SqlCommand("select Word from Words where GameID=@GameID and Player=@Player and Word=@Word", conn, trans);
                    command.Parameters.AddWithValue("@GameID", gameID);
                    command.Parameters.AddWithValue("@Player", data.UserToken);
                    command.Parameters.AddWithValue("@Word", data.Word.Trim());

                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            score = 0;
                        }
                    }

                    command = new SqlCommand("insert into Words (Word, GameID, Player, Score) values(@Word, @GameID, @Player, @Score)", conn, trans);
                    command.Parameters.AddWithValue("@Word", data.Word);
                    command.Parameters.AddWithValue("@GameID", gameID);
                    command.Parameters.AddWithValue("@Player", data.UserToken);
                    command.Parameters.AddWithValue("@Score", score);
                    command.ExecuteNonQuery();

                    SetStatus(OK);
                    return(new WordScore()
                    {
                        Score = score
                    });
                }
                catch (Exception)
                {
                    trans.Rollback();
                    SetStatus(InternalServerError);
                    return(null);
                }
                finally
                {
                    if (trans.Connection != null)
                    {
                        trans.Commit();
                    }
                }
            }
        }