-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBestQLController.php
133 lines (121 loc) · 3.79 KB
/
BestQLController.php
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
<?php
namespace Budabot\User\Modules\BESTQL_MODULE;
/**
* @author Nadyita (RK5) <nadyita@hodorraid.org>
*
* @Instance
*
* Commands this controller contains:
* @DefineCommand(
* command = 'bestql',
* accessLevel = 'all',
* description = 'Find breakpoints for bonuses',
* help = 'bestql.txt',
* alias = 'breakpoints'
* )
*/
class BestQLController {
/**
* Name of the module.
* Set automatically by module loader.
*/
public $moduleName;
/**
* @Inject
* @var \Budabot\Core\Text $text
*/
public $text;
/**
* @Inject
* @var \Budabot\Core\SettingManager $settingManager
*/
public $settingManager;
/**
* Try to determine the bonus for an interpolated QL
*
* @param array $itemSpecs An associative array [QLX => bonus X, QLY => bonus Y]
* @param int $searchedQL The QL we want to interpolate to
* @return int|bool The interpolated bonus at the given QL or false if out of range
*/
public function calcStatFromQL($itemSpecs, $searchedQL) {
$lastSpec = null;
foreach ($itemSpecs as $itemQL => $itemBonus) {
if ($lastSpec === null) {
$lastSpec = [$itemQL, $itemBonus];
} else {
if ($lastSpec[0] <= $searchedQL && $itemQL >= $searchedQL) {
$multi = (1 / ($itemQL - $lastSpec[0]));
return $lastSpec[1] + ( ($itemBonus-$lastSpec[1]) * ($multi *($searchedQL-($lastSpec[0]-1)-1)));
} else {
$lastSpec = [$itemQL, $itemBonus];
}
}
}
return false;
}
/**
* @HandlesCommand("bestql")
* @Matches("/^bestql ([0-9 ]+)$/i")
* @Matches("{^bestql ([0-9 ]+) (<a href=(?:'|'|\x22)itemref://\d+/\d+/\d+(?:'|'|\x22)>[^<]+</a>)$}i")
*/
public function bestqlCommand($message, $channel, $sender, $sendto, $args) {
$itemSpecs = [];
$itemToScale = null;
if (count($args) > 2) {
$itemPattern = "{<a href=(?:'|'|\")itemref://(\d+)/(\d+)/(\d+)(?:'|'|\")>([^<]+)</a>}";
preg_match($itemPattern, $args[2], $itemToScale);
}
$specPairs = preg_split('/\s+/', $args[1]);
if (count($specPairs) < 4) {
$msg = "You have to provide at least 2 bonuses at 2 different QLs.";
$sendto->reply($msg);
return;
}
for ($i = 1; $i < count($specPairs); $i += 2) {
$itemSpecs[(int)$specPairs[$i-1]] = (int)$specPairs[$i];
}
ksort($itemSpecs);
$msg = '';
$numFoundItems = 0;
$oldRequirement = 0;
$maxAttribute = $specPairs[count($specPairs)-1];
$oldValue = null;
for ($searchedQL = min(array_keys($itemSpecs)); $searchedQL <= max(array_keys($itemSpecs)); $searchedQL++) {
$value = $this->calcStatFromQL($itemSpecs, $searchedQL);
if ($value === false) {
$msg = "I was unable to find any breakpoints for the given stats.";
$sendto->reply($msg);
return;
}
$value = round($value);
if (count($specPairs) % 2) {
if ($value > $maxAttribute) {
$msg = "The highest QL is <highlight>".($searchedQL-1)."<end> with a requirement of <highlight>$oldRequirement<end>. QL $searchedQL already requires $value.";
$sendto->reply($msg);
return;
}
$oldRequirement = $value;
} elseif ($oldValue !== $value) {
$msg .= sprintf("<tab>QL <highlight>%'_3d<end> has stat <highlight>%d<end>.", $searchedQL, $value);
if ($itemToScale) {
$msg .= " " . $this->text->makeItem($itemToScale[1], $itemToScale[2], $searchedQL, $itemToScale[4]);
}
$msg .= "\n";
$numFoundItems++;
$oldValue = $value;
}
}
$msg = preg_replace('/(_+)/', '<black>$1<end>', $msg);
$blob = $this->text->makeBlob("items", $msg, "Calculated breakpoints for your item");
if (is_string($blob)) {
$msg = "Found <highlight>$numFoundItems<end> $blob with different stats.";
$sendto->reply($msg);
return;
}
$pages = [];
for ($i = 0; $i < count($blob); $i++) {
$pages[] = "Found <highlight>$numFoundItems<end> ".$blob[$i]." with different stats.";
}
$sendto->reply($pages);
}
}