-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserverMod.js
More file actions
302 lines (243 loc) · 9.38 KB
/
Copy pathserverMod.js
File metadata and controls
302 lines (243 loc) · 9.38 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
WARNING: THIS IS STILL EXPERIMENTAL STUFF
I want to have the ability to assign specific behaviors to each room without messing with the main engine
So I'm creating a module for server-side modifications (mods). There is one also for the client.
Their naming convention is roomIdFunction.
The functions are called by the engine at crucial points, only if they exist.
*/
//called at the beginning
module.exports.initMod = function (io, gameState, DATA) {
console.silentLog("MOD: Initialized");
//EVERYTHING GLOBALLLLL
global.gameState = gameState;
global.io = io;
global.DATA = DATA;
// lightState on and off
// projector, hall ,class, cave
global.lightState = [0, 0, 0, 0];
//global function ffs
global.random = function (min, max) {
return Math.random() * (max - min) + min;
}
// this turns on and off the projector every minute
// setInterval(function () {
// global.lightState[0] = global.lightState[0] === 1 ? 0 : 1;
// // emit change to all clients
// io.sockets.emit('changeBgAnim', 'bg' + global.lightState.join(""))
// }, 60 * 1000); //every minute changes
global.VIPList = [];
global.roomStates = {
cave: {
talk: false,
talkCounter: 0
}
}
global.truffautTalk = [
"Soy François Truffaut",
"me conoceras por...",
"los 400 golpes, la nouvelle vague,",
"y toda esa mandanga.",
"Está chulo esto,",
"Es un buen sitio...",
"para una sala de montaje.",
"aquí cabrían muchas mesas",
"Y hackintosh con FCP",
"y con premiere",
"premiere es igual de malo que FCP",
"esto de aventuras visuales esta muy bien",
"¿se puede uno apuntar?",
"Una vez comiendo con Hitch",
"le dije la frase de Jan Desmut:",
"<<El equipo que come unido...",
"permanece unido.>>",
"y que eso luego se nota en las pelis.",
"¿sabes lo que me contesto?",
"que el espectador es gilipollas",
"¿como te quedas?",
"con moita calma, eso le dije a Hitch",
"bueno, vamos a lo que vamos...",
"pospo a tope!!!",
"¿que nos impide?",
"tal vez la impresora, que no funciona.",
"¿que donde están las llaves? ",
"en el viriato, me han dicho",
"no se lo que es",
"creo que es la Jane d'arc zamorana",
"pero no se como salir de aquí",
"ahora, si me permites",
"tengo que hacer una introspección."
];
global.increaseTalkCounter = function() {
global.roomStates['cave'].talkCounter++;
if (global.roomStates['cave'].talkCounter > global.truffautTalk.length - 1) {
global.roomStates['cave'].talkCounter = 0;
global.roomStates['cave'].talk = false;
}
}
global.resetTruffaut = function() {
global.roomStates['cave'].talk = false;
global.roomStates['cave'].talkCounter = 0;
global.roomStates['cave'].followPlayer = false;
}
//Example of NPC creation and behavior
var truffaut = new NPC(
{
id: "truffaut",
nickName: "truffaut",
room: "cave",
x: 23,
y: 189,
avatar: 1,
colors: [2, 2, 1, 5],
labelColor: "#1e839d"
});
truffaut.behavior = setTimeout(function ramble() {
var dice = random(0, 100);
var talkCounter = global.roomStates['cave'].talkCounter;
var talk = global.roomStates['cave'].talk;
if (dice < 70) {
if (talk) {
truffaut.talk(global.truffautTalk[talkCounter]);
global.increaseTalkCounter();
truffaut.behavior = setTimeout(ramble, random(2000, 3000));
} else {
truffaut.move(random(10, 90) * 2, random(70, 78) * 2);
truffaut.behavior = setTimeout(ramble, random(2000, 3000));
}
} else if (dice < 90) {
truffaut.move(random(10, 90) * 2, random(70, 98) * 2);
truffaut.behavior = setTimeout(ramble, random(2000, 3000));
} else {
truffaut.behavior = setTimeout(ramble, random(2000, 3000));
}
}, random(2000, 3000));
}
module.exports.anyRoomJoin = function(player, roomId) {
// music
switch (roomId) {
case 'desierto':
io.to(player.id).emit('loopMusic', 'zombie');
break;
default:
io.to(player.id).emit('loopMusic', 'intro');
break;
}
}
// the cave uses the VIPRoom rules in original likelike
module.exports.caveJoin = function (playerObject, roomId) {
// console.silentLog(playerObject.nickName + " enters the VIP room");
//...
this.setLightState();
setTimeout(function() {
global.gameState.NPCs['truffaut'].talk('Hola aventurero');
setTimeout(function() {
global.roomStates['cave'].talk = true;
}, 1000)
}, 1000)
global.VIPList.push(playerObject.id);
if (global.VIPList.length > 2) {
var expelled = global.VIPList.shift();
//force leave
if (io.sockets.sockets[expelled] != null) {
this.transferPlayer(expelled, "cave", "hall", 34 * 2, 70 * 2);
io.to(expelled).emit('godMessage', "Sorry, we had to expel you to make room for " + playerObject.nickName);
}
}
if (global.VIPList.length === 1) {
global.resetTruffaut();
}
}
// the cave uses the VIPRoom rules in original likelike
module.exports.caveIntro = function (newComerId, introObj) {
//the problem with the auto expulsion in cave is that the intro is sent before it's the other person is expelled
//so the intros arrive too late creating ghosts.
//since the server has the real list I can override the intro after the fact and expel the ghost. Ugly but necessary.
// console.silentLog("Obsolete intro? " + gameState.players[introObj.id].room);
this.setLightState();
if (gameState.players[introObj.id].room != "Cave")
io.to("Cave").emit("playerLeft", { id: introObj.id, room: "Cave", disconnect: false });
}
//force change room
module.exports.transferPlayer = function (playerId, from, to, x, y) {
console.silentLog(playerId + " is transfered to " + to);
var s = io.sockets.sockets[playerId];
var p = gameState.players[playerId];
if (s != null)
if (p.room != null) {
//var from = p.room;
s.leave(from);
s.join(to);
//broadcast the change to everybody in the current room
//from the client perspective leaving the room is the same as disconnecting
io.to(from).emit("playerLeft", { id: playerId, room: from, disconnect: false });
//same for joining, sending everybody in the room the player state
p.room = to;
p.x = p.destinationX = x;
p.y = p.destinationY = y;
p.new = false;
//check if there is a custom function in the MOD to call at this point
if (this[from + "Leave"] != null) {
//call it!
this[from + "Leave"](p, from);
}
io.to(to).emit("playerJoined", p);
//check if there is a custom function in the MOD to call at this point
if (this[to + "Join"] != null) {
//call it!
this[to + "Join"](p, to);
}
//check if there are NPCs in this room and make them send info to the player
for (var NPCId in gameState.NPCs) {
var npc = gameState.NPCs[NPCId];
if (npc.room == to) {
npc.sendIntroTo(playerId);
}
}
}
}
//if a player leaves the room on their own accord make sure they are removed from the list as well
module.exports.caveLeave = function (playerObject, roomId) {
//console.silentLog(playerObject.nickName + " exits the VIP room");
var index = global.VIPList.indexOf(playerObject.id);
if (index !== -1) {
global.VIPList.splice(index, 1);
}
if (global.VIPList.length === 1) {
global.resetTruffaut();
}
}
module.exports.hallJoin = function(player, roomId) {
// console.silentLog("MOD: " + player.nickName + " entered room " + roomId);
this.setLightState();
}
module.exports.classroomJoin = function(player, roomId) {
// console.silentLog("MOD: " + player.nickName + " entered room " + roomId);
this.setLightState();
}
// LIGHTS
module.exports.onHallLight = function (pId) {
// console.silentLog('switch hall');
global.lightState[1] = global.lightState[1] ? 0 : 1
this.setLightState();
}
module.exports.onClassroomLight = function (pId) {
// console.silentLog('switch classroom');
global.lightState[2] = global.lightState[2] ? 0 : 1
this.setLightState();
}
module.exports.onCaveLight = function (pId) {
// console.silentLog('switch cave');
global.lightState[3] = global.lightState[3] ? 0 : 1
this.setLightState();
}
//
module.exports.setLightState = function (pId) {
// emit to all clients
io.sockets.emit('changeBgAnim', 'bg' + global.lightState.join(""));
}
module.exports.onMedvedkin = function (pId) {
global.lightState[0] = global.lightState[0] === 1 ? 0 : 1;
// emit change to all clients
io.sockets.emit('changeBgAnim', 'bg' + global.lightState.join(""));
io.sockets.emit('polcaToggle');
}