summaryrefslogtreecommitdiff
path: root/npc/functions/player-cache.txt
blob: f134f9538a8727d2d24ab5ed6bf56dd23e871858 (plain) (blame)
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// Player cache system
//
// Holds a cache of online players in-memory even after logout. This greatly
// reduces the need to query the SQL database for data that is frequently
// accessed.
//
// NOTE: This NPC uses the new public/private function call system to avoid
//       polluting the global function namespace
//
// Example usage:
//    .@account_id = "playerCache"::name2account("Reid");
//

-	script	playerCache	FAKE_NPC,{
    /**
     * "playerCache"::name2account("char name") => account id
     *
     * Searches through the player cache to convert a char name to a account ID.
     * If the player is not found, checks the SQL table. If the player doesn't
     * exist, returns false.
     *
     * @param 0 - the char name to search
     * @return the account id of the char
     */
    public function name2account {
        if (getarg(0) == "") {
            return false;
        }

        if (.@acc = getcharid(CHAR_ID_ACCOUNT, getarg(0))) {
            // player is currently online
            return .@acc;
        }

        if (.@acc = htget(.name_to_account, getarg(0), 0)) {
            // player found in the hash table
            return .@acc;
        }

        // player still not found: now we try SQL
        .@name$ = escape_sql(getarg(0));

        if (SERVER_USES_VAULT) {
            query_sql(sprintf("SELECT c.account_id, c.char_id, r.value "
                "FROM `char` c "
                "JOIN `global_acc_reg_num_db` r ON r.account_id = c.account_id "
                "WHERE r.key = '##VAULT' AND r.index = 0 AND c.name = '%s' "
                "LIMIT 1;", .@name$),
                .@acc, .@char, .@vault);

            if (.@vault > 0) {
                .vault_to_account[.@vault] = .@acc;
                .account_to_vault[.@acc] = .@vault;
            }
        } else {
            query_sql(sprintf("SELECT account_id, char_id FROM `char` WHERE `name`='%s' LIMIT 1;", .@name$), .@acc, .@char);
        }

        if (.@acc > 0) {
            // player found: add to our cache
            htput(.name_to_account, getarg(0), .@acc);
            .account_to_name$[.@acc] = getarg(0);
            htput(.name_to_char, getarg(0), .@char);
            .char_to_name$[.@char] = getarg(0);

            return .@acc;
        }

        // player doesn't exist
        return false;
    }

    /**
     * "playerCache"::name2char("char name") => char id
     *
     * Searches through the player cache to convert a char name to a char ID.
     * If the player is not found, checks the SQL table. If the player doesn't
     * exist, returns false.
     *
     * @param 0 - the char name to search
     * @return the char id of the char
     */
    public function name2char {
        if ((.@acc = name2account(getarg(0))) != 0) {
            return htget(.name_to_char, getarg(0), false);
        }

        return false;
    }

    /**
     * "playerCache"::char2account(char id) => account id
     *
     * Searches through the player cache to convert a char ID to an account ID.
     * If the player is not found, checks the SQL table. If the player doesn't
     * exist, returns false.
     *
     * @param 0 - the char ID to search
     * @return the account id of the char
     */
    public function char2account {
        if (getarg(0) == 0) {
            return false;
        }

        .@name$ = .char_to_name$[getarg(0)];

        if (.@name$ != "") {
            if (.@acc = getcharid(CHAR_ID_ACCOUNT, .@name$)) {
                // player is currently online
                return .@acc;
            }

            if (.@acc = htget(.name_to_account, .@name$, 0)) {
                // player found in the hash table
                return .@acc;
            }
        }

        // player still not found: now we try SQL
        if (SERVER_USES_VAULT) {
            query_sql(sprintf("SELECT c.account_id, c.name, r.value "
                "FROM `char` c "
                "JOIN `global_acc_reg_num_db` r ON r.account_id = c.account_id "
                "WHERE r.key = '##VAULT' AND r.index = 0 AND c.char_id = '%d' "
                "LIMIT 1;", getarg(0)),
                .@acc, .@name$, .@vault);

            if (.@vault > 0) {
                .vault_to_account[.@vault] = .@acc;
                .account_to_vault[.@acc] = .@vault;
            }
        } else {
            query_sql(sprintf("SELECT account_id, name FROM `char` WHERE `char_id`='%d' LIMIT 1;", getarg(0)), .@acc, .@name$);
        }

        if (.@acc > 0) {
            // player found: add to our cache
            htput(.name_to_account, .@name$, .@acc);
            .account_to_name$[.@acc] = .@name$;
            htput(.name_to_char, .@name$, getarg(0));
            .char_to_name$[getarg(0)] = .@name$;

            return .@acc;
        }

        // player doesn't exist
        return false;
    }

    /**
     * "playerCache"::account2char(account id) => char id
     *
     * Searches through the player cache to convert an account ID into a
     * char ID. If the player is not found, returns false.
     *
     * NOTE: this is a weak reference; an account ID does not uniquely identify
     *       a character
     *
     * @param 0 - the account ID to search for
     * @return the char ID of the char
     */
    public function account2char {
        if (getarg(0) == 0) {
            return false;
        }

        if ((.@name$ = strcharinfo(PC_NAME, "", getarg(0))) != "") {
            // player is online
            return getcharid(CHAR_ID_CHAR, .@name$);
        } else if ((.@name$ = .account_to_name$[getarg(0)]) != "") {
            // found in our cache
            return htget(.name_to_char, .@name$, false);
        }

        // player still not found: now we try SQL
        if (SERVER_USES_VAULT) {
            query_sql(sprintf("SELECT c.char_id, c.name, r.value "
                "FROM `char` c "
                "JOIN `global_acc_reg_num_db` r ON r.account_id = c.account_id "
                "WHERE r.key = '##VAULT' AND r.index = 0 AND c.account_id = '%d' "
                "ORDER BY c.last_login DESC LIMIT 1;", getarg(0)),
                .@char, .@name$, .@vault);

            if (.@vault > 0) {
                .vault_to_account[.@vault] = getarg(0);
                .account_to_vault[getarg(0)] = .@vault;
            }
        } else {
            query_sql(sprintf("SELECT char_id, name FROM `char` WHERE account_id='%d' ORDER BY last_login DESC LIMIT 1;", getarg(0)), .@char, .@name$);
        }

        if (.@char > 0) {
            // player found: add to our cache
            htput(.name_to_account, .@name$, getarg(0));
            .account_to_name$[getarg(0)] = .@name$;
            htput(.name_to_char, .@name$, .@char);
            .char_to_name$[.@char] = .@name$;

            return .@char;
        }

        // not found
        return false;
    }

    /**
     * "playerCache"::account2name(account id) => char name
     *
     * Searches through the player cache to convert an account ID into a
     * char name. If the account is not found, returns an empty string.
     *
     * NOTE: this is a weak reference; an account ID does not uniquely identify
     *       a character
     *
     * @param 0 - the account ID to search for
     * @return the name of the char
     */
    public function account2name {
        if ((.@char = account2char(getarg(0))) != false) {
            return .char_to_name$[.@char];
        }

        return "";
    }

    /**
     * "playerCache"::char2name(char id) => char name
     *
     * Searches through the player cache to convert a char ID to a char name.
     * If the player is not found, checks the SQL table. If the player doesn't
     * exist, returns an empty string.
     *
     * @param 0 - the char ID to search
     * @return the name of the char
     */
    public function char2name {
        if ((.@acc = char2account(getarg(0))) != 0) {
            return .account_to_name$[.@acc];
        }

        // player not found
        return "";
    }

    /**
     * "playerCache"::vault2account(vault id) => account id
     *
     * Searches through the player cache to convert a Vault account ID into a
     * game account ID. If the account is not found, returns false
     *
     * NOTE: this is a weak reference; a Vault ID does not uniquely identify
     *       a game account
     *
     * @param 0 - the Vault ID to search for
     * @return the account id of the char
     */
    public function vault2account {
        if (getarg(0) == 0) {
            return false;
        }

        if (!SERVER_USES_VAULT) {
            return getarg(0);
        } else if (.@acc = .vault_to_account[getarg(0)]) {
            // found in the cache
            return .@acc;
        }

        // player still not found: now we try SQL
        query_sql(sprintf("SELECT c.account_id, c.char_id, c.name "
            "FROM `char` c "
            "JOIN `global_acc_reg_num_db` r ON r.account_id = c.account_id "
            "WHERE r.key = '##VAULT' AND r.index = 0 AND r.value = %d "
            "ORDER BY c.`last_login` DESC LIMIT 1;", getarg(0)),
            .@acc, .@char, .@name$);

        if (.@char > 0) {
            // player found: add to our cache
            htput(.name_to_account, .@name$, .@acc);
            .account_to_name$[.@acc] = .@name$;
            htput(.name_to_char, .@name$, .@char);
            .char_to_name$[.@char] = .@name$;
            .vault_to_account[getarg(0)] = .@acc;
            .account_to_vault[.@acc] = getarg(0);

            return .@acc;
        }

        return false;
    }

    /**
     * "playerCache"::vault2char(vault id) => char id
     *
     * Searches through the player cache to convert a Vault account ID into a
     * char id. If the player is not found, returns false
     *
     * NOTE: this is a weak reference; a Vault ID does not uniquely identify
     *       a character
     *
     * @param 0 - the Vault ID to search for
     * @return the char id
     */
    public function vault2char {
        if ((.@acc = vault2account(getarg(0))) != 0) {
            return account2char(.@acc);
        }

        // player not found
        return false;
    }

    /**
     * "playerCache"::vault2name(vault id) => account id
     *
     * Searches through the player cache to convert a Vault account ID into a
     * char name. If the player is not found, returns an empty string
     *
     * NOTE: this is a weak reference; a Vault ID does not uniquely identify
     *       a character
     *
     * @param 0 - the Vault ID to search for
     * @return the name of the char
     */
    public function vault2name {
        if ((.@acc = vault2account(getarg(0))) != 0) {
            return account2name(.@acc);
        }

        // player not found
        return "";
    }

    /**
     * "playerCache"::account2vault(account id) => vault id
     *
     * Searches through the player cache to convert a game account ID into a
     * Vault account ID. If the account is not found, returns false
     *
     * @param 0 - the account ID to search for
     * @return the Vault ID associated with the account
     */
    public function account2vault {
        if (!SERVER_USES_VAULT) {
            return getarg(0);
        }

        account2char(getarg(0)); // will fetch vault id
        return .account_to_vault[getarg(0)];
    }

    /**
     * "playerCache"::name2vault(char name) => vault id
     *
     * Searches through the player cache to convert a character name into a
     * Vault account ID. If the account is not found, returns false
     *
     * @param 0 - the char name to search for
     * @return the Vault ID associated with the account
     */
    public function name2vault {
        return account2vault(name2account(getarg(0)));
    }

    /**
     * "playerCache"::char2vault(char id) => vault id
     *
     * Searches through the player cache to convert a char id into a
     * Vault account ID. If the account is not found, returns false
     *
     * @param 0 - the char id to search for
     * @return the Vault ID associated with the account
     */
    public function char2vault {
        return account2vault(char2account(getarg(0)));
    }

    /**
     * Registers a public local function that will be called when the char name
     * associated with an account changes. The previous account ID will be
     * passed as first argument to the provided function.
     *
     * @param 0 - the public local function
     */
    public function addNameHandler {
        return addEventListener(.name_handlers, getarg(0));
    }

    /**
     * Registers a public local function that will be called when the account
     * associated with a Vault account changes. The previous account id will be
     * passed as first argument to the provided function and the previous
     * char name will be passed as second argument.
     *
     * @param 0 - the public local function
     */
    public function addVaultHandler {
        return addEventListener(.vault_handlers, getarg(0));
    }



////////////////////////////////////////////////////////////////////////////////

    /**
     * updates the char name cache and fires event handlers when the name
     * associated with an account changes (such as when switching char)
     */
    private function updateName {
        // get the cached name:
        .@old$ = .account_to_name$[playerattached()];

        // now update the cache:
        htput(.name_to_account, strcharinfo(PC_NAME), playerattached());
        .account_to_name$[playerattached()] = strcharinfo(PC_NAME);
        htput(.name_to_char, strcharinfo(PC_NAME), getcharid(CHAR_ID_CHAR));
        .char_to_name$[getcharid(CHAR_ID_CHAR)] = strcharinfo(PC_NAME);

        if (.@old$ != "" && .@old$ != .account_to_name$[playerattached()]) {
            // fire event handlers
            dispatchEvent(.name_handlers, .@old$);
            return true;
        }

        return false;
    }

    /**
     * updates the Vault account cache and fires event handlers when the account
     * associated with a Vault account changes (different game account on same
     * Vault account)
     */
    private function updateVault {
        // get the cached id:
        .@old = .vault_to_account[getvaultid()];
        .@old$ = .account_to_name$[.@old];

        // now update the cache:
        .vault_to_account[getvaultid()] = playerattached();
        .account_to_vault[playerattached()] = getvaultid();

        if (.@old > 0 && .@old != .vault_to_account[getvaultid()]) {
            // fire event handlers
            dispatchEvent(.vault_handlers, .@old, .@old$);
            return true;
        }

        return false;
    }

    /**
     * adds/updates the player to the player cache
     */
    private function updateCache {
        updateName();

        if (SERVER_USES_VAULT) {
            updateVault();
        }

        return;
    }

    /**
     * force-add all online players to the cache (used on init and reload)
     */
    private function forceCache {
        .@count = getunits(BL_PC, .@units, false);

        for (.@i = 0; .@i < .@count; ++.@i) {
            attachrid(.@units[.@i]);
            updateCache();
            detachrid();
        }

        return;
    }

OnPCLoginEvent:
    updateCache();
    end;

OnInit:
    .name_to_account = htnew(); // Map<char name, account id>
    .account_to_name$[0] = ""; // sparse Array<char name> indexed by account id
    .name_to_char = htnew(); // Map<char name, char id>
    .char_to_name$[0] = ""; // sparse Array<char name> indexed by char id

    if (SERVER_USES_VAULT) {
        .vault_to_account[0] = 0; // sparse Array<vault id> indexed by account id
        .account_to_vault[0] = 0; // sparse Array<account id> indexed by vault id
    }

    // event handlers:
    .vault_handlers = htnew();
    .name_handlers = htnew();

    // reloadscript handler:
    forceCache();
}