Ejemplo n.º 1
0
        public static byte[] Serialize(User user)
        {
            IFormatter formatter = new BinaryFormatter();
            Stream stream = new MemoryStream();

            formatter.Serialize(stream, user);

            byte[] buffer = ((MemoryStream)stream).ToArray();

            return buffer;
        }
Ejemplo n.º 2
0
        public static User Deserialize(byte[] buffer)
        {
            User user = new User();

            Stream strm = new MemoryStream(buffer);

            IFormatter frr = new BinaryFormatter();

            user = (User)frr.Deserialize(strm);

            return user;
        }
Ejemplo n.º 3
0
        private void ButtonName_Click(object sender, EventArgs e)
        {
            string returnMessage = "";
            string errorMessage = "";
            bool status = false;
            //validation to check if it empty
            if ((UserNameTextBox.Text == "") || (IPTextBox.Text == ""))
            {
                errorMessage = "Enter a name\nEnter a IP adress\n";
                ErrorMessage.Text = errorMessage;
                //no thing
            }
            else
            {
                if (this.checkPunc(UserNameTextBox.Text) == true)
                {
                    ErrorMessage.Text = "Please do not use punctuation";
                }
                else
                {

                    try
                    {
                        //connection to server
                        this.myConnectionToServer = new TCPIPconnectorClient(9000, System.Net.IPAddress.Parse(IPTextBox.Text.ToString()));
                    }
                    catch (Exception ex)
                    {
                        errorMessage = "Cannot establish a \nconnection to server\n";
                        ErrorMessage.Text = errorMessage;
                        status = true;
                    }
                    //if status equal to false continue with the program
                    if (status == false)
                    {

                        ErrorMessage.Text = "";
                        //establish connection
                        returnMessage = myConnectionToServer.Connect();
                        //if returnMessage is empty then continue
                        if (returnMessage == "")
                        {
                            //disable the username and IP
                            UserNameTextBox.Enabled = false;
                            IPTextBox.Enabled = false;
                            //undisable the question options
                            QuestionA.Checked = true;
                            QuestionB.Checked = true;
                            QuestionC.Checked = true;
                            QuestionD.Checked = true;
                            QuestionGroupBox.Enabled = true;
                            ErrorMessage.Text = "";
                            //create new user
                            this.myConnectionToServer.Send("!newUser");
                            //wait for the server name
                            this.myConnectionToServer.Read();
                            //create new user
                            User user = new User();
                            //username
                            user.name = UserNameTextBox.Text;
                            user.password = "";
                            ButtonName.Enabled = false;

                            //send an object so it will be seralized
                            this.myConnectionToServer.Send(User.Serialize(user));

                            System.Threading.Thread.Sleep(500);

                            this.myConnectionToServer.Send("!question1");

                            byte[] buffer = this.myConnectionToServer.ReadObject();
                            Question q = Question.Deserialize(buffer);
                            //establish the first questions
                            lbQuestion.Text = "[1] " + q.content;
                            QuestionA.Text = "A) " + q.a;
                            QuestionB.Text = "B) " + q.b;
                            QuestionC.Text = "C) " + q.c;
                            QuestionD.Text = "D) " + q.d;
                            GameCounter.Enabled = true;
                            connection = true;
                            NextButton.Enabled = true;
                        }
                        else
                        {
                            //return error message
                            ErrorMessage.Text = "Count not connect";
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public List<TriviaLib.User> GetLeaderBoard()
        {
            List<TriviaLib.User> list = new List<TriviaLib.User>();

            string query = "SELECT Name, Sum(Time) as Points FROM useranswer "
                + "INNER JOIN user ON user.userid = useranswer.userid "
                + "GROUP BY Name ORDER BY Points DESC;";

            this.command.CommandText = query;
            this.connection.Open();
            this.reader = this.command.ExecuteReader();
            while (this.reader.Read())
            {
                TriviaLib.User u = new TriviaLib.User();
                u.name = this.reader["Name"].ToString();
                u.totalPoints = Convert.ToInt32(this.reader["Points"]);

                list.Add(u);
            }

            if (this.connection.State == System.Data.ConnectionState.Open)
            {
                this.connection.Close();
            }

            return list;
        }
Ejemplo n.º 5
0
        private void UserInteraction(object usr)
        {
            User user = (User)usr;

            string message = "";

            while (true)
            {
                try
                {
                    message = this.connector.Read(user.socket);
                }
                catch (Exception e)
                {
                    Logging.LogEvent("UserInteraction", e.Message);
                    break;
                }

                Logging.LogEvent("UserInteraction", "User #" + user.id + " said -> " + message);

                if (message == "")
                {
                    int count = 1;
                    while (true)
                    {
                        string nMessage = connector.Read(user.socket);
                        if (nMessage == "")
                        {
                            count++;
                        }
                        else
                        {
                            break;
                        }
                        if (count > 20)
                        {
                            message = TCPIPconnector.kClientLeft;
                            break;
                        }
                    }
                }

                if (message == "!admin")
                {
                    TriviaLib.User admin = new TriviaLib.User();

                    try
                    {
                        this.connector.Send("!ok", user.socket);
                        admin = TriviaLib.User.Deserialize(this.connector.ReadObject(user.socket));
                    }
                    catch (Exception e)
                    {
                        Logging.LogEvent("UserInteraction", e.Message);
                        break;
                    }

                    try
                    {
                        if (this.DBConnector.ValidateAdmin(admin))
                        {
                            try
                            {
                                this.connector.Send("!ok", user.socket);
                            }
                            catch (Exception e)
                            {
                                Logging.LogEvent("UserInteraction", e.Message);
                                break;
                            }
                            user.name = admin.name;
                            this.AdminInteraction(user);
                            break;
                        }
                        else
                        {
                            try
                            {
                                this.connector.Send("!error", user.socket);
                            }
                            catch (Exception e)
                            {
                                Logging.LogEvent("UserInteraction", e.Message);
                                break;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Logging.LogEvent("UserInteraction", e.Message);
                        break;
                    }
                }

                if (message == "!answer")
                {
                    byte[] buffer = null;
                    try
                    {
                        this.connector.Send("!ok", user.socket);

                        buffer = this.connector.ReadObject(user.socket);
                    }
                    catch (Exception e)
                    {
                        Logging.LogEvent("UserInteraction", e.Message);
                        break;
                    }

                    if (buffer != null)
                    {
                        Question quest = Question.Deserialize(buffer);

                        quest.number = user.currentQuestion;

                        Question tmp = null;
                        try
                        {
                            tmp = this.DBConnector.GetQuestion(quest.number);
                        }
                        catch (Exception e)
                        {
                            Logging.LogEvent("UserInteraction", e.Message);
                            break;
                        }

                        if (tmp.answer != quest.answer)
                        {
                            quest.time = 0;
                        }
                        else
                        {
                            quest.time = 20 - quest.time;
                            user.totalScore += quest.time;
                        }

                        try
                        {
                            this.mutex.WaitOne();
                            this.mutex.Reset();
                            this.DBConnector.AddUserAnswer(TriviaServer.answerID, user.id, quest.number, quest.answer, quest.time);
                            TriviaServer.answerID++;
                            this.mutex.Set();
                        }
                        catch (Exception e)
                        {
                            Logging.LogEvent("UserInteraction", e.Message);
                            break;
                        }
                    }
                }

                if (message == "!leaderboard")
                {
                    List<TriviaLib.User> list = null;
                    try
                    {
                        list = this.DBConnector.GetLeaderBoard();
                    }
                    catch (Exception e)
                    {
                        Logging.LogEvent("UserInteraction", e.Message);
                        break;
                    }

                    try
                    {
                        for (int counter = 0; counter < list.Count; counter++)
                        {
                            this.connector.Send(TriviaLib.User.Serialize(list[counter]), user.socket);
                            this.connector.Read(user.socket);
                        }
                        this.connector.Send(TriviaLib.User.Serialize(new TriviaLib.User()), user.socket);
                    }
                    catch (Exception e)
                    {
                        Logging.LogEvent("UserInteraction", e.Message);
                        break;
                    }
                }

                if (message == "!score")
                {
                    try
                    {
                        this.connector.Send(user.totalScore.ToString(), user.socket);
                    }
                    catch (Exception e)
                    {
                        Logging.LogEvent("UserInteraction", e.Message);
                        break;
                    }
                }

                if (message == "!newUser")
                {
                    byte[] buffer = null;
                    try
                    {
                        this.connector.Send("!ok", user.socket);
                        buffer = this.connector.ReadObject(user.socket);
                    }
                    catch (Exception e)
                    {
                        Logging.LogEvent("UserInteraction", e.Message);
                        break;
                    }
                    if (buffer != null)
                    {
                        TriviaLib.User nUser = TriviaLib.User.Deserialize(buffer);

                        try
                        {
                            user.id = this.DBConnector.AddNewUser(nUser);
                        }
                        catch (Exception e)
                        {
                            Logging.LogEvent("UserInteraction", e.Message);
                            break;
                        }

                        user.name = nUser.name;
                    }
                }

                if (message == "!allQuestions")
                {
                    List<Question> list = null;

                    try
                    {
                        list = this.DBConnector.GetAllQuestions();
                    }
                    catch (Exception e)
                    {
                        Logging.LogEvent("UserInteraction", e.Message);
                        break;
                    }

                    try
                    {
                        for (int counter = 0; counter < list.Count; counter++)
                        {
                            this.connector.Send(TriviaLib.Question.Serialize(list[counter]), user.socket);
                            this.connector.Read(user.socket);
                        }
                        this.connector.Send(TriviaLib.Question.Serialize(new TriviaLib.Question()), user.socket);
                    }
                    catch (Exception e)
                    {
                        Logging.LogEvent("UserInteraction", e.Message);
                        break;
                    }
                }

                if (message.Length > 9)
                {
                    if ((message.Substring(0, 9) == "!question") && (message.Length > 9))
                    {
                        int qId = Convert.ToInt32(message.Substring(9));

                        if (qId < 11)
                        {

                            Question q = null;
                            try
                            {
                                q = this.DBConnector.GetQuestion(qId);
                            }
                            catch (Exception e)
                            {
                                Logging.LogEvent("UserInteraction", e.Message);
                                break;
                            }
                            q.answer = "";
                            user.currentQuestion = qId;

                            byte[] buffer = Question.Serialize(q);

                            try
                            {
                                this.connector.Send(buffer, user.socket);
                            }
                            catch (Exception e)
                            {
                                Logging.LogEvent("UserInteraction", e.Message);
                                break;
                            }
                        }
                        else
                        {
                            Question q = new Question();

                            byte[] buffer = Question.Serialize(q);

                            try
                            {
                                this.connector.Send(buffer, user.socket);
                            }
                            catch (Exception e)
                            {
                                Logging.LogEvent("UserInteraction", e.Message);
                                break;
                            }
                        }
                    }
                }

                if (message == TCPIPconnector.kClientLeft)
                {
                    Logging.LogEvent("UserInteraction", "Client #" + user.id + " left");
                    break;
                }
            }

            this.userList.Remove(user);

            user.socket.Close();
        }
Ejemplo n.º 6
0
        private void AdminInteraction(User admin)
        {
            string message = "";

            while (true)
            {
                try
                {
                    message = this.connector.Read(admin.socket);
                }
                catch (Exception e)
                {
                    Logging.LogEvent("AdminInteraction", e.Message);
                    break;
                }

                Logging.LogEvent("AdminInteraction", "User #" + admin.id + " said -> " + message);

                if (message == "")
                {
                    int count = 1;
                    while (true)
                    {
                        string nMessage = connector.Read(admin.socket);
                        if (nMessage == "")
                        {
                            count++;
                        }
                        else
                        {
                            break;
                        }
                        if (count > 20)
                        {
                            message = TCPIPconnector.kClientLeft;
                            break;
                        }
                    }
                }

                if (message == "!leaderboard")
                {
                    List<TriviaLib.User> list = null;

                    try
                    {
                        list = this.DBConnector.GetLeaderBoard();
                    }
                    catch (Exception e)
                    {
                        Logging.LogEvent("AdminInteraction", e.Message);
                        break;
                    }

                    try
                    {
                        for (int counter = 0; counter < list.Count; counter++)
                        {
                            this.connector.Send(TriviaLib.User.Serialize(list[counter]), admin.socket);
                            this.connector.Read(admin.socket);
                        }
                        this.connector.Send(TriviaLib.User.Serialize(new TriviaLib.User()), admin.socket);
                    }
                    catch (Exception e)
                    {
                        Logging.LogEvent("AdminInteraction", e.Message);
                        break;
                    }
                }

                if (message == "!data")
                {
                    List<AdminQuestion> list;

                    if (TriviaServer.answerID > 1)
                    {
                        try
                        {
                            list = this.DBConnector.GetAdminStats();

                            if (list.Count < 10)
                            {
                                for (int i = list[(list.Count - 1)].number; i < 10; i++)
                                {
                                    AdminQuestion q = new AdminQuestion();
                                    q.number = i + 1;
                                    q.content = this.DBConnector.GetQuestion(i + 1).content;
                                    q.avgTime = 0;
                                    q.answered = 0;

                                    list.Add(q);
                                }
                            }
                        }
                        catch(Exception e)
                        {
                            Logging.LogEvent("AdminInteraction", e.Message);
                            break;
                        }
                    }
                    else
                    {
                        list = new List<AdminQuestion>();
                    }

                    try
                    {
                        for (int counter = 0; counter < list.Count; counter++)
                        {
                            list[counter].answered = list[counter].answered * 100;
                            this.connector.Send(AdminQuestion.Serialize(list[counter]), admin.socket);
                            this.connector.Read(admin.socket);
                        }
                        this.connector.Send(AdminQuestion.Serialize(new AdminQuestion()), admin.socket);
                    }
                    catch (Exception e)
                    {
                        Logging.LogEvent("AdminInteraction", e.Message);
                        break;
                    }
                }

                if (message.Length > 5)
                {
                    if ((message.Substring(0, 5) == "!edit") && (message.Length > 5))
                    {
                        int qId = Convert.ToInt32(message.Substring(5));

                        if (qId < 11)
                        {
                            Question q = new Question();
                            try
                            {
                                q = this.DBConnector.GetQuestion(qId);
                            }
                            catch (Exception e)
                            {
                                Logging.LogEvent("AdminInteraction", e.Message);
                                break;
                            }

                            byte[] buffer = Question.Serialize(q);
                            Question newQuestion = new Question();

                            try
                            {
                                this.connector.Send(buffer, admin.socket);

                                newQuestion = Question.Deserialize(this.connector.ReadObject(admin.socket));
                            }
                            catch (Exception e)
                            {
                                Logging.LogEvent("AdminInteraction", e.Message);
                                break;
                            }

                            if ((q.content != newQuestion.content)
                                    || (q.answer != newQuestion.answer)
                                    || (q.a != newQuestion.a)
                                    || (q.b != newQuestion.b)
                                    || (q.c != newQuestion.c)
                                    || (q.d != newQuestion.d))
                            {
                                newQuestion.number = q.number;
                                try
                                {
                                    this.DBConnector.ChangeQuestion(newQuestion);
                                }
                                catch (Exception e)
                                {
                                    Logging.LogEvent("AdminInteraction", e.Message);
                                    break;
                                }
                            }
                        }
                        else
                        {
                            Question q = new Question();

                            byte[] buffer = Question.Serialize(q);

                            try
                            {
                                this.connector.Send(buffer, admin.socket);
                            }
                            catch (Exception e)
                            {
                                Logging.LogEvent("AdminInteraction", e.Message);
                                break;
                            }
                        }
                    }
                }

                if (message == "!live")
                {
                    try
                    {
                        for (int counter = 0; counter < this.userList.Count; counter++)
                        {
                            TriviaLib.User u = new TriviaLib.User();
                            u.name = this.userList[counter].name;
                            u.currentQuestion = this.userList[counter].currentQuestion;
                            u.totalPoints = this.userList[counter].totalScore;

                            this.connector.Send(TriviaLib.User.Serialize(u), admin.socket);
                            this.connector.Read(admin.socket);
                        }
                        this.connector.Send(TriviaLib.User.Serialize(new TriviaLib.User()), admin.socket);
                    }
                    catch (Exception e)
                    {
                        Logging.LogEvent("AdminInteraction", e.Message);
                        break;
                    }
                }

                if (message == TCPIPconnector.kClientLeft)
                {
                    Logging.LogEvent("AdminInteraction", "Client #" + admin.id + " left");
                    break;
                }
            }
        }