class LZRTag::Player::Base
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.
Attributes
@return [String] The player's DeviceID
, which is derived from the ESP's MAC
@return [Hash<Time>] Hash of the last few recorded shot times,
used for hit arbitration
@return [Integer] 0..255, shot ID of the player
@return [Time] Time at which the player status was last updated
@return [String] Name of the player, set externally
@return [String] status-string of the player. Should be “OK”
Public Class Methods
# File lib/lzrtag/player/base_player.rb, line 33 def initialize(deviceID, handler) @handler = handler; @mqtt = handler.mqtt; @DeviceID = deviceID; @status = ""; @name = ""; @last_status_update = Time.at(0); @connected = false; @hitIDTimetable = Hash.new(Time.new(0)); end
Public Instance Methods
# File lib/lzrtag/player/base_player.rb, line 78 def _tick_connection() return unless @connected if((Time.now() - @last_status_update) > 60) @connected = false; @handler.send_event(:playerDisconnected, self); end end
Trigger a clear of all topics @note Do not call this function yourself, except when deregistering a player! @private
# File lib/lzrtag/player/base_player.rb, line 114 def clear_all_topics() self.id = nil; end
@return [Boolean] Whether this player is connected
# File lib/lzrtag/player/base_player.rb, line 88 def connected?() return @connected end
Set the Shot ID of the player. @note Do not call this function yourself - the Handler
must
assign unique IDs to ensure proper game functionality!
@private
# File lib/lzrtag/player/base_player.rb, line 96 def id=(n) return if @id == n; if(!n.nil?) raise ArgumentError, "ID must be integer or nil!" unless n.is_a? Integer; raise ArgumentError, "ID out of range (0<ID<256)" unless n < 256 and n > 0; @id = n; else @id = nil; end _pub_to("CFG/ID", @id, retain: true); end
# File lib/lzrtag/player/base_player.rb, line 118 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
@private
# File lib/lzrtag/player/base_player.rb, line 54 def on_mqtt_data(data, topic) case topic[1..topic.length].join("/") when "Connection" return if @status == data; @status = data; return if @status == "OK" return if @status == "" return if !@connected @connected = false; @handler.send_event(:playerDisconnected, self); when "CFG/Name" @name = data; when "Ping" @last_status_update = Time.now(); if(@status == "OK" && (!@connected)) @connected = true @handler.send_event(:playerConnected, self); end end end
Private Instance Methods
# File lib/lzrtag/player/base_player.rb, line 48 def _pub_to(key, data, retain: false) @mqtt.publish_to("Lasertag/Players/#{@DeviceID}/#{key}", data, retain: retain, qos: 1); end