Class: LZRTag::Player::Base
- Inherits:
-
Object
- Object
- LZRTag::Player::Base
- Defined in:
- lib/lzrtag/player/base_player.rb
Overview
The base player class. This class is not instantiated by the user, but instead on a on-demand basis by the LZRTag::Handler::Base when a new PlayerID needs to be registered. The player classes process and send MQTT data, handle events, and keep track of per-player infos like life, damage, ammo, team, etc. etc.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#DeviceID ⇒ String
readonly
The player’s DeviceID, which is derived from the ESP’s MAC.
-
#handler ⇒ Object
readonly
Returns the value of attribute handler.
-
#hitIDTimetable ⇒ Hash<Time>
Hash of the last few recorded shot times, used for hit arbitration.
-
#id ⇒ Integer
0..255, shot ID of the player.
-
#name ⇒ String
readonly
Name of the player, set externally.
-
#status ⇒ String
readonly
Status-string of the player.
Instance Method Summary collapse
-
#clear_all_topics ⇒ Object
Trigger a clear of all topics.
-
#connected? ⇒ Boolean
Whether this player is connected.
-
#initialize(deviceID, handler) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ Object (also: #to_s)
- #on_mqtt_data(data, topic) ⇒ Object
Constructor Details
#initialize(deviceID, handler) ⇒ Base
Returns a new instance of Base.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/lzrtag/player/base_player.rb', line 28 def initialize(deviceID, handler) @handler = handler; @mqtt = handler.mqtt; @DeviceID = deviceID; @status = ""; @name = ""; @hitIDTimetable = Hash.new(Time.new(0)); end |
Instance Attribute Details
#DeviceID ⇒ String (readonly)
Returns The player’s DeviceID, which is derived from the ESP’s MAC.
14 15 16 |
# File 'lib/lzrtag/player/base_player.rb', line 14 def DeviceID @DeviceID end |
#handler ⇒ Object (readonly)
Returns the value of attribute handler.
12 13 14 |
# File 'lib/lzrtag/player/base_player.rb', line 12 def handler @handler end |
#hitIDTimetable ⇒ Hash<Time>
Returns Hash of the last few recorded shot times, used for hit arbitration.
26 27 28 |
# File 'lib/lzrtag/player/base_player.rb', line 26 def @hitIDTimetable end |
#id ⇒ Integer
Returns 0..255, shot ID of the player.
23 24 25 |
# File 'lib/lzrtag/player/base_player.rb', line 23 def id @id end |
#name ⇒ String (readonly)
Returns Name of the player, set externally.
17 18 19 |
# File 'lib/lzrtag/player/base_player.rb', line 17 def name @name end |
#status ⇒ String (readonly)
Returns status-string of the player. Should be “OK”.
20 21 22 |
# File 'lib/lzrtag/player/base_player.rb', line 20 def status @status end |
Instance Method Details
#clear_all_topics ⇒ Object
Do not call this function yourself, except when deregistering a player!
Trigger a clear of all topics
89 90 91 |
# File 'lib/lzrtag/player/base_player.rb', line 89 def clear_all_topics() self.id = nil; end |
#connected? ⇒ Boolean
Returns Whether this player is connected.
63 64 65 |
# File 'lib/lzrtag/player/base_player.rb', line 63 def connected?() return @status == "OK" end |
#inspect ⇒ Object Also known as: to_s
93 94 95 96 97 98 99 100 |
# File 'lib/lzrtag/player/base_player.rb', line 93 def inspect() iString = "#<Player:#{@deviceID}##{@id ? @id : "OFFLINE"}, Team=#{@team}"; iString += ", DEAD" if @dead iString += ", Battery=#{@battery.round(2)}" iString += ", Ping=#{@ping.ceil}ms>"; return iString; end |
#on_mqtt_data(data, topic) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/lzrtag/player/base_player.rb', line 46 def on_mqtt_data(data, topic) case topic[1..topic.length].join("/") when "Connection" return if @status == data; oldStatus = @status; @status = data; if(@status == "OK") @handler.send_event(:playerConnected, self); elsif(oldStatus == "OK") @handler.send_event(:playerDisconnected, self); end when "CFG/Name" @name = data; end end |