-
Notifications
You must be signed in to change notification settings - Fork 3
/
converter.js
63 lines (54 loc) · 1.5 KB
/
converter.js
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
'use strict';
// This file reads in input.gior, parses/deserializes it, and writes the result to output.gioreplay.
var fs = require('fs');
var LZString = require('lz-string');
// fs.readFileSync returns a Buffer since we didn't specify an option.
var input = fs.readFileSync('./input.gior');
// Returns an object that represents the replay.
// @param serialized A serialized replay Buffer.
function deserialize(serialized) {
var obj = JSON.parse(
LZString.decompressFromUint8Array(
new Uint8Array(serialized)
)
);
var replay = {};
var i = 0;
replay.version = obj[i++];
replay.id = obj[i++];
replay.mapWidth = obj[i++];
replay.mapHeight = obj[i++];
replay.usernames = obj[i++];
replay.stars = obj[i++];
replay.cities = obj[i++];
replay.cityArmies = obj[i++]
replay.generals = obj[i++];
replay.mountains = obj[i++];
replay.moves = obj[i++].map(deserializeMove);
replay.afks = obj[i++].map(deserializeAFK);
replay.teams = obj[i++];
replay.map_title = obj[i++]; // only available when version >= 7
return replay;
};
function deserializeMove(serialized) {
return {
index: serialized[0],
start: serialized[1],
end: serialized[2],
is50: serialized[3],
turn: serialized[4],
};
}
function deserializeAFK(serialized) {
return {
index: serialized[0],
turn: serialized[1],
};
}
// Write the converted file.
try {
fs.writeFileSync('./output.gioreplay', JSON.stringify(deserialize(input)));
console.log('Conversion successful!');
} catch (e) {
console.error('Failed to write output file', e);
}