JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
hosts multiple games at once, licensing
[peach-cgt.git] / server.coffee
index 91250cb..211ac7d 100644 (file)
@@ -1,3 +1,19 @@
+# Peach CGT -- Card Game Table simulator
+# Copyright (C) 2011  Jason Woofenden
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Affero General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option) any
+# later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
 listen_port = 8333
 sys = require 'sys'
 fs = require 'fs'
@@ -44,7 +60,11 @@ js_handler = (args, out, request, url_parts) ->
                if err
                        return out.end "Server failed to read #{filename}"
                if convert
-                       out.end coffee.compile data
+                       try
+                               converted = coffee.compile data
+                       catch e
+                               out.end "alert(\"server faild to compile #{filename}\");"
+                       out.end converted
                else
                        out.end data
 
@@ -103,28 +123,45 @@ get_handler = (args, out, request, url_parts) ->
 set_handler = (args, out, request, url_parts) ->
        unless args.game?.length
                out.writeHead 404, "Content-Type": 'text/plain'
-               out.end 'Missing (or empty) "game" argument'
+               out.end '{"status":1,"text_status":"Missing (or empty) game argument"}'
                return
 
        unless args.agent is 'p1' or args.agent is 'p2'
                out.writeHead 404, "Content-Type": 'text/plain'
-               out.end '"agent" argument must be set to p1 or p2'
+               out.end '{"status":2,"text_status":"agent argument must be set to p1 or p2"}'
                return
 
        unless args.messages?
                out.writeHead 404, "Content-Type": 'text/plain'
-               out.end '"messages" argument must be set'
+               out.end '{"status":3,"text_status":"messages argument must be set"}'
+               return
+
+       try
+               messages = JSON.parse args.messages
+       catch e
+               out.writeHead 400, "Content-Type": 'text/plain'
+               out.end '{"status":4,"text_status":"Invalid JSON"}'
                return
 
+       # special handling of 'new_game' api, because for this one we don't have a
+       # game object to pass the message to
+       if messages?[0]?[0] is 'new_game'
+               message = messages.shift()
+               slug = message[1]
+               if games[slug]?
+                       out.writeHead 403, "Content-Type": 'text/plain'
+                       out.end '{"status":6,"text_status":"Game already exists"}'
+                       return
+               game = games[slug] = new_game slug, 'server'
+
        unless games[args.game]?
                out.writeHead 404, "Content-Type": 'text/plain'
-               out.end 'Game not found'
+               out.end '{"status":5,"text_status":"Game not found"}'
                return
 
        game = games[args.game]
 
-       # FIXME add error checking (json validity at least)
-       game.process_messages JSON.parse args.messages
+       game.process_messages messages
 
        out.writeHead 200, "Content-Type": 'text/plain'
        out.end '{"status":0,"text_status":"Success"}'
@@ -162,8 +199,8 @@ forward_events = (message...) ->
                @p2_queue.push message
                answer_soon this
 
-new_game = (id) ->
-       game = games[id] = model.new 'server'
+new_game = (slug) ->
+       game = games[slug] = model.new slug, 'server'
        game.p1_waiter = false
        game.p2_waiter = false
        game.p1_queue = []
@@ -192,11 +229,6 @@ new_game = (id) ->
 
        return game
 
-test_init = ->
-       test_game = new_game 'test'
-test_init()
-
-
 http_server = http.createServer (req, res) ->
        url_parts = url.parse req.url, true
        if url_parts.query is undefined