Exemple #1
0
        public void NewGame(string s)
        {
            if (s.Equals("e"))
            {
                this.win++;

                MessageBox.Show("you won with");
            }
            else if (s.Equals("c"))
            {
                this.lost++;

                MessageBox.Show("you lost");
            }
            else if (s.Equals("t"))
            {
                MessageBox.Show("Draw");
            }

            if (win > 0)

            {
                this.Game_Running = false;

                using (PlayersEntities1 player = new PlayersEntities1())
                {
                    DateTime foo      = DateTime.UtcNow;
                    long     unixTime = ((DateTimeOffset)foo).ToUnixTimeSeconds();
                    Player   p        = player.Players.FirstOrDefault(r => r.Username == logedUser);
                    Player   p2       = player.Players.FirstOrDefault(r => r.Username == clientUsername);
                    Game     game     = player.Games.Find(this.GameId);
                    game.WinnerId = p.Id;
                    if (game.Player_Game.HasValue)
                    {
                        maxPlayers = game.Player_Game.Value;
                    }
                    game.Status = "Closed";
                    //ovo je nepotrebno jer vec postoji jos treba testirat               player.Games.Add(game);
                    p.Games.Add(game);
                    p2.Games.Add(game);
                    player.SaveChanges();
                }


                winForm.pictureBox1.Image = Properties.Resources.winner;

                winForm.ShowDialog();
                DialogResult dialog = MessageBox.Show("New Game", "Exit", MessageBoxButtons.YesNo);

                if (dialog == DialogResult.Yes)
                {
                    this.win  = 0;
                    this.lost = 0;


                    OnFormUnsub("Form");
                    using (PlayersEntities1 p = new PlayersEntities1())
                    {
                        playerCount += tcpServer.ConnectedClientsCount;
                        Game game = new Game {
                            Status = "Open", Players_In_Game = playerCount, Player_Game = maxPlayers, Game_Type = gameType
                        };

                        p.Games.Add(game);

                        Player player = p.Players.FirstOrDefault(r => r.Username == logedUser);
                        player.Games.Add(game);
                        p.SaveChanges();
                        this.GameId = game.Id;
                    }

                    MessageBox.Show(this.GameId.ToString());
                }
                else if (dialog == DialogResult.No)
                {
                    OnFormUnsub("Form");

                    tcpServer.BroadcastLine("kicked");
                    Client_Disconnect(null, tcpClient);

                    //  this.OnFormClosing(new FormClosingEventArgs(CloseReason.UserClosing, false));
                }
            }

            else if (lost > 1)
            {
                this.Game_Running = false;

                using (PlayersEntities1 player = new PlayersEntities1())
                {
                    Player p1   = player.Players.FirstOrDefault(r => r.Username == logedUser);
                    Player p2   = player.Players.FirstOrDefault(r => r.Username == clientUsername);
                    Game   game = player.Games.Find(this.GameId);
                    game.WinnerId = p2.Id;
                    if (game.Player_Game.HasValue)
                    {
                        maxPlayers = game.Player_Game.Value;
                    }

                    game.Status = "Closed";
                    //visak testirat  player.Games.Add(game);
                    p1.Games.Add(game);
                    p2.Games.Add(game);
                    player.SaveChanges();
                }



                winForm.pictureBox1.Image = Properties.Resources.lost;
                winForm.ShowDialog();
                DialogResult dialog = MessageBox.Show("New Game", "exit", MessageBoxButtons.YesNo);

                if (dialog == DialogResult.Yes)
                {
                    this.win  = 0;
                    this.lost = 0;
                    OnFormUnsub("Form");
                    using (PlayersEntities1 p = new PlayersEntities1())
                    {
                        playerCount += tcpServer.ConnectedClientsCount;
                        Game game = new Game {
                            Status = "Open", Players_In_Game = playerCount, Player_Game = maxPlayers, Game_Type = gameType
                        };

                        p.Games.Add(game);

                        Player player = p.Players.FirstOrDefault(r => r.Username == logedUser);
                        player.Games.Add(game);
                        p.SaveChanges();
                        this.GameId = game.Id;
                    }
                }
                else if (dialog == DialogResult.No)
                {
                    OnFormUnsub("Form");

                    tcpServer.BroadcastLine("kicked");
                    Client_Disconnect(null, tcpClient);
                }
            }

            else
            {
                DialogResult dialog = MessageBox.Show("Continue", "exit", MessageBoxButtons.YesNo);



                if (dialog == DialogResult.Yes)
                {
                    OnFormUnsub("Form");
                }
                //need to add lost for server couse he left
                if (dialog == DialogResult.No)
                {
                    OnFormUnsub("Form");
                    tcpServer.BroadcastLine("kicked");
                    Client_Disconnect(null, tcpClient);
                }
            }
        }
Exemple #2
0
        public void Client_Received(object sender, SimpleTCP.Message m)
        {
            string end    = m.MessageString.Substring(0, m.MessageString.Length - 1);
            string kicked = m.MessageString.Substring(0, m.MessageString.Length - 1);

            if (kicked.Equals(left))
            {
                button2_Click(this, EventArgs.Empty);
            }
            if (end.Equals("e") || end.Equals("e!!"))
            {
                this.serverWin++;

                if (this.serverWin > 0)
                {
                    MessageBox.Show("you lost");

                    winForm.pictureBox1.Image = Properties.Resources.lost;
                    winForm.ShowDialog();
                    DialogResult dialog = MessageBox.Show("New Game", "exit", MessageBoxButtons.YesNo);

                    if (dialog == DialogResult.Yes)
                    {
                        if (gameType == 3)
                        {
                            this.serverWin   = 0;
                            this.clientWin   = 0;
                            f.MouseClicked  -= this.OnMOuseClicked;
                            this.ClientMove -= f.OnClientMove;
                            f.Dispose();
                        }
                        else
                        {
                            this.serverWin   = 0;
                            this.clientWin   = 0;
                            g.MouseClicked  -= this.OnMOuseClicked;
                            this.ClientMove -= g.OnClientMove;
                            g.Dispose();
                        }
                    }

                    //klijent ne zeli novu igru te izlazi iz servera
                    else if (dialog == DialogResult.No)
                    {
                        try
                        {
                            button2_Click(null, new EventArgs());
                            //this.Client_FormClosing(null, new FormClosingEventArgs(CloseReason.None,false));
                            if (gameType == 3)
                            {
                                if (!f.IsDisposed)
                                {
                                    f.Dispose();
                                }
                            }
                            else
                            {
                                if (!g.IsDisposed)
                                {
                                    g.Dispose();
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show(e.Message.ToString());
                        }
                    }
                }

                //server dobio partiju u malim
                else
                {
                    DialogResult dialog = MessageBox.Show("Continue", "exit", MessageBoxButtons.YesNo);

                    if (dialog == DialogResult.Yes)
                    {
                        if (gameType == 3)
                        {
                            f.MouseClicked  -= this.OnMOuseClicked;
                            this.ClientMove -= f.OnClientMove;

                            f.Dispose();
                        }
                        else
                        {
                            g.MouseClicked  -= this.OnMOuseClicked;
                            this.ClientMove -= g.OnClientMove;

                            g.Dispose();
                        }
                    }
                    else if (dialog == DialogResult.No)
                    {
                        button2_Click(null, new EventArgs());
                        if (gameType == 3)
                        {
                            if (!f.IsDisposed)
                            {
                                f.Dispose();
                            }
                        }
                        else
                        {
                            if (!g.IsDisposed)
                            {
                                g.Dispose();
                            }
                        }
                    }
                }
            }
            else if (end.Equals("c") || end.Equals("c!!"))
            {
                this.gameRunning = false;
                this.clientWin++;
                if (this.clientWin > 0)
                {
                    MessageBox.Show("you won");

                    winForm.pictureBox1.Image = Properties.Resources.winner;
                    winForm.ShowDialog();
                    DialogResult dialog = MessageBox.Show("New Game", "exit", MessageBoxButtons.YesNo);

                    if (dialog == DialogResult.Yes)
                    {
                        if (gameType == 3)
                        {
                            this.serverWin   = 0;
                            this.clientWin   = 0;
                            f.MouseClicked  -= this.OnMOuseClicked;
                            this.ClientMove -= f.OnClientMove;
                            f.Dispose();
                        }
                        else
                        {
                            this.serverWin   = 0;
                            this.clientWin   = 0;
                            g.MouseClicked  -= this.OnMOuseClicked;
                            this.ClientMove -= g.OnClientMove;
                            g.Dispose();
                        }
                    }
                    else if (dialog == DialogResult.No)
                    {
                        button2_Click(null, new EventArgs());

                        if (gameType == 3)
                        {
                            if (!f.IsDisposed)
                            {
                                f.Dispose();
                            }
                        }
                        else
                        {
                            if (!g.IsDisposed)
                            {
                                g.Dispose();
                            }
                        }
                    }
                }


                else
                {
                    MessageBox.Show("opponent won");
                    DialogResult dialog = MessageBox.Show("Continue", "exit", MessageBoxButtons.YesNo);
                    if (dialog == DialogResult.Yes)
                    {
                        if (gameType == 3)
                        {
                            f.MouseClicked  -= this.OnMOuseClicked;
                            this.ClientMove -= f.OnClientMove;

                            f.Dispose();
                        }
                        else
                        {
                            g.MouseClicked  -= this.OnMOuseClicked;
                            this.ClientMove -= g.OnClientMove;

                            g.Dispose();
                        }
                    }
                    else if (dialog == DialogResult.No)
                    {
                        button2_Click(null, new EventArgs());
                        if (gameType == 3)
                        {
                            if (!f.IsDisposed)
                            {
                                f.Dispose();
                            }
                        }
                        else
                        {
                            if (!g.IsDisposed)
                            {
                                g.Dispose();
                            }
                        }
                    }
                }
            }
            else if (end.Equals("t") || end.Equals("t!!"))
            {
                MessageBox.Show("Game draw");
                DialogResult dialog = MessageBox.Show("Continue", "exit", MessageBoxButtons.YesNo);

                if (dialog == DialogResult.Yes)
                {
                    if (gameType == 3)
                    {
                        f.MouseClicked  -= this.OnMOuseClicked;
                        this.ClientMove -= f.OnClientMove;

                        f.Dispose();
                    }
                    else
                    {
                        g.MouseClicked  -= this.OnMOuseClicked;
                        this.ClientMove -= g.OnClientMove;

                        g.Dispose();
                    }
                }
                else if (dialog == DialogResult.No)
                {
                    button2_Click(null, new EventArgs());
                    if (gameType == 3)
                    {
                        if (!f.IsDisposed)
                        {
                            f.Dispose();
                        }
                    }
                    else
                    {
                        if (!g.IsDisposed)
                        {
                            g.Dispose();
                        }
                    }
                }
            }
            textBox1.Invoke((MethodInvoker) delegate()
            {
                //da ne postavlja pitcureboxove u listview
                string m1 = m.MessageString.Substring(0, m.MessageString.Length - 1);
                if ((!m1.Equals("pictureBox1")) && (!m1.Equals("pictureBox2")) && (!m1.Equals("pictureBox3")) && (!m1.Equals("pictureBox4")))
                {
                    if (m1.Length > 800)
                    {
                        listView1.Items.Add("game started" + "\n");
                    }
                    else
                    {
                        listView1.Items.Add(m.MessageString + "\n");
                    }
                }
                potez = m.MessageString;
            });
            start = m.MessageString;

            if (start.Length > 800)
            {
                //bez ovoga ne uspije ucitat cili string

                BeginInvoke((MethodInvoker) delegate
                {
                    string s = start.Substring(0, start.Length - 1);
                    try
                    {
                        if (gameType == 3)
                        {
                            f = JsonConvert.DeserializeObject <Form1>(s);
                        }
                        else
                        {
                            g = JsonConvert.DeserializeObject <GameFour>(s);
                        }
                        Thread.Sleep(1);
                    }

                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message.ToString());
                    }


                    if (gameType == 3)
                    {
                        var p1 = f.p1;
                        var p2 = f.p2;
                        f.p1   = p2;
                        f.p2   = p1;

                        f.Text               = "client";
                        f.MouseClicked      += this.OnMOuseClicked;
                        this.ClientMove     += f.OnClientMove;
                        client.DataReceived += f.GameRecived;
                        this.gameRunning     = true;
                        f.FormClosing       += this.Game_Form_Closing;

                        f.Show();
                    }
                    else
                    {
                        var p1 = g.p1;
                        var p2 = g.p2;
                        g.p1   = p2;
                        g.p2   = p1;


                        g.Text               = "client";
                        g.MouseClicked      += this.OnMOuseClicked;
                        this.ClientMove     += g.OnClientMove;
                        this.gameRunning     = true;
                        client.DataReceived += g.GameRecived;

                        g.FormClosing += this.Game_Form_Closing;

                        g.Show();
                    }
                });
            }



            string mess = potez.Substring(0, potez.Length - 1);

            if ((mess.Equals("pictureBox1")) || (mess.Equals("pictureBox2")) || (mess.Equals("pictureBox3")) || mess.Equals("pictureBox4"))
            {
                ClientMove(mess);
            }
        }