Пример #1
0
        public void RoundStart()
        {
            RoundCounter++;
            wordToGuess = GenerateWord().Trim();
            ClassToSend msg = new ClassToSend();

            msg.Type        = Type.RoundStartTest;
            msg.WordToGuess = HideWord(wordToGuess);
            msg.Turn        = false;
            msg.DrawerName  = Users[Drawer].Name;
            List <Task> tasks = new List <Task>();

            foreach (ServerUser user in Users)
            {
                if (user.Id == Drawer)
                {
                    ClassToSend DrawerMsg = new ClassToSend(msg);
                    DrawerMsg.Turn        = true;
                    DrawerMsg.WordToGuess = wordToGuess;
                    tasks.Add(SendMessage(user, DrawerMsg));
                }
                else
                {
                    tasks.Add(SendMessage(user, msg));
                }
            }
            Task.WaitAll(tasks.ToArray());
        }
Пример #2
0
 private void RoundStart(ClassToSend msg)
 {
     time = Server.RoundTime;
     timer.Start();
     Counter = 0;
     RoundCounter++;
     Down                   = false;
     txtChat.Enabled        = true;
     btnGuess.Enabled       = true;
     groupBox1.Visible      = msg.Turn;
     pictureBox1.Enabled    = msg.Turn;
     lblWordToGuess.Text    = msg.WordToGuess;
     lblDrawerName.Visible  = true;
     gbChangeRound.Visible  = false;
     lblChangeRound.Visible = false;
     if (msg.Turn)
     {
         lblDrawerName.Text = "You are drawing!";
     }
     else
     {
         lblDrawerName.Text = $"{msg.DrawerName} is drawing!";
     }
     Drawer.Lines.Clear();
     pictureBox1.Invalidate();
 }
Пример #3
0
        void AcceptClients()
        {
            int Id = 0;

            while (true)
            {
                try
                {
                    TcpClient   CurrentClient = Listener.AcceptTcpClient();
                    ClassToSend MSG           = (ClassToSend)bf.Deserialize(CurrentClient.GetStream());
                    ClassToSend msg           = new ClassToSend();
                    msg.Id = Id;
                    Users.Add(new ServerUser(Id++, MSG.Name, CurrentClient));
                    msg.Users = Users;
                    msg.Type  = Type.LobbyUpdate;
                    foreach (var user in Users)
                    {
                        SendMessage(user, msg);
                    }
                }
                catch (Exception e)
                {
                    Console.Write(e.Message);
                }
            }
        }
Пример #4
0
        public RemoteUser(string Name, string Address, int port = 25565)
        {
            this.Name = Name;
            Client    = new TcpClient(Address, port);
            ClassToSend msg = new ClassToSend();

            msg.Name = this.Name;
            bf.Serialize(Client.GetStream(), msg);
        }
Пример #5
0
 public Lobby(string Name, int port = 25565)//Server+Client Lobby
 {
     InitializeComponent();
     this.Name    = Name;
     this.Port    = port;
     createServer = true;
     Drawer       = new ClassToSend();
     points       = 0;
     RoundCounter = 0;
 }
Пример #6
0
 private void Drawing(ClassToSend msg)
 {
     txtChat.Enabled        = true;
     btnGuess.Enabled       = true;
     groupBox1.Visible      = true;
     lblWordToGuess.Text    = msg.WordToGuess;
     pictureBox1.Enabled    = msg.Turn;
     gbChangeRound.Visible  = false;
     lblChangeRound.Visible = false;
 }
Пример #7
0
        public void InitializeUsers()
        {
            ClassToSend msg = new ClassToSend();

            msg.Type = Type.Start;
            foreach (ServerUser user in Users)
            {
                SendMessage(user, msg);
            }
        }
Пример #8
0
 private void CorrectWord(ClassToSend msg)
 {
     lblWordToGuess.Text = msg.GuessedWord;
     txtLog.Text        += "You guessed the word!\r\n";
     listPlayers.Items[msg.Id].SubItems[1].Text = "Points: " + msg.Points;
     txtChat.Enabled       = false;
     btnGuess.Enabled      = false;
     txtLog.SelectionStart = txtLog.Text.Length;
     txtLog.ScrollToCaret();
 }
Пример #9
0
 public Lobby(string Name, string Address, int port = 25565)//Client Lobby
 {
     InitializeComponent();
     this.Name    = Name;
     this.Address = Address;
     this.Port    = port;
     createServer = false;
     Drawer       = new ClassToSend();
     points       = 0;
     RoundCounter = 0;
 }
Пример #10
0
 private void SendMessage(ClassToSend msg)
 {
     try
     {
         bf.Serialize(user.Client.GetStream(), msg);
     }
     catch (Exception ex)
     {
         DialogResult = DialogResult.Abort;
     }
 }
