Beispiel #1
0
        public void refresh_list()
        {
            if (listView1.Items.Count > 0)
            {
                listView1.Items.Clear();
            }
            using (PlayersEntities1 players = new PlayersEntities1())
            {
                var player = players.Players.ToList();
                foreach (Player p in player)
                {
                    var role = p.PlayerRoles.FirstOrDefault(r1 => r1.PlayerId == p.Id);
                    if (role.Role.Id == 2)
                    {
                        ListViewItem item1 = new ListViewItem();

                        /*/
                         * item1.SubItems.Add(server.Username);
                         * item1.SubItems.Add(server.Win_Rate.ToString());
                         *
                         * listView2.Items.Add(item1);
                         * /*/

                        item1.Tag  = p.Id;
                        item1.Text = p.Id.ToString();
                        item1.SubItems.Add(p.Username);
                        listView1.Items.Add(item1);
                    }
                }
            }
        }
Beispiel #2
0
        private void MyStats_Click(object sender, EventArgs e)
        {
            int     wins    = 0;
            int     lost    = 0;
            int     total   = 0;
            decimal percent = 0;

            using (PlayersEntities1 players = new PlayersEntities1())
            {
                Player player = players.Players.FirstOrDefault(r => r.Username == logedUser);

                var games = player.Games.ToList();
                total = games.Count;
                foreach (var g in games)
                {
                    if (g.WinnerId == player.Id)
                    {
                        wins++;
                    }
                    else
                    {
                        lost++;
                    }
                }
            }
            try
            {
                percent = (decimal)wins / total * 100;
                DialogResult d = MessageBox.Show("User:"******"\n" + "Win:" + wins + "\n" + "Lost:" + lost + "\n" + "Total Played:" + total + "\n" + "Percent:" + percent, "Statistic", MessageBoxButtons.OK);
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.StackTrace);
            }
        }
Beispiel #3
0
        // subscribe to form1 closing
        public void OnFormClosing()
        {
            if (this.Game_Running)
            {
                MessageBox.Show("server left so he lost the game");

                //server left the game so he lost the game
                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;

                    game.Status = "Closed";
                    //visak testirat player.Games.Add(game);
                    p1.Games.Add(game);
                    p2.Games.Add(game);
                    player.SaveChanges();
                }
                this.Game_Running = false;
                button1_Click(this, EventArgs.Empty);
            }
            else
            {
                OnFormUnsub("f");
            }
        }
Beispiel #4
0
        private void Server_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (tcpServer.IsStarted)
            {
                //server  se gasi te se uklanja igra koja je stvorena prilikom pokretanja servera a nije se odigrala
                using (PlayersEntities1 players = new PlayersEntities1())
                {
                    Player p = players.Players.FirstOrDefault(r => r.Username == this.logedUser);
                    p.IpAddress = "";

                    Game game = players.Games.FirstOrDefault(r => r.Id == this.GameId);
                    if (game.Status.Equals("Open"))
                    {
                        p.Games.Remove(game);
                        players.Games.Remove(game);
                        players.SaveChanges();
                    }
                    tcpServer.Stop();
                    if (gameType == 3)
                    {
                        if (!f.IsDisposed)
                        {
                            OnFormUnsub("f");
                        }
                    }
                    else
                    {
                        if (!g.IsDisposed)
                        {
                            OnFormUnsub("g");
                        }
                    }
                }
            }
        }
