Пример #1
0
        public static void Process(Client client, byte[] buffer, Settings Opt)
        {
            PacketReader packet = new PacketReader(buffer);

            switch (packet.Type)
            {
                case -1:
                    {
                        Console.WriteLine("Accepted connection: {0}", client.m_socket.RemoteEndPoint);
                        client.Send(new Packets.PacketFFEF((short)(client.handshake ^ 0x7e41)));
                        break;
                    }
                case -3:
                    { break; }
                case 1706:
                    {
                        //request characters?
                        uint AcctId = BitConverter.ToUInt32(buffer, 8);
                        int UniId = BitConverter.ToInt32(buffer, 12);

                        packet.Skip(4);
                        uint tAcct = packet.ReadUInt();
                        int tUni = packet.ReadInt();

                        SqlDB.LoadUser(client, AcctId, UniId);
                        List<Character> listTamers = SqlDB.GetCharacters(client.AccountID);
                        client.Send(new Packets.Lobby.CharList(listTamers));
                        break;
                    }
                case 1703:
                    {
                        client.Send(packet.ToArray());
                        break;
                    }
                case 1302:
                    {
                        //Name Availability
                        string name = packet.ReadString();
                        if (SqlDB.NameAvail(name))
                            client.Send(new Packets.Lobby.NameCheck(1));
                        else
                            client.Send(new Packets.Lobby.NameCheck(0));
                        break;
                    }
                case 1303:
                    {
                        //Create Character
                        int position = packet.ReadByte();
                        int model = packet.ReadInt();
                        string name = packet.ReadZString();
                        packet.Seek(31);
                        int digiModel = packet.ReadInt();
                        string digiName = packet.ReadZString();

                        Console.WriteLine("CreateChar {0} {1}", (CharacterModel)model, name);

                        int charId = SqlDB.CreateCharacter(client.AccountID, position, model, name, digiModel);
                        int digiId = (int)SqlDB.CreateDigimon((uint)charId, digiName, digiModel);
                        SqlDB.SetPartner(charId, digiId);
                        SqlDB.SetTamer(charId, digiId);

                        client.Send(new Packets.Lobby.ConfirmCreate());
                        break;
                    }
                case 1304:
                    {
                        int slot = packet.ReadInt();
                        string code = packet.ReadString();
                        bool canDelete = SqlDB.VerifyCode(client.AccountID, code);
                        if (canDelete)
                        {
                            if (SqlDB.DeleteTamer(client.AccountID, slot))
                                client.Send(new Packets.Lobby.CharDelete(1));
                            else
                                client.Send(new Packets.Lobby.CharDelete(0));
                        }
                            else
                             client.Send(new Packets.Lobby.CharDelete(2));
                        break;
                    }
                case 1305:
                    {
                        //Request Map Server
                        int slot = packet.ReadInt();
                        Position pLoc = null;
                        try
                        {
                            SqlDB.SetLastChar(client.AccountID, slot);
                            pLoc = SqlDB.GetTamerPosition(client.AccountID, slot);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }
                        client.Send(new Packets.Lobby.ServerIP(Opt.GameServer.IP.ToString(), Opt.GameServer.Port,
                            pLoc.Map, pLoc.MapName));
                        break;
                    }
                default:
                    Console.WriteLine("Unknown Packet Type: {0}", packet.Type);
                    Console.WriteLine(packet.ToString());
                    break;
            }
        }
 void server_OnAccept(Client client)
 {
     client.Handshake();
     listClients.Dispatcher.BeginInvoke(new Action(() => { clients.Add(client); }));
 }
 void server_OnClose(Client client)
 {
     try
     {
         clients.Remove(client);
     }
     catch (Exception e)
     {
         Console.WriteLine("Error: OnClose\n{0}", e);
     }
 }