Пример #11
0
 private void RoundEnd(ClassToSend msg)
 {
     timer.Stop();
     groupBox1.Visible      = false;
     txtChat.Enabled        = false;
     btnGuess.Enabled       = false;
     lblDrawerName.Visible  = false;;
     pictureBox1.Enabled    = false;
     gbChangeRound.Visible  = true;
     lblChangeRound.Visible = true;
     lblWordToGuess.Text    = "The word was " + msg.WordToGuess + "!";
 }
Пример #12
0
 public ClassToSend(ClassToSend cts)
 {
     this.WordToGuess = cts.WordToGuess;
     this.Type        = cts.Type;
     this.GuessedWord = cts.GuessedWord;
     this.Name        = cts.Name;
     this.Id          = cts.Id;
     this.Turn        = cts.Turn;
     this.Users       = new List <ServerUser>();
     this.Lines       = new List <Line>();
     this.Points      = cts.Points;
     this.RoundId     = cts.RoundId;
     this.DrawerName  = cts.DrawerName;
 }
Пример #13
0
        private void btnGuess_Click(object sender, EventArgs e)
        {
            if (txtChat.Text.Trim().Equals(""))
            {
                txtChat.Text = "";
                return;
            }
            ClassToSend msg = new ClassToSend();

            msg.Type        = Type.Guess;
            msg.Id          = user.Id;
            msg.GuessedWord = txtChat.Text.Trim();
            msg.RoundId     = RoundCounter;
            SendMessage(msg);
            txtChat.Text = "";
        }
Пример #14
0
        public void UpdatePoints(ServerUser user, ClassToSend msg)
        {
            foreach (ServerUser uss in Users)
            {
                if (uss.Id == user.Id)
                {
                    continue;
                }

                ClassToSend newMsg = new ClassToSend(msg);

                newMsg.Type   = Type.PointUpdate;
                newMsg.Points = user.points;
                newMsg.Id     = user.Id;
                SendMessage(uss, newMsg);
            }
        }
Пример #15
0
        public void MakeViewList(ClassToSend msg)
        {
            listPlayers.Items.Clear();
            listPlayers.View = View.Tile;
            listPlayers.Columns.Add("Player");
            listPlayers.Columns.Add("Points");
            var lvi = listPlayers.Items.Add(msg.Users[0].Name, msg.Users[0].Id % 4);

            lvi.SubItems.Add("Points: 0");
            listPlayers.Items[0].Group = listPlayers.Groups[0];
            for (int i = 1; i < msg.Users.Count; i++)
            {
                var tmp = listPlayers.Items.Add(msg.Users[i].Name, msg.Users[i].Id % 4);
                tmp.SubItems.Add("Points: 0");
                listPlayers.Items[msg.Users[i].Id].Group = listPlayers.Groups[0];
            }
        }
Пример #16
0
        public void ActionToGuess(ClassToSend msg)
        {
            if (msg.Type == Type.Guess)
            {
                if (msg.GuessedWord.Equals(wordToGuess))
                {
                    if (msg.Id == Drawer)
                    {
                        return;
                    }
                    playersGuessing--;
                    foreach (ServerUser user in Users)
                    {
                        if (user.Id.Equals(msg.Id))
                        {
                            ClassToSend newMsg = new ClassToSend(msg);

                            user.points += Points;
                            Points       = (int)Math.Ceiling(Points / 2.0);
                            UpdatePoints(user, newMsg);
                            newMsg.Type   = Type.Guessed;
                            newMsg.Points = user.points;
                            SendMessage(user, newMsg);
                        }
                        else
                        {
                            ClassToSend newMsg = new ClassToSend(msg);

                            newMsg.Type = Type.Another;
                            SendMessage(user, newMsg);
                        }
                    }
                }
                else
                {
                    msg.Type = Type.Miss;
                    foreach (var user in Users)
                    {
                        SendMessage(user, msg);
                    }
                }
            }
        }
Пример #17
0
        public void SendingPicture(BlockingCollection <ClassToSend> bc)
        {
            ClassToSend msg = null;

            bc.TryTake(out msg);
            if (msg != null && RoundCounter == msg.RoundId)
            {
                if (msg.Type == Type.Picture)
                {
                    foreach (ServerUser user in Users)
                    {
                        if (!msg.Name.Equals(user.Name))
                        {
                            SendMessage(user, msg);
                        }
                    }
                }
                ActionToGuess(msg);
            }
        }