Beispiel #5
0
        //refresh button
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            listView2.Items.Clear();
            listView3.Clear();
            using (PlayersEntities1 play = new PlayersEntities1())
            {
                var games = play.Games.ToList();
                var p     = play.Players.ToList();
                if (p != null)
                {
                    foreach (var server in p)
                    {
                        var playerGames = server.Games.Where(r => r.Status != "Closed");

                        string s = server.IpAddress;
                        if (s != "")
                        {
                            foreach (var g in playerGames)
                            {
                                if (g.Status != "Closed")
                                {
                                    ListViewItem item1 = new ListViewItem();

                                    /*/
                                     * item1.SubItems.Add(server.Username);
                                     * item1.SubItems.Add(server.Win_Rate.ToString());
                                     *
                                     * listView2.Items.Add(item1);
                                     * /*/

                                    item1.Tag  = server.IpAddress;
                                    item1.Text = server.Username;


                                    item1.SubItems.Add(g.Players_In_Game.ToString());
                                    item1.SubItems.Add(g.Player_Game.ToString());
                                    item1.SubItems.Add("Card Number" + g.Game_Type.ToString());
                                    item1.SubItems.Add(server.Win_Rate.ToString());

                                    listView2.Items.Add(item1);
                                }
                            }
                        }
                        if (server.Status.Equals("Online"))
                        {
                            if (!server.Username.Contains(logedUser))
                            {
                                ListViewItem item2 = new ListViewItem();
                                item2.Text       = server.Username;
                                item2.Tag        = server;
                                item2.ImageIndex = 1;
                                listView3.Items.Add(item2);
                            }
                        }
                    }
                }
            }
        }
Beispiel #6
0
        public void OnNumberIncrease(object sender, EventArgs e)
        {
            using (PlayersEntities1 players = new PlayersEntities1())
            {
                Game g = players.Games.FirstOrDefault(r => r.Id == this.GameId);
                g.Players_In_Game = 2;

                players.SaveChanges();
            }
        }
Beispiel #7
0
 private void ServerClient_FormClosing(object sender, FormClosingEventArgs e)
 {
     using (PlayersEntities1 player = new PlayersEntities1())
     {
         Player logoff = player.Players.FirstOrDefault(r => r.Username == this.logedUser);
         logoff.Status = "Offline";
         player.SaveChanges();
     }
     this.Dispose();
     Application.Exit();
 }
Beispiel #8
0
 private void logOutToolStripMenuItem_Click(object sender, EventArgs e)
 {
     using (PlayersEntities1 player = new PlayersEntities1())
     {
         Player p = player.Players.FirstOrDefault(r => r.Username == logedUser);
         p.Status = "Offline";
         player.SaveChanges();
     }
     this.Dispose();
     Application.Restart();
 }
Beispiel #9
0
        private void ServerList_Load(object sender, EventArgs e)
        {
            listView3.View = View.LargeIcon;
            this.listView3.LargeImageList = this.imageList1;

            imageList1.Images.Add("play", Properties.Resources.dot);
            this.client.logedUser = logedUser;
            using (PlayersEntities1 players = new PlayersEntities1())
            {
                var PlayersOnline = players.Players.ToList();

                foreach (var online in PlayersOnline)
                {
                    var playerGames = online.Games.Where(r => r.Status != "Closed");

                    string s = online.IpAddress;
                    if (s != "")
                    {
                        foreach (var g in playerGames)
                        {
                            ListViewItem item3 = new ListViewItem();



                            item3.Tag  = online.IpAddress;
                            item3.Text = online.Username;

                            item3.SubItems.Add(g.Players_In_Game.ToString());
                            item3.SubItems.Add(g.Player_Game.ToString());
                            item3.SubItems.Add(g.Game_Type.ToString());
                            item3.SubItems.Add(online.Win_Rate.ToString());

                            listView2.Items.Add(item3);
                        }
                    }
                    ;


                    // lista online igraca
                    if (online.Status.Equals("Online"))
                    {
                        if (!online.Username.Equals(logedUser))
                        {
                            ListViewItem item2 = new ListViewItem();
                            item2.ImageIndex = 1;
                            item2.Text       = online.Username;
                            listView3.Items.Add(item2);
                        }
                    }
                }
            }
        }
Beispiel #10
0
        public void OnNumberDecrease(object sender, EventArgs e)
        {
            using (PlayersEntities1 players = new PlayersEntities1())

            {
                if (s.Game_Running.Equals(false))
                {
                    this.GameId = s.GameId;
                    Game g = players.Games.FirstOrDefault(r => r.Id == this.GameId);
                    if (g.Status.Equals("Open"))
                    {
                        g.Players_In_Game = 1;

                        players.SaveChanges();
                    }
                }
            }
        }
