Exemplo n.º 1
0
        //useful stuff https://www.youtube.com/watch?v=_PCBTiXL884

        public void SaveCharacter()
        {
            int xp = 0;

            switch (ClassType)
            {
            case ClassType.Fighter:
            {
                Fighter newCharacter = new Fighter(characterName, characterRace, originalAttributes, HitPoints, xp);
                newCharacter.Save();
                break;
            }

            case ClassType.Thief:
            {
                Thief newCharacter = new Thief(characterName, characterRace, originalAttributes, HitPoints, xp);
                newCharacter.Save();
                break;
            }

            case ClassType.Cleric:
            {
                Cleric newCharacter = new Cleric(characterName, characterRace, originalAttributes, HitPoints, xp);
                newCharacter.Save();
                break;
            }

            case ClassType.MagicUser:
            {
                MagicUser newCharacter = new MagicUser(characterName, characterRace, originalAttributes, HitPoints, xp);
                newCharacter.Save();
                break;
            }
            }
        }
        public List <Character> LoadCharacters()
        {
            string query = "SELECT * FROM character";

            using (connection = new SqlConnection(ConnectionString))
                using (SqlCommand command = new SqlCommand(query, connection))
                    using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                    {
                        DataTable characterTable = new DataTable();
                        adapter.Fill(characterTable);

                        List <Character> characters = new List <Character>();

                        foreach (DataRow row in characterTable.Rows)
                        {
                            int       characterId            = row.Field <int>("Id");
                            string    name                   = row.Field <string>("Name");
                            ClassType characterClass         = (ClassType)row.Field <int>("Class");
                            Race      characterRace          = (Race)row.Field <int>("Race");
                            Dictionary <Attribute, int> dict = new Dictionary <Attribute, int>();
                            dict.Add(Attribute.Strength, row.Field <int>("Strength"));
                            dict.Add(Attribute.Dexterity, row.Field <int>("Dexterity"));
                            dict.Add(Attribute.Intelligence, row.Field <int>("Intelligence"));
                            dict.Add(Attribute.Wisdom, row.Field <int>("Wisdom"));
                            dict.Add(Attribute.Constitution, row.Field <int>("Constitution"));
                            dict.Add(Attribute.Charisma, row.Field <int>("Charisma"));
                            int HitPoints           = row.Field <int>("HitPoints");
                            int xp                  = row.Field <int>("XP");
                            int equippedToBody      = row.Field <int>("EquippedToBody");
                            int equippedToRightHand = row.Field <int>("EquippedToRightHand");
                            int equippedToLeftHand  = row.Field <int>("EquippedToLeftHand");

                            EquipmentItems bodyEquipment      = null;
                            EquipmentItems rightHandEquipment = null;
                            EquipmentItems leftHandEquipment  = null;

                            if (equippedToBody != 0)
                            {
                                bodyEquipment = LoadEquipment(equippedToBody);
                            }

                            if (equippedToRightHand != 0)
                            {
                                rightHandEquipment = LoadEquipment(equippedToRightHand);
                            }

                            if (equippedToLeftHand != 0)
                            {
                                leftHandEquipment = LoadEquipment(equippedToLeftHand);
                            }

                            Character character = null;

                            switch (characterClass)
                            {
                            case ClassType.Fighter:
                            {
                                character = new Fighter(name, characterRace, dict, HitPoints, xp);
                                break;
                            }

                            case ClassType.Thief:
                            {
                                character = new Thief(name, characterRace, dict, HitPoints, xp);
                                break;
                            }

                            case ClassType.MagicUser:
                            {
                                character = new MagicUser(name, characterRace, dict, HitPoints, xp);
                                break;
                            }

                            case ClassType.Cleric:
                            {
                                character = new Cleric(name, characterRace, dict, HitPoints, xp);
                                break;
                            }
                            }

                            if (bodyEquipment != null)
                            {
                                character.EquipToBody(bodyEquipment);
                            }

                            if (rightHandEquipment != null)
                            {
                                character.EquipToHand(rightHandEquipment, Hand.Right);
                            }

                            if (leftHandEquipment != null)
                            {
                                character.EquipToHand(leftHandEquipment, Hand.Left);
                            }

                            character.SetCharacterId(characterId);

                            characters.Add(character);
                        }
                        return(characters);
                    }
        }