Esempio n. 1
0
        public static void GetAllMaps()
        {
            sLogger.Info("Loading all maps in memory...");

            using (var connection = sDefaultPool.GetConnection())
            {
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = "SELECT `id`, `doc_id`, `type`, `weather`, `portal_x`, `portal_y`, `reborn_map`, `reborn_portal`, `light` FROM `map`";
                    command.Prepare();

                    sLogger.Debug("Executing SQL: {0}", GetSqlCommand(command));

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            UInt32 mapId = reader.GetUInt32("id");

                            GameMap.Info info = new GameMap.Info
                            {
                                DocId     = reader.GetUInt16("doc_id"),
                                Type      = reader.GetUInt32("type"),
                                Weather   = (WeatherType)reader.GetUInt32("weather"),
                                PortalX   = reader.GetUInt16("portal_x"),
                                PortalY   = reader.GetUInt16("portal_y"),
                                RebornMap = reader.GetUInt32("reborn_map"),
                                Light     = reader.GetUInt32("light")
                            };

                            if (!MapManager.CreateMap(mapId, info))
                            {
                                sLogger.Error("Failed to create map {0}.", mapId);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Authenticate a account ID / token pair.
        /// </summary>
        /// <param name="Client">The client object requesting the authentication.</param>
        /// <param name="aAccountId">The account ID to authenticate.</param>
        /// <param name="aToken">The token generated by the AccServer.</param>
        /// <returns>True if the account/token pair exist, false otherwise.</returns>
        public static Boolean Authenticate(Client aClient, UInt32 aAccountId, UInt32 aToken)
        {
            bool authenticated = false;
            bool success       = false;

            if (aToken < 0x04000000U)
            {
                sLogger.Warn("Got an invalid token from {0}!", aClient.IPAddress);
                return(false);
            }

            int year, month, day, hour, minute;

            year   = (int)(((aToken >> 20) & 0x3F) + 2000);
            month  = (int)((aToken >> 16) & 0x0F);
            day    = (int)((aToken >> 11) & 0x1F);
            hour   = (int)((aToken >> 6) & 0x1F);
            minute = (int)(aToken & 0x3F);

            DateTime datetime = new DateTime(year, month, day, hour, minute, 0, DateTimeKind.Utc);

            if ((DateTime.UtcNow - datetime).TotalMinutes > 5)
            {
                sLogger.Warn("Got an expired token from {0}!", aClient.IPAddress);
                return(false);
            }

            using (var connection = sAccPool.GetConnection())
            {
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = "SELECT COUNT(*) FROM `account` WHERE `id` = @id AND `token` = @token";
                    command.Parameters.AddWithValue("@id", aAccountId);
                    command.Parameters.AddWithValue("@token", aToken);
                    command.Prepare();

                    sLogger.Debug("Executing SQL: {0}", GetSqlCommand(command));

                    try
                    {
                        int count = Convert.ToInt32(command.ExecuteScalar());
                        authenticated = count == 1;

                        sLogger.Debug("Found {0} accounts for {1}.", count, aAccountId);
                    }
                    catch (MySqlException exc)
                    {
                        sLogger.Error("Failed to execute the following cmd : \"{0}\"\nError {1}: {2}",
                                      GetSqlCommand(command), exc.Number, exc.Message);
                    }
                }

                if (authenticated)
                {
                    using (var command = connection.CreateCommand())
                    {
                        command.CommandText = "SELECT `name` FROM `account` WHERE `id` = @id";
                        command.Parameters.AddWithValue("@id", aAccountId);
                        command.Prepare();

                        sLogger.Debug("Executing SQL: {0}", GetSqlCommand(command));

                        try
                        {
                            using (var reader = command.ExecuteReader())
                            {
                                int count = 0;

                                while (reader.Read())
                                {
                                    ++count;

                                    aClient.AccountID = aAccountId;
                                    aClient.Account   = reader.GetString("name");
                                }
                                success = count == 1;

                                if (count != 1)
                                {
                                    sLogger.Error("The command should return only one result, not {0}.", count);
                                }
                            }
                        }
                        catch (MySqlException exc)
                        {
                            sLogger.Error("Failed to execute the following cmd : \"{0}\"\nError {1}: {2}",
                                          GetSqlCommand(command), exc.Number, exc.Message);
                        }
                    }
                }
            }

            return(authenticated & success);
        }