Beispiel #11
0
        private void ServerClient_Load(object sender, EventArgs e)
        {
            using (PlayersEntities1 players = new PlayersEntities1())
            {
                Player p = players.Players.FirstOrDefault(r => r.Username == logedUser);

                var role = p.PlayerRoles.FirstOrDefault(r1 => r1.PlayerId == p.Id);
                if (role.Role.Id == 1)
                {
                    button1.Visible = true;
                    button2.Visible = true;
                }
                else
                {
                    button1.Visible = false;
                    button2.Visible = false;
                }
            }
        }
Beispiel #12
0
        private void start_Click(object sender, EventArgs e)
        {
            if (s == null)
            {
                s = new Server();
                s.NumberDecrease += OnNumberDecrease;
                s.NumberIncrease += OnNumberIncrease;
            }
            else if (s.IsDisposed)
            {
                s = new Server();
                s.NumberDecrease += OnNumberDecrease;
                s.NumberIncrease += OnNumberIncrease;
            }
            using (PlayersEntities1 p = new PlayersEntities1())
            {
                Player play = p.Players.FirstOrDefault(r => r.Username == logedUser);
                play.IpAddress = getIP();
                Game game = new Game {
                    Status = "Open", Players_In_Game = 1, Game_Type = gameType, Player_Game = playerNumber
                };
                play.Games.Add(game);

                p.SaveChanges();

                var games = play.Games.ToList();
                foreach (var g in games)
                {
                    if (g.Status != "Closed")
                    {
                        this.GameId = g.Id;
                        s.GameId    = g.Id;
                    }
                }
                s.Start(play.IpAddress, 7000);
                s.logedUser = play.Username;
                this.Hide();
                s.FormClosing += this.formClosed;
                s.gameType     = gameType;
                s.Show();
            }
        }
Beispiel #13
0
 private void PlayerList_Load(object sender, EventArgs e)
 {
     using (PlayersEntities1 players = new PlayersEntities1())
     {
         var player = players.Players.ToList();
         foreach (Player p in player)
         {
             var role = p.PlayerRoles.FirstOrDefault(r1 => r1.PlayerId == p.Id);
             if (role.Role.Id == 2)
             {
                 ListViewItem item1 = new ListViewItem();
                 item1.Tag  = p.Id;
                 item1.Text = p.Id.ToString();
                 item1.SubItems.Add(p.Username);
                 item1.SubItems.Add(p.Email);
                 listView1.Items.Add(item1);
             }
         }
     }
 }
Beispiel #14
0
        public void Client_Disconnect(object sender, TcpClient e)
        {
            if (tcpClient == e)
            {
                if (listView2.Items.Count > 0)
                {
                    StartGame.Enabled = false;
                    listView2.Items.RemoveAt(0);

                    usercnt--;
                    OnNumberDecrease(this, null);
                    tcpClient.Close();
                    tcpClient = null;
                }
                //player left while game is running so he lost the game
                if (Game_Running)
                {
                    using (PlayersEntities1 players = new PlayersEntities1())
                    {
                        Game game = players.Games.Find(this.GameId);


                        Player player1 = players.Players.FirstOrDefault(r => r.Username == this.logedUser);
                        Player player2 = players.Players.FirstOrDefault(r => r.Username == this.clientUsername);
                        game.WinnerId = player1.Id;
                        game.Status   = "Closed";
                        //visak testirat     players.Games.Add(game);
                        player1.Games.Add(game);
                        player2.Games.Add(game);
                        players.SaveChanges();
                    }
                    this.Game_Running = false;
                    OnFormUnsub("Form");
                }
                else
                {
                }
                MessageBox.Show(this.clientUsername + " left the server");
            }
        }
Beispiel #15
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                int selected = listView1.SelectedIndices[0];

                if (int.TryParse(listView1.Items[selected].Text, out int numb))
                {
                    player_id = numb;
                }

                using (PlayersEntities1 players = new PlayersEntities1())
                {
                    Player player = players.Players.Find(player_id);

                    var games = player.Games.ToList();
                    var roles = player.PlayerRoles.ToList();
                    foreach (var game in games)
                    {
                        player.Games.Remove(game);
                    }
                    var role = player.PlayerRoles.FirstOrDefault(r1 => r1.PlayerId == player.Id);
                    if (role.Role.Id == 2)
                    {
                        players.PlayerRoles.Remove(role);
                    }


                    players.Players.Remove(player);

                    players.SaveChanges();
                }
                MessageBox.Show("User deleted sucesfull");
            }
            else
            {
                MessageBox.Show("Select user first");
            }
            refresh_list();
        }
