Class: LZRTag::Player::Base

Inherits:
Object
  • Object
show all
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

Hardware

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#DeviceIDString (readonly)

Returns The player’s DeviceID, which is derived from the ESP’s MAC.

Returns:

  • (String)

    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

#handlerObject (readonly)

Returns the value of attribute handler.



12
13
14
# File 'lib/lzrtag/player/base_player.rb', line 12

def handler
  @handler
end

#hitIDTimetableHash<Time>

Returns Hash of the last few recorded shot times, used for hit arbitration.

Returns:

  • (Hash<Time>)

    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
  @hitIDTimetable
end

#idInteger

Returns 0..255, shot ID of the player.

Returns:

  • (Integer)

    0..255, shot ID of the player



23
24
25
# File 'lib/lzrtag/player/base_player.rb', line 23

def id
  @id
end

#nameString (readonly)

Returns Name of the player, set externally.

Returns:

  • (String)

    Name of the player, set externally



17
18
19
# File 'lib/lzrtag/player/base_player.rb', line 17

def name
  @name
end

#statusString (readonly)

Returns status-string of the player. Should be “OK”.

Returns:

  • (String)

    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_topicsObject

Note:

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.

Returns:

  • (Boolean)

    Whether this player is connected



63
64
65
# File 'lib/lzrtag/player/base_player.rb', line 63

def connected?()
	return @status == "OK"
end

#inspectObject 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