-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCustomTeamColors.pm
183 lines (161 loc) · 6.42 KB
/
CustomTeamColors.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package CustomTeamColors;
use strict;
use File::Spec::Functions qw/catfile/;
use List::Util qw'all max';
use SpadsPluginApi;
my $pluginVersion='0.1';
my $requiredSpadsVersion='0.12.34';
sub getVersion { return $pluginVersion; }
sub getRequiredSpadsVersion { return $requiredSpadsVersion; }
my @cutomTeamColorsRulesFields=(['nbTeams','teamSize','aiBotTeamSize'],['teamsColors']);
sub new {
my $class=shift;
my $self = { customColors => {},
colorRules => [] };
bless($self,$class);
return undef unless($self->onReloadConf());
slog("Plugin loaded (version $pluginVersion)",3);
return $self;
}
sub parseLcColorDef {
my $colorDef=shift;
$colorDef='#'."$1$1$2$2$3$3" if($colorDef=~ /^#([0-9a-f])([0-9a-f])([0-9a-f])$/);
my %color;
if($colorDef =~ /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/) {
my ($red,$green,$blue)=map {hex($_)} ($1,$2,$3);
%color=(red => $red, green => $green, blue => $blue);
}elsif($colorDef =~ /^rgb\(\s*(\d{1,3}\%?)\s*,\s*(\d{1,3}\%?)\s*,\s*(\d{1,3}\%?)\s*\)$/) {
my ($red,$green,$blue)=($1,$2,$3);
($red,$green,$blue) = map {/^(\d+)\%$/ ? int($1*2.55+0.5) : $_+0} ($red,$green,$blue);
return undef unless(all {$_ < 256} ($red,$green,$blue));
%color=(red => $red, green => $green, blue => $blue);
}elsif($colorDef =~ /^hsl\(\s*(\d{1,3})\s*,\s*(\d{1,3}(?:\.\d{1,3})?\%?)\s*,\s*(\d{1,3}(?:\.\d{1,3})?\%?)\s*\)$/) {
my ($hue,$saturation,$lightness)=($1,$2,$3);
return undef unless($hue < 360);
($saturation,$lightness) = map {/^(.+)\%$/ ? $1/100 : $_} ($saturation,$lightness);
return undef unless(all {$_ <= 1} ($saturation,$lightness));
my ($red,$green,$blue)=::hslToRgb($hue,$saturation,$lightness);
%color=(red => $red, green => $green, blue => $blue);
}elsif($colorDef =~ /^hsv\(\s*(\d{1,3})\s*,\s*(\d{1,3}(?:\.\d{1,3})?\%?)\s*,\s*(\d{1,3}(?:\.\d{1,3})?\%?)\s*\)$/) {
my ($hue,$saturation,$value)=($1,$2,$3);
return undef unless($hue < 360);
($saturation,$value) = map {/^(.+)\%$/ ? $1/100 : $_} ($saturation,$value);
return undef unless(all {$_ <= 1} ($saturation,$value));
my ($red,$green,$blue)=::hsvToRgb($hue,$saturation,$value);
%color=(red => $red, green => $green, blue => $blue);
}else{
return undef;
}
return \%color;
}
sub onReloadConf {
my $self=shift;
my $etcDir=getSpadsConf()->{etcDir};
my $customColorsFile=catfile($etcDir,'CustomTeamColors.colors.conf');
if(! -f $customColorsFile) {
slog("Custom colors definition file not found: $customColorsFile",1);
return 0;
}
my $openRes=open(my $customColorsFh,'<',$customColorsFile);
if(! $openRes || ! $customColorsFh) {
slog("Unable to read configuration file ($customColorsFile: $!)",1);
return 0;
}
my %customColors;
while(local $_ = <$customColorsFh>) {
next if(/^\s*(?:\#.*)?$/);
if(/^\s*([^\s:]*[^\s:])\s*:\s*((?:.*[^\s])?)\s*$/) {
my ($colorName,$colorDef)=map {lc($_)} ($1,$2);
if(exists $customColors{$colorName}) {
slog("Duplicate definition for color \"$colorName\" found in file \"$customColorsFile\"",1);
return 0;
}
$customColors{$colorName}=parseLcColorDef($colorDef);
if(! defined $customColors{$colorName}) {
slog("Invalid color definition \"$colorDef\" found for color \"$colorName\" in file \"$customColorsFile\"",1);
return 0;
}
}else{
chomp($_);
slog("Invalid configuration line \"$_\" in file \"$customColorsFile\"",2);
return 0;
}
}
close($customColorsFh);
my $colorRulesFile=catfile($etcDir,'CustomTeamColors.rules.conf');
my $spads=getSpadsConfFull();
my $r_rules=SpadsConf::loadTableFile($spads->{log},$colorRulesFile,\@cutomTeamColorsRulesFields,$spads->{macros});
return 0 unless(%{$r_rules});
foreach my $r_colorRule (@{$r_rules->{''}}) {
return 0 unless($#{$r_colorRule} == 1 && %{$r_colorRule->[1]} && exists $r_colorRule->[1]{teamsColors});
my @teamColorsDefs=split(/\s+/,$r_colorRule->[1]{teamsColors});
foreach my $teamColorsDef (@teamColorsDefs) {
my @colorDefs=split(/;/,$teamColorsDef);
foreach my $colorDef (@colorDefs) {
$colorDef=lc($colorDef);
if(exists $customColors{$colorDef}) {
$colorDef=$customColors{$colorDef};
}elsif(my $r_color = parseLcColorDef($colorDef)) {
$colorDef=$r_color;
}else{
slog("Invalid color \"$colorDef\" referenced in \"$colorRulesFile\"",1);
return 0;
}
}
$teamColorsDef=\@colorDefs;
}
$r_colorRule->[1]{teamsColors}=\@teamColorsDefs;
}
$self->{customColors}=\%customColors;
$self->{colorRules}=$r_rules->{''};
return 1;
}
sub onUnload {
slog('Plugin unloaded',3);
}
sub fixColors {
my ($self,$r_battleStructure)=@_[0,3];
my @orderedTeamNbs = sort {$a <=> $b} keys %{$r_battleStructure};
my $nbTeams=scalar @orderedTeamNbs;
my $teamSize=max(map {scalar keys %{$r_battleStructure->{$_}}} @orderedTeamNbs);
my $aiBotTeamSize=0;
my ($playerTeam,$aiBotTeam);
if($nbTeams == 2) {
foreach my $teamNb (@orderedTeamNbs) {
my $r_idsInTeam=$r_battleStructure->{$teamNb};
if(all {@{$r_idsInTeam->{$_}{players}} && ! @{$r_idsInTeam->{$_}{bots}} } (keys %{$r_idsInTeam})) {
$playerTeam=$teamNb;
}elsif(all {@{$r_idsInTeam->{$_}{bots}} && ! @{$r_idsInTeam->{$_}{players}} } (keys %{$r_idsInTeam})) {
$aiBotTeam=$teamNb;
}
}
if(defined $playerTeam && defined $aiBotTeam) {
$teamSize=scalar keys %{$r_battleStructure->{$playerTeam}};
$aiBotTeamSize=scalar keys %{$r_battleStructure->{$aiBotTeam}};
}
}
my %context=(nbTeams => $nbTeams, teamSize => $teamSize, aiBotTeamSize => $aiBotTeamSize);
my %idColors;
my $r_teamsColorsList=SpadsConf::findMatchingData(\%context,$self->{colorRules});
if(@{$r_teamsColorsList}) {
my $r_teamsColors=$r_teamsColorsList->[0]{teamsColors};
if($aiBotTeamSize && $playerTeam > $aiBotTeam) {
$r_teamsColors=[$r_teamsColors->[1],$r_teamsColors->[0]];
}
for my $teamIdx (0..$#orderedTeamNbs) {
my $team=$orderedTeamNbs[$teamIdx];
my $r_teamColors=$r_teamsColors->[$teamIdx];
last unless(defined $r_teamColors);
my @orderedIdNbs=sort {$a <=> $b} keys %{$r_battleStructure->{$team}};
for my $idIdx (0..$#orderedIdNbs) {
my $r_color=$r_teamColors->[$idIdx];
last unless(defined $r_color);
$idColors{$orderedIdNbs[$idIdx]}=$r_color;
}
}
}else{
return undef;
}
return \%idColors;
}
1;