-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimporter.js
163 lines (148 loc) · 6.22 KB
/
importer.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import {renderSidebarTab} from "./src/renderSidebarTab.js";
import {canvasInit, canvasTearDown} from "./src/canvas.js";
import {notifyBuilding} from "./src/BuildingDialog.js";
import {notifyFactions} from "./src/FactionsDialog.js";
import {notifyBuildings} from "./src/BuildingsDialog.js";
import {notifyPeople} from "./src/PeopleDialog.js";
import {FANTASY_TOWN_GENERATOR_ORIGIN, SOCKET_EVENT} from "./src/constants.js";
import {ClientNotifications, notifyFTG, notifyOtherClients} from "./src/notifier.js";
import {FtgEvent} from "./src/listener.js";
import {notifyDistrict} from "./src/DistrictDialog.js";
Hooks.on("renderSidebarTab", renderSidebarTab);
Hooks.on("canvasInit", canvasInit);
Hooks.on("canvasTearDown", canvasTearDown);
Hooks.on("init", () => {
game.socket.on(SOCKET_EVENT, ({ type, data }) => {
if (type === ClientNotifications.NEW_PLAYER_LOCATION) {
// buildingId = data
notifyFTG("NEW_PLAYER_CURRENT_LOCATION", data)
} else if (type === ClientNotifications.NEW_SETTLEMENT_TIME) {
// { newTime, stateId } = data
notifyFTG("NEW_SETTLEMENT_TIME", data)
} else if (type === ClientNotifications.BUILDING_UPDATE) {
// { buildingId, stateId } = data
notifyFTG("UPDATE_BUILDING", data)
} else if (type === ClientNotifications.PERSON_UPDATE) {
// { personId, stateId } = data
notifyFTG("UPDATE_PERSON", data)
} else if (type === ClientNotifications.PEOPLE_UPDATE) {
// { stateId } = data
notifyFTG("UPDATE_PEOPLE", data)
} else if (type === ClientNotifications.BUILDINGS_UPDATE) {
// { stateId } = data
notifyFTG("UPDATE_BUILDINGS", data)
}
});
});
window.addEventListener(
"message",
(event) => {
if (event.origin === FANTASY_TOWN_GENERATOR_ORIGIN) {
const parsedData = JSON.parse(event.data)
if (parsedData.type === FtgEvent.BUILDING) {
notifyBuilding(parsedData.data);
} else if (parsedData.type === FtgEvent.FACTIONS) {
notifyFactions(parsedData.data);
} else if (parsedData.type === FtgEvent.BUILDINGS) {
notifyBuildings(parsedData.data);
} else if (parsedData.type === FtgEvent.PEOPLE) {
notifyPeople(parsedData.data);
} else if (parsedData.type === FtgEvent.NEW_PLAYER_CURRENT_LOCATION) {
notifyOtherClients(ClientNotifications.NEW_PLAYER_LOCATION, parsedData.data);
} else if (parsedData.type === FtgEvent.NEW_SETTLEMENT_TIME) {
notifyOtherClients(ClientNotifications.NEW_SETTLEMENT_TIME, parsedData.data);
} else if (parsedData.type === FtgEvent.BUILDING_UPDATE) {
const { data, stateId } = parsedData.data;
notifyOtherClients(ClientNotifications.BUILDING_UPDATE, { buildingId: data.id, stateId });
} else if (parsedData.type === FtgEvent.PERSON_UPDATE) {
const { data, stateId } = parsedData.data;
notifyOtherClients(ClientNotifications.PERSON_UPDATE,{ personId: data.id, stateId });
} else if (parsedData.type === FtgEvent.PEOPLE_UPDATE) {
const { stateId } = parsedData.data;
notifyOtherClients(ClientNotifications.PEOPLE_UPDATE,{ stateId });
} else if (parsedData.type === FtgEvent.BUILDINGS_UPDATE) {
const { stateId } = parsedData.data;
notifyOtherClients(ClientNotifications.BUILDINGS_UPDATE,{ stateId });
} else if (parsedData.type === FtgEvent.DISTRICT) {
notifyDistrict(parsedData.data);
}
// Capture mouse move events from the fantasy-town-generator iframe so that drag controls still work
// properly
if (parsedData.type === FtgEvent.MOUSE_MOVE) {
window.dispatchEvent(new MouseEvent('mousemove', parsedData.data));
window.dispatchEvent(new PointerEvent('pointermove', parsedData.data));
}
}
}
);
Handlebars.registerHelper('towngenHasOpenStatus', function (value) {
return value !== 'NOT_APPLICABLE';
});
Handlebars.registerHelper('towngenNotEmpty', function (value) {
return value !== null && value !== undefined && value !== '' && value.length !== 0;
});
Handlebars.registerHelper('towngenJson', function (value) {
return JSON.stringify(value)
});
Handlebars.registerHelper('towngenPrice', function (price) {
let result = '';
if (price.pp > 0) {
result += `${price.pp} pp `;
}
if (price.gp > 0) {
result += `${price.gp} gp `;
}
if (price.ep > 0) {
result += `${price.ep} ep `;
}
if (price.sp > 0) {
result += `${price.sp} sp `;
}
if (price.cp > 0) {
result += `${price.cp} cp `;
}
return result;
});
Handlebars.registerHelper('towngenFormatPeople', function (value) {
if (value === 0) {
return 'No-one';
}
if (value === 1) {
return 'One person';
}
return value + ' people';
});
Handlebars.registerHelper('towngenCapitalizeEnum', function (value) {
if (!value) {
return '';
}
const removedUnderscores = value.replace(/_/g, ' ');
return removedUnderscores.charAt(0) + removedUnderscores.slice(1).toLowerCase();
});
Handlebars.registerHelper('towngenLowercaseEnum', function (value) {
if (!value) {
return '';
}
return value.replace(/_/g, ' ').toLowerCase();
});
Handlebars.registerHelper('towngenAOrAn', function (value) {
const firstChar = value.toLowerCase().charAt(0);
if (['a', 'e', 'i', 'o', 'u'].includes(firstChar)) {
return 'an';
}
return 'a';
});
const formatList = items => {
if (items.length === 0) return '';
if (items.length === 1) return items[0];
return `${items.slice(0, items.length - 1).join(', ')} and ${items[items.length - 1]}`;
}
Handlebars.registerHelper('towngenFormatList', function (items) {
return formatList(items);
});
Handlebars.registerHelper('towngenFormatEnumList', function (items) {
return formatList(items.map(item => item.replace(/_/g, ' ').toLowerCase()));
});
Handlebars.registerHelper('towngenIsGm', function () {
return game?.user?.isGM;
});