-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.coffee
170 lines (134 loc) · 4.51 KB
/
server.coffee
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
# Load requirements.
express = require 'express'
events = require 'events'
app = express()
http = require 'http'
fs = require 'fs'
# Serve static content.
app.use('/static', express.static __dirname + '/static')
# Start server.
server = http.createServer app
server.listen process.env.PORT or 4000
io = require('socket.io').listen server
PLAYERS_PER_GAME = 4
###### EXTRACT THIS LATER
class ChordGenerator extends events.EventEmitter
@NOTES_MAJOR = [0, 4, 7]
@NOTES_MINOR = [0, 3, 7]
@ROOTS = ['C', 'C#', 'D', 'Eb', 'E', 'F', 'F#', 'G', 'G#', 'A', 'Bb', 'B']
@TYPES = ['Major', 'Minor']
constructor: ->
@target = null
@notes = []
@mu = new MIDIUtil
getRandomChord: =>
@current = ChordGenerator.ROOTS[Math.floor(Math.random() * ChordGenerator.ROOTS.length)]
@currentNotes = []
for i in ChordGenerator.NOTES_MAJOR
@currentNotes.push ChordGenerator.ROOTS[(ChordGenerator.ROOTS.indexOf(@current) + i) % ChordGenerator.ROOTS.length]
@current
noteOn: (note) =>
noteName = @mu.midiToNoteName note
console.log "Note on: #{noteName}"
@notes.push(noteName) if noteName not in @notes
console.log "Notes: #{@notes}"
console.log "CurrentNotes: #{@currentNotes}"
console.log "Value: " + this.arraysEqual @notes, @currentNotes
if noteName not in @currentNotes
@score -= (@level * 20)
if this.arraysEqual @notes, @currentNotes
console.log "Chord matched!"
@notes = []
this.emit 'chordMatched'
noteOff: (note) =>
@notes = (x for x in @notes when x != @mu.midiToNoteName note)
arraysEqual: (a, b) ->
unless a instanceof Array and b instanceof Array
return false
unless a.length is b.length
return false
for x in a
if b.indexOf(x) < 0
return false
return true
getNotes: =>
@currentNotes
exports.ChordGenerator = ChordGenerator
class Game extends events.EventEmitter
constructor: ->
console.log "Starting a new game."
@players = [] # Players participating in this game.
@chordGenerator = new ChordGenerator # Generator for chords
@score = 0 # Number of points
@level = 0 # Number of chords successfully played.
@timer = null # How long until we finish the game?
@chordGenerator.on 'chordMatched', =>
@level += 1
@score = @score + (100 * @level)
p.emit 'gotIt' for p in @players
this.newTurn()
addPlayer: (player) =>
console.log "Adding player (game)"
@players.push player
player.on 'note_on', (note) =>
@chordGenerator.noteOn note
player.on 'note_off', (note) =>
@chordGenerator.noteOff note
start: =>
# Run a game.
console.log("Starting game.")
p.emit 'gameStart' for p in @players
this.newTurn()
newTurn: =>
console.log("New turn.")
timeout = 20000 - (1000 * @level)
if @timer?
clearTimeout @timer
@timer = setTimeout this.end, timeout
target = @chordGenerator.getRandomChord()
console.log "Broadcasting target " + target + " to all players."
p.emit('target', target, timeout) for p in @players
if @level < 5
p.emit('hint', @chordGenerator.getNotes()) for p in @players
end: =>
console.log "Game over; players scored #{@score}"
p.emit('gameOver', @level, @score) for p in @players
class GameServer extends events.EventEmitter
constructor: ->
@waitingroom = []
@games = []
addPlayer: (player) ->
console.log "Adding player (gameserver)."
@waitingroom.push player
if @waitingroom.length >= PLAYERS_PER_GAME
game = new Game
for i in [1..PLAYERS_PER_GAME]
game.addPlayer @waitingroom.pop()
game.start()
else
player.emit 'waiting', @waitingroom.length, PLAYERS_PER_GAME
class MIDIUtil
@NOTES = ['C', 'C#', 'D', 'Eb', 'E', 'F', 'F#', 'G', 'G#', 'A', 'Bb', 'B']
midiToNoteName: (num) ->
target = num % MIDIUtil.NOTES.length
MIDIUtil.NOTES[target]
offsetFromRoot: (root, target) ->
start = MIDIUtil.NOTES.indexOf root
end = MIDIUtil.NOTES.indexOf target
if end >= start
return end-start
else
return (MIDIUtil.NOTES.length-start)+end
exports.MIDIUtil = MIDIUtil
###### STOP EXTRACTING
gameServer = new GameServer
# Serve standard template.
app.get '/', (req, res) ->
fs.createReadStream('./views/index.html').pipe res
# Do things on connection.
io.sockets.on 'connection', (socket) ->
gameServer.addPlayer socket
socket.on 'note_on', (note) ->
socket.broadcast.emit 'note_on', note
socket.on 'note_off', (note) ->
socket.broadcast.emit 'note_off', note