-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator-bin.js
More file actions
88 lines (76 loc) · 2.48 KB
/
generator-bin.js
File metadata and controls
88 lines (76 loc) · 2.48 KB
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
const generator = require("./generators/generator");
const MAX_UINT32 = require( 'const-max-uint32');
const uuidV1 = require('uuid/v1');
const randomstring = require("randomstring");
const replaceall = require("replaceall");
const fs = require('fs');
const uuid = uuidV1();
const serverSeed = replaceall("-", "", uuid); // is generated for every game session
console.log("Server Seed:", serverSeed);
const playerWallet = "0x6a0fe2de79f61f2fd2f6caf528e4dec6ff8ef90e";
console.log("Player wallet:", playerWallet);
// binary sequence generation parameters (length, module, outputDir etc)
const length = 1000000000; // 1kkk
// const length = 100;
console.log("Sequence length:", length);
const mod = MAX_UINT32;
console.log("Module:", mod);
const outputDir = ".\\out";
const outputFile = outputDir + "\\sequence." + generator.algorithm + "." + length + "." + mod + ".bin";
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
fs.open(outputFile, 'w', function (status, fd) {
if (status) {
console.log(status.message);
return;
}
var before = Date.now();
var buffer = new Buffer(4000000);
var offset = 0;
for (var nonce = 1; nonce <= length; nonce++) {
// client seed may (and may not) be changed for every random value
var clientSeed = randomstring.generate({
length: 4,
charset: 'alphanumeric'
});
var result = generator.generate(serverSeed, clientSeed, playerWallet, nonce, mod);
buffer.writeUInt32LE(result, offset);
offset += 4;
if (nonce % 200000 == 0) {
console.log("nonce:", nonce, ". Duration:", Date.now() - before, "ms");
}
if (offset == buffer.length) {
fs.writeSync(fd, buffer, 0, buffer.length, null);
offset = 0;
}
}
if (offset != 0) {
fs.writeSync(fd, buffer, 0, offset, null);
}
fs.closeSync(fd);
console.log("The data have written to file:", outputFile, ". Total duration:", Date.now() - before, "ms");
// printBinValues(outputFile);
});
// print int 32 values from binary file to control written data
function printBinValues(filePath) {
console.log("Reading binary data from file:", filePath);
const stats = fs.statSync(filePath);
const fileSizeInBytes = stats.size;
fs.open(filePath, 'r', function (status, fd) {
if (status) {
console.log(status.message);
return;
}
var buffer = new Buffer(fileSizeInBytes);
fs.read(fd, buffer, 0, buffer.length, 0, function (err, data) {
if (err) {
throw err;
}
for (var i = 0; i < fileSizeInBytes; i += 4) {
var value = buffer.readUInt32LE(i);
console.log(value);
};
});
});
}