-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProcessData.js
executable file
·108 lines (76 loc) · 2.94 KB
/
ProcessData.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
let fs = require('fs');
let path = require('path');
let rnUtils = require('./RoomNameUtils');
let shards = require('./shards');
let RoomDataManager = require('./RoomDataManager');
let PortalDataManager = require('./PortalDataManager');
module.exports = function() {
let allianceData = getAllianceData();
shards.forEach((shard,i) => processData(shard, allianceData, i > 0));
let directory = path.join(process.cwd(), 'public', 'js');
createDirIfNotExists(directory);
let filename = path.join(directory, 'shards.js');
console.log(filename);
fs.writeFileSync(filename, `let shards = ${JSON.stringify(shards)};`);
};
function getAllianceData() {
let alliancesString = fs.readFileSync('./alliances.js', 'utf8');
let alliances = JSON.parse(alliancesString);
let alliancesByPlayer = {};
Object.keys(alliances).forEach(allianceName => {
let alliance = alliances[allianceName];
alliance.members.forEach(member => {
alliancesByPlayer[member] = alliance.abbreviation;
});
});
return {playersByAlliance: alliances, allianceByPlayer: alliancesByPlayer}
}
function processData(shard, allianceData, append=false) {
let portalData = getPortalDataManager(shard);
let roomManager = getRoomDataManager(shard);
let sectors = {};
let rooms = roomManager.getRooms();
console.log('Started processing sectors');
Object.keys(rooms).forEach(roomName => {
let sectorName = rnUtils.getSectorCenter(roomName);
if (!sectors[sectorName]) {
sectors[sectorName] = {players: {}, rooms: {}, alliances: {}};
}
let sector = sectors[sectorName];
let room = rooms[roomName];
sector.players[roomManager.getRoomOwner(roomName).toLowerCase()] = true;
let alliance = roomManager.getRoomAlliance(roomName, allianceData);
if (alliance) {
sector.alliances[alliance.toLowerCase()] = true;
}
room.alliance = alliance;
sector.rooms[roomName] = room;
});
console.log('Finished processing sectors');
let directory = path.join(process.cwd(), 'public', 'js');
createDirIfNotExists(directory);
let filename = path.join(directory, 'data.js');
console.log(filename);
let method = append ? fs.appendFileSync : fs.writeFileSync;
let toWrite = ` allData.${shard}_portals = ${JSON.stringify(portalData.parsed)};
allData.${shard}_rooms = ${JSON.stringify(rooms)};
allData.${shard}_sectors = ${JSON.stringify(sectors)};
allData.${shard}_updated = ${Date.now()}; `;
if (!append) {
toWrite = `let allData = {}; ${toWrite}`;
}
method(filename, toWrite);
}
function createDirIfNotExists(dirPath) {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
}
function getPortalDataManager(shard) {
let portalsString = fs.readFileSync(`./${shard}/portals.js`, 'utf8');
return new PortalDataManager(portalsString);
}
function getRoomDataManager(shard) {
let roomsString = fs.readFileSync(`./${shard}/rooms.js`, 'utf8');
return new RoomDataManager(roomsString);
}