Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: e162486985
Fetching contributors…

Cannot retrieve contributors at this time

file 77 lines (59 sloc) 1.896 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
# Don't get too excited, this is not a real test!

connect = require 'connect'

ObjectSync = require '../lib/object-sync'

server = connect.createServer()

id_counter = 1

# local object database
objects = {}
object_owners = {}


sync = ObjectSync.listen server,
  destroy: (id, sid, callback) ->
    if not objects[id]
      return callback
        code: 'invalid_id'
    delete objects[id]
    if object_owners[sid]
      idx = object_owners[sid].indexOf id
      object_owners[sid].splice idx, 1 if idx isnt -1
      delete object_owners[sid] if not object_owners[sid].length
    callback null
    
  update: (obj, sid, callback) ->
    if objects[obj.id]
      same = true
      for prop, val of obj when objects[obj.id][prop] isnt val
        same = false
        objects[obj.id][prop] = val
      callback null, objects[obj.id], not same
    else callback
      code: 'invalid_id'
      
  create: (obj, sid, callback) ->
    obj.id = id_counter++
    objects[obj.id] = obj

    object_owners[sid] or= []
    object_owners[sid].push obj.id

    callback null, obj
    
  fetch: (ids, sid, callback) ->
    results = []
    for id in ids
      results.push (objects[id] or null)
    callback null, results


# remove all objects the disconnecting player created
sync.on 'disconnect', (sid) ->
  if object_owners[sid]
    ids = [].concat(object_owners[sid])
    sync.destroy id for id in ids

# serve list of of game objects to new clients
server.use '/init', (req, res, next) ->
  keys = Object.keys objects
  response = JSON.stringify keys
  headers =
    'Content-Type': 'application/json; charset=utf-8'
    'Content-Length': Buffer.byteLength response
  res.writeHead 200, headers
  res.end response
  

server.use '/coffee', connect.compiler
  src: './static/coffee'
  enable: ['coffeescript']

server.use '/', connect.staticProvider './static'
server.listen 80
module.exports = server
Something went wrong with that request. Please try again.