Пример #1
0
        public bool EnterClientToDB(Clients client)
        {
            bool value;

            using (MineSweeperDB1Entities ctx = new MineSweeperDB1Entities())
            {
                var cli = from c in ctx.Clients
                          where c.UserName == client.UserName
                          select c.UserName;

                if (cli.ToList().Count == 0)
                {
                    //enter username+password to the db as a new client
                    ctx.Clients.Add(client);
                    ctx.SaveChanges();
                    value = true;
                }
                //if the username already exists, put a message
                else
                {
                    value = false;
                }
            }
            return(value);
        }
Пример #2
0
        public string[][] ShowAllGames()
        {
            string[][] GList = null;
            using (MineSweeperDB1Entities ctx = new MineSweeperDB1Entities())
            {
                GList = new string[ctx.Games.Count()][];

                for (int i = 0; i < ctx.Games.Count(); i++)
                {
                    GList[i] = new string[4];
                    for (int j = 0; j < 4; j++)
                    {
                        string isTie = ctx.Games.ToArray()[i].tie ? "yes" : "no";
                        if (j == 0)
                        {
                            GList[i][j] = ctx.Games.ToArray()[i].EndingDate.ToString();
                        }
                        if (j == 1)
                        {
                            GList[i][j] = ctx.Games.ToArray()[i].Winner;
                        }
                        if (j == 2)
                        {
                            GList[i][j] = ctx.Games.ToArray()[i].Loser;
                        }
                        if (j == 3)
                        {
                            GList[i][j] = isTie;
                        }
                    }
                }
            }
            return(GList);
        }
Пример #3
0
        public void EnterGameToDB(Games game)
        {
            using (MineSweeperDB1Entities ctx = new MineSweeperDB1Entities())
            {
                var query = (from g in ctx.Games
                             select g.Id).Max();

                game.Id = query + 1;
                ctx.Games.Add(game);
                num_of_games = game.Id;
                ctx.SaveChanges();
            }
            GameDisconnect(game);
        }
Пример #4
0
 public bool SearchUsernamePasswordInDB(Clients client)
 {
     using (MineSweeperDB1Entities ctx = new MineSweeperDB1Entities())
     {
         var cli = from c in ctx.Clients
                   where c.UserName == client.UserName &&
                   c.Password == client.Password
                   select c.UserName;
         //if the client exists in the database
         if (cli.ToList().Count != 0)
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
Пример #5
0
        public string[][] ShowAllPlayers()
        {
            string[][] clients = null;
            using (MineSweeperDB1Entities ctx = new MineSweeperDB1Entities())
            {
                clients = new string[ctx.Clients.Count()][];
                for (int i = 0; i < ctx.Clients.Count(); i++)
                {
                    Clients c = ctx.Clients.ToArray()[i];
                    //the games c won
                    var gamesCWon = (from g in ctx.Games
                                     where g.Winner == c.UserName
                                     select g).ToArray();
                    //the games c lost
                    var gamesCLost = (from g in ctx.Games
                                      where g.Loser == c.UserName
                                      select g).ToArray();
                    //the games where there was a tie and c participated
                    var gamesTie = from g in ctx.Games
                                   where g.tie == true &&
                                   (g.Loser == c.UserName ||
                                    g.Winner == c.UserName)
                                   select g;

                    double gamesPlayed = gamesCWon.Count() + gamesCLost.Count();
                    double percent;
                    if (gamesPlayed == 0)
                    {
                        percent = 0;
                    }
                    else
                    {
                        percent = (gamesCWon.Count() / gamesPlayed) * 100;
                    }
                    clients[i] = new string[6];
                    for (int j = 0; j < 6; j++)
                    {
                        if (j == 0)
                        {
                            clients[i][j] = ctx.Clients.ToArray()[i].UserName;
                        }
                        if (j == 1)
                        {
                            clients[i][j] = gamesPlayed.ToString();
                        }
                        if (j == 2)
                        {
                            clients[i][j] = gamesCWon.Count().ToString();
                        }
                        if (j == 3)
                        {
                            clients[i][j] = gamesCLost.Count().ToString();
                        }
                        if (j == 4)
                        {
                            clients[i][j] = gamesTie.Count().ToString();
                        }
                        if (j == 5)
                        {
                            clients[i][j] = percent.ToString();
                        }
                    }
                }
            }
            return(clients);
        }