X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=server.coffee;h=29b3e50c2a9ab9675f493e9ed8ca2d8cd80c0036;hb=f12edd687531576f662976ccb4d24a9f5cd42fd9;hp=c7d71b4a1ca6cf75054de9dd1b5088de1b3de8ff;hpb=4eb019697492c1f826548e05f0811cbef887a8ad;p=peach-cgt.git diff --git a/server.coffee b/server.coffee index c7d71b4..29b3e50 100644 --- a/server.coffee +++ b/server.coffee @@ -23,11 +23,17 @@ css_handler = (args, out, request, url_parts) -> 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 @@ -37,7 +43,10 @@ js_handler = (args, out, request, url_parts) -> 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) -> @@ -65,7 +74,6 @@ javascript_handler = (args, out, request, url_parts) -> 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' @@ -93,7 +101,6 @@ get_handler = (args, out, request, url_parts) -> 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' @@ -120,7 +127,7 @@ set_handler = (args, out, request, url_parts) -> 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) -> @@ -136,8 +143,9 @@ answer_now = (game) -> queue = game.p2_queue game.p2_waiter = false game.p2_queue = [] - waiter.writeHead 200, 'Content-Type': 'text/javascript' - waiter.end JSON.stringify queue + timeout 2000, -> # FIXME remove this delay for player 2 (just here to test lag handling) + waiter.writeHead 200, 'Content-Type': 'text/javascript' + waiter.end JSON.stringify queue # this marks a game as "dirty" and makes sure there's exactly one timeout # that'll respond to any clients that are waiting, and now have messages. @@ -154,7 +162,6 @@ forward_events = (message...) -> unless message[1] is 'p2' @p2_queue.push message answer_soon this - console.log this new_game = (id) -> game = games[id] = model.new 'server' @@ -163,12 +170,16 @@ new_game = (id) -> 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) -> @@ -184,20 +195,6 @@ new_game = (id) -> test_init = -> test_game = new_game 'test' - timeout 4000, -> - test_game.set_cards 'server', [ - { text: "Wildabeast 2/2", x: 220, y: 200} - { text: "Wild beast 2/2", x: 350, y: 200} - { text: "Angora bunny 1/1", x: 500, y: 200} - { text: "Ambulatory Cactus 2/1", x: 650, y: 200} - { text: "Ent 0/5", x: 800, y: 200} - { text: "Carnivore 2/1", x: 220, y: 420} - { text: "Herbivore 1/2", x: 350, y: 420} - { text: "Stone Wall 0/10", x: 500, y: 420} - { text: "Log 0/1", x: 650, y: 420} - { text: "Ent 0/5", x: 800, y: 420} - ] - test_init() @@ -223,10 +220,10 @@ http_server = http.createServer (req, res) -> 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'