out.end css
js_handler = (args, out, request, url_parts) ->
+ convert = false
basename = url_parts.pathname.substr 1, (url_parts.pathname.length - 4)
if basename is 'client'
filename = 'client.coffee'
+ convert = true
else if basename is 'common'
filename = 'common.coffee'
+ convert = true
+ else if basename is 'cs_cards'
+ filename = 'cs_cards.js'
+ convert = false
else
error = "Unknown js basename: #{basename}"
console.log error
fs.readFile filename, 'utf8', (err, data) ->
if err
return out.end "Server failed to read #{filename}"
- out.end coffee.compile data
+ if convert
+ out.end coffee.compile data
+ else
+ out.end data
html_handler = (args, out, request, url_parts) ->
fs.readFile 'index.html', 'utf8', (err, data) ->
get_handler = (args, out, request, url_parts) ->
- console.log "get handler: ", args
unless args.game?.length
out.writeHead 404, "Content-Type": 'text/plain'
out.end 'Missing (or empty) "game" argument'
answer_soon game # in case there's something queued already
set_handler = (args, out, request, url_parts) ->
- console.log "set handler: ", args
unless args.game?.length
out.writeHead 404, "Content-Type": 'text/plain'
out.end 'Missing (or empty) "game" argument'
game.process_messages JSON.parse args.messages
out.writeHead 200, "Content-Type": 'text/plain'
- out.end 'ok'
+ out.end '{"status":0,"text_status":"Success"}'
# don't call this directly, call answer_soon instead
answer_now = (game) ->
unless message[1] is 'p2'
@p2_queue.push message
answer_soon this
- console.log this
new_game = (id) ->
game = games[id] = model.new 'server'
game.p1_queue = []
game.p2_queue = []
- game.on 'move', (agent, card, x, y) ->
- forward_events.call this, 'move', agent, card, x, y
+ game.on 'move', (agent, card, x, y, z, pile) ->
+ forward_events.call this, 'move', agent, card, x, y, z, pile
game.on 'mark', (agent, card, state) ->
forward_events.call this, 'mark', agent, card, state
game.on 'flip', (agent, card, state) ->
forward_events.call this, 'flip', agent, card, state
+ game.on 'new_cards', (agent, cards) ->
+ # server assigns card numbers, and tells both clients
+ # (unlike all other api calls, sending agent expects to get this one back)
+ forward_events.call this, 'new_cards', 'server', cards
game.on 'set_cards', (agent, cards) ->
forward_events.call this, 'set_cards', agent, cards
game.on 'send_state', (agent) ->
test_init = ->
test_game = new_game 'test'
- timeout 4000, ->
- test_game.set_cards 'server', [
- { text: "Wildabeast 2/2", x: 20, y: 140}
- { text: "Wild beast 2/2", x: 150, y: 140}
- { text: "Angora bunny 1/1", x: 300, y: 140}
- { text: "Ambulatory Cactus 2/1", x: 450, y: 140}
- { text: "Ent 0/5", x: 600, y: 140}
- ]
-
test_init()
req.on 'end', ->
query = url_parts.query
post_args = querystring.parse data
- console.log data, post_args
for key, parg of post_args
query[key] = parg
return set_handler query, res, req, url_parts
+ return
else if rel_path.substr(rel_path.length - 4) is '/get'
return get_handler url_parts.query, res, req, url_parts
else if rel_path.substr(rel_path.length - 4) is '.ico'