Ejemplo n.º 1
0
        private void createPlayerButton_Click(object sender, RoutedEventArgs e)
        {
            Log.i(this, "Create Player Button clicked");

            //Verifying the static validity of a name
            string playerName = playerNameTextBox.Text;

            if (playerName.Length < MIN_PLAYER_NAME_LENGTH)
            {
                errorTextBlock.Text = "Name is too short. Please add " + (MIN_PLAYER_NAME_LENGTH - playerName.Length).ToString() + " characters.";
            }
            else if (playerName.Length > MAX_PLAYER_NAME_LENGTH)
            {
                errorTextBlock.Text = "Name is too long. Please remove " + (playerName.Length - MAX_PLAYER_NAME_LENGTH).ToString() + " characters.";
            }
            else
            {
                Services.Entity.Player player = new Services.Entity.Player()
                {
                    Name = playerName
                };

                using (var db = new Persistence())
                {
                    var players = db.Players;
                    if (players == null)
                    {
                        Log.e(this, "Players in Database returned null");
                        return;
                    }

                    //Verifying collisions
                    if (players.ToList().Exists(p => p.Name.Equals(playerName)))
                    {
                        errorTextBlock.Text = "Sorry, name " + playerName + " is already used.";
                        return;
                    }

                    //Persisting the Player
                    players.Add(player);
                    db.SaveChanges();

                    //After saving changes, the ID in our entity was automatically generated
                    //The new Player will be the selected Player
                    Utility.SettingsSave <int?>("selectedPlayer", player.Id);

                    Log.i(this, "Player " + player.Name + " added");
                }

                //If everything goes well, navigate back to Main Menu
                Frame.Navigate(typeof(MainMenu));
            }
        }
Ejemplo n.º 2
0
        void InitializeAndLoadPlayers()
        {
            using (var db = new Persistence())
            {
                //Creates the Database if it does not exist
                db.Database.EnsureCreated();

                var players = db.Players;

                //Displays all Players
                UpdatePlayers(db.Players);

                //Selects last selected Player
                int?selectedPlayerId = Utility.SettingsLoad <int?>("selectedPlayer");
                if (selectedPlayerId != null)
                {
                    //If we used the lambda expression to get the first player without making sure there is one, game could crash
                    //We fix the problem before announcing the error by erasing the saved ID
                    if (db.Players.Count(p => p.Id == selectedPlayerId) != 1)
                    {
                        Utility.SettingsSave <int?>("selectedPlayer", null);
                        Log.e(this, "Wrong number of players with a requested ID! There are in total " + db.Players.Count().ToString() + " players in DB. Deleting setting choice.");
                        return;
                    }

                    //Instance of the last selected Player
                    Services.Entity.Player player = db.Players.Where(p => p.Id == selectedPlayerId).First();

                    //This is not expected to happen, so we log an error and fix the problem
                    if (player == null)
                    {
                        Utility.SettingsSave <int?>("selectedPlayer", null);
                        Log.e(this, "Player selection restored, but there was no such player in database. Deleting setting choice.");
                        return;
                    }

                    //Selects a Player for the first time
                    PlayersComboBox.SelectedItem = player;
                    Log.i(this, "Restored player selection choice to " + player.Name);
                }
                else
                {
                    //By default, the Main Menu is in state of no Player being selected
                    Log.i(this, "There was no saved ID for selected Player");
                }
            }
            Log.i(this, "Players initialized and loaded");
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Called after a new CheckBox choice of a current Player.
        /// Takes care of persisting the choice.
        /// Also enables or disables the New Game button.
        /// </summary>
        /// <param name="player">new current Player</param>
        void AfterSelectedPlayer(Services.Entity.Player player)
        {
            //Storing the instance of the selected Player to the Singleton controller
            PlayerController.Player = player;

            //If the player is not valid, overwrites the stored selected player value by null, otherwise saves him
            //Enables or disables the New Game button
            if (player == null)
            {
                Utility.SettingsSave <int?>("selectedPlayer", null);
                newGameButton.Style = (Style)Resources["NewGameButtonDisabled"];
                Log.i(this, "Deleted player selection choice");
            }
            else
            {
                Utility.SettingsSave <int?>("selectedPlayer", player.Id);
                newGameButton.Style = (Style)Resources["NewGameButtonEnabled"];
                Log.i(this, "Saved player selection choice");
            }
        }