Пример #1
0
        // This function is for when a new player logs into the game
        // It will check if the lobby is full (6 players), if it's full they can't enter
        // It will check if the username is already taken, if so they're denied also
        // It will then find the connection id of the client and save that to a local (server) list together with some other info (name, score, etc)

        public async Task LoginUser(User user)
        {
            try
            {
                // Check if username is taken
                bool isUsernameAvailable = UsersController.IsNameAvailable(user.Name);

                if (UsersController.AmountOfUsers() >= 6 || UsersController.isGameStarting())
                {
                    await Clients.Caller.SendAsync("LobbyFull");

                    return;
                }

                if (!isUsernameAvailable)
                {
                    await Clients.Caller.SendAsync("UsernameChecked", isUsernameAvailable);

                    return;
                }

                // Add the hub connectionId to our user
                user.ConnectionId = Context.ConnectionId;

                // Add this new User to the list in UsersController
                // Add Name, Token, ClientId, ReadyState = false, Score = 0
                UsersController.AddUser(user);

                // Fetch the updated list of users
                var users = UsersController.GetAllUsers();

                // Give all players a updated list of the lobby
                await Clients.Others.SendAsync("UpdateLobby", users);

                await Clients.Caller.SendAsync("UsernameChecked", isUsernameAvailable);

                // This way you send a message to specific user based on id
                //await Clients.Client(user.ConnectionId).SendAsync("UsernameChecked", isUsernameAvailable);
            }
            catch
            {
                Console.WriteLine("Something went wrong logging in user");
            }
        }
        private void saveCalorieLimitBtnStart_Click(object sender, EventArgs e)
        {
            if (calorieLimitTextBoxStart.Text == null || calorieLimitTextBoxStart.Text == "")
            {
                MessageBox.Show("Ne možete spremiti dnevni kalorijski unos jer ga niste izračunali! Ispunite formu, izračunajte kalorijski unos i pokušajte ponovno spremiti.");
                return;
            }

            _userController.AddUser(Double.Parse(calorieLimitTextBoxStart.Text, CultureInfo.InvariantCulture));

            IFoodDatabaseController _foodDatabaseController = new FoodDatabaseController(_formsFactory, _repositoryFactory);
            IFoodController         _foodController         = new FoodController(_formsFactory, _repositoryFactory);
            User user            = _userController.GetUser();
            var  dailyIntakeForm = new frmDailyIntakeWindow(_foodDatabaseController, _foodController, _repositoryFactory, user);

            this.Close();
            th = new Thread(delegate()
            {
                RunOnNextForm(dailyIntakeForm);
            });
            th.SetApartmentState(ApartmentState.STA);
            th.Start();
        }