blib/lib/Games/TF2/Server/Player.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Games/TF2/Server.pm ↑ Summary blib/lib/Games/TF2/Demo/Item/ConsoleCommand.pm »
1 package Games::TF2::Server::Player;
2  
3 1 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 our $VERSION = '0.01';
9  
10 1 ▶use Object::Pad 0.800;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 0 ▶use Games::TF2::Server;
Subroutine BEGIN: called CC=1 SCAR=2.6
13  
14 class Games::TF2::Server::Player :isa(Games::TF2::Server) {
15 use Readonly;
16 use Scalar::Util qw(looks_like_number);
17  
18 Readonly my $MAX_STEAM_ID => '18446744073709551615';
19 Readonly my $MAX_BUTTONS => 4_294_967_295;
20 Readonly my $MAX_IMPULSE => 255;
21 Readonly my %IS_CONTROL => map { $_ => 1 } qw(buttons view_angles movement impulse weapon_select);
22  
23 ADJUST :params ( :$steam_id = undef, %unknown ) {
24 $self->_argument_error("steam_id must be a SteamID64 from 0 to $MAX_STEAM_ID") if defined $steam_id && !_is_steam_id($steam_id);
25 $self->_connect( 1, { steam_id => defined $steam_id ? "$steam_id" : undef }, \%unknown );
26 }
27  
28 method set_controls (%controls) {
29 my @unknown = grep { !$IS_CONTROL{$_} } sort keys %controls;
30 $self->_argument_error( 'set_controls does not take ' . join( ', ', @unknown ) . ' (known: ' . join( ', ', sort keys %IS_CONTROL ) . ')' ) if @unknown;
31 my $buttons = $controls{buttons} // 0;
32 my $view_angles = $controls{view_angles} // [ 0, 0, 0 ];
33 my $movement = $controls{movement};
34 my $impulse = $controls{impulse} // 0;
35 my $weapon_select = $controls{weapon_select} // 0;
36 $self->_argument_error("buttons must be an integer from 0 to $MAX_BUTTONS") unless _is_count($buttons) && $buttons <= $MAX_BUTTONS;
37 $self->_argument_error('view_angles must be [ pitch, yaw, roll ] in degrees') unless _is_vector($view_angles);
38 $self->_argument_error('movement must be [ forward, side, up ]') if defined $movement && !_is_vector($movement);
39 $self->_argument_error("impulse must be an integer from 0 to $MAX_IMPULSE") unless _is_count($impulse) && $impulse <= $MAX_IMPULSE;
40 $self->_argument_error('weapon_select must be an entity index') unless _is_count($weapon_select);
41 $self->_connection->set_controls( $buttons, $view_angles->@*, defined $movement ? 1 : 0, ( $movement // [ 0, 0, 0 ] )->@*, $impulse, $weapon_select );
42 return $self;
43 }
44  
45 method _subscribe_self :override ($on) {
46 $self->_connection->subscribe_entity_self($on);
47 return;
48 }
49  
50 sub _is_count ($value) {
51 return defined $value && !ref $value && $value =~ /\A[0-9]+\z/;
52 }
53  
54 sub _is_vector ($value) {
55 return ref $value eq 'ARRAY' && $value->@* == 3 && !grep { !looks_like_number($_) } $value->@*;
56 }
57  
58 sub _is_steam_id ($value) {
59 return 0 unless _is_count($value) && length $value <= length $MAX_STEAM_ID;
60 my $digits = sprintf( '%0*s', length $MAX_STEAM_ID, $value =~ s/\A0+(?=.)//r );
61 return $digits le $MAX_STEAM_ID;
62 }
63 }
64  
65 1;
66  
67 __END__
« blib/lib/Games/TF2/Server.pm ↑ Summary blib/lib/Games/TF2/Demo/Item/ConsoleCommand.pm »