Пример #18
0
        public void RoundEnd()
        {
            Points = 20;
            if (Drawer == -1)
            {
                Drawer++;
                return;
            }
            ClassToSend msg = new ClassToSend();

            msg.Type        = Type.RoundEnd;
            msg.WordToGuess = wordToGuess;
            List <Task> tasks = new List <Task>();

            foreach (ServerUser user in Users)
            {
                tasks.Add(SendMessage(user, msg));
            }
            Task.WaitAll(tasks.ToArray());
            Drawer = (Drawer + 1) % Users.Count;
        }
Пример #19
0
 public void RunServer(BlockingCollection <ClassToSend> bc)
 {
     //Initialize
     try
     {
         while (true)
         {
             ClassToSend msg = (ClassToSend)bf.Deserialize(Client.GetStream());
             if (msg.Type == Type.Ping)
             {
                 continue;
             }
             msg.Id   = this.Id;
             msg.Name = this.Name;
             bc.Add(msg);
         }
     }
     catch (Exception e)
     {
     }
 }
Пример #20
0
 public void DelegateMethod()
 {
     if (DialogResult == DialogResult.Retry)
     {
         return;
     }
     myDelegate = new MessageFilter(MessageFilterMethod);
     UserThread = new Thread(() => {
         try
         {
             while (true)
             {
                 ClassToSend msg = user.RunClient();
                 label5.Invoke(myDelegate, new Object[] { msg });
             }
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.Message);
         }
     });
     UserThread.Start();
 }
Пример #21
0
        public Task SendMessage(ServerUser user, ClassToSend msg)
        {
            if (user.ConnectionClosed)
            {
                return(new Task(() => { }));
            }
            Task t = new Task(new Action(() => {
                user.s.WaitOne();
                try
                {
                    bf.Serialize(user.Client.GetStream(), msg);
                }
                catch (Exception e)
                {
                    PlayerLeft            = true;
                    user.ConnectionClosed = true;
                    Console.WriteLine(e.Message);
                }
                user.s.Release(1);
            }));

            t.Start();
            return(t);
        }
Пример #22
0
 public void StartGame()
 {
     Listener.Stop();
     LobbyThread.Abort();
     GameThread = new Thread(() =>
     {
         Queue = new ConcurrentQueue <ClassToSend>();
         BlockingCollection <ClassToSend> bc = new BlockingCollection <ClassToSend>(Queue);
         StartThreads(bc);
         InitializeUsers();
         RoundEnd();
         RoundStart();
         playersGuessing = Users.Count() - 1;
         DateTime time   = DateTime.Now;
         PingTime        = DateTime.Now;
         while (true)
         {
             SwitchRound(ref time, bc);
             Ping();
             if (PlayerLeft)
             {
                 foreach (ServerUser u in Users)
                 {
                     ClassToSend msg = new ClassToSend();
                     msg.Type        = Type.CloseConnection;
                     SendMessage(u, msg);
                 }
                 break;
             }
             SendingPicture(bc);
             //Preprakjanje
         }
     });
     GameThread.IsBackground = true;
     GameThread.Start();
 }
Пример #23
0
 private void AnotherGuessed(ClassToSend msg)
 {
     txtLog.Text          += msg.Name + " guessed the word!\r\n";
     txtLog.SelectionStart = txtLog.Text.Length;
     txtLog.ScrollToCaret();
 }
Пример #24
0
        public void MessageFilterMethod(ClassToSend msg)
        {
            switch (msg.Type)
            {
            case Type.Start:
            {
                SetControls();
            }
            break;

            case Type.Picture:
            {
                Drawer = msg;
                pictureBox1.Invalidate();
            }
            break;

            case Type.RoundStartTest:
            {
                RoundStart(msg);
            }
            break;

            case Type.RoundEnd:
            {
                RoundEnd(msg);
            }
            break;

            case Type.Drawing:
            {
                Drawing(msg);
            }
            break;

            case Type.TimesUp:
            {
                MessageBox.Show("Time's up!");
            }
            break;

            case Type.Guessed:
            {
                CorrectWord(msg);
            }
            break;

            case Type.PointUpdate:
            {
                listPlayers.Items[msg.Id].SubItems[1].Text = "Points: " + msg.Points;
            }
            break;

            case Type.Another:
            {
                AnotherGuessed(msg);
            }
            break;

            case Type.Miss:
            {
                if (!msg.Name.Equals(user.Name))
                {
                    txtLog.Text += msg.Name + ": " + msg.GuessedWord + "\r\n";
                }
                else
                {
                    txtLog.Text += "You: " + msg.GuessedWord + "\r\n";
                }
            }
            break;

            case Type.LobbyUpdate:
            {
                user.Id = msg.Id;
                MakeViewList(msg);
            }
            break;

            case Type.CloseConnection:
            {
                DialogResult = DialogResult.Abort;
                this.Close();
            }
            break;

            default:
            {
            }
            break;
            }
        }