44
55class PowerTarget extends AbstractTarget
66{
7- public const REGEX = '/^(\d{1,2}:\d{2})\s*-\s*(\d{1,2}:\d{2})\s*(mpk|mpm)?$/ ' ;
7+ public const REGEX_WATTS = '/^(\d+)(?:\s*-\s*(\d+))?\s*(w|watts)$/i ' ;
8+ public const REGEX_CP = '/^(\d+)(?:\s*-\s*(\d+))?\s*(%cp|pcp)$/i ' ;
89
9- public static function testPower ($ powerText ): false |\App \Library \Parser \Model \Target \PowerTarget
10+ public static function testPower ($ powerText, ? int $ criticalPower = null ): false |\App \Library \Parser \Model \Target \PowerTarget
1011 {
11- $ result = $ powerText && preg_match (self ::REGEX , (string ) $ powerText , $ power );
12- if (!$ result ) {
13- return false ;
14- }
15- if (!isset ($ power [1 ])) {
16- return false ;
17- }
18- if (empty ($ power [1 ])) {
19- return false ;
20- }
21- if (!isset ($ power [2 ])) {
22- return false ;
12+ $ powerText = (string ) $ powerText ;
13+
14+ // First, try to match percentage-based power (%cp)
15+ if ($ criticalPower > 0 && preg_match (self ::REGEX_CP , $ powerText , $ matches )) {
16+ if (!isset ($ matches [1 ]) || empty ($ matches [1 ])) {
17+ return false ;
18+ }
19+
20+ $ fromPercent = (int ) $ matches [1 ];
21+ $ from = (int ) round ($ criticalPower * ($ fromPercent / 100 ));
22+
23+ // If a range is provided (e.g., 90-95%cp), calculate the 'to' value.
24+ if (isset ($ matches [2 ]) && !empty ($ matches [2 ])) {
25+ $ toPercent = (int ) $ matches [2 ];
26+ $ to = (int ) round ($ criticalPower * ($ toPercent / 100 ));
27+ } else {
28+ $ to = $ from + 1 ;
29+ }
30+
31+ return new PowerTarget ($ from , $ to );
2332 }
24- if (empty ($ power [2 ])) {
25- return false ;
33+
34+ // If %cp doesn't match or isn't applicable, try to match absolute watts
35+ if (preg_match (self ::REGEX_WATTS , $ powerText , $ matches )) {
36+ if (!isset ($ matches [1 ]) || empty ($ matches [1 ])) {
37+ return false ;
38+ }
39+
40+ $ from = (int ) $ matches [1 ];
41+
42+ if (isset ($ matches [2 ]) && !empty ($ matches [2 ])) {
43+ $ to = (int ) $ matches [2 ];
44+ } else {
45+ $ to = $ from + 1 ;
46+ }
47+
48+ return new PowerTarget ($ from , $ to );
2649 }
27- return new PowerTarget ($ power [1 ], $ power [2 ]);
50+
51+ return false ;
2852 }
2953
30- public function __construct (protected $ from , protected $ to )
54+ public function __construct (protected int $ from , protected int $ to )
3155 {
56+ // Ensure 'from' is always less than 'to'.
57+ if ($ this ->from > $ this ->to ) {
58+ [$ this ->from , $ this ->to ] = [$ this ->to , $ this ->from ]; // Swap values
59+ }
60+
3261 }
3362
3463 protected function getTypeId (): int
@@ -38,7 +67,7 @@ protected function getTypeId(): int
3867
3968 protected function getTypeKey (): string
4069 {
41- return 'power.zone ' ;
70+ return 'power.range ' ;
4271 }
4372
4473 protected function getTargetValueOne ()
0 commit comments