Skip to content
This repository was archived by the owner on Feb 12, 2021. It is now read-only.

Game Code

Edward Smale edited this page Nov 10, 2020 · 2 revisions

Game codes are unique codes to identify games, they are sent as 32-bit signed integers. Due to rising popularly of the game, V2 codes were added to support more games and to make collisions less common. V1 codes are 4 characters long and use standard ASCII encoding. V2 codes are 6 characters long and use a special character map to map the characters to integer values.

Each character in a V2 code is mapped to certain byte values.

Character Value
A 19
B 15
C 13
D 0A
E 08
F 0B
G 0C
H 0D
I 16
J 0F
K 10
L 06
M 18
N 17
O 12
P 07
Q 00
R 03
S 09
T 04
U 0E
V 14
W 01
X 02
Y 05
Z 11

The values can then be combined into a single integer using the following expression: code = (((bytes[0] + 26 * bytes[1]) & 0x3FF) | (((bytes[2] + 26 * (bytes[3] + 26 * (bytes[4] + 26 * bytes[5]))) << 10) & 0x3FFFFC00) | 0x80000000);

Example code

function V1Code2Int(code) {
	return code.charCodeAt(0)
		| code.charCodeAt(1) << 8
		| code.charCodeAt(2) << 16
		| code.charCodeAt(3) << 24;
}

function Int2V1Code(code) {
	let code = "";
	code += String.fromCharCode(code & 0xFF);
	code += String.fromCharCode((code >> 8) & 0xFF);
	code += String.fromCharCode((code >> 16) & 0xFF);
	code += String.fromCharCode((code >> 24) & 0xFF);
	return code;
}

const V2 = "QWXRTYLPESDFGHUJKZOCVBINMA";
const V2Map = [ 25, 21, 19, 10, 8, 11, 12, 13, 22, 15, 16, 6, 24, 23, 18, 7, 0, 3, 9, 4, 14, 20, 1, 2, 5, 17 ];

function V2Code2Int(code) {
    var a = V2Map[code.charCodeAt(0) - 65];
    var b = V2Map[code.charCodeAt(1) - 65];
    var c = V2Map[code.charCodeAt(2) - 65];
    var d = V2Map[code.charCodeAt(3) - 65];
    var e = V2Map[code.charCodeAt(4) - 65];
    var f = V2Map[code.charCodeAt(5) - 65];

    var one = (a + 26 * b) & 0x3FF;
    var two = (c + 26 * (d + 26 * (e + 26 * f)));

    return (one | ((two << 10) & 0x3FFFFC00) | 0x80000000);
}

function Int2V2Code(bytes) {
    var a = bytes & 0x3FF;
    var b = (bytes >> 10) & 0xFFFFF;

    return V2[a % 26] +
        V2[Math.floor(a / 26)] +
        V2[b % 26] +
        V2[Math.floor(b / 26 % 26)] +
        V2[Math.floor(b / (26 * 26) % 26)] +
        V2[Math.floor(b / (26 * 26 * 26) % 26)];
}

Clone this wiki locally