示例#1
0
 /// <summary>
 /// Called when paused status changes.
 /// </summary>
 /// <param name="isPaused">Paused status.</param>
 private void OnPausedChanged(bool isPaused)
 {
     if (isPaused)
     {
         SessionTimer.Stop();
     }
     else
     {
         SessionTimer.Start();
     }
 }
示例#2
0
 /// <summary>
 /// Called when the recording status changes.
 /// </summary>
 /// <param name="isRecording">Recording status.</param>
 private void OnRecordingChanged(bool isRecording)
 {
     if (!isRecording)
     {
         SessionTimer.Stop();
         Duration = 0;
     }
     else
     {
         SessionTimer.Start();
     }
 }
示例#3
0
    /**
     * This function creates a MidTest Competitive session.
     */
    private void Create__MidTest_Compet_Session(string outputFile)
    {
        SessionTimer  st = new SessionTimer((int)(7.5F * 60 * 1000));         //7.5 minutes
        Configuration c  = new Configuration(Configuration.TWO_PC,
                                             Configuration.THERAPIST_PLAYING,
                                             Configuration.NOT_COLLABORATIVE,
                                             Configuration.NOT_RALLY);
        Session s          = new Session(st, c);
        string  serialized = JsonConvert.SerializeObject(s);
        string  destFile   = outputFile.Replace("###", "MidTest_Compet_Session_2PC");

        GeneralUtils.WriteContentToFile(destFile, serialized);
    }
示例#4
0
    /**
     * This function creates a Pre/Post Test Rally session.
     */
    private void Create__PrePostTest_Rally_Session(string outputFile)
    {
        SessionTimer  st = new SessionTimer((int)(5F * 60 * 1000));         //5 minutes
        Configuration c  = new Configuration(Configuration.ONE_PC,
                                             Configuration.THERAPIST_PLAYING,
                                             Configuration.COLLABORATIVE,
                                             Configuration.RALLY);
        Session s          = new Session(st, c);
        string  serialized = JsonConvert.SerializeObject(s);
        string  destFile   = outputFile.Replace("###", "PrePostTest_Rally_Session_1PC");

        GeneralUtils.WriteContentToFile(destFile, serialized);
    }
