Advertisement
Zeriab

Scheduler

Oct 6th, 2011
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.82 KB | None | 0 0
  1. #==============================================================================
  2. # ** Scheduler
  3. #------------------------------------------------------------------------------
  4. #  This class allows to schedule a proc or method call a given amount of frames
  5. #  into the future with any amount of arguments
  6. #==============================================================================
  7. class Scheduler
  8.   #============================================================================
  9.   # ** Order
  10.   #----------------------------------------------------------------------------
  11.   #  An order is a proc, method or something else which has 'call' as a method,
  12.   #  and the arguments to pass along.
  13.   #============================================================================
  14.   # Create an struct for containing the data
  15.   Order = Struct.new(:callable, :arguments)
  16.   # Extend the class with a call-method which calls the callable with the args
  17.   class Order
  18.     #------------------------------------------------------------------------
  19.     # * Call the callable with the present arguments
  20.     #------------------------------------------------------------------------
  21.     def call
  22.       callable.call(*arguments)
  23.     end
  24.   end
  25.   #============================================================================
  26.   # ** RecurringOrder
  27.   #----------------------------------------------------------------------------
  28.   #  An order which is recurring every specified amount of time until
  29.   #  FalseClass is returned from the call.
  30.   #  Note that arguments remain the same for each call
  31.   #============================================================================
  32.   # Create an struct for containing the data
  33.   RecurringOrder = Struct.new(:callable, :arguments, :frames)
  34.   # Extend the class with a call-method which calls the callable with the args
  35.   class RecurringOrder
  36.     #------------------------------------------------------------------------
  37.     # * Call the callable with the present arguments
  38.     #------------------------------------------------------------------------
  39.     def call
  40.       result = callable.call(*arguments)
  41.       unless result == FalseClass
  42.         Scheduler.schedule_recurring(frames, frames, callable, *arguments)
  43.       end
  44.     end
  45.   end
  46.  
  47.   #============================================================================
  48.   # ** Mapping
  49.   #----------------------------------------------------------------------------
  50.   # Maps an index to an array. Values can be added to these value.
  51.   # Each array starts empty.
  52.   #============================================================================
  53.   class Mapping
  54.     #------------------------------------------------------------------------
  55.     # * Initialization
  56.     #------------------------------------------------------------------------
  57.     def initialize
  58.       @mapping = {}
  59.     end
  60.     #------------------------------------------------------------------------
  61.     # * Add an value to a given index
  62.     #------------------------------------------------------------------------
  63.     def add(index, value)
  64.       @mapping[index] = [] if @mapping[index].nil?
  65.       @mapping[index] << value
  66.     end
  67.     #------------------------------------------------------------------------
  68.     # * Retrieve the list of values mapped to the index
  69.     #------------------------------------------------------------------------
  70.     def get(index)
  71.       return [] if @mapping[index].nil?
  72.       @mapping[index]
  73.     end
  74.     #------------------------------------------------------------------------
  75.     # * Delete the array the index is mapped to. Conceptually it is now empty
  76.     #------------------------------------------------------------------------
  77.     def empty(index)
  78.       @mapping.delete(index)
  79.     end
  80.   end
  81.  
  82.   #--------------------------------------------------------------------------
  83.   # * Initialization
  84.   #--------------------------------------------------------------------------
  85.   def initialize
  86.     # This maps
  87.     @mapping = Mapping.new
  88.     @tick = 0
  89.   end
  90.   #--------------------------------------------------------------------------
  91.   # * Scheduling
  92.   #--------------------------------------------------------------------------
  93.   def schedule(frames, callable, *arguments)
  94.     # Create an order
  95.     order = Order.new(callable, arguments)
  96.     @mapping.add(frames + @tick, order)
  97.   end
  98.   #--------------------------------------------------------------------------
  99.   # * Scheduling
  100.   #--------------------------------------------------------------------------
  101.   def schedule_recurring(frames, frames_to_wait, callable, *arguments)
  102.     # Create an order
  103.     order = RecurringOrder.new(callable, arguments, frames_to_wait)
  104.     @mapping.add(frames + @tick, order)
  105.   end
  106.   #--------------------------------------------------------------------------
  107.   # * Update the scheduler
  108.   #--------------------------------------------------------------------------
  109.   def update
  110.     # Get the orders for the current tick
  111.     orders = @mapping.get(@tick)
  112.     # Delete the mapping's reference to the list of orders
  113.     @mapping.empty(@tick)
  114.     # Call each order
  115.     for order in orders
  116.       order.call
  117.     end
  118.     # Advance the tick (next frame)
  119.     @tick += 1
  120.   end
  121.  
  122.   #--------------------------------------------------------------------------
  123.   # * 'Singleton' principle used although you can easily make
  124.   #   an extra scheduler. (Class method only works for this)
  125.   #--------------------------------------------------------------------------
  126.   @@instance = self.new
  127.   def self.instance
  128.     return @@instance
  129.   end
  130.   ## Class methods point to the equivalent instance methods
  131.   def self.schedule_recurring(*args) instance.schedule_recurring(*args); end
  132.   def self.schedule(*args) instance.schedule(*args); end
  133.   def self.update(*args) instance.update(*args); end
  134. end
  135.  
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement