JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Piles display and removing cards mostly working
[peach-cgt.git] / server.coffee
index ab0ca9f..089523a 100644 (file)
@@ -65,7 +65,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 +92,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 +118,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 +134,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 +153,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,26 +161,42 @@ 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) ->
+               forward_events.call this, 'move', agent, card, x, y, z
        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 'set_cards', (cards) ->
-               forward_events.call this, 'set_cards', cards
+       game.on 'set_cards', (agent, cards) ->
+               forward_events.call this, 'set_cards', agent, cards
+       game.on 'send_state', (agent) ->
+               timeout 10, =>
+                       if agent is 'p1'
+                               @p1_queue.push ['set_cards', 'server', @cards]
+                               answer_soon this
+                       if agent is 'p2'
+                               @p2_queue.push ['set_cards', 'server', @cards]
+                               answer_soon this
 
        return game
 
 test_init = ->
        test_game = new_game 'test'
-       timeout 4000, ->
-               test_game.set_cards [
-                       { 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}
+       timeout 2, ->
+               test_game.set_cards 'server', [
+                       { text: "Wildabeast 2/2", x: 220, y: 200, owner: 'p2'}
+                       { text: "Boar 2/2", x: 360, y: 200, owner: 'p2', pile: 'p2_discard'}
+                       { text: "Angora bunny 1/1", x: 500, y: 200, owner: 'p2'}
+                       { text: "Ambulatory Cactus 2/1", x: 660, y: 200, owner: 'p2'}
+                       { text: "Ent 0/5", x: 800, y: 200, owner: 'p2'}
+                       { text: "Carnivore 2/1", x: 220, y: 420, owner: 'p1'}
+                       { text: "Herbivore 1/2", x: 360, y: 420, owner: 'p1'}
+                       { text: "Stone Wall 0/10", x: 500, y: 420, owner: 'p1', pile: 'p1_draw', flipped: true}
+                       { text: "Log 0/1", x: 660, y: 420, owner: 'p1', pile: 'p1_draw', flipped: true}
+                       { text: "Ent 0/5", x: 800, y: 420, owner: 'p1', pile: 'p1_draw', flipped: true}
+                       { text: "Barricade 0/10", x: 500, y: 420, owner: 'p1', pile: 'p1_draw', flipped: true}
+                       { text: "O(log(n)) 0/1", x: 660, y: 420, owner: 'p1', pile: 'p1_draw', flipped: true}
+                       { text: "Fence 0/5", x: 800, y: 420, owner: 'p1', pile: 'p1_draw', flipped: true}
                ]
 
 test_init()
@@ -210,7 +224,6 @@ 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