示例#5
0
        public SessionInformation Command(string command)
        {
            var commandDetail = CommandResolver.Resolve(command);

            if (commandDetail.CommandType == CommandType.Draw)
            {
                DrawTable();
            }

            if (commandDetail.CommandType == CommandType.Undo)
            {
                if (Checkmate)
                {
                    WriteError("Checkmate !!!");
                }

                if (Checkmate == false && MovementInstructions.Keys.Count != 0)
                {
                    var lastInstraction = GetLastMovement();
                    var lastResult      = MovementInstructions[lastInstraction];

                    if (lastResult.Eated != null)
                    {
                        CurrentPlayer.Stones.Add(lastResult.Eated);
                        PreviousPlayer.Eats.Remove(lastResult.Eated);
                    }

                    lastInstraction.MovingStone.ForceMove(lastInstraction.FromLocation);

                    SetPlayerReturn();

                    MovementInstructions.Remove(lastInstraction);
                    DrawTable();

                    SessionTimer.Restart();
                }
            }

            if (commandDetail.CommandType == CommandType.Stat)
            {
                DrawStatistics();
            }

            if (commandDetail.CommandType == CommandType.Play && Checkmate == false)
            {
                if (commandDetail.IsCorrect == false)
                {
                    WriteError(commandDetail.ReturnMessage);

                    MovementInstructions.Add(default, new MovementResult(false, null, null, null, commandDetail.ReturnMessage));
示例#6
0
    //Ensure only one session timer exists
    private void Awake()
    {
        //Destroy if already exists
        if (SessionTimerinstance && SessionTimerinstance != this)
        {
            Destroy(gameObject);
        }
        else
        {
            SessionTimerinstance = this;
        }

        //Keep alive over multiple scenes
        DontDestroyOnLoad(gameObject);
    }
示例#7
0
        /// <summary>
        /// Lists sold book(s)
        /// </summary>
        /// <param name="adminId"></param>
        /// <returns>List</returns>
        public List <SoldBook> SoldItems(int adminId)
        {
            List <SoldBook> bookList = new List <SoldBook>();

            if (Security.AdminCheck(adminId) && SessionTimer.AdminCheckSessionTimer(adminId) == false)
            {
                using (var db = new EFContext())
                {
                    SessionTimer.AdminSetSessionTimer(adminId);
                    return(db.SoldBooks?.OrderBy(n => n.PurchaseDate).ToList());
                }
            }
            else
            {
                return(bookList);
            }
        }
示例#8
0
        /// <summary>
        /// Lists user(s) matching keyword
        /// </summary>
        /// <param name="adminId"></param>
        /// <param name="keyword"></param>
        /// <returns>List</returns>
        public List <User> FindUser(int adminId, string keyword)
        {
            List <User> userList = new List <User>();

            if (Security.AdminCheck(adminId) && SessionTimer.AdminCheckSessionTimer(adminId) == false)
            {
                using (var db = new EFContext())
                {
                    SessionTimer.AdminSetSessionTimer(adminId);
                    return(db.Users?.Where(x => x.Name.Contains(keyword)).OrderBy(n => n.Name).ToList());
                }
            }
            else
            {
                return(userList);
            }
        }
示例#9
0
        /// <summary>
        /// Lists user(s) in database
        /// </summary>
        /// <param name="adminId"></param>
        /// <returns>List</returns>
        public List <User> ListUsers(int adminId)
        {
            List <User> userList = new List <User>();

            if (Security.AdminCheck(adminId) && SessionTimer.AdminCheckSessionTimer(adminId) == false)
            {
                using (var db = new EFContext())
                {
                    SessionTimer.AdminSetSessionTimer(adminId);
                    return(db.Users?.OrderBy(n => n.Name).ToList());
                }
            }
            else
            {
                return(userList);
            }
        }
        /// <summary>
        /// Adds a new book to database
        /// User object as parameter for handling session timer and ping function
        /// </summary>
        /// <param name="admin"></param>
        public static void AddBookToInventory(User admin)
        {
            bool isBookAdded = false;

            do
            {
                if (SessionTimer.CheckSessionTimer(admin.SessionTimer) == true || admin.SessionTimer == DateTime.MinValue)
                {
                    Console.Clear();
                    GeneralMessage.AdminNotLoggedIn();
                    break;
                }

                Console.Clear();
                var api = new API();
                Console.Write("Enter title:");
                var title = Console.ReadLine();

                Console.Write("Enter author:");
                var author = Console.ReadLine();

                Console.Write("Enter price:");
                int.TryParse(Console.ReadLine(), out var price);

                Console.Write("Enter amount:");
                int.TryParse(Console.ReadLine(), out var amount);

                var result = api.AddBook(admin.Id, title, author, price, amount);
                if (result == false)
                {
                    var input = ErrorMessage.ErrorAbort("adding a new book", "you entered incorrect data");
                    if (input != "")
                    {
                        Console.Clear();
                        break;
                    }
                }
                else
                {
                    SuccessMessage.SuccessWithString("Book added to inventory");
                    isBookAdded = true;
                    UserController.SendPing(admin.Id);
                }
            } while (isBookAdded == false);
        }
示例#11
0
        /// <summary>
        /// Calculates sum of price of sold book(s)
        /// </summary>
        /// <param name="adminId"></param>
        /// <returns>int</returns>
        public int?MoneyEarned(int adminId)
        {
            int?earned = null;

            if (Security.AdminCheck(adminId) && SessionTimer.AdminCheckSessionTimer(adminId) == false)
            {
                using (var db = new EFContext())
                {
                    var books = db.SoldBooks?.ToList();
                    earned = books.Sum(b => b.Price);
                    SessionTimer.AdminSetSessionTimer(adminId);
                    return(earned);
                }
            }
            else
            {
                return(earned);
            }
        }
示例#12
0
        /// <summary>
        ///Sends ping to server
        /// </summary>
        /// <param name="userId"></param>
        /// <returns>string</returns>
        public string Ping(int userId)
        {
            var ping = string.Empty;

            using (var db = new EFContext())
            {
                var user = db.Users?.FirstOrDefault(u => u.Id == userId);
                if (user != null && SessionTimer.CheckSessionTimer(user.SessionTimer) == false)
                {
                    ping = "Pong";
                    user.SessionTimer = DateTime.Now;
                    db.Update(user);
                    db.SaveChanges();
                    Startup.sessionCookie = user;
                }
            }

            return(ping);
        }
示例#13
0
    // Start is called before the first frame update
    private void Awake()
    {
        //DontDestroyOnLoad(gameObject);
        //Check if instance already exists
        if (Instance == null)
        {
            //if not, set instance to this
            Instance = this;
        }

        //If instance already exists and it's not this:
        else if (Instance != this)
        {
            //Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
            Destroy(gameObject);
        }

        //     //Sets this to not be destroyed when reloading scene
        // DontDestroyOnLoad(gameObject);
    }
示例#14
0
        /// <summary>
        /// Sets amount of book
        /// ADDENDUM: Added return value int amount
        /// </summary>
        /// <param name="adminId"></param>
        /// <param name="bookId"></param>
        /// <param name="amount"></param>
        /// <returns>Tuple (bool, int)</returns>
        public (bool isAmountSet, int amount) SetAmount(int adminId, int bookId, int amount)
        {
            bool isAmountSet = false;

            if (Security.AdminCheck(adminId) && SessionTimer.AdminCheckSessionTimer(adminId) == false)
            {
                using (var db = new EFContext())
                {
                    var book = db.Books?.FirstOrDefault(i => i.Id == bookId);
                    if (book != null)
                    {
                        book.Amount = amount;
                        SessionTimer.AdminSetSessionTimer(adminId);
                        db.Update(book);
                        db.SaveChanges();
                        isAmountSet = true;
                    }
                }
            }
            return(isAmountSet, amount);
        }
示例#15
0
        /// <summary>
        /// Login user
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns>int</returns>
        public int?Login(string userName, string password)
        {
            using (var db = new EFContext())
            {
                var user = db.Users?.FirstOrDefault(u => u.Name == userName);
                if (user != null && user.Password == password && user.IsActive)
                {
                    user.SessionTimer = SessionTimer.SetSessionTimer(user.Id);
                    user.LastLogin    = user.SessionTimer;
                    db.Update(user);
                    db.SaveChanges();
                    Startup.sessionCookie = user;

                    return(user.Id);
                }
                else
                {
                    return(null);
                }
            }
        }
        /// <summary>
        /// Creates a new User object.
        /// User object as parameter for handling session timer and ping function.
        /// </summary>
        /// <param name="admin"></param>
        public static void AddUser(User admin)
        {
            bool isUserCreated = false;

            do
            {
                if (SessionTimer.CheckSessionTimer(admin.SessionTimer) == true || admin.SessionTimer == DateTime.MinValue)
                {
                    Console.Clear();
                    GeneralMessage.AdminNotLoggedIn();
                    break;
                }

                Console.Clear();
                var api = new API();
                Console.Write("Enter name: ");
                var name = Console.ReadLine();

                Console.Write("Enter password: "******"adding a user", "or a user with that name already exists");
                    if (input != "")
                    {
                        Console.Clear();
                        break;
                    }
                }
                else
                {
                    SuccessMessage.SuccessWithString("A new user was added with name", $"{name} and password {password}");

                    UserController.SendPing(admin.Id);
                    isUserCreated = true;
                }
            } while (isUserCreated == false);
        }
示例#17
0
        /// <summary>
        /// User buys book
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="bookId"></param>
        /// <returns>bool</returns>
        public bool BuyBook(int userId, int bookId)
        {
            bool isPurchaseSuccessful = false;

            using (var db = new EFContext())
            {
                var user = db.Users?.FirstOrDefault(x => x.Id == userId);

                if (user != null)
                {
                    if (SessionTimer.CheckSessionTimer(user.SessionTimer) == false)
                    {
                        var book = db.Books?.FirstOrDefault(x => x.Id == bookId);

                        if (book != null && book.Amount > 0)
                        {
                            SoldBook soldBook = new SoldBook();
                            soldBook.Title        = book.Title;
                            soldBook.Author       = book.Author;
                            soldBook.CategoryId   = book.CategoryId;
                            soldBook.Price        = book.Price;
                            soldBook.PurchaseDate = DateTime.Now;
                            soldBook.UserId       = user.Id;

                            book.Amount--;

                            db.Update(book);
                            db.Update(soldBook);
                            isPurchaseSuccessful  = true;
                            user.SessionTimer     = SessionTimer.SetSessionTimer(user.Id);
                            Startup.sessionCookie = user;
                            db.Update(user);
                            db.SaveChanges();
                        }
                    }
                }
            }

            return(isPurchaseSuccessful);
        }
        /// <summary>
        /// Adds book to category
        /// User object as parameter for handling session timer and ping function
        /// </summary>
        /// <param name="admin"></param>
        public static void AddBookToCategory(User admin)
        {
            bool isBookAdded = false;

            do
            {
                if (SessionTimer.CheckSessionTimer(admin.SessionTimer) == true || admin.SessionTimer == DateTime.MinValue)
                {
                    Console.Clear();
                    GeneralMessage.AdminNotLoggedIn();
                    break;
                }

                Console.Clear();
                var api = new API();

                Console.Write("Enter book Id:");
                int.TryParse(Console.ReadLine(), out int bookId);

                Console.Write("Enter category Id:");
                int.TryParse(Console.ReadLine(), out int categoryId);

                var result = api.AddBookToCategory(admin.Id, bookId, categoryId);
                if (result == false)
                {
                    var input = ErrorMessage.ErrorAbort("adding a book to a category", "you entered incorrect data");
                    if (input != "")
                    {
                        Console.Clear();
                        break;
                    }
                }
                else
                {
                    SuccessMessage.SuccessWithString("Book assigned to category");
                    isBookAdded = true;
                    UserController.SendPing(admin.Id);
                }
            } while (isBookAdded == false);
        }
        /// <summary>
        /// Sets Book objects Amount
        /// User object as parameter for handling session timer and ping function
        /// </summary>
        /// <param name="admin"></param>
        public static void SetAmount(User admin)
        {
            bool isAmountSet = false;

            do
            {
                if (SessionTimer.CheckSessionTimer(admin.SessionTimer) == true || admin.SessionTimer == DateTime.MinValue)
                {
                    Console.Clear();
                    GeneralMessage.AdminNotLoggedIn();
                    break;
                }

                Console.Clear();
                var api = new API();
                Console.Write("Enter book Id:");
                int.TryParse(Console.ReadLine(), out var bookId);

                Console.Write("Enter amount:");
                int.TryParse(Console.ReadLine(), out var amount);

                var result = api.SetAmount(admin.Id, bookId, amount);
                if (result.isAmountSet == false)
                {
                    var input = ErrorMessage.ErrorAbort("setting the amount", "you entered incorrect data");
                    if (input != "")
                    {
                        Console.Clear();
                        break;
                    }
                }
                else
                {
                    SuccessMessage.SuccessWithInt("Amount set to", result.amount);
                    isAmountSet = true;
                    UserController.SendPing(admin.Id);
                }
            } while (isAmountSet == false);
        }
示例#20
0
        /// <summary>
        /// Gets customer who bought most books
        /// </summary>
        /// <param name="adminId"></param>
        /// <returns>string</returns>
        public User BestCostumer(int adminId)
        {
            User bestCostumer = new User();

            if (Security.AdminCheck(adminId) && SessionTimer.AdminCheckSessionTimer(adminId) == false)
            {
                using (var db = new EFContext())
                {
                    //Solution found at:https://stackoverflow.com/questions/2655759/how-to-get-the-most-common-value-in-an-int-array-c

                    var costumer = db.SoldBooks.GroupBy(u => u.UserId).OrderByDescending(u => u.Count()).Select(u => u.Key).First();

                    bestCostumer = db.Users?.FirstOrDefault(u => u.Id == costumer);
                    SessionTimer.AdminSetSessionTimer(adminId);
                    return(bestCostumer);
                }
            }
            else
            {
                return(bestCostumer);
            }
        }
        /// <summary>
        /// Retrieves all Users matching search term based on User.Name
        /// User object as parameter for handling session timer and ping function
        /// </summary>
        /// <param name="admin"></param>
        public static List <List <object> > FindUser(User admin)
        {
            List <List <object> > userListData = new List <List <object> >();
            bool isUsersFound = false;

            do
            {
                if (SessionTimer.CheckSessionTimer(admin.SessionTimer) == true || admin.SessionTimer == DateTime.MinValue)
                {
                    Console.Clear();
                    GeneralMessage.AdminNotLoggedIn();
                    break;
                }

                Console.Clear();
                var api = new API();
                Console.Write("Enter search term: ");
                var keyword = Console.ReadLine();
                var result  = api.FindUser(admin.Id, keyword);
                if (result.Count < 1)
                {
                    var input = ErrorMessage.ErrorAbort("retrieving users", "the database is corrupt/empty, or your search term gave no matches");
                    if (input != "")
                    {
                        Console.Clear();
                        break;
                    }
                }
                else
                {
                    userListData = UserConverters.UserConverter(result);

                    UserController.SendPing(admin.Id);
                    isUsersFound = true;
                }
            } while (isUsersFound == false);
            return(UserView.UserListReader(userListData));
        }
示例#22
0
        /// <summary>
        /// Updates category
        /// </summary>
        /// <param name="adminId"></param>
        /// <param name="categoryId"></param>
        /// <param name="name"></param>
        /// <returns>bool</returns>
        public bool UpdateCategory(int adminId, int categoryId, string name)
        {
            bool isCategoryUpdated = false;

            if (Security.AdminCheck(adminId) && SessionTimer.AdminCheckSessionTimer(adminId) == false)
            {
                using (var db = new EFContext())
                {
                    var category = db.BookCategories?.FirstOrDefault(c => c.Id == categoryId);

                    if (category != null)
                    {
                        category.Name = name;
                        SessionTimer.AdminSetSessionTimer(adminId);
                        db.Update(category);
                        db.SaveChanges();
                        isCategoryUpdated = true;
                    }
                }
            }

            return(isCategoryUpdated);
        }
        /// <summary>
        /// Adds a new category to database
        /// User object as parameter for handling session timer and ping function
        /// </summary>
        /// <param name="admin"></param>
        public static void AddCategory(User admin)
        {
            bool isCategoryCreated = false;

            do
            {
                if (SessionTimer.CheckSessionTimer(admin.SessionTimer) == true || admin.SessionTimer == DateTime.MinValue)
                {
                    Console.Clear();
                    GeneralMessage.AdminNotLoggedIn();
                    break;
                }

                Console.Clear();
                var api = new API();

                Console.Write("Enter name:");
                var categoryName = Console.ReadLine();

                var result = api.AddCategory(admin.Id, categoryName);
                if (result == false)
                {
                    var input = ErrorMessage.ErrorAbort("creating a new category", "there already is a category with that name");
                    if (input != "")
                    {
                        Console.Clear();
                        break;
                    }
                }
                else
                {
                    SuccessMessage.SuccessWithString("A new category was created with the name", $"{categoryName}");
                    isCategoryCreated = true;
                    UserController.SendPing(admin.Id);
                }
            } while (isCategoryCreated == false);
        }
示例#24
0
        /// <summary>
        /// Updates properties of book
        /// </summary>
        /// <param name="adminId"></param>
        /// <param name="id"></param>
        /// <param name="title"></param>
        /// <param name="author"></param>
        /// <param name="price"></param>
        /// <returns>bool</returns>
        public bool UpdateBook(int adminId, int id, string title, string author, int price)
        {
            bool isBookUpdated = false;

            if (Security.AdminCheck(adminId) && SessionTimer.AdminCheckSessionTimer(adminId) == false)
            {
                using (var db = new EFContext())
                {
                    var book = db.Books?.FirstOrDefault(x => x.Id == id);
                    if (book != null)
                    {
                        book.Title  = title;
                        book.Author = author;
                        book.Price  = price;
                        SessionTimer.AdminSetSessionTimer(adminId);
                        db.Update(book);
                        db.SaveChanges();
                        isBookUpdated = true;
                    }
                }
            }

            return(isBookUpdated);
        }
        /// <summary>
        /// Activates a user.
        /// User object as parameter for handling session timer and ping function.
        /// </summary>
        /// <param name="admin"></param>
        public static void ActivateUser(User admin)
        {
            bool isUserActivated = false;

            do
            {
                if (SessionTimer.CheckSessionTimer(admin.SessionTimer) == true || admin.SessionTimer == DateTime.MinValue)
                {
                    Console.Clear();
                    GeneralMessage.AdminNotLoggedIn();
                    break;
                }

                Console.Clear();
                var api = new API();
                Console.Write("Enter user Id: ");
                int.TryParse(Console.ReadLine(), out int userId);
                var result = api.ActivateUser(admin.Id, userId);
                if (result == false)
                {
                    var input = ErrorMessage.ErrorAbort("activating a user", "you entered incorrect data");
                    if (input != "")
                    {
                        Console.Clear();
                        break;
                    }
                }
                else
                {
                    SuccessMessage.SuccessWithString("User is now activated");

                    UserController.SendPing(admin.Id);
                    isUserActivated = true;
                }
            } while (isUserActivated == false);
        }
示例#26
0
        public static void BuyBook(User user)
        {
            bool isBookPurchased = false;
            var  api             = new API();

            do
            {
                if (SessionTimer.CheckSessionTimer(user.SessionTimer) == true || user.SessionTimer == DateTime.MinValue)
                {
                    Console.Clear();
                    BuyBookMessage.UserNotLoggedIn();
                    break;
                }
                else
                {
                    Console.Clear();
                    Console.Write("Enter the Id for the book you want to buy: ");
                    int.TryParse(Console.ReadLine(), out var bookId);
                    var result = api.BuyBook(user.Id, bookId);
                    if (result == false)
                    {
                        var input = BuyBookMessage.Error();
                        if (input != "")
                        {
                            Console.Clear();
                            break;
                        }
                    }
                    else
                    {
                        BuyBookMessage.Success();
                        isBookPurchased = true;
                    }
                }
            } while (isBookPurchased == false);
        }
        /// <summary>
        /// Deletes one(1) Book object from database
        /// User object as parameter for handling session timer and ping function
        /// </summary>
        /// <param name="admin"></param>
        public static void DeleteBook(User admin)
        {
            bool isBookDeleted = false;

            do
            {
                if (SessionTimer.CheckSessionTimer(admin.SessionTimer) == true || admin.SessionTimer == DateTime.MinValue)
                {
                    Console.Clear();
                    GeneralMessage.AdminNotLoggedIn();
                    break;
                }

                Console.Clear();
                var api = new API();
                Console.Write("Enter book Id: ");
                int.TryParse(Console.ReadLine(), out var bookId);

                var result = api.DeleteBook(admin.Id, bookId);
                if (result == false)
                {
                    var input = ErrorMessage.ErrorAbort("deleting the book", "you entered incorrect data");
                    if (input != "")
                    {
                        Console.Clear();
                        break;
                    }
                }
                else
                {
                    SuccessMessage.SuccessWithString("Book deleted");
                    isBookDeleted = true;
                    UserController.SendPing(admin.Id);
                }
            } while (isBookDeleted == false);
        }
示例#28
0
        /// <summary>
        /// Inactivates user
        /// </summary>
        /// <param name="adminId"></param>
        /// <param name="userId"></param>
        /// <returns>bool</returns>
        public bool InactivateUser(int adminId, int userId)
        {
            bool isInactivated = false;

            if (Security.AdminCheck(adminId) && SessionTimer.AdminCheckSessionTimer(adminId) == false)
            {
                using (var db = new EFContext())
                {
                    var user = db.Users?.FirstOrDefault(u => u.Id == userId);

                    if (user != null)
                    {
                        user.IsActive = false;

                        SessionTimer.AdminSetSessionTimer(adminId);
                        db.Update(user);
                        db.SaveChanges();

                        isInactivated = true;
                    }
                }
            }
            return(isInactivated);
        }
示例#29
0
        public void SetUp()
        {
            // Arrange
            _MockSystemEvents = new Mock<ISystemEvents>();
            _MockPostponeBreakPeriod = new Mock<IPeriod>();
            _MockPeriodBetweenBreaks = new Mock<IPeriod>();
            _MockPeriodFactory = new Mock<IPeriodFactory>();
            _MockPeriodFactory.Setup(m => m.PeriodBetweenBreaks()).Returns(_MockPeriodBetweenBreaks.Object);
            _MockPeriodFactory.Setup(m => m.PostponeBreakPeriod()).Returns(_MockPostponeBreakPeriod.Object);

            _SessionTimer = new SessionTimer(_MockSystemEvents.Object, _MockPeriodFactory.Object);
        }
示例#30
0
        /// <summary>
        /// Main menu method. This is the center for the application, program flow starts and ends here.
        /// User object as parameter for handling session timer and ping function.
        /// Prints different menu's depending on user being logged in/logged out/have administrator privileges
        /// </summary>
        /// <param name="user"></param>
        public static void MainMenu(User user)
        {
            while (isMainMenuRunning)
            {
                UserController.SendPing(user.Id);
                user = Startup.sessionCookie;
                Console.Clear();
                ASCII.MainMenuASCII();

                //If user is logged out
                if (SessionTimer.CheckSessionTimer(user.SessionTimer) == true)
                {
                    Console.WriteLine("[1] Browse books");
                    Console.WriteLine("[2] Browse categories");
                    Console.WriteLine("[3] Buy book\n\n");
                    Console.WriteLine("[4] Register");
                    Console.WriteLine("[5] Login");
                    Console.WriteLine("[6] Quit application");

                    int.TryParse(Console.ReadLine(), out var mainMenuLoggedOutInput);

                    switch (mainMenuLoggedOutInput)
                    {
                    case 1:
                        BookMenuController.BookMenu();
                        break;

                    case 2:
                        CategoryMenuController.CategoryMenu();
                        break;

                    case 3:
                        UserController.BuyBook(user);
                        break;

                    case 4:
                        UserController.RegisterNewUser();
                        break;

                    case 5:
                        UserController.LogInUser();
                        break;

                    case 6:
                        isMainMenuRunning = false;
                        break;
                    }
                }

                //If user is logged in
                else if (SessionTimer.CheckSessionTimer(user.SessionTimer) == false && user.IsAdmin == false)
                {
                    Console.WriteLine("[1] Browse books");
                    Console.WriteLine("[2] Browse categories");
                    Console.WriteLine("[3] Buy book\n\n");
                    Console.WriteLine("[4] Logout");
                    Console.WriteLine("[5] Quit application");

                    int.TryParse(Console.ReadLine(), out var mainMenuLoggedInInput);

                    switch (mainMenuLoggedInInput)
                    {
                    case 1:
                        BookMenuController.BookMenu();
                        break;

                    case 2:
                        CategoryMenuController.CategoryMenu();
                        break;

                    case 3:
                        UserController.BuyBook(user);
                        break;

                    case 4:
                        UserController.LogOutUser(user);

                        break;

                    case 5:
                        isMainMenuRunning = false;
                        break;
                    }
                }

                //If user is logged in and is admin
                else if (SessionTimer.CheckSessionTimer(user.SessionTimer) == false && user.IsAdmin == true)
                {
                    Console.WriteLine("[1] Browse books");
                    Console.WriteLine("[2] Browse categories");
                    Console.WriteLine("[3] Buy book\n\n");
                    Console.WriteLine("[4] Administrator menu");
                    Console.WriteLine("[5] Logout");
                    Console.WriteLine("[6] Quit application");

                    int.TryParse(Console.ReadLine(), out var mainMenuAdminInput);

                    switch (mainMenuAdminInput)
                    {
                    case 1:
                        BookMenuController.BookMenu();
                        break;

                    case 2:
                        CategoryMenuController.CategoryMenu();
                        break;

                    case 3:
                        UserController.BuyBook(user);
                        break;

                    case 4:
                        AdminMenuController.AdminMainMenu(user);
                        break;

                    case 5:
                        UserController.LogOutUser(user);
                        break;

                    case 6:
                        isMainMenuRunning = false;
                        break;
                    }
                }
            }
        }
示例#31
0
        public virtual void Start()
        {
            Logger?.SetFileName(_logFileName);
            CurrentPlayer = Players[CurrentIndexer];
            NextPlayer    = Players[CurrentIndexer == 0 ? 1 : 0];

            DrawTable();

            string command = string.Empty;

            if (StartingCommands.Count != 0)
            {
                command = StartingCommands[0];
            }



            return;

            while (command != "quit" && command != "exit")
            {
                if (string.IsNullOrEmpty(command))
                {
                    ShowInfo();
                }
                else
                {
                    string[] commandArr = command.Split(' ');
                    if (commandArr.Length == 2 && commandArr[0].Length == 2 && commandArr[1].Length == 2)
                    {
                        command = $"play {command}";
                    }

                    if (command == "draw")
                    {
                        DrawTable();
                        if (Checkmate)
                        {
                            WriteError("Checkmate !!!");
                        }
                    }

                    if (command == "undo")
                    {
                        if (Checkmate)
                        {
                            WriteError("Checkmate !!!");
                        }

                        if (Checkmate == false && MovementInstructions.Keys.Count != 0)
                        {
                            var lastInstraction = GetLastMovement();
                            var lastResult      = MovementInstructions[lastInstraction];

                            if (lastResult.Eated != null)
                            {
                                CurrentPlayer.Stones.Add(lastResult.Eated);
                                PreviousPlayer.Eats.Remove(lastResult.Eated);
                            }

                            lastInstraction.MovingStone.ForceMove(lastInstraction.FromLocation);

                            SetPlayerReturn();

                            MovementInstructions.Remove(lastInstraction);
                            DrawTable();

                            SessionTimer.Restart();
                        }
                    }

                    if (Checkmate == false && command.StartsWith("play"))
                    {
                        var commandDetail = CommandResolver.Resolve(command);
                        if (commandDetail.IsCorrect == false)
                        {
                            WriteError(commandDetail.ReturnMessage);
                            command = WaitCommand();
                            continue;
                        }

                        var fromLocation = Table.GetLocation(commandDetail.From_X, commandDetail.From_Y);

                        var stone          = Table.Stones.GetFromLocation(fromLocation);
                        var targetLocation = Table.GetLocation(commandDetail.To_X, commandDetail.To_Y);

                        if (targetLocation == null)
                        {
                            WriteError("Location is not found!");
                            command = WaitCommand();
                            continue;
                        }

                        if (stone == null)
                        {
                            WriteError("Stone is not found at given location!");
                            command = WaitCommand();
                            continue;
                        }

                        var instraction = Instruction.CreateOne(stone, targetLocation, this, command);

                        var result = instraction.TryDo();
                        if (result.IsOK)
                        {
                            MovementInstructions.Add(instraction, result);

                            if (result.Eated != null)
                            {
                                CurrentPlayer.Eat(result.Eated);
                                Table.Stones.Remove(result.Eated);
                            }

                            if (result.CheckRemoved)
                            {
                                Check = false;
                            }

                            WriteMessage(result.Message);
                            IsCheck(stone);

                            SessionTimer.Stop();
                            CurrentPlayer.Duration += SessionTimer.ElapsedMilliseconds;

                            if (Checkmate)
                            {
                                DrawTable();
                                WriteEmpty();
                                WriteError("Checkmate !!!");
                            }
                            else
                            {
                                SetPlayerReturn();
                                DrawTable();

                                SessionTimer.Restart();
                            }
                        }
                        else
                        {
                            WriteError(result.Message);
                        }
                    }

                    if (command == "stat")
                    {
                        DrawStatistics();
                    }
                }

                if (StartingCommands.Count == 0)
                {
                    var lastMovementInstaction = GetLastMovement();
                    if (lastMovementInstaction != null)
                    {
                        WriteLastCommand(lastMovementInstaction.Log);
                    }

                    command = WaitCommand();
                }
                else
                {
                    var current = StartingCommands.First();
                    StartingCommands.Remove(current);
                    if (StartingCommands.Count != 0)
                    {
                        command = StartingCommands[0];
                    }
                    else
                    {
                        var lastMovementInstaction = GetLastMovement();
                        if (lastMovementInstaction != null)
                        {
                            WriteLastCommand(lastMovementInstaction.Log);
                        }
                        command = WaitCommand();
                    }
                }
            }

            Logger?.SaveInstructions(MovementInstructions.Where(m => m.Value.IsOK).Select(m => m.Key));
        }