Class: LZRTag::Handler::HitArb

Inherits:
Base
  • Object
show all
Defined in:
lib/lzrtag/handler/hitArb_handler.rb

Overview

Hit arbitration handling class. This class extends the Handler::Base class, adding important features to ensure that each player shot is only counted once. Additionally, hooks can manipulate this behavior and prevent friendly-fire, or enable multiple hits in the case of a shotgun!

Shot arbitration is performed by listening to “Lasertag/Game/Events”, waiting for a ‘type: “hit”’ If such a JSON payload is found, hit and source players will be determined, and every available hook’s “process_raw_hit” function is called. If this function returns false, the hit will be “vetoed” and does not count at all. However, a hook can raise NoArbitration, preventing this shot from being logged and thusly enabling multiple hits

See Also:

Direct Known Subclasses

Count

Instance Attribute Summary

Attributes inherited from Base

#idTable, #mqtt

Instance Method Summary collapse

Methods inherited from Base

#[], #add_hook, #consume_event, #each, #num_connected, #remove_hook, #send_event

Constructor Details

#initialize(*data, **options) ⇒ HitArb

Returns a new instance of HitArb.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lzrtag/handler/hitArb_handler.rb', line 22

def initialize(*data, **options)
	super(*data, **options);

	@mqtt.subscribe_to "Lasertag/Game/Events" do |data|
		begin
			data = JSON.parse(data, symbolize_names: true);

			if(data[:type] == "hit")
				_handle_hitArb(data);
			end
		rescue JSON::ParserError
		end
	end
end

Instance Method Details

#_handle_hitArb(data) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/lzrtag/handler/hitArb_handler.rb', line 41

def _handle_hitArb(data)
	unless  (hitPlayer = get_player(data[:target])) and
			  (sourcePlayer = get_player(data[:shooterID])) and
			  (arbCode = data[:arbCode])
		return
	end

	return if (sourcePlayer.hitIDTimetable[arbCode] + 1) > Time.now();

	veto = false;
	arbitrateShot = true;

	hookList = Array.new();
	hookList << @hooks;
	if(@currentGame)
		hookList << @currentGame.hookList
	end
	hookList.flatten

	hookList.each do |h|
		begin
			veto |= !(h.process_raw_hit(hitPlayer, sourcePlayer));
		rescue NoArbitration
			arbitrateShot = false;
		end
	end
	return if veto;

	if arbitrateShot
		sourcePlayer.hitIDTimetable[arbCode] = Time.now();
	end

	send_event(:playerHit, hitPlayer, sourcePlayer);
end

#process_raw_hitObject



37
38
39
# File 'lib/lzrtag/handler/hitArb_handler.rb', line 37

def process_raw_hit(*)
	return true;
end