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

DeviceID[R]

@return [String] The player's DeviceID, which is derived from the ESP's MAC

connected[R]
handler[R]
hitIDTimetable[RW]

@return [Hash<Time>] Hash of the last few recorded shot times,

used for hit arbitration
id[R]

@return [Integer] 0..255, shot ID of the player

last_status_update[R]

@return [Time] Time at which the player status was last updated

name[R]

@return [String] Name of the player, set externally

status[R]

@return [String] status-string of the player. Should be “OK”

Public Class Methods

new(deviceID, handler) click to toggle source
# 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

_tick_connection() click to toggle source
# 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
clear_all_topics() click to toggle source

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
connected?() click to toggle source

@return [Boolean] Whether this player is connected

# File lib/lzrtag/player/base_player.rb, line 88
def connected?()
        return @connected
end
id=(n) click to toggle source

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
inspect() click to toggle source
# 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
Also aliased as: to_s
on_mqtt_data(data, topic) click to toggle source

@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
to_s()
Alias for: inspect

Private Instance Methods

_pub_to(key, data, retain: false) click to toggle source
# 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