-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplaying.js
78 lines (66 loc) · 3.03 KB
/
playing.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
var Ending = require('./ending.js');
var Playing = function(game) {
this.onStasisStart = function(event, channel) {
console.log('Channel %s is trying to enter after the game has started');
channel.hangup();
}
this.onDtmfReceived = function(event, participant) {
var nextRoom = null;
if (event.digit == '2') {
// They want to go up
console.log('Channel %s wants to move to the room above them', participant.channel.id);
nextRoom = participant.room.up;
} else if (event.digit == '8') {
// They want to go down
console.log('Channel %s wants to move to the room below them', participant.channel.id);
nextRoom = participant.room.down;
} else if (event.digit == '4') {
// They want to go left
console.log('Channel %s wants to move to the room to the left of them', participant.channel.id);
nextRoom = participant.room.left;
} else if (event.digit == '6') {
// They want to go right
console.log('Channel %s wants to move to the room to the right of them', participant.channel.id);
nextRoom = participant.room.right;
} else if (event.digit == '0') {
// They want to grab any hiding participant
if (participant.role != 'seeker') {
console.log('Channel %s wants to grab hiders but they are not a seeker', participant.channel.id);
return;
}
console.log('Channel %s is grabbing all hiders in room %d(bridge %s)', participant.channel.id, participant.room.id, participant.room.bridge.id);
game.webSocketServer.notifyObservers(JSON.stringify({ type: 'catch_attempt', room: participant.room.id, channel: participant.channel.id, id: participant.id }));
participant.room.occupants.forEach(function(item) {
if (item.role != 'hider') {
return;
}
console.log('Channel %s was caught by %s, they are now a seeker', item.id, item.channel.id);
game.hiders--;
game.seekers++;
item.role = 'seeker';
game.webSocketServer.notifyObservers(JSON.stringify({ type: 'hider_caught', room: participant.room.id, channel: item.channel.id, id: item.id }));
});
console.log('Seeker count is now %d and hider count is now %d', game.seekers, game.hiders);
if (game.hiders == 0) {
// All the hiders are gone, the game can end
console.log('The game has no hiders left in it, considering it ended');
game.webSocketServer.notifyObservers(JSON.stringify({ type: 'game_ended' }));
game.maze.playSoundAll('sound:beeperr');
//XXX Transition to next state (Maybe allocate a new game and go back to pregame?)
//
game.state = new Ending(game);
} else {
participant.room.bridge.play({media: 'sound:beep'});
}
return;
}
if (nextRoom == null) {
console.log('Channel %s tried to move in a direction where no room exists', participant.channel.id);
participant.playSound(game.ari, 'sound:oops1');
game.webSocketServer.notifyObservers(JSON.stringify({ type: 'invalid_room_move', room: participant.room.id, channel: participant.channel.id, id: participant.id, direction: event.digit }));
return;
}
game.joinRoom(nextRoom, participant);
}
};
module.exports = Playing;