Example #1
0
        static IQService()
        {
            onlineUsers = new List<User>();
            queue = new List<string>();
            rooms = new List<Room>();
            database = new IQDatabase();

            Thread kick = new Thread(new ThreadStart(kickOffline));
            kick.IsBackground = true;
            kick.Start();

            Thread roomWatcher = new Thread(new ThreadStart(closeRooms));
            roomWatcher.IsBackground = true;
            roomWatcher.Start();

            Logger.log(Errorlevel.INFO, "Initialized successful!");
        }
Example #2
0
        public bool answerQuestion(string user, int id)
        {
            bool ret = getRoomByUserName(user).Answer(getUserByUserName(user), id);
            using (IQDatabase database = new IQDatabase())
            {
                try
                {
                    dbUserSet u = database.dbUserSet.First(x => x.name.Equals(user));
                    u.questions++;
                    if (ret) u.goodanswers++;
                    database.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logger.log(Errorlevel.WARN, ex.Message + Environment.NewLine + ex.StackTrace);
                }
            }

            return ret;
        }
Example #3
0
        private void turn(object sender, DoWorkEventArgs e)
        {
            int turn = 0;
            do
            {
                actualPlayer = null;
                actualCell = null;
                using (IQDatabase database = new IQDatabase())
                {
                    question = new Question(database.dbQuestionSet.OrderBy(r => Guid.NewGuid()).First());
                }
                selectNextMove();

                timeleft = turnTimeout / 1000;
                Stopwatch stopper = new Stopwatch();
                stopper.Start();
                // Lépésre vár
                while (stopper.ElapsedMilliseconds < turnTimeout && actualCell == null)
                {
                    lock (lockObject)
                    {
                        timeleft = (turnTimeout / 1000) - (int)(stopper.ElapsedMilliseconds / 1000);
                    }
                    Thread.Sleep(IQService.Pingperiod);
                }

                if (actualCell != null)
                {

                    actualCell.Owner.State = States.ANSWER;

                    stopper.Restart();
                    while (stopper.ElapsedMilliseconds < turnTimeout &&
                        (actualPlayer.State == States.ANSWER ||
                        (actualCell.Owner.State == States.ANSWER && actualCell.Owner.Name != null)))
                    {
                        lock (lockObject)
                        {
                            timeleft = (turnTimeout / 1000) - (int)(stopper.ElapsedMilliseconds / 1000);
                        }
                        Thread.Sleep(IQService.Pingperiod);
                    }
                    timeleft = 0;

                    // Lejárt az idő: a válasz rossz
                    if (actualPlayer.AnswerResult == null)
                        actualPlayer.AnswerResult = new AnswerResult() { Answer = false, Time = DateTime.Now };
                    if (actualCell.Owner.AnswerResult == null)
                        actualCell.Owner.AnswerResult = new AnswerResult() { Answer = false, Time = DateTime.Now };

                    if (actualPlayer.AnswerResult.Answer == false || actualPlayer.AnswerResult.Time > actualCell.Owner.AnswerResult.Time)
                    {
                        // Védő nyert
                        actualCell.Owner.Point += 10;
                        actualPlayer.Point -= 10;
                    }
                    else
                    {
                        // Támadó nyert
                        actualCell.Owner.Point -= 10;
                        actualPlayer.Point += 10;
                        actualCell.Owner = actualPlayer;
                    }

                    actualCell.Owner.State = States.IDLE;
                    actualCell.Owner.AnswerResult = null;
                }
                actualPlayer.State = States.IDLE;
                actualPlayer.AnswerResult = null;
                turn++;
            } while (turn < maxturns);

            // Játék vége
            foreach (User u in Players)
            {
                u.State = States.FINISHED;
            }
            Thread.Sleep(IQService.Timeout);
            finished = true;
        }
Example #4
0
        public bool Login(string user, string pass)
        {
            // debug, login előtt regisztrál
            if (Debug)
            {
                using (IQDatabase database = new IQDatabase())
                {
                    try
                    {
                        database.dbUserSet.Add(new dbUserSet()
                        {
                            name = user,
                            pass = pass,
                            email = "",
                            goodanswers = 0,
                            played = 0,
                            questions = 0,
                            win = 0
                        });
                        database.SaveChanges();

                    }
                    catch (Exception) { }
                }
            }

            bool userfound = false;
            try
            {
                using (IQDatabase database = new IQDatabase())
                {
                    userfound = (from users in database.dbUserSet
                                 where users.name.Equals(user) && users.pass.Equals(pass)
                                 select users).Count().Equals(1);
                }
            }
            catch (Exception)
            {
            }

            if (onlineUsers.Exists(x => x.Name.Equals(user)))
            {
                Logger.log(Errorlevel.INFO, user + " tried to log in twice");
                return false;
            }
            else if (userfound)
            {
                User login = new User(user);
                onlineUsers.Add(login);

                Logger.log(Errorlevel.INFO, user + " logged in");
                return true;
            }
            else
            {
                Logger.log(Errorlevel.INFO, (String.IsNullOrEmpty(user) ? "NULL OR EMPTY" : user) + " failed to log in");
                return false;
            }
        }
Example #5
0
        private void stop(object sender, RunWorkerCompletedEventArgs e)
        {
            using (IQDatabase database = new IQDatabase())
            {
                try
                {
                    Statistic s = new Statistic(this);
                    string name = s.Users[0].Name;
                    database.dbUserSet.First(x => x.name.Equals(name)).win++;

                    foreach (User u in s.Users)
                    {
                        name = u.Name;
                        database.dbUserSet.First(x => x.name.Equals(name)).played++;
                    }
                    database.SaveChanges();

                }
                catch (Exception ex)
                {
                    Logger.log(Errorlevel.WARN, ex.Message + Environment.NewLine + ex.StackTrace);
                }
            }
            /*
            using (IQDatabase database = new IQDatabase())
            {
                try
                {

                    IQueryable<dbUserSet> dbul = database.dbUserSet.Where(x =>
                        x.name.Equals(s.Users[0].Name) ||
                        x.name.Equals(s.Users[1].Name) ||
                        x.name.Equals(s.Users[2].Name) ||
                        x.name.Equals(s.Users[3].Name)
                        );
                    foreach (dbUserSet u in dbul)
                    {
                        if (u.name.Equals(s.Users[0].Name))
                        {
                            u.win++;
                        }
                        u.played++;
                    }
                    database.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logger.log(Errorlevel.ERROR, ex.Message + Environment.NewLine + ex.StackTrace);
                }
            }*/
        }