Table = function(socket) { this.id = rng.uid(); this.socket = socket; this.players = {}; Table._byId[this.id] = this; Table._instances.push(this); };
HarrowBox.prototype._copy = function(obj, json){ //throw new Error('TODO') var typeName = obj.type var clazz = this.classesByName[typeName] if(!clazz){ throw new Error('cannot find object type: ' + typeName) } var newJson if(json){ //newJson = JSON.parse(JSON.stringify(json)) newJson = copyUnder(json, obj) }else{ newJson = copyUnder({}, obj) } var uuid = seedrandom.uid() var newObj = new clazz(uuid) newObj._box = this var local = this newObj._instantiateNew(newJson, this.w, function(typeName, json){ return local.make(typeName, json) }) newObj.manyEdits = 0 this.objs[uuid] = newObj this.objList.push(newObj) return newObj }
MapHandle.prototype.putNew = function(newKey, newTypeName, json){ if(this.schema.type.value.type !== 'object') _.errout('cannot putNew - values are not objects'); if(arguments.length === 2){ if(_.isObject(newTypeName)){ json = newTypeName newTypeName = undefined } } json = json || {} var type = u.getOnlyPossibleType(this, newTypeName); this.adjustPathLocal(newKey) //var temporary = this.makeTemporaryId() var id = seedrandom.uid() var e = {typeCode: type.code, id: id}; this.persistEdit(editCodes.didPutNew, e) var n = this._makeAndSaveNew(json, type, id) this.obj[newKey] = n //n.prepare() this.emit(e, 'put', newKey, n) return n }
ObjectSetHandle.prototype.addNew = function(typeName, json){ //if(arguments.length === 1){ if(_.isObject(typeName)){ //cb = json json = typeName typeName = undefined } //} json = json || {} var type = u.getOnlyPossibleType(this, typeName); //var temporary = this.makeTemporaryId() var id = seedrandom.uid() var local = this var n = this._makeAndSaveNew(json, type, id, undefined, function(fc){ var edit = {typeCode: type.code, id: id, following: fc} local.saveEdit(editCodes.addedNew, edit) }) if(n === undefined) _.errout('failed to addNew') this.emit({}, 'add', n) this.obj.push(n) return n }
function HarrowBox(config, data, classesByName, classesByCode, agg){ this.agg = agg var w = this.w = agg.w this.classesByName = classesByName this.classesByCode = classesByCode if(config){ var clazz = classesByName[config.type] var uuid = seedrandom.uid() this.root = new clazz(uuid) this.root._box = this w.beginUpdate() var local = this this.objList = [this.root] this.objs = {} this.objs[uuid] = this.root function makeCb(typeName, json){ return local.make(typeName, json, makeCb) } this.root._instantiateNew(config, w, makeCb) w.endUpdate() }else{ this.originalData = data.fullBuffer this.root = data.objList[0] this.objList = data.objList this.objs = data.objMap for(var i=0;i<data.objList.length;++i){ var obj = data.objList[i] obj._box = this } //console.log('applying deserialized updates: ' + data.updates.length) //this.applyUpdates(data.updates) } var editListeners = this.editListeners = [] var beforeEditListeners = this.beforeEditListeners = [] agg.onEdit = function(){ for(var i=0;i<editListeners.length;++i){ var listener = editListeners[i] listener() } } agg.beforeEdit = function(){ for(var i=0;i<beforeEditListeners.length;++i){ var listener = beforeEditListeners[i] listener() } } }
Player = function(socket, table, name) { this.id = rng.uid(); this.table = table; this.socket = socket; this.name = name; this.direction = null; this.score = null; Player._byId[this.id] = this; Player._instances.push(this); table.addPlayer(this); console.log('New player on table '+table.id+': '+this.name); };
HarrowBox.prototype.make = function(typeName, json){ var clazz = this.classesByName[typeName] if(!clazz){ throw new Error('cannot find object type: ' + typeName) } var uuid = seedrandom.uid() var obj = new clazz(uuid) obj._box = this var local = this obj._instantiateNew(json, this.w, function(typeName, json){ return local.make(typeName, json) }) obj.manyEdits = 0 this.objs[uuid] = obj this.objList.push(obj) return obj }
ObjectHandle.prototype.setPropertyToNew = function(propertyName, typeName, json){ if(_.isObject(typeName)){ json = typeName typeName = undefined } var pt = this.typeSchema.properties[propertyName]; _.assertDefined(pt); json = json || {} var type = u.getOnlyPossibleObjectPropertyType(this, pt, typeName); //var remaining = this.adjustPath(pt.code) this.adjustTopObjectToOwn() this.adjustCurrentObject(this.getImmediateObject()) this.adjustCurrentProperty(pt.code) //console.log('setting to new: ' + this.parent.prepared) var id = seedrandom.uid()//this.makeTemporaryId(); this.persistEdit(editCodes.wasSetToNew, {typeCode: type.code, id: id}) var edits = jsonutil.convertJsonToEdits(this.getFullSchema(), type.name, json, id); if(edits.length > 0){ //this.adjustPath(temporary) this.saveEdit(editCodes.selectObject, {id: id}) for(var i=0;i<edits.length;++i){ var e = edits[i] this.persistEdit(e.op, e.edit) } //this.saveEdit(editCodes.ascend1, {}) } //_.assert(this.part[0] > 0) // typeSchema, edits, objId, part, parent, isReadonlyIfEmpty){ var n = new ObjectHandle(type, edits, id, [pt.code], this); if(this.objectApiCache === undefined) this.objectApiCache = {} this.objectApiCache[id] = n; //this.saveTemporaryForLookup(temporary, n, this) n.prepare() _.assertObject(n) this.emit({}, 'setProperty', propertyName, n)//() //TODO rewrite parent property stuff //this.parent.replaceObjectHandle(this, n, this.part) this[propertyName] = n return n /* var remaining = this.parent.adjustPath(this.part) if(remaining.length === 0){ this.persistEdit('selectProperty', {typeCode: pt.code}) }else if(remaining[0] !== pt.code){ if(remaining.length > 1) this.ascendBy(remaining.length-1) this.persistEdit('reselectProperty', {typeCode: pt.code}) } this.persistEdit('setToNew', {newType: objectSchema.code, temporary: temporary}) delete this.cachedProperties[propertyName] this[propertyName] = this.property(propertyName); return this[propertyName]; */ }
//var wrap = require('./wrap') var analytics = require('./analytics') var snapshotSerialization = require('./snapshot_serialization') var pathmerger = require('./pathmerger') var pu = require('./../http/js/paramutil') var editFp = require('./tcp_shared').editFp var editCodes = editFp.codes var editNames = editFp.names var bw = require("buffered-writer"); var random = require('seedrandom') var QuerySyncId = random.uid() var vcModule = require('./viewfacade') function makePoller(config){ var fastPollFunctions = [] var slowerPollFunctions = [] var slowPollFunctions = [] var FastPollRate = 10 var SlowerPollRate = 100 var SlowPollRate = 1000