-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAllianceController.php
413 lines (374 loc) · 11.5 KB
/
AllianceController.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<?php declare(strict_types=1);
namespace Nadybot\User\Modules\ALLIANCE_MODULE;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
use Nadybot\Core\{
AccessLevelProvider,
AccessManager,
Attributes as NCA,
BuddylistManager,
CmdContext,
DB,
Event,
EventManager,
LoggerWrapper,
ModuleInstance,
Modules\PLAYER_LOOKUP\Guild,
Modules\PLAYER_LOOKUP\GuildManager,
Nadybot,
ParamClass\PNonNumber,
ParamClass\PRemove,
SQLException,
Text,
Util,
};
use Nadybot\Modules\ORGLIST_MODULE\{
FindOrgController,
Organization,
};
use stdClass;
/**
* @author Nadyita (RK5) <nadyita@hodorraid.org>
*/
#[
NCA\Instance,
NCA\HasMigrations,
NCA\DefineCommand(
command: "alliance",
accessLevel: "mod",
description: "Manage orgs of the alliance",
)
]
class AllianceController extends ModuleInstance implements AccessLevelProvider {
public const DB_MEMBERS = "alliance_members_<myname>";
public const DB_ORGS = "alliance_orgs_<myname>";
#[NCA\Inject]
public FindOrgController $findOrgController;
#[NCA\Inject]
public AccessManager $accessManager;
#[NCA\Inject]
public GuildManager $guildManager;
#[NCA\Inject]
public BuddylistManager $buddylistManager;
#[NCA\Inject]
public EventManager $eventManager;
#[NCA\Inject]
public Nadybot $chatBot;
#[NCA\Inject]
public DB $db;
#[NCA\Inject]
public Text $text;
#[NCA\Inject]
public Util $util;
#[NCA\Logger]
public LoggerWrapper $logger;
/** Which bot rank will all alliance members get */
#[NCA\Setting\Rank(accessLevel: "superadmin")]
public string $allianceMappedRank = "guild";
/**
* The rank for each member of this bot's alliance
* [(string)name => (int)rank]
* @var array<string,int>
*/
public array $allianceMembers = [];
#[NCA\Setup]
public function setup(): void {
$this->accessManager->registerProvider($this);
$query = $this->db->table(static::DB_MEMBERS, "a")
->leftJoin("players as p", function(JoinClause $join) {
$join->on("a.name", "p.name")
->where("p.dimension", $this->db->getDim())
->on("p.guild_id", "a.org_id");
})
->where("a.mode", "!=", "del")
->select("a.name");
$this->allianceMembers = $query->selectRaw(
"COALESCE(" . $query->grammar->wrap("p.guild_rank_id") . ", 6)".
$query->as("guild_rank_id")
)->get()
->reduce(function (array $carry, stdClass $row) {
$carry[(string)$row->name] = (int)$row->guild_rank_id;
return $carry;
}, []);
}
/**
* Make everyone in the alliance a "guild" member
*/
public function getSingleAccessLevel(string $sender): ?string {
if (isset($this->allianceMembers[$sender])) {
return $this->allianceMappedRank;
}
return null;
}
#[NCA\Event(
name: "timer(24hrs)",
description: "Download all alliance org information"
)]
public function downloadOrgRostersEvent(Event $eventObj): void {
$this->updateOrgRosters();
}
/**
* Do a manual alliance roster update
*/
#[NCA\HandlesCommand("alliance")]
public function allianceUpdateCommand(
CmdContext $context,
#[NCA\Str("update")] string $action
): void {
$context->reply("Starting Alliance Roster update");
$this->updateOrgRosters([$context, "reply"], "Finished Alliance Roster update");
}
public function updateOrgRosters(?callable $callback=null, mixed ...$args): void {
$this->logger->notice("Starting Alliance Roster update");
/** @var Collection<AllianceOrg> */
$orgs = $this->db->table(static::DB_ORGS)->asObj(AllianceOrg::class);
$i = 0;
foreach ($orgs as $org) {
$i++;
// Get the org info
$this->guildManager->getByIdAsync(
$org->org_id,
null,
true,
[$this, "updateRosterForGuild"],
function() use (&$i, $callback, $args): void {
if (--$i === 0) {
if (isset($callback)) {
$callback(...$args);
}
$this->logger->notice("Finished Alliance Roster update");
}
}
);
}
}
public function updateRosterForGuild(?Guild $org, ?callable $callback, mixed ...$args): void {
// Check if JSON file was downloaded properly
if ($org === null) {
$this->logger->error("Error downloading the guild roster JSON file");
return;
}
if (count($org->members) === 0) {
$this->logger->error("The organisation {$org->orgname} has no members. Not changing its roster");
return;
}
// Save the current org_members table in a var
/** @var array<string,AllianceMember> */
$dbEntries = $this->db->table(static::DB_MEMBERS)
->asObj(AllianceMember::class)
->keyBy("name")
->toArray();
$this->db->beginTransaction();
// Going through each member of the org and add or update his/her status
foreach ($org->members as $member) {
// don't do anything if $member is the bot itself
if (strtolower($member->name) === strtolower($this->chatBot->char->name)) {
continue;
}
// If there's already data about the character just update them
if (isset($dbEntries[$member->name])) {
if ($dbEntries[$member->name]->mode === "del") {
// members who are not on notify should not be on the buddy list but should remain in the database
$this->buddylistManager->remove($member->name, 'alliance');
unset($this->allianceMembers[$member->name]);
} elseif (isset($member->guild_rank_id)) {
// add org members who are on notify to buddy list
$this->buddylistManager->add($member->name, 'alliance');
$this->allianceMembers[$member->name] = $member->guild_rank_id;
// if member was added to notify list manually, switch mode to org and let guild roster update from now on
if ($dbEntries[$member->name]->mode === "add") {
$this->db->table(static::DB_MEMBERS)
->where("name", $member->name)
->update(["mode" => "org"]);
}
}
// Else insert their data
} elseif (isset($member->guild_rank_id)) {
// add new org members to buddy list
$this->buddylistManager->add($member->name, 'alliance');
$this->allianceMembers[$member->name] = $member->guild_rank_id;
$this->db->table(static::DB_MEMBERS)
->insert([
"org_id" => $org->guild_id,
"name" => $member->name,
"mode" => "org",
]);
}
unset($dbEntries[$member->name]);
}
$this->db->commit();
// remove buddies who are no longer org members
foreach ($dbEntries as $name => $buddy) {
if ($buddy->org_id === $org->guild_id && $buddy->mode !== 'add') {
$this->db->table(static::DB_MEMBERS)
->where("name", $name)
->where("org_id", $org->guild_id)
->delete();
$this->buddylistManager->remove($name, 'alliance');
unset($this->allianceMembers[$name]);
}
}
$this->logger->notice("Finished Roster update for {$org->orgname}");
if (isset($callback)) {
$callback(...$args);
}
}
/**
* Add an organization to your alliance
*/
#[NCA\HandlesCommand("alliance")]
public function allianceAddCommand(
CmdContext $context,
#[NCA\Str("add")] string $action,
PNonNumber $orgName
): void {
$hasOrglist = $this->eventManager->getKeyForCronEvent(86400, "findorgcontroller.parseAllOrgsEvent") !== null;
if (!$hasOrglist) {
$context->reply(
$this->text->blobWrap(
"In order to be able to search for orgs by name, you need to ",
$this->text->makeBlob(
"enable the ORGLIST_MODULE",
"[" . $this->text->makeChatcmd(
"enable it now",
"/tell <myname> config mod ORGLIST_MODULE enable all"
) . "] and wait a bit"
),
"."
)
);
return;
}
if (!$this->findOrgController->isReady()) {
$this->findOrgController->sendNotReadyError($context);
return;
}
$orgs = $this->findOrgController->lookupOrg($orgName());
$count = count($orgs);
if ($count === 0) {
$context->reply("No matches found.");
return;
}
$blob = $this->formatResults($orgs);
$msg = $this->text->makeBlob("Org Search Results for '{$orgName}' ($count)", $blob);
$context->reply($msg);
}
public function getOrg(int $orgId): ?Organization {
return $this->db->table("organizations")
->where("id", $orgId)
->asObj(Organization::class)
->first();
}
/**
* Add an organization to your alliance
*/
#[NCA\HandlesCommand("alliance")]
public function allianceAddIdCommand(
CmdContext $context,
#[NCA\Str("add")] string $action,
int $orgId
): void {
$org = $this->getOrg($orgId);
if (!isset($org)) {
$context->reply("No organization with ID <highlight>{$orgId}<end> found.");
return;
}
$alliance = new AllianceOrg();
$alliance->org_id = $org->id;
$alliance->added_by = $context->char->name;
$alliance->added_dt = time();
try {
$this->db->insert(static::DB_ORGS, $alliance, null);
} catch (SQLException $e) {
$context->reply("The organization <highlight>{$org->name}<end> is already a member of this alliance.");
return;
}
$context->reply("Added the organization <highlight>{$org->name}<end> to this alliance.");
$this->guildManager->getByIdAsync(
$org->id,
null,
true,
[$this, "updateRosterForGuild"],
[$context, "reply"],
"Added all members of <highlight>{$org->name}<end> to the roster."
);
}
/**
* Show a list of the orgs in your alliance
*/
#[NCA\HandlesCommand("alliance")]
public function allianceListCommand(
CmdContext $context,
#[NCA\Str("list")] string $action
): void {
$query = $this->db->table(static::DB_ORGS, "a")
->join("organizations as o", "a.org_id", "o.id")
->join(static::DB_MEMBERS . " as m", "m.org_id", "a.org_id")
->groupBy("a.org_id", "a.added_dt", "a.added_by", "o.name")
->select("a.*", "o.name");
$query->selectRaw($query->rawFunc("COUNT", "*", "members")->getValue());
/** @var Collection<AllianceOrgStats> */
$orgs = $query->asObj(AllianceOrgStats::class);
$count = $orgs->count();
if ($count === 0) {
$context->reply("There are currently no orgs in your alliance.");
return;
}
$blob = "";
foreach ($orgs as $org) {
$blob .= "<pagebreak><header2>{$org->name} ({$org->org_id})<end>\n".
"<tab>Members: <highlight>{$org->members}<end>\n".
"<tab>Joined: <highlight>" . $this->util->date($org->added_dt) . "<end>\n".
"<tab>Added by: <highlight>{$org->added_by}<end>\n".
"<tab>Action: [" . $this->text->makeChatcmd("remove", "/tell <myname> alliance rem {$org->org_id}") . "]\n\n";
}
$msg = $this->text->makeBlob("Orgs in your alliance ($count)", $blob);
$context->reply($msg);
}
/**
* Remove an org from your alliance
*/
#[NCA\HandlesCommand("alliance")]
public function allianceRemCommand(
CmdContext $context,
PRemove $action,
int $orgId
): void {
$org = $this->getOrg($orgId);
if (!isset($org)) {
$context->reply("No organization with ID <highlight>{$orgId}<end> found.");
return;
}
$deleted = $this->db->table(static::DB_ORGS)
->where("org_id", $org->id)
->delete();
if ($deleted < 1) {
$context->reply("The organization <highlight>{$org->name}<end> is not member of this alliance.");
return;
}
$this->db->table(static::DB_MEMBERS)
->where("org_id", $org->id)
->asObj(AllianceMember::class)
->each(function (AllianceMember $member) {
$this->buddylistManager->remove($member->name, "alliance");
});
$deleted = $this->db->table(static::DB_MEMBERS)
->where("org_id", $org->id)
->delete();
$context->reply(
"Removed the organization <highlight>{$org->name}<end> ".
"along with <highlight>{$deleted}<end> members from this alliance."
);
}
/**
* @param Organization[] $orgs
*/
public function formatResults(array $orgs): string {
$blob = '';
foreach ($orgs as $org) {
$addLink = $this->text->makeChatcmd('add', "/tell <myname> alliance add {$org->id}");
$blob .= "<{$org->faction}>{$org->name}<end> ({$org->id}) - {$org->num_members} members [$addLink]\n\n";
}
return $blob;
}
}