Beispiel #1
0
 public bool Equals(CubicBezierCurve3D other)
 {
     return(P0.Equals(other.P0) &&
            P1.Equals(other.P1) &&
            P2.Equals(other.P2) &&
            P3.Equals(other.P3));
 }
Beispiel #2
0
 public bool Equals(ValidationValueTypeModel other)
 {
     return
         (P1 == other.P1 &&
          P2.Equals(other.P2) &&
          (P3 == null == (other.P3 == null) ||
           P3?.Zip(other.P3).All(t => t.First == t.Second) == true));
 }
Beispiel #3
0
        public override bool Equals(object obj)
        {
            var other = obj as Triangle;

            return(base.Equals(other) ||
                   ((other != null) &&
                    P1.Equals(other.P1) &&
                    P2.Equals(other.P2) &&
                    P3.Equals(other.P3)));
        }
 public bool IsOppositeVertexLinked(Vector3 vertex)
 {
     if (P2.Equals(vertex))
     {
         return(OrigEdge.PrevEdge != null);
     }
     else if (P1.Equals(vertex))
     {
         return(OrigEdge.NextEdge != null);
     }
     return(false);
 }
Beispiel #5
0
        public bool Equals(Line other, float tolerance = 0.0001f)
        {
            if (!Parallel(other, tolerance))
            {
                return(false);
            }

            if (P1.Equals(other.P1) || P1.Equals(other.P2) ||
                P2.Equals(other.P1) || P2.Equals(other.P2))
            {
                return(true);
            }

            return(Parallel(new Line(P1, other.P1), tolerance));
        }
 public bool Intersects(Maths.Line line, out Vector2 intersection)
 {
     intersection = Vector2.Empty;
     if (Equation.Intersect(line, out intersection))
     {
         if (P1.Equals(intersection) || P2.Equals(intersection))
         {
             return(true);
         }
         var segLength = (P2 - P1).Length;
         //var interVec = (intersection - P1);
         var maxDist = Math.Max((intersection - P1).Length, (intersection - P2).Length);
         return(maxDist <= segLength);
     }
     return(false);
 }
 public override bool Equals(object obj)
 {
     return(obj is Triangle triangle &&
            Transform.Equals(triangle.Transform) &&
            Material.Equals(triangle.Material) &&
            Parent == triangle.Parent &&
            HasParent == triangle.HasParent &&
            P1.Equals(triangle.P1) &&
            P2.Equals(triangle.P2) &&
            P3.Equals(triangle.P3) &&
            Edge1.Equals(triangle.Edge1) &&
            Edge2.Equals(triangle.Edge2) &&
            Normal.Equals(triangle.Normal) &&
            N1.Equals(triangle.N1) &&
            N2.Equals(triangle.N2) &&
            N3.Equals(triangle.N3) &&
            IsSmoothed == triangle.IsSmoothed);
 }
Beispiel #8
0
 public bool Equals(Polygon2double p) =>
 P1.Equals(p.P1) && P2.Equals(p.P2) && P3.Equals(p.P3) && P4.Equals(p.P4);
Beispiel #9
0
 public bool Equals(Polygon2BigRational p) =>
 P1.Equals(p.P1) && P2.Equals(p.P2) && P3.Equals(p.P3) && P4.Equals(p.P4);
Beispiel #10
0
 public bool Contains(Vector2 position)
 {
     return(P1.Equals(position, 0.001f) || P2.Equals(position, 0.001f));
 }
