Skip to content

Commit 13a0f74

Browse files
committed
feat: Support commom offline player algorithm
1 parent b99eb02 commit 13a0f74

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

packages/user/offline.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { v3, v4 } from 'uuid'
1+
import { createHash } from 'crypto'
2+
import { v4 } from 'uuid'
23

34
/**
45
* Random generate a new token by uuid v4. It can be client or auth token.
@@ -8,13 +9,22 @@ export function newToken() {
89
return v4().replace(/-/g, '')
910
}
1011

12+
export function getUUIDOfOfflinePlayer(username: string) {
13+
const md5Bytes = createHash('md5').update(`OfflinePlayer:${username}`).digest()
14+
md5Bytes[6] &= 0x0f /* clear version */
15+
md5Bytes[6] |= 0x30 /* set to version 3 */
16+
md5Bytes[8] &= 0x3f /* clear variant */
17+
md5Bytes[8] |= 0x80 /* set to IETF variant */
18+
return md5Bytes.toString('hex')
19+
}
20+
1121
/**
1222
* Create an offline auth. It'll ensure the user game profile's `uuid` is the same for the same `username`.
1323
*
1424
* @param username The username you want to have in-game.
1525
*/
1626
export function offline(username: string, uuid?: string) {
17-
const id = (uuid || v3(username, '00000000-0000-0000-0000-000000000000')).replace(/-/g, '')
27+
const id = (uuid || getUUIDOfOfflinePlayer(username))
1828
const prof = {
1929
id,
2030
name: username,

0 commit comments

Comments
 (0)