Class: GameOverseer::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/gameoverseer/services/service.rb

Overview

Services are at the heart on GameOverseer

Subclass this class to implement a service

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeService

This method should not be overridden, you should instead implement #setup with no arguments



18
19
20
21
22
23
24
25
# File 'lib/gameoverseer/services/service.rb', line 18

def initialize
  if defined?(self.setup)
    @client_id = 0
    @safe_methods = []
    setup
    enable
  end
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



7
8
9
# File 'lib/gameoverseer/services/service.rb', line 7

def client_id
  @client_id
end

#safe_methodsObject (readonly)

Returns the value of attribute safe_methods.



8
9
10
# File 'lib/gameoverseer/services/service.rb', line 8

def safe_methods
  @safe_methods
end

Class Method Details

.inherited(subclass) ⇒ Object

Adds the class that subclassed this class to a list for activation later

Parameters:



12
13
14
15
# File 'lib/gameoverseer/services/service.rb', line 12

def self.inherited(subclass)
  Services.register(subclass)
  GameOverseer::Console.log "Service> added '#{subclass}' to Services::List."
end

Instance Method Details

#after(milliseconds, &block) ⇒ Object

Calls Proc after milliseconds have passed, async.

Parameters:

  • milliseconds (Integer)
    Float

    Time to wait before calling the block

  • block (Proc)


98
99
100
101
102
103
# File 'lib/gameoverseer/services/service.rb', line 98

def after(milliseconds, &block)
  Thread.new do
    sleep(milliseconds/1000.0)
    block.call
  end
end

#channel_managerChannelManager

Returns Active instance of ChannelManager.

Returns:



58
59
60
# File 'lib/gameoverseer/services/service.rb', line 58

def channel_manager
  ChannelManager.instance
end

#client_managerClientManager

Returns Current instance of ClientManager.

Returns:



68
69
70
# File 'lib/gameoverseer/services/service.rb', line 68

def client_manager
  ClientManager.instance
end

#data_to_method(data) ⇒ Object

Uses the ‘mode’ from a packet to call the method of the same name

Parameters:

  • data (Hash)

    data from packet



74
75
76
77
78
79
80
81
# File 'lib/gameoverseer/services/service.rb', line 74

def data_to_method(data)
  raise "No safe methods defined!" unless @safe_methods.size > 0
  @safe_methods.each do |method|
    if data['mode'] == method.to_s
      self.send(data['mode'], data)
    end
  end
end

#enableObject

Called when services are first initialized, put active code here, in a thread.



32
33
# File 'lib/gameoverseer/services/service.rb', line 32

def enable
end

#every(milliseconds, &block) ⇒ Object

Calls Proc immediately then every milliseconds, async.

Parameters:

  • milliseconds (Integer)
    Float

    Time to wait before calling the block

  • block (Proc)


86
87
88
89
90
91
92
93
# File 'lib/gameoverseer/services/service.rb', line 86

def every(milliseconds, &block)
  Thread.new do
    loop do
      block.call
      sleep(milliseconds/1000.0)
    end
  end
end

#log(string, color = Gosu::Color::RED) ⇒ Object

String to be logged

Parameters:

  • string (String)

    text to log

  • color (Gosu::Color) (defaults to: Gosu::Color::RED)

    color of text in console



108
109
110
# File 'lib/gameoverseer/services/service.rb', line 108

def log(string, color = Gosu::Color::RED)
  GameOverseer::Console.log_with_color(string, color)
end

#message_managerMessageManager

Returns Instance of MessageManager.

Returns:



63
64
65
# File 'lib/gameoverseer/services/service.rb', line 63

def message_manager
  MessageManager.instance
end

#process(data) ⇒ Object

Called when a message is recieved for this channel.

Parameters:

  • data (Hash)

    The data from the packet



37
38
# File 'lib/gameoverseer/services/service.rb', line 37

def process(data)
end

#set_safe_methods(array) ⇒ Object

Sets methods that are safe for #data_to_method to call

Parameters:

  • array (Array)

    Array of strings or symbols that match a method in the service class



52
53
54
55
# File 'lib/gameoverseer/services/service.rb', line 52

def set_safe_methods(array)
  raise "argument must be an array of strings or symbols" unless array.is_a?(Array)
  @safe_methods = array
end

#setupObject

Called before #enable there should be no active code here, only setup variables.



28
29
# File 'lib/gameoverseer/services/service.rb', line 28

def setup
end

#versionObject



40
41
42
43
44
45
46
47
48
# File 'lib/gameoverseer/services/service.rb', line 40

def version
  # Please use the sematic versioning system,
  # http://semver.org
  #
  # e.g.
  # "1.5.9"
  # (Major.Minor.Patch)
  "0.0.0-default"
end