├── LICENSE
├── README.md
├── app.js
├── app
├── ending.js
├── game.js
├── maze.js
├── participant.js
├── playing.js
├── pregame.js
├── room.js
└── ws.js
├── frontend
├── css
│ └── style.css
├── images
│ ├── hider.png
│ └── seeker.png
├── index.html
├── js
│ ├── 3rdparty
│ │ ├── easeljs-0.8.1.min.js
│ │ ├── jquery-2.1.3.min.js
│ │ ├── randomColor.min.js
│ │ └── soundjs-0.6.1.min.js
│ ├── connection.js
│ ├── events.js
│ ├── game.js
│ ├── gameboard.js
│ └── message-simulator.js
└── sounds
│ ├── catch-attempt.mp3
│ ├── game-ended.mp3
│ ├── game-started.mp3
│ ├── hider-caught.mp3
│ ├── invalid-room-move.mp3
│ ├── join-game.mp3
│ ├── join-room.mp3
│ ├── leave-game.mp3
│ ├── leave-room.mp3
│ └── sources.txt
└── package.json
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
203 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # hidenseek
2 | Asterisk ARI Hide 'n' Seek
3 |
4 | This provides a simple Hide 'n' Seek game that demonstrates the Asterisk Rest Interface (ARI) in a fun way.
5 |
6 | # Installation
7 | This game has a few dependencies, such as the "websocket" and "ari-client" npm modules, which are listed in the package.json file. To install these, simply run the following
8 | from your command line.
9 | ````
10 | npm install
11 | ````
12 |
13 | # Asterisk configuration
14 | In order to use ARI applications, you will need to enable Asterisk's HTTP server, as well as enabling ARI. A simple
15 | http.conf file you can use would be the following
16 | ````
17 | [general]
18 | enabled = yes
19 | bindaddr = 0.0.0.0
20 | bindport = 8088
21 | ````
22 | A simple ari.conf file you can use would be the following
23 | ````
24 | [general]
25 | enabled = yes
26 | allowed_origins = *
27 | [asterisk]
28 | type = user
29 | password = asterisk
30 | ````
31 | Note that the code in the hide 'n' seek application has the built-in assumption that they can use ARI as user "asterisk"
32 | with password "asterisk". If you wish to use a different username and password combination, you will need to set ari.conf
33 | to use your desired username and password, and alter the ````client.connect()``` line in client.js to use the appropriate
34 | HTTP URI, username and password you would like.
35 |
36 | Once you have ARI configured, you will need to provide a way for incoming calls to reach your ARI application. The
37 | application name is "hide-n-seek". It is suggested that you provide two entry points to the application, one for hiders
38 | and one for seekers. The following is a suggested dialplan:
39 | ````
40 | [default]
41 | exten => 500,1,NoOp()
42 | same => n,Stasis(hide-n-seek,hider)
43 | same => n,Hangup()
44 |
45 | exten => 501,1,NoOp()
46 | same => n,Stasis(hide-n-seek,seeker)
47 | same => n,Hangup()
48 | ````
49 | With this setup, callers who dial 500 will be hiders, and callers who dial 501 will be seekers.
50 |
51 | # Running the application
52 | Once you have Asterisk configured, and you have the prerequisites installed, running the app simply requires you to start
53 | Asterisk, and then run the following with HOST and PORT set to your Asterisk HOST and PORT
54 | ````
55 | export HOST=http://localhost
56 | export PORT=8088
57 | node app.js
58 | ````
59 | from your checkout of this repo. The client will connect to Asterisk and will be ready to accept incoming calls.
60 |
61 | # Viewing the game from a browser
62 | Point your browser at http://localhost:3000
63 |
64 | Enjoy!
65 |
--------------------------------------------------------------------------------
/app.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var client = require('ari-client');
4 | var Game = require('./app/game');
5 | var express = require('express');
6 |
7 | var host = process.env.HOST || 'http://127.0.0.1';
8 | var port = process.env.PORT || '8088';
9 |
10 | console.log('connecting to Asterisk on ' + host + ':' + port);
11 | client.connect(host + ':' + port, 'asterisk', 'asterisk')
12 | .then(function(ari) {
13 | var game;
14 |
15 | function onDtmfReceived(event, channel) {
16 | var participant = game.participants.filter(function(item) {
17 | return item.channel.id === channel.id;
18 | })[0];
19 | game.state.onDtmfReceived(event, participant);
20 | }
21 |
22 | function onStasisStart(event, channel) {
23 | game.state.onStasisStart(event, channel);
24 | channel.on('ChannelDtmfReceived', onDtmfReceived);
25 | }
26 |
27 | function onStasisEnd(event, channel) {
28 | console.log('Channel %s leaving hide and seek', channel.id);
29 | var participant = game.participants.filter(function(item) {
30 | return item.channel.id === channel.id;
31 | })[0];
32 | // Drop the respective count
33 | if (participant.role == 'seeker') {
34 | game.seekers--;
35 | } else if (participant.role == 'hider') {
36 | game.hiders--;
37 | }
38 | console.log('Seeker count is now %d and hider count is now %d', game.seekers, game.hiders);
39 | // It's safe to call joinRoom with a null room, it'll just end up removing it from the one it is in
40 | game.joinRoom(null, participant);
41 | game.webSocketServer.notifyObservers(JSON.stringify({ type: 'leave_game', channel: channel.id, id: participant.id, role: participant.role }));
42 | // Since the channel is going away remove it as a valid participant
43 | var i = game.participants.indexOf(participant);
44 | game.participants.splice(i, 1);
45 |
46 | if (game.hiders == 0 || game.seekers == 0) {
47 | console.log('The game has no hiders or seekers left in it, considering it ended');
48 | game.webSocketServer.notifyObservers(JSON.stringify({ type: 'game_ended' }));
49 | game.maze.playSoundAll('sound:beep');
50 | }
51 | }
52 |
53 | game = new Game(ari, null);
54 | ari.on('StasisStart', onStasisStart);
55 | ari.on('StasisEnd', onStasisEnd);
56 | ari.start('hide-n-seek');
57 | })
58 | .catch(function (err) {
59 | console.log(err); // TODO: don't know why this isn't called on invalid host/port
60 | });
61 |
62 | var app = express();
63 | app.use(express.static('frontend'));
64 | var webserverPort = process.env.WEBSERVERPORT || 3000;
65 | app.listen(webserverPort, function() {
66 | console.log('static webserver listening on port ' + webserverPort);
67 | });
68 |
--------------------------------------------------------------------------------
/app/ending.js:
--------------------------------------------------------------------------------
1 | var Ending = function(game) {
2 | this.onStasisStart = function(event, channel) {
3 | console.log('Channel %s is trying to enter after the game has ended', channel.id);
4 | channel.hangup();
5 | }
6 | this.onDtmfReceived = function(event, participant) {
7 | // Just ignore
8 | return;
9 | }
10 |
11 | game.end();
12 | }
13 |
14 | module.exports = Ending;
15 |
--------------------------------------------------------------------------------
/app/game.js:
--------------------------------------------------------------------------------
1 | var Pregame = require('./pregame');
2 | var Maze = require('./maze');
3 | var Participant = require('./participant');
4 | var WebSocket = require('./ws');
5 |
6 | var Game = function(ari, websock) {
7 | this.ari = ari;
8 | this.participants = [];
9 | this.participantID = 1;
10 | this.hiders = 0;
11 | this.seekers = 0;
12 | this.state = new Pregame(this);
13 | this.maze = new Maze(ari);
14 |
15 | if (websock != null) {
16 | this.webSocketServer = websock;
17 | } else {
18 | this.webSocketServer = new WebSocket(this);
19 | }
20 |
21 | this.addParticipant = function(channel, role) {
22 | var participant = new Participant(channel, role, this.participantID++);
23 | if (role == 'seeker') {
24 | this.seekers++;
25 | } else if (role == 'hider') {
26 | this.hiders++;
27 | }
28 | this.participants.push(participant);
29 | this.webSocketServer.notifyObservers(JSON.stringify({ type: 'join_game', channel: channel.id, id: participant.id, role: participant.role }));
30 |
31 | return participant;
32 | }
33 |
34 | this.joinRoom = function(room, participant) {
35 | if (participant.room) {
36 | console.log('Channel %s leaving room %d(bridge %s)', participant.channel.id, participant.room.id, participant.room.bridge.id);
37 | var i = participant.room.occupants.indexOf(participant);
38 | participant.room.occupants.splice(i, 1);
39 | if (participant.role == 'seeker') {
40 | participant.room.bridge.play({media: 'sound:confbridge-leave'});
41 | }
42 | this.webSocketServer.notifyObservers(JSON.stringify({ type: 'leave_room', room: participant.room.id, channel: participant.channel.id, id: participant.id, role: participant.role }));
43 | }
44 | participant.room = room;
45 | if (room) {
46 | console.log('Channel %s entering room %d(bridge %s) as %s', participant.channel.id, room.id, room.bridge.id, participant.role);
47 | room.occupants.push(participant);
48 | // addChannel will move the channel as appropriate, we don't need to explicitly remove
49 | if (participant.role == 'seeker') {
50 | room.bridge.play({media: 'sound:confbridge-join'});
51 | }
52 | room.bridge.addChannel({channel: participant.channel.id});
53 | participant.playSound(ari, 'number:' + room.id);
54 | this.webSocketServer.notifyObservers(JSON.stringify({ type: 'join_room', room: participant.room.id, channel: participant.channel.id, id: participant.id, role: participant.role }));
55 | }
56 | }
57 |
58 | this.end = function() {
59 | for (var i = 0; i < this.participants.length; i++) {
60 | this.participants[i].channel.hangup();
61 | }
62 | this.maze.end();
63 | }
64 | };
65 |
66 | module.exports = Game;
67 |
--------------------------------------------------------------------------------
/app/maze.js:
--------------------------------------------------------------------------------
1 | var Room = require('./room');
2 |
3 | function horizontalLink(leftRoom, rightRoom) {
4 | leftRoom.right = rightRoom;
5 | rightRoom.left = leftRoom;
6 | }
7 |
8 | function verticalLink(upRoom, downRoom) {
9 | upRoom.down = downRoom;
10 | downRoom.up = upRoom;
11 | }
12 |
13 | var Maze = function(ari) {
14 | this.rooms = [];
15 |
16 | var room1 = new Room(ari, 1);
17 | this.rooms.push(room1);
18 | var room2 = new Room(ari, 2);
19 | this.rooms.push(room2);
20 | var room3 = new Room(ari, 3);
21 | this.rooms.push(room3);
22 | horizontalLink(room1, room2);
23 | horizontalLink(room2, room3);
24 |
25 | var room4 = new Room(ari, 4);
26 | this.rooms.push(room4);
27 | var room5 = new Room(ari, 5);
28 | this.rooms.push(room5);
29 | var room6 = new Room(ari, 6);
30 | this.rooms.push(room6);
31 | var room7 = new Room(ari, 7);
32 | this.rooms.push(room7);
33 | var room14 = new Room(ari, 14);
34 | this.rooms.push(room14);
35 | horizontalLink(room4, room5);
36 | horizontalLink(room5, room6);
37 | horizontalLink(room6, room7);
38 | verticalLink(room2, room4);
39 | verticalLink(room3, room5);
40 |
41 | var room8 = new Room(ari, 8);
42 | this.rooms.push(room8);
43 | var room9 = new Room(ari, 9);
44 | this.rooms.push(room9);
45 | var room10 = new Room(ari, 10);
46 | this.rooms.push(room10);
47 | var room11 = new Room(ari, 11);
48 | this.rooms.push(room11);
49 | var room12 = new Room(ari, 12);
50 | this.rooms.push(room12);
51 | var room13 = new Room(ari, 13);
52 | this.rooms.push(room13);
53 | horizontalLink(room8, room9);
54 | horizontalLink(room10, room11);
55 | horizontalLink(room11, room12);
56 | horizontalLink(room12, room13);
57 | verticalLink(room4, room9);
58 | verticalLink(room6, room10);
59 | verticalLink(room7, room11);
60 | verticalLink(room14, room13);
61 |
62 | var room15 = new Room(ari, 15);
63 | this.rooms.push(room15);
64 | var room16 = new Room(ari, 16);
65 | this.rooms.push(room16);
66 | var room17 = new Room(ari, 17);
67 | this.rooms.push(room17);
68 | var room18 = new Room(ari, 18);
69 | this.rooms.push(room18);
70 | var room19 = new Room(ari, 19);
71 | this.rooms.push(room19);
72 | var room20 = new Room(ari, 20);
73 | this.rooms.push(room20);
74 | var room21 = new Room(ari, 21);
75 | this.rooms.push(room21);
76 | horizontalLink(room15, room16);
77 | horizontalLink(room16, room17);
78 | horizontalLink(room17, room18);
79 | horizontalLink(room18, room19);
80 | horizontalLink(room20, room21);
81 | verticalLink(room8, room15);
82 | verticalLink(room10, room18);
83 | verticalLink(room11, room19);
84 | verticalLink(room13, room21);
85 |
86 | var room22 = new Room(ari, 22);
87 | this.rooms.push(room22);
88 | var room23 = new Room(ari, 23);
89 | this.rooms.push(room23);
90 | var room24 = new Room(ari, 24);
91 | this.rooms.push(room24);
92 | var room25 = new Room(ari, 25);
93 | this.rooms.push(room25);
94 | horizontalLink(room23, room24);
95 | verticalLink(room16, room22);
96 | verticalLink(room18, room23);
97 | verticalLink(room21, room25);
98 |
99 | this.getRandomRoom = function() {
100 | var roomNumber = Math.floor(Math.random() * (this.rooms.length));
101 | return this.rooms[roomNumber];
102 | }
103 |
104 | this.playSoundAll = function(sound) {
105 | this.rooms.forEach(function(room) {
106 | if (room.occupants.length > 0) {
107 | room.bridge.play({media: sound});
108 | }
109 | });
110 | }
111 |
112 | this.end = function() {
113 | for (var i = 0; i < this.rooms.length; i++) {
114 | this.rooms[i].bridge.destroy();
115 | }
116 | }
117 | };
118 |
119 | module.exports = Maze;
120 |
--------------------------------------------------------------------------------
/app/participant.js:
--------------------------------------------------------------------------------
1 | var Participant = function(channel, role, id) {
2 | // Channel for the participant
3 | this.channel = channel;
4 | // Role of the participant
5 | this.role = role;
6 | // Numerical identifier of the participant
7 | this.id = id;
8 | // Room the participant is currently in
9 | this.room = null;
10 | // Playback queue
11 | this.playbacks = [];
12 |
13 | this.playSound = function(ari, sound) {
14 | var participant = this;
15 | participant.playbacks.push(sound);
16 |
17 | function onPlaybackFinished() {
18 | var played = participant.playbacks.shift();
19 |
20 | console.log('Completed playing %s on channel %s', played, participant.channel.id);
21 |
22 | var prompt = participant.playbacks[0];
23 |
24 | if (prompt == null) {
25 | console.log('Playback queue on channel %s is now empty', participant.channel.id);
26 | return;
27 | }
28 |
29 | console.log('Playing %s on channel %s', prompt, participant.channel.id);
30 | participant.channel.play({media: prompt})
31 | .then(function (playback) {
32 | playback.on('PlaybackFinished', onPlaybackFinished);
33 | });
34 | }
35 |
36 | // If this is the first thing in the queue start playing it back
37 | // Otherwise it'll play in order once the current thing finishes
38 | if (participant.playbacks.length == 1) {
39 | console.log('Playing initial sound %s on channel %s', sound, participant.channel.id);
40 | participant.channel.play({media: sound})
41 | .then(function (playback) {
42 | playback.on('PlaybackFinished', onPlaybackFinished);
43 | });
44 | } else {
45 | console.log('Something is already playing on channel %s, adding %s to queue', participant.channel.id, sound);
46 | }
47 | }
48 | }
49 | module.exports = Participant;
50 |
--------------------------------------------------------------------------------
/app/playing.js:
--------------------------------------------------------------------------------
1 | var Ending = require('./ending.js');
2 |
3 | var Playing = function(game) {
4 | this.onStasisStart = function(event, channel) {
5 | console.log('Channel %s is trying to enter after the game has started');
6 | channel.hangup();
7 | }
8 |
9 | this.onDtmfReceived = function(event, participant) {
10 | var nextRoom = null;
11 |
12 | if (event.digit == '2') {
13 | // They want to go up
14 | console.log('Channel %s wants to move to the room above them', participant.channel.id);
15 | nextRoom = participant.room.up;
16 | } else if (event.digit == '8') {
17 | // They want to go down
18 | console.log('Channel %s wants to move to the room below them', participant.channel.id);
19 | nextRoom = participant.room.down;
20 | } else if (event.digit == '4') {
21 | // They want to go left
22 | console.log('Channel %s wants to move to the room to the left of them', participant.channel.id);
23 | nextRoom = participant.room.left;
24 | } else if (event.digit == '6') {
25 | // They want to go right
26 | console.log('Channel %s wants to move to the room to the right of them', participant.channel.id);
27 | nextRoom = participant.room.right;
28 | } else if (event.digit == '0') {
29 | // They want to grab any hiding participant
30 | if (participant.role != 'seeker') {
31 | console.log('Channel %s wants to grab hiders but they are not a seeker', participant.channel.id);
32 | return;
33 | }
34 |
35 | console.log('Channel %s is grabbing all hiders in room %d(bridge %s)', participant.channel.id, participant.room.id, participant.room.bridge.id);
36 |
37 | game.webSocketServer.notifyObservers(JSON.stringify({ type: 'catch_attempt', room: participant.room.id, channel: participant.channel.id, id: participant.id }));
38 |
39 | participant.room.occupants.forEach(function(item) {
40 | if (item.role != 'hider') {
41 | return;
42 | }
43 | console.log('Channel %s was caught by %s, they are now a seeker', item.id, item.channel.id);
44 | game.hiders--;
45 | game.seekers++;
46 | item.role = 'seeker';
47 | game.webSocketServer.notifyObservers(JSON.stringify({ type: 'hider_caught', room: participant.room.id, channel: item.channel.id, id: item.id }));
48 | });
49 |
50 | console.log('Seeker count is now %d and hider count is now %d', game.seekers, game.hiders);
51 |
52 | if (game.hiders == 0) {
53 | // All the hiders are gone, the game can end
54 | console.log('The game has no hiders left in it, considering it ended');
55 | game.webSocketServer.notifyObservers(JSON.stringify({ type: 'game_ended' }));
56 | game.maze.playSoundAll('sound:beeperr');
57 | //XXX Transition to next state (Maybe allocate a new game and go back to pregame?)
58 | //
59 | game.state = new Ending(game);
60 | } else {
61 | participant.room.bridge.play({media: 'sound:beep'});
62 | }
63 |
64 | return;
65 | }
66 |
67 | if (nextRoom == null) {
68 | console.log('Channel %s tried to move in a direction where no room exists', participant.channel.id);
69 | participant.playSound(game.ari, 'sound:oops1');
70 | game.webSocketServer.notifyObservers(JSON.stringify({ type: 'invalid_room_move', room: participant.room.id, channel: participant.channel.id, id: participant.id, direction: event.digit }));
71 | return;
72 | }
73 |
74 | game.joinRoom(nextRoom, participant);
75 | }
76 | };
77 |
78 | module.exports = Playing;
79 |
--------------------------------------------------------------------------------
/app/pregame.js:
--------------------------------------------------------------------------------
1 | var Playing = require('./playing');
2 |
3 | var Pregame = function(game) {
4 | this.onStasisStart = function(event, channel) {
5 | channel.answer(function(err) {
6 | var room = game.maze.getRandomRoom();
7 | var joiner = game.addParticipant(channel, event.args[0]);
8 | console.log('Channel %s entered app', channel.id);
9 | console.log('Seeker count is now %d and hider count is now %d', game.seekers, game.hiders);
10 | joiner.playSound(game.ari, 'sound:queue-thereare');
11 | joiner.playSound(game.ari, 'number:' + joiner.id);
12 | joiner.playSound(game.ari, 'sound:conf-enteringno');
13 | game.joinRoom(room, joiner);
14 | });
15 | }
16 |
17 | this.onDtmfReceived = function(event, participant) {
18 | if (event.digit == '*') {
19 | if (participant.role != 'seeker') {
20 | console.log('Channel %s wants to start the game but they are not a seeker', participant.channel.id);
21 | participant.playSound(game.ari, 'sound:beeperr');
22 | return;
23 | } else if (game.hiders == 0) {
24 | console.log('Channel %s wants to start the game but there are no hiders', participant.channel.id);
25 | participant.playSound(game.ari, 'sound:beeperr');
26 | return;
27 | }
28 | game.webSocketServer.notifyObservers(JSON.stringify({ type: 'game_started' }));
29 | game.maze.playSoundAll('sound:beep');
30 | //XXX This transition should probably be less explicit and hidden inside a game state machine.
31 | game.state = new Playing(game);
32 | } else {
33 | console.log('Channel %s pressed DTMF %s but that is invalid in the Pregame state', participant.channel.id, event.digit);
34 | }
35 | }
36 | };
37 |
38 | module.exports = Pregame;
39 |
--------------------------------------------------------------------------------
/app/room.js:
--------------------------------------------------------------------------------
1 | var Room = function(ari, id) {
2 | // Numerical room ID
3 | this.id = id;
4 | // Room to the left
5 | this.left = null;
6 | // Room to the right
7 | this.right = null;
8 | // Room upward
9 | this.up = null;
10 | // Room downward
11 | this.down = null;
12 | // Room occupants
13 | this.occupants = [];
14 | // Underlying conference bridge
15 | this.bridge = ari.Bridge();
16 | this.bridge.create({type: 'mixing,dtmf_events'});
17 | }
18 |
19 | module.exports = Room;
20 |
--------------------------------------------------------------------------------
/app/ws.js:
--------------------------------------------------------------------------------
1 | var http = require('http');
2 | var webSocketServer = require('websocket').server;
3 |
4 | var WebSocket = function(game) {
5 | var self = this;
6 | this.observers = [];
7 |
8 | function onObserverConnect(request) {
9 | console.log('New observer connection from ' + request.origin);
10 |
11 | var connection = request.accept(null, request.origin);
12 | connection.on('close', onObserverDisconnect);
13 |
14 | self.observers.push(connection);
15 |
16 | game.participants.forEach(function(participant) {
17 | connection.sendUTF(JSON.stringify({ type: 'join_game', channel: participant.channel.id, role: participant.role }));
18 | connection.sendUTF(JSON.stringify({ type: 'join_room', room: participant.room.id, channel: participant.channel.id, role: participant.role }));
19 | });
20 | }
21 |
22 | function onObserverDisconnect(connection) {
23 | console.log('Observer disconnected');
24 |
25 | var i = self.observers.indexOf(connection);
26 | self.observers.splice(i, 1);
27 | }
28 |
29 | var server = http.createServer(function(request, response) {
30 | });
31 | server.listen(6066, function() {
32 | console.log('WebSocket server listening on port 6066');
33 | });
34 |
35 | var wsServer = new webSocketServer({
36 | httpServer: server
37 | });
38 | wsServer.on('request', onObserverConnect);
39 |
40 | this.notifyObservers = function(message) {
41 | self.observers.forEach(function(connection) {
42 | connection.sendUTF(message);
43 | });
44 | }
45 | }
46 |
47 | module.exports = WebSocket;
48 |
--------------------------------------------------------------------------------
/frontend/css/style.css:
--------------------------------------------------------------------------------
1 | .avatar {
2 | display: none;
3 | border-radius:50%;
4 | overflow:hidden;
5 | }
6 |
7 |
--------------------------------------------------------------------------------
/frontend/images/hider.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/putnopvut/hidenseek/8a8bd6548f04c93a8340e841c31455d7f27a44bd/frontend/images/hider.png
--------------------------------------------------------------------------------
/frontend/images/seeker.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/putnopvut/hidenseek/8a8bd6548f04c93a8340e841c31455d7f27a44bd/frontend/images/seeker.png
--------------------------------------------------------------------------------
/frontend/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |