 ### Update Game State ###
 
 updateState = ->
-	server.broadcast JSON.stringify({'server': 'test'})
+	sys.puts "Doing step for #{snakes.length} snakes"
+	snake.doStep() for snake in snakes
+	# server.broadcast JSON.stringify({'server': 'test'})
 
-# tick = setInterval updateState, 5000
+tick = setInterval updateState, 5000
 
 ### Start Server ###	
 


class Snake
	constructor: (@id) ->
		@elements = [[-8, 200], [-7, 200], [-6, 200], [-5, 200], [-4, 200], [-3, 200], [-2, 200], [-1, 200]]
		@direction = "right"
		
	doStep: ->
		moveElement i for i in [0..7]
		
	moveElement: (i) ->
		switch @direction
			when "left" then @elements[i][0] -= 1
			when "right" then @elements[i][1] += 1
			when "up" then @elements[i][0] -= 1
			when "down" then @elements[i][0] += 1

### Handle Connections ###

server.addListener "connection", (connection) ->
	clientId = autoClient
	autoClient += 1
	
	snakes.push new Snake clientId
	
	connection.addListener "message", (message) ->
		sys.puts("Client #{clientId} says: " + message)

server.addListener "close", (connection) ->
	sys.puts("Client disconnected")

sys = require 'sys'
util = require 'util'
websocket = require 'websocket-server'
server = websocket.createServer()