Пример #4
0
        public static void SaveTamerPosition(Client client)
        {
            int lastChar = -1, charId = -1;
            try
            {
                using (MySqlConnection con = Connect())
                using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM `acct` WHERE `accountId` = @acct", con))
                {
                    cmd.Parameters.AddWithValue("@acct", client.AccountID);

                    using (MySqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.SingleRow))
                    {
                        if (dr.HasRows && dr.Read())
                        {
                            lastChar = (int)dr["lastChar"];
                            if (lastChar != -1)
                            {
                                charId = (int)dr[string.Format("char{0}", lastChar + 1)];
                            }
                        }
                    }
                }
                if (lastChar != -1)
                {
                    Character Tamer = client.Tamer;
                    using (MySqlConnection con = Connect())
                    {
                        Query qry = new Query(Query.QueryMode.UPDATE, "chars",new Tuple<string,object>("characterId", Tamer.CharacterId));
                        qry.Add("map", Tamer.Location.Map);
                        qry.Add("x", Tamer.Location.PosX);
                        qry.Add("y", Tamer.Location.PosY);
                        using (MySqlCommand cmd = qry.GetCommand(con))
                        {
                            cmd.ExecuteNonQuery();
                        }

                        for (int i = 0; i < Tamer.DigimonList.Length; i++)
                        {
                            if (Tamer.DigimonList[i] != null)
                                SaveDigimon(Tamer.DigimonList[i]);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        void PacketProcessor(Client client, byte[] buffer, int length)
        {
            int type = BitConverter.ToInt16(buffer, 2);

            PacketLogic.Process(client, buffer, Opt);
        }
Пример #6
0
        public static void LoadTamer(Client client)
        {
            int lastChar = -1, charId = -1;
            try
            {
                using (MySqlConnection con = Connect())
                using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM `acct` WHERE `accountId` = @acct", con))
                {
                    cmd.Parameters.AddWithValue("@acct", client.AccountID);
                    using (MySqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.SingleRow))
                    {
                        if (dr.HasRows && dr.Read())
                        {
                            lastChar = (int)dr["lastChar"];
                            if (lastChar != -1)
                            {
                                charId = (int)dr[string.Format("char{0}", lastChar + 1)];
                            }
                        }
                    }
                }

                if (lastChar != -1)
                {
                    using (MySqlConnection con = Connect())
                    using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM `chars` WHERE `characterId` = @char", Connect()))
                    {
                        cmd.Parameters.AddWithValue("@char", charId);
                        using (MySqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.SingleRow))
                        {

                            if (dr.HasRows && dr.Read())
                            {
                                Character tamer = new Character();

                                tamer.CharacterId = Convert.ToUInt32((int)dr["characterId"]);
                                tamer.AccountId = Convert.ToUInt32((int)dr["accountId"]);
                                tamer.Model = (CharacterModel)(int)dr["charModel"];
                                tamer.Name = (string)dr["charName"];
                                tamer.Level = (int)dr["charLv"];
                                tamer.InventorySize = (int)dr["inventoryLimit"];
                                tamer.StorageSize = (int)dr["storageLimit"];
                                tamer.ArchiveSize = (int)dr["archiveLimit"];
                                tamer.Location = new Helpers.Position((int)dr["map"], (int)dr["x"], (int)dr["y"]);
                                tamer.MaxHP = (int)dr["maxHP"];
                                tamer.MaxDS = (int)dr["maxDS"];
                                tamer.HP = (int)dr["HP"];
                                tamer.DS = (int)dr["DS"];
                                tamer.AT = (int)dr["AT"];
                                tamer.DE = (int)dr["DE"];
                                tamer.EXP = (int)dr["experience"];
                                tamer.MS = (int)dr["MS"];
                                tamer.Fatigue = (int)dr["Fatigue"];
                                tamer.Starter = (int)dr["starter"];
                                tamer.Money = (int)dr["money"];

                                //Incubator
                                tamer.Incubator = (int)dr["incubator"];
                                tamer.IncubatorLevel = (int)dr["incubatorLevel"];
                                if (tamer.Incubator == 0) tamer.IncubatorLevel = 0;

                                try { tamer.Inventory = ItemList.Deserialize((byte[])dr["inventory"]); }
                                catch { tamer.Inventory = new ItemList(63); }
                                try
                                {
                                    tamer.Equipment = ItemList.Deserialize((byte[])dr["equipment"]);
                                }
                                catch
                                {
                                    tamer.Equipment = new ItemList(27);
                                }
                                try { tamer.Storage = ItemList.Deserialize((byte[])dr["storage"]); }
                                catch { tamer.Storage = new ItemList(70); }
                                try { tamer.Quests = QuestList.Deserialize((byte[])dr["quests"]); }
                                catch { tamer.Quests = new QuestList(); };
                                try
                                {
                                    BinaryFormatter f = new BinaryFormatter();
                                    using (MemoryStream m = new MemoryStream((byte[])dr["archive"]))
                                        tamer.ArchivedDigimon = (uint[])f.Deserialize(m);
                                }
                                catch { tamer.ArchivedDigimon = new uint[40]; }

                                Digimon partner = LoadDigimon((uint)(int)dr["partner"]);
                                tamer.Partner = partner;
                                tamer.Partner.Location = tamer.Location.Clone(); ;

                                if (dr["mercenary1"] != DBNull.Value)
                                {
                                    int mercId = (int)dr["mercenary1"];
                                    Digimon merc = LoadDigimon((uint)mercId);
                                    tamer.DigimonList[1] = merc;
                                }
                                if (dr["mercenary2"] != DBNull.Value)
                                {
                                    int mercId = (int)dr["mercenary2"];
                                    Digimon merc = LoadDigimon((uint)mercId);
                                    tamer.DigimonList[2] = merc;
                                }

                                client.Tamer = tamer;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Пример #7
0
        public static void SaveTamer(Client client)
        {
            int lastChar = -1, charId = -1;

            try
            {
                using (MySqlConnection con = Connect())
                using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM `acct` WHERE `accountId` = @acct", con))
                {
                    cmd.Parameters.AddWithValue("@acct", client.AccountID);
                    using (MySqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.SingleRow))
                        if (dr.HasRows && dr.Read())
                        {
                            lastChar = (int)dr["lastChar"];
                            if (lastChar != -1)
                            {
                                charId = (int)dr[string.Format("char{0}", lastChar + 1)];
                            }
                        }
                }

                if (lastChar != -1)
                {
                    Character Tamer = client.Tamer;
                    using (MySqlConnection con = Connect())
                    {
                        Query qry = new Query(Query.QueryMode.UPDATE, "chars", new Tuple<string, object>("characterId", Tamer.CharacterId));
                        qry.Add("charModel", (int)Tamer.Model);
                        qry.Add("charName", Tamer.Name);
                        qry.Add("charLv", Tamer.Level);
                        qry.Add("experience", Tamer.EXP);
                        qry.Add("money", Tamer.Money);

                        qry.Add("partner", Tamer.DigimonList[0].DigiId);
                        if (Tamer.DigimonList[1] == null) qry.Add("mercenary1", null);
                        else qry.Add("mercenary1", Tamer.DigimonList[1].DigiId);
                        if (Tamer.DigimonList[2] == null) qry.Add("mercenary2", null);
                        else qry.Add("mercenary2", Tamer.DigimonList[2].DigiId);

                        qry.Add("map", Tamer.Location.Map);
                        qry.Add("x", Tamer.Location.PosX);
                        qry.Add("y", Tamer.Location.PosY);

                        qry.Add("inventoryLimit", Tamer.InventorySize);
                        qry.Add("storageLimit", Tamer.StorageSize);
                        qry.Add("archiveLimit", Tamer.ArchiveSize);

                        qry.Add("maxHP", Tamer.MaxHP);
                        qry.Add("maxDS", Tamer.MaxDS);
                        qry.Add("HP", Tamer.HP);
                        qry.Add("DS", Tamer.DS);
                        qry.Add("AT", Tamer.AT);
                        qry.Add("DE", Tamer.DE);
                        qry.Add("MS", Tamer.MS);
                        qry.Add("Fatigue", Tamer.Fatigue);

                        qry.Add("incubator", Tamer.Incubator);
                        qry.Add("incubatorLevel", Tamer.IncubatorLevel);

                        BinaryFormatter f = new BinaryFormatter();
                        using (MemoryStream m = new MemoryStream())
                        {
                            f.Serialize(m, Tamer.ArchivedDigimon);
                            qry.Add("archive", m.ToArray());
                        }

                        qry.Add("inventory", Tamer.Inventory.Serialize());
                        qry.Add("equipment", Tamer.Equipment.Serialize());
                        qry.Add("storage", Tamer.Storage.Serialize());
                        qry.Add("quests", Tamer.Quests.Serialize());

                        using (MySqlCommand cmd = qry.GetCommand(con))
                        {
                            cmd.ExecuteNonQuery();
                        }
                    }
                    for (int i = 0; i < Tamer.DigimonList.Length; i++)
                    {
                        if (Tamer.DigimonList[i] != null)
                            SaveDigimon(Tamer.DigimonList[i]);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
 void m_auth_OnRead(Client client, byte[] buffer, int length)
 {
     //TODO: Packet Response Logic
     PacketLogic.Process(client, buffer);
 }
 void server_OnClose(Client client)
 {
     try
     {
         clients.Remove(client);
     }
     catch { }
 }
 void m_auth_OnAccept(Client state)
 {
     state.Handshake();
     clients.Add(state);
 }
Пример #11
0
        public static int Validate(Client client, string user, string pass)
        {
            int level = 0;
            try
            {
                using (MySqlConnection Connection = Connect())
                using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM `acct` WHERE `username` = @user", Connection))
                {
                    cmd.Parameters.AddWithValue("@user", user);

                    using (MySqlDataReader read = cmd.ExecuteReader())
                    {
                        if (read.HasRows)
                        {
                            if (read.Read())
                            {
                                if (read["username"].ToString() == user && read["password"].ToString() == SHA2(pass))
                                {
                                    level = (int)read["level"];
                                    client.AccessLevel = (int)read["level"];
                                    client.Username = user;
                                    client.AccountID = Convert.ToUInt32((int)read["accountId"]); ;
                                }
                                else
                                {
                                    //Wrong Pass
                                    level = -2;
                                }
                            }
                        }
                        else

                        { level = -3; }
                    }

                }
                using (MySqlConnection Connection = Connect())
                using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM `chars` WHERE `accountId` = @id", Connect()))
                {
                    cmd.Parameters.AddWithValue("@id", client.AccountID);
                    using (MySqlDataReader read = cmd.ExecuteReader())
                    {
                        if (read.HasRows)
                        {
                            while (read.Read())
                            {
                                client.Characters++;
                            }
                        }
                        else client.Characters = 0;
                    }
                }

            }
            catch (MySqlException e)
            {
                Console.WriteLine("Error: Validate\n{0}", e);
                level = -1;
            }
            return level;
        }
Пример #12
0
        /// <summary>
        /// Loads all user data into the Client class. Used by the Lobby Server
        /// </summary>
        /// <param name="client">client</param>
        /// <param name="AccountID">AccountID to find</param>
        /// <param name="UniId">Unique ID</param>
        public static void LoadUser(Client client, uint AccountID, int UniId)
        {
            try
            {
                using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM `acct` WHERE `accountId` = @id", Connection))
                {
                    cmd.Parameters.AddWithValue("@id", AccountID);

                    using (MySqlDataReader read = cmd.ExecuteReader(CommandBehavior.SingleRow))
                    {
                        if (read.HasRows)
                        {
                            if (read.Read() && (int)read["uniId"] == UniId)
                            {
                                client.AccessLevel = (int)read["level"];
                                client.AccountID = AccountID;

                                client.UniqueID = UniId;
                            }
                        }
                    }
                }
            }
            catch (MySqlException e)
            {
                Console.WriteLine("Error: Validate\n{0}", e);
            }
        }
Пример #13
0
        /// <summary>
        /// Loads all user information into the Client class
        /// </summary>
        /// <param name="client">Client</param>
        public static void LoadUser(Client client)
        {
            try
            {
                using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM `acct` WHERE `username` = @user", Connection))
                {
                    cmd.Parameters.AddWithValue("@user", client.Username);

                    using (MySqlDataReader read = cmd.ExecuteReader(CommandBehavior.SingleRow))
                    {
                        if (read.HasRows)
                        {
                            if (read.Read())
                            {
                                client.AccessLevel = (int)read["level"];
                                client.AccountID = Convert.ToUInt32((int)read["accountId"]);

                                int uniId = RNG.Next(1, int.MaxValue);
                                using (MySqlCommand updateUniId =
                                    new MySqlCommand("UPDATE `acct` SET `uniId` = @uniId WHERE `accountId` = @id", Connect()))
                                {
                                    updateUniId.Parameters.AddWithValue("@uniId", uniId);
                                    updateUniId.Parameters.AddWithValue("@id", read["accountId"]);
                                    updateUniId.ExecuteNonQuery();

                                    client.UniqueID = uniId;
                                }
                            }
                        }
                    }
                }
            }
            catch (MySqlException e)
            {
                Console.WriteLine("Error: Validate\n{0}", e);
            }
        }
Пример #14
0
        public static void Process(Client client, byte[] buffer)
        {
            PacketReader packet = null;
            try
            {
                packet = new PacketReader(buffer);
            }
            catch
            {
                return;
            }
            switch (packet.Type)
            {
                case -1:
                    {
                        /*
                        PacketWriter resp = new PacketWriter();
                        resp.Type(-2);
                        resp.WriteBytes(new byte[] { 0xcf, 0xa6, 0x8f, 0xd8, 0xb4, 0x4e });
                         * */
                        Console.WriteLine("Accepted connection: {0}", client.m_socket.RemoteEndPoint);

                        packet.Skip(8);
                        ushort u1 = (ushort)packet.ReadShort();
                        ushort u2 = (ushort)packet.ReadShort();

                        client.Send(new Packets.PacketFFEF((short)(client.handshake ^ 0x7e41)));
                        break;
                    }
                case 3301:
                    {
                        //Login information
                        string user = Encoding.ASCII.GetString(buffer, 13, buffer[12]);
                        string pass = Encoding.ASCII.GetString(buffer, 15 + buffer[12], buffer[buffer[12] + 14]);

                        Console.WriteLine("Receiving login request: {0}", user);
            #if CREATE
                        SqlDB.CreateUser(user, pass);
                        Console.WriteLine("Creating user {0}...", user);
            #else
                        int success = SqlDB.Validate(client, user, pass);
                        switch (success)
                        {
                            case -1:
                                //Banned or non-existent
                                Console.WriteLine("Banned or nonexistent login: {0}", user);
                                client.Send(new Packets.Auth.LoginMessage(string.Format("This username has been banned.")));
                                break;
                            case -2:
                                //Wrong Pass;
                                Console.Write("Incorrect password: {0}", user);
                                client.Send(new Packets.Auth.LoginMessage("The password provided does not match."));
                                break;
                            case -3:
                                client.Send(new Packets.Auth.LoginMessage("This username does not exist."));
                                break;
                            default:
                                //Normal Login
                                Console.WriteLine("Successful login: {0}\n Sending Server List", user);
                                client.Send(new Packets.Auth.ServerList(SqlDB.GetServers(), user, client.Characters));
                                break;
                        }
            #endif
                        break;
                    }
                case 1702:
                    {
                        //Requesting IP of Server
                        int serverID = BitConverter.ToInt32(buffer, 4);
                        KeyValuePair<int, string> server = SqlDB.GetServer(serverID);
                        SqlDB.LoadUser(client);
                        client.Send(new Packets.Auth.ServerIP(server.Value, server.Key, client.AccountID, client.UniqueID));
                        break;
                    }
                case 0x6A5:
                    {
                        client.Send(new Packets.Auth.ServerList(SqlDB.GetServers(), client.Username, client.Characters));
                        break;
                    }
                case -3:
                    break;
                default:
                    {
                        Console.WriteLine("Unknown Packet ID: {0}", packet.Type);
                        Console.WriteLine(Packet.Visualize(buffer));
                        break;
                    }
            }
        }