Class: RuneRb::Game::World::Action

Inherits:
Object
  • Object
show all
Includes:
System::Log
Defined in:
app/game/world/models/action.rb

Overview

A Action object encapsulates a Fiber that will execute code, then transfer execution context to a target Fiber.

Since:

  • 0.9.3

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from System::Log

#class_name, #err, #err!, #log, #log!, #symbolize_file

Constructor Details

#initialize(params, &work) ⇒ Action

Constructs a new Action object.

Parameters:

  • params (Hash)

    Initial parameters for the Action.

  • work (Proc)

    parameters passed to the block operations

Since:

  • 0.9.3


26
27
28
29
30
31
# File 'app/game/world/models/action.rb', line 26

def initialize(params, &work)
  @id = params[:id] || Druuid.gen
  @assets = params[:assets] || []
  @priority = params[:priority] || :LOW
  @work = work
end

Instance Attribute Details

#idInteger, Symbol (readonly)

Returns the ID of the object.

Returns:

  • (Integer, Symbol)

    the ID of the object.

Since:

  • 0.9.3


9
10
11
# File 'app/game/world/models/action.rb', line 9

def id
  @id
end

#prioritySymbol (readonly)

Returns the priority of the Job.

Returns:

  • (Symbol)

    the priority of the Job.

Since:

  • 0.9.3


13
14
15
# File 'app/game/world/models/action.rb', line 13

def priority
  @priority
end

#processFiber (readonly)

Returns the Fiber that will execute the job.

Returns:

  • (Fiber)

    the Fiber that will execute the job.


17
# File 'app/game/world/models/action.rb', line 17

attr :worker

#targetFiber (readonly)

Returns the target Fiber.

Returns:

  • (Fiber)

    the target Fiber.

Since:

  • 0.9.3


21
22
23
# File 'app/game/world/models/action.rb', line 21

def target
  @target
end

#workerFiber (readonly)

Returns the Fiber that will execute the job.

Returns:

  • (Fiber)

    the Fiber that will execute the job.

Since:

  • 0.9.3


17
18
19
# File 'app/game/world/models/action.rb', line 17

def worker
  @worker
end

Instance Method Details

#<=>(other) ⇒ Object

Mutual comparison operator. Used to sort the Job by it's priority.

Parameters:

  • other (Job)

    the compared Job

Since:

  • 0.9.3


57
58
59
# File 'app/game/world/models/action.rb', line 57

def <=>(other)
  RuneRb::Game::World::ACTION_PRIORITIES[@priority] <=> RuneRb::Game::World::ACTION_PRIORITIES[other.priority]
end

#inspectObject

Since:

  • 0.9.3


51
52
53
# File 'app/game/world/models/action.rb', line 51

def inspect
  "[id]: #{@id}\t||\t[Priority]: #{@priority}"
end

#start(auto: false) ⇒ Object

Attempts to execute all operations in the Job in sequential order.

Since:

  • 0.9.3


34
35
36
37
38
39
40
41
42
43
# File 'app/game/world/models/action.rb', line 34

def start(auto: false)
  raise 'No operation to perform!' if @work.nil?

  @worker = Fiber.new do
    @work.call
    @target&.transfer
  end

  auto ? @worker.resume : @worker
end

#target_to(fiber) ⇒ Object

Update the <@target> Fiber to the passed object.

Parameters:

  • fiber (Fiber)

    the new target Fiber.

Since:

  • 0.9.3


47
48
49
# File 'app/game/world/models/action.rb', line 47

def target_to(fiber)
  @target = fiber
end