Beispiel #16
0
        private void button1_Click(object sender, EventArgs e)
        {
            using (PlayersEntities1 player = new PlayersEntities1())
            {
                Player p = player.Players.FirstOrDefault(r => r.Username == textBox1.Text);
                if (p != null)
                {
                    if (p.Password.Equals(textBox2.Text))
                    {
                        if (!p.Status.Equals("Online"))
                        {
                            ServerClient s = new ServerClient();

                            p.Status = "Online";
                            player.SaveChanges();

                            s.logedUser = p.Username;

                            s.Show();
                            this.Hide();
                        }
                        else
                        {
                            MessageBox.Show("You are already logged in");
                            textBox1.Text = "";
                            textBox2.Text = "";
                        }
                    }

                    else
                    {
                        MessageBox.Show("wrong password");
                    }
                }
                else
                {
                    MessageBox.Show("User dont exist");
                }
            }
        }
Beispiel #17
0
        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult dialog = MessageBox.Show("ResetDB", "Exit", MessageBoxButtons.OKCancel);

            if (dialog == DialogResult.OK)
            {
                using (PlayersEntities1 players = new PlayersEntities1())
                {
                    var games = players.Games.ToList();
                    var pl    = players.Players.ToList();

                    for (int i = 0; i < games.Count; i++)
                    {
                        if (games[i].Id >= 4)
                        {
                            for (int j = 0; j < pl.Count; j++)
                            {
                                for (int k = 0; k < pl[j].Games.Count; k++)
                                {
                                    pl[j].Games.Remove(games[i]);
                                }
                            }
                            players.SaveChanges();
                        }
                    }
                    for (int j = 0; j < games.Count; j++)
                    {
                        if (games[j].Id > 4)
                        {
                            players.Games.Remove(games[j]);
                        }
                    }

                    players.SaveChanges();
                }
            }
        }
Beispiel #18
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);
                }
            }
        }
Beispiel #19
0
        private void TopPlayers_Click(object sender, EventArgs e)
        {
            int wins = 0;
            int pl_count;

            using (PlayersEntities1 players = new PlayersEntities1())
            {
                var player = players.Players.ToList();
                pl_count = player.Count;
                foreach (var pl in player)
                {
                    var games = pl.Games.ToList();
                    foreach (var g in games)
                    {
                        if (pl.Id == g.WinnerId)
                        {
                            wins++;
                        }
                    }
                    try
                    {
                        if (games.Count > 0)
                        {
                            pl.Win_Rate = (decimal)(100 * wins / games.Count);
                            wins        = 0;
                        }
                        else
                        {
                            pl.Win_Rate = (decimal)0;
                        }
                    }
                    catch (Exception e2)
                    {
                        MessageBox.Show(e2.Message);
                    }
                }
                players.SaveChanges();

                if (pl_count >= 3)
                {
                    var           p       = players.Players.OrderByDescending(r => r.Win_Rate).ThenByDescending(r => r.Games.Count).Take(3).ToList();
                    StringBuilder builder = new StringBuilder();


                    foreach (var str in p)
                    {
                        builder.Append("User:"******"  " + "Win rate:" + str.Win_Rate.ToString() + "%" + " Total Games:" + str.Games.Count.ToString()).AppendLine();
                    }


                    //MessageBox.Show(builder.ToString());
                    MetroFramework.MetroMessageBox.Show(this, builder.ToString(), "Top players");
                }
                else
                {
                    var p = players.Players.OrderByDescending(r => r.Win_Rate).ThenByDescending(r => r.Games.Count).Take(pl_count).ToList();


                    StringBuilder builder = new StringBuilder();


                    foreach (var str in p)
                    {
                        builder.Append("User:"******"  " + "Win rate:" + str.Win_Rate.ToString() + "%" + " Total Games:" + str.Games.Count.ToString()).AppendLine();
                    }


                    // MessageBox.Show(builder.ToString());
                    MetroFramework.MetroMessageBox.Show(this, builder.ToString());
                }
            }
        }