Skip to content

Commit

Permalink
feat: Support commom offline player algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Feb 22, 2024
1 parent b99eb02 commit 13a0f74
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/user/offline.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { v3, v4 } from 'uuid'
import { createHash } from 'crypto'
import { v4 } from 'uuid'

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

export function getUUIDOfOfflinePlayer(username: string) {
const md5Bytes = createHash('md5').update(`OfflinePlayer:${username}`).digest()
md5Bytes[6] &= 0x0f /* clear version */
md5Bytes[6] |= 0x30 /* set to version 3 */
md5Bytes[8] &= 0x3f /* clear variant */
md5Bytes[8] |= 0x80 /* set to IETF variant */
return md5Bytes.toString('hex')
}

/**
* Create an offline auth. It'll ensure the user game profile's `uuid` is the same for the same `username`.
*
* @param username The username you want to have in-game.
*/
export function offline(username: string, uuid?: string) {
const id = (uuid || v3(username, '00000000-0000-0000-0000-000000000000')).replace(/-/g, '')
const id = (uuid || getUUIDOfOfflinePlayer(username))
const prof = {
id,
name: username,
Expand Down

0 comments on commit 13a0f74

Please sign in to comment.