Beispiel #11
0
        /// <summary>
        /// Play a word in a game
        ///
        /// If Word is null or empty when trimmed, or if GameID or
        /// UserToken is missing or 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.
        /// </summary>
        /// <param name="gameID"></param>
        /// <param name="UserToken"></param>
        /// <param name="Word"></param>
        /// <returns></returns>
        public WordScore PlayWord(string gameID, WordCheck info)
        {
            try {
                string      word      = info.Word.Trim();
                string      userToken = info.UserToken;
                BoggleBoard PlayBoard;
                string      status1;
                int         score = 0;
                bool        canPlay;
                DateTime    time;
                int         TimeLimit;

                if (word.Length == 0 || userToken == null || gameID == null)
                {
                    SetStatus(Forbidden);
                    return(null);
                }

                if (!(new Regex(@"\d+").IsMatch(gameID)))
                {
                    SetStatus(Forbidden);
                    return(null);
                }
                //Creates a sql connection
                using (SqlConnection conn = new SqlConnection(BoggleDB))
                {
                    //open connection
                    conn.Open();

                    using (SqlTransaction trans = conn.BeginTransaction())
                    {
                        using (SqlCommand command = new SqlCommand("select GameStatus, StartTime, TimeLimit from Games where GameID = @GameID",
                                                                   conn, trans))
                        {
                            command.Parameters.AddWithValue("@GameID", gameID);

                            using (SqlDataReader reader = command.ExecuteReader())
                            {
                                //added check to make sure game was in table
                                if (!reader.HasRows)
                                {
                                    SetStatus(Forbidden);
                                    return(null);
                                }

                                reader.Read();
                                status1 = reader.GetString(0);

                                // else we get the time the game started and the calculated time limit from the 2 players.
                                time      = reader.GetDateTime(1);
                                TimeLimit = (int)reader.GetValue(2);

                                double elapsedTime = Math.Round((DateTime.Now.TimeOfDay.TotalSeconds - time.TimeOfDay.TotalSeconds), 0);

                                // If the elapsed time is greater than or equal to the time limit, the game is marked as completed so no
                                // Actions can be made to the game.
                                if (elapsedTime >= (double)TimeLimit)
                                {
                                    SetStatus(Conflict);
                                    status1 = "completed";
                                }
                            }
                        }
                    }
                    using (SqlTransaction trans = conn.BeginTransaction())
                    {
                        // Gets thge players and board based on the given GameID.
                        using (SqlCommand command = new SqlCommand("select Player1, Player2, Board, GameStatus from Games where GameID = @GameID",
                                                                   conn, trans))
                        {
                            string P1;
                            string P2;
                            string status;
                            command.Parameters.AddWithValue("@GameID", gameID);
                            using (SqlDataReader reader = command.ExecuteReader())
                            {
                                if (!reader.HasRows)
                                {
                                    SetStatus(Forbidden);
                                    return(null);
                                }

                                reader.Read();

                                P1     = reader.GetString(0);
                                P2     = reader.GetString(1);
                                status = reader.GetString(3);

                                if (!P1.Equals(userToken) && !P2.Equals(userToken))
                                {
                                    SetStatus(Forbidden);
                                    return(null);
                                }
                                if (status1 != "active")
                                {
                                    SetStatus(Conflict);
                                    return(null);
                                }
                                PlayBoard = new BoggleBoard(reader.GetString(2));
                                // Checks to make sure P1 and P2 are in the game.
                                if (P1 == null || P2 == null)
                                {
                                    SetStatus(Forbidden);
                                    return(null);
                                }

                                // If game status is Active, sets a bool that allows up to play a word. If it is anything but Active,
                                // we can't play a word.
                                if (status == "active")
                                {
                                    canPlay = true;
                                }
                                else
                                {
                                    canPlay = false;
                                    SetStatus(Conflict);
                                }
                            }
                        }

                        // If we can play a word, we go in to the if statement. If not, we set the status to 409 - Conflict and return null
                        if (canPlay)
                        {
                            // Checks to make sure the word can be formed on the game board.
                            if (PlayBoard.CanBeFormed(word))
                            {
                                string scoreString;
                                // Sets the score initially to the word score from the dictionary. Can be changed below if the word has already been played by
                                // the player.
                                if (dictionary.TryGetValue(word.ToUpper(), out scoreString))
                                {
                                    int.TryParse(scoreString, out score);
                                    // Gets all the words played by a player in a game.
                                    using (SqlCommand command2 = new SqlCommand("select Word from Words where GameID = @GameID and Player = @Player",
                                                                                conn, trans))
                                    {
                                        command2.Parameters.AddWithValue("@GameID", gameID);
                                        command2.Parameters.AddWithValue("@Player", userToken);
                                        using (SqlDataReader reader1 = command2.ExecuteReader())
                                        {
                                            // Reads each row/word that a player has played for a game.
                                            while (reader1.Read())
                                            {
                                                // If the word has been played, score is updated to 0.
                                                if (reader1.GetString(0) == word)
                                                {
                                                    score = 0;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            // If the word can't be formed on the board or is an invalid word, score is set to -1.
                            else
                            {
                                score = -1;
                            }

                            // Inserts the word into the Words table. The word is accociated with a GameID, Player, and score.
                            using (SqlCommand command3 = new SqlCommand("Insert into Words (Word, GameID, Player, Score) values (@Word, @GameID, @Player, @Score)",
                                                                        conn, trans))
                            {
                                command3.Parameters.AddWithValue("@Word", word);
                                command3.Parameters.AddWithValue("@GameID", gameID);
                                command3.Parameters.AddWithValue("@Player", userToken);
                                command3.Parameters.AddWithValue("@Score", score);

                                if (command3.ExecuteNonQuery() != 1)
                                {
                                    SetStatus(BadRequest);
                                    return(null);
                                }
                            }


                            SetStatus(OK);

                            //comit transaction and return the usertoken
                            trans.Commit();
                            // Returns a WordScore object that reflect the score of the word a player just played.
                            return(new WordScore {
                                Score = score.ToString()
                            });
                        }
                        // Only gets done when the game status is anything other than Active.

                        {
                            SetStatus(Conflict);
                            return(null);
                        }
                    }
                }
            }
            catch (Exception)
            {
                SetStatus(Conflict);
                return(null);
            }
        }
Beispiel #12
0
        private async void Battle()
        {
            Dictionary <string, char[]> actions     = new Dictionary <string, char[]>();
            Dictionary <string, int>    roundscore  = new Dictionary <string, int>();
            Dictionary <string, int>    battlescore = new Dictionary <string, int>();

            List <string> processedUsernames    = new List <string>();
            List <string> disqualifiedUsernames = new List <string>();

            // get the current max tournamentid from the database and add 1 for further inserts
            string idsql        = "SELECT Count(*) FROM tournamentscores";
            int    tournamentID = Int32.Parse(await dBHandler.ExecuteSingleSelectSQL(idsql)) + 1;

            Console.WriteLine(DateTime.Now);
            Console.WriteLine("####  Battle started ####" + Environment.NewLine + "Fighters:");
            foreach (string username in usernames)
            {
                Console.WriteLine(username);
                string sql = $"SELECT action FROM actions WHERE username = '******'";
                actions[username] = (await dBHandler.ExecuteSingleSelectSQL(sql)).ToCharArray();
            }

            // set round scores for all users to 0
            foreach (string username in usernames)
            {
                roundscore[username] = 0;
            }

            // set battle scores for all users to 0
            foreach (string username in usernames)
            {
                battlescore[username] = 0;
            }

            // lets fight
            foreach (string P1 in usernames)
            {
                foreach (string P2 in usernames)
                {
                    // P2 is already completely processed, no need to do it the other way around
                    if (!processedUsernames.Contains(P2) && !disqualifiedUsernames.Contains(P1) && !disqualifiedUsernames.Contains(P2))
                    {
                        if (!P2.Equals(P1))
                        {
                            Console.WriteLine("\n#### " + P1 + " vs " + P2 + " ####");

                            bool P1Special = false;
                            bool P2Special = false;


                            // check for RSVLP :O
                            string stringP1Special = new string(actions[P1]);
                            string stringP2Special = new string(actions[P2]);

                            if (stringP1Special.Equals("RSVLP") && stringP2Special.Equals("RSVLP"))
                            {
                                Console.WriteLine("\nBoth " + P2 + " and " + P1 + " chose the special Action. Both are disqualified from the tournament.\n");
                                Console.WriteLine("##################################");
                                disqualifiedUsernames.Add(P1);
                                disqualifiedUsernames.Add(P2);
                                break;
                            }
                            else if (stringP1Special.Equals("RSVLP"))
                            {
                                Console.WriteLine("\n" + P1 + " beats " + P2 + " by using the special action.\n");
                                Console.WriteLine("##################################");
                                battlescore[P1] = battlescore[P1] + 2;
                                battlescore[P2] = battlescore[P1] - 2;
                                roundscore[P1]  = 0;
                                roundscore[P2]  = 0;
                                break;
                            }
                            else if (stringP2Special.Equals("RSVLP"))
                            {
                                Console.WriteLine("\n" + P2 + " beats " + P1 + " by using the special action.\n");
                                Console.WriteLine("##################################");
                                battlescore[P2] = battlescore[P2] + 2;
                                battlescore[P1] = battlescore[P1] - 2;
                                roundscore[P1]  = 0;
                                roundscore[P2]  = 0;
                                break;
                            }

                            for (int i = 0; i < 5; i++)
                            {
                                Console.WriteLine("\nRound " + (i + 1) + ":");

                                char P1action = actions[P1][i];
                                char P2action = actions[P2][i];

                                bool draw = false;

                                foreach (KeyValuePair <char, char> pair in effectiveness)
                                {
                                    if (P1action.Equals(pair.Key) && P2action.Equals(pair.Value))
                                    {
                                        Console.WriteLine(P1action + " by " + P1 + " beats " + P2action + " by " + P2 + ".");
                                        roundscore[P1]++;
                                        draw = false;
                                        break;
                                    }
                                    else if (P2action.Equals(pair.Key) && P1action.Equals(pair.Value))
                                    {
                                        Console.WriteLine(P2action + " by " + P2 + " beats " + P1action + " by " + P1 + ".");
                                        roundscore[P2]++;
                                        draw = false;
                                        break;
                                    }
                                    else
                                    {
                                        draw = true;
                                    }
                                }
                                if (draw)
                                {
                                    Console.WriteLine(P1action + " by " + P1 + " and " + P2action + " by " + P2 + " are a draw.");
                                }
                            }

                            if (roundscore[P1] > roundscore[P2])
                            {
                                Console.WriteLine("\n" + P1 + " beats " + P2 + ".\n");
                                Console.WriteLine("##################################");
                                battlescore[P1]++;
                                battlescore[P2]--;
                                roundscore[P1] = 0;
                                roundscore[P2] = 0;
                            }
                            else if (roundscore[P2] > roundscore[P1])
                            {
                                Console.WriteLine("\n" + P2 + " beats " + P1 + ".\n");
                                Console.WriteLine("##################################");
                                battlescore[P2]++;
                                battlescore[P1]--;
                                roundscore[P1] = 0;
                                roundscore[P2] = 0;
                            }
                            else
                            {
                                // players are removed from the tournament
                                Console.WriteLine("\n" + P2 + " and " + P1 + " are even. Both are disqualified from the tournament.\n");
                                Console.WriteLine("##################################");
                                disqualifiedUsernames.Add(P1);
                                disqualifiedUsernames.Add(P2);
                            }
                        }
                    }
                }

                processedUsernames.Add(P1);
            }

            battlescore = battlescore.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);

            // check if its a battlescore draw and set admin
            if ((battlescore.Values.ElementAt(1)) < battlescore.Values.ElementAt(0))
            {
                string countsql = "SELECT COUNT(*) FROM users WHERE admin = true";
                int    count    = Int32.Parse(await dBHandler.ExecuteSingleSelectSQL(countsql));

                if (count > 0)
                {
                    string reset = $"UPDATE users SET admin = false WHERE admin = true";
                    await dBHandler.ExecuteInsertOrDeleteSQL(reset);
                }

                string update = $"UPDATE users SET admin = true WHERE username = '******'";
                await dBHandler.ExecuteInsertOrDeleteSQL(update);

                string selectScore = $"SELECT score FROM stats WHERE username = '******'";
                int    score       = Int32.Parse(await dBHandler.ExecuteSingleSelectSQL(selectScore)) + 1;

                string updateStats = $"UPDATE stats SET score = {score} WHERE username = '******'";
                await dBHandler.ExecuteInsertOrDeleteSQL(updateStats);

                Console.WriteLine("\n" + battlescore.Keys.ElementAt(0) + " won and is now administrator.\n");
            }

            Console.WriteLine("TournamentID: " + tournamentID);
            Console.WriteLine("Scoreboard: ");

            for (int i = 0; i < battlescore.Count; i++)
            {
                if (i > 0)
                {
                    if (battlescore.Values.ElementAt(i) < battlescore.Values.ElementAt(0))
                    {
                        string selectScore = $"SELECT score FROM stats WHERE username = '******'";
                        int    score       = Int32.Parse(await dBHandler.ExecuteSingleSelectSQL(selectScore)) - 1;

                        string updateStats = $"UPDATE stats SET score = {score} WHERE username = '******'";
                        await dBHandler.ExecuteInsertOrDeleteSQL(updateStats);
                    }
                }

                string selectgamesPlayed = $"SELECT games_played FROM stats WHERE username = '******'";
                int    gamesPlayed       = Int32.Parse(await dBHandler.ExecuteSingleSelectSQL(selectgamesPlayed)) + 1;

                string updateGamesPlayed = $"UPDATE stats SET games_played = {gamesPlayed} WHERE username = '******'";
                await dBHandler.ExecuteInsertOrDeleteSQL(updateGamesPlayed);

                Console.WriteLine(battlescore.Keys.ElementAt(i) + " -> " + battlescore.Values.ElementAt(i) + " points.");

                string insert = $"INSERT INTO tournamentscores VALUES ('{tournamentID}', '{battlescore.Keys.ElementAt(i)}', '{battlescore.Values.ElementAt(i)}')";
                await dBHandler.ExecuteInsertOrDeleteSQL(insert);
            }

            Console.WriteLine("\n\n");

            DBCleanup(usernames);
        }
Beispiel #13
0
 public bool Contains(Vector3 position)
 {
     return(P1.Equals(position) || P2.Equals(position));
 }