-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetpatterns.js
144 lines (127 loc) · 3.94 KB
/
getpatterns.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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const fs = require("fs");
const http = require("http");
const StreamZip = require("node-stream-zip");
import sortedUniqBy from "lodash-es/sortedUniqBy";
import Pattern from "./src/pattern.js";
import {presetFromRle} from "./src/rle.js";
/**
* The hostname of the server where the ZIP file should be downloaded from.
*
* @type {string}
*/
const UPDATE_HOSTNAME = "www.conwaylife.com";
/**
* The path to the ZIP file on the server.
*
* @type {string}
*/
const UPDATE_PATH = "/patterns/all.zip";
/**
* The path where the downloaded ZIP file should be saved.
*
* @type {string}
*/
const ZIP_FILE = "patterns.zip";
/**
* The name of the JSON file to output patterns to.
*
* @type {string}
*/
const JSON_FILE = "patterns.json";
/**
* Downloads a file from a remote server if a newer version is available.
*
* @param {[type]} filename - The name of the local file.
* @param {[type]} hostname - The remote server's hostname.
* @param {[type]} path - The path to the file on the remote server.
* @param {function()} callback - Called when the update finishes, even if the file was not
* downloaded.
*/
function updateFile(filename, hostname, path, callback) {
let time;
try {
time = fs.statSync(filename).mtime.toGMTString();
} catch (e) {
time = null;
}
const headers = {};
if (time !== null) {
headers["If-Modified-Since"] = time;
}
const request = http.get({hostname, path, headers});
request.on("response", response => {
if (response.statusCode === 200) {
// Download the new version.
console.log("Updating " + filename + "...");
const file = fs.createWriteStream(filename);
response.pipe(file);
file.on("finish", () => {
console.log(filename + " has been updated");
file.close(callback);
});
} else if (response.statusCode === 304) {
// We don't need to do anything.
console.log(filename + " is up-to-date");
callback();
} else {
console.error(
"Error updating " + filename + ": Server responded with " + response.statusCode + " " +
response.statusMessage
);
callback();
}
});
request.on("error", error => {
console.error("Error updating " + filename + ": " + error.message);
callback();
});
}
/**
* Returns the list of pattern presets in the given ZIP file.
*
* @param {StreamZip} zip - The ZIP file.
* @return {PatternPreset[]} The list of pattern presets in the given ZIP file.
*/
function getPatternPresets(zip) {
return (
Object.values(zip.entries())
.filter(entry => entry.name.endsWith(".rle"))
.map(entry => presetFromRle(zip.entryDataSync(entry).toString()))
);
}
updateFile(ZIP_FILE, UPDATE_HOSTNAME, UPDATE_PATH, () => {
const zip = new StreamZip({file: ZIP_FILE, storeEntries: true});
zip.on("ready", () => {
// Get the patterns from the ZIP file and filter out patterns we don't want.
let patterns = getPatternPresets(zip).filter(preset => {
try {
const pattern = Pattern.fromPreset(preset);
return (
preset.name !== ""
&& !/agar/i.test(preset.description)
&& preset.rule === "B3/S23"
&& pattern.width <= 50
&& pattern.height <= 50
);
} catch (e) {
console.error((preset.name || "(no name)") + ": " + e.message);
return false;
}
});
// Sort the patterns alphabetically by name, but with pattern names that start with a number or
// symbol at the end.
patterns.sort((a, b) => {
if (/^[A-Za-z]/.test(a.name) !== /^[A-Za-z]/.test(b.name)) {
return /^[A-Za-z]/.test(a.name) ? -1 : 1;
} else if (a.name !== b.name) {
return a.name < b.name ? -1 : 1;
} else {
return 0;
}
});
// Remove duplicate pattern names.
patterns = sortedUniqBy(patterns, "name");
fs.writeFileSync(JSON_FILE, JSON.stringify(patterns, null, 2));
console.log("Wrote " + patterns.length + " patterns to " + JSON_FILE);
});
});