summaryrefslogtreecommitdiff
path: root/npc/functions/player-cache.txt
blob: 204d1fe12e61df587c1087d53005919d42a9784e (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
// 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 (.@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));
        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 (.@char = getcharid(CHAR_ID_CHAR, getarg(0))) {
            // player is currently online
            return .@char;
        }

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

        // player still not found: now we try SQL
        .@name$ = escape_sql(getarg(0));
        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 .@char;
        }

        // player doesn't exist
        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 {
        .@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
        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
     *
     * 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 ((.@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);
        }

        // not found
        return false;
    }

    // TODO: char2name

    /**
     * "playerCache"::vault2account(vault id) => account id
     *
     * Searches through the player cache to convert a Vault account ID into a
     * game account ID
     *
     * 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 (!SERVER_USES_VAULT) {
            return false;
        } else if (.@acc = .vault_to_account[getarg(0)]) {
            // found in the cache
            return .@acc;
        }
        return false;
    }

    // TODO: vault2char (weak reference)
    // TODO: vault2name (weak reference)

    /**
     * "playerCache"::account2vault(account id) => vault id
     *
     * Searches through the player cache to convert a game account ID into a
     * Vault account ID
     *
     * @param 0 - the account ID to search for
     * @return the Vault ID associated with the account
     */
    public function account2vault {
        if (!SERVER_USES_VAULT) {
            return false;
        } else if (.@vault = .account_to_vault[getarg(0)]) {
            return .@vault;
        } else {
            // TODO: find in SQL
            return false;
        }
        return false;
    }

    // TODO: char2vault
    // TODO: name2vault

    /**
     * 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();
}