Inheritance: LinqToDB.Data.DataConnection
 private void InitTestDb()
 {
     if (!File.Exists("Gateway.sqlite"))
         using (var db = new DBGateway())
             db.CreateTable<Character>();
 }
        public static bool HandleCharEnum(GatewayConnection client, BinaryReader br)
        {
            var pkt = PacketHelper.Parse<CMSG_CHAR_ENUM>(br);

            // TODO: Move to gateway service?

            using (var db = new DBGateway())
            {
                var dbCharacters = db.Character.Where(c => c.AccountID == client.AccountID);
                if (dbCharacters.Count() == 0)
                {
                    client.SendPacket(WorldOpcodes.SMSG_CHAR_ENUM, PacketHelper.Build(new SMSG_CHAR_ENUM { Length = 0 }));
                    return true;
                }

                var characters = new List<CharEnumEntry>();
                foreach (var character in dbCharacters)
                {
                    var gameChar = new CharEnumEntry
                    {
                        GUID = character.CharacterID,

                        Name = character.Name,
                        Race = (WoWRace)character.Race,
                        Class = (WoWClass)character.Class,
                        Gender = (WoWGender)character.Gender,
                        Skin = character.Skin,
                        Face = character.Face,
                        HairStyle = character.HairStyle,
                        HairColor = character.HairColor,
                        FacialHair = character.FacialHair,

                        Level = character.Level,
                        Zone = character.Zone,
                        Map = character.Zone,
                        X = character.X,
                        Y = character.Y,
                        Z = character.Z,

                        GuildId = character.GuildId,
                        CharacterFlags = character.CharacterFlags,
                        FirstLogin = (byte)(character.FirstLogin ? 1 : 0),

                        // TODO: Get rest from database
                        PetDisplayId = 0,
                        PetLevel = 0,
                        PetFamily = 0,

                        Equipment = new CharEnumEquipmentEntry[(int)WoWEquipSlot.Tabard + 1],
                        FirstBagDisplayId = 0,
                        FirstBagInventoryType = 0,
                    };

                    for (int i = (int)WoWEquipSlot.Head; i < (int)WoWEquipSlot.Tabard + 1; i++)
                        gameChar.Equipment[i] = new CharEnumEquipmentEntry { DisplayInfoId = 0, InventoryType = 0 };

                    characters.Add(gameChar);
                }

                client.SendPacket(WorldOpcodes.SMSG_CHAR_ENUM, PacketHelper.Build(new SMSG_CHAR_ENUM
                {
                    Length = (byte)characters.Count,
                    Characters = characters.ToArray(),
                }));
            }

            return true;
        }
        public static bool HandleCharCreate(GatewayConnection client, BinaryReader br)
        {
            var pkt = PacketHelper.Parse<CMSG_CHAR_CREATE>(br);
            pkt.Name = pkt.Name.UppercaseFirst();

            using (var db = new DBGateway())
            {
                // TODO: If PvP-server, make sure same-faction
                // TODO: Check for reserved names

                // Check if name already is taken
                if (db.Character.Count(c => c.Name == pkt.Name) > 0)
                {
                    client.SendPacket(WorldOpcodes.SMSG_CHAR_CREATE, PacketHelper.Build(new SMSG_CHAR_CREATE { Code = CharacterCreationCode.NameInUse }));
                    return true;
                }

                db.Insert(new Character
                {
                    AccountID = client.AccountID,

                    Name = pkt.Name,
                    Race = (byte)pkt.Race,
                    Class = (byte)pkt.Class,
                    Gender = (byte)pkt.Gender,
                    Skin = pkt.Skin,
                    Face = pkt.Face,
                    HairStyle = pkt.HairStyle,
                    HairColor = pkt.HairColor,
                    FacialHair = pkt.FacialHair,

                    Level = 1,
                    // TODO: Get spawn information based on race
                    Zone = 12,
                    Map = 0,
                    X = -8954.42f,
                    Y = -158.558f,
                    Z = 81.8225f,

                    GuildId = 0, // New characters don't have guilds!
                    CharacterFlags = 0,
                    FirstLogin = false, //TODO: set true
                });
            }

            client.SendPacket(WorldOpcodes.SMSG_CHAR_CREATE, PacketHelper.Build(new SMSG_CHAR_CREATE { Code = CharacterCreationCode.Success }));
            return true;
        }
        public static bool HandleCharDelete(GatewayConnection client, BinaryReader br)
        {
            var pkt = PacketHelper.Parse<CMSG_CHAR_DELETE>(br);

            using (var db = new DBGateway())
            {
                db.Character.Delete(c => c.CharacterID == pkt.GUID);
            }

            client.SendPacket(WorldOpcodes.SMSG_CHAR_DELETE, PacketHelper.Build(new SMSG_CHAR_DELETE { Code = CharacterDeleteCode.Success }));

            return true;
        }