From fc9a6e5c9722ec889b7361c1f010fc79544a5411 Mon Sep 17 00:00:00 2001 From: "Clarence \"Sparr\" Risher" Date: Wed, 27 Sep 2017 18:08:45 -0700 Subject: [PATCH] WIP move createFlag code to RoomPosition --- src/game/rooms.js | 79 ++++++++++++++++++++++++----------------------- 1 file changed, 40 insertions(+), 39 deletions(-) diff --git a/src/game/rooms.js b/src/game/rooms.js index d000a7e1..d827b569 100644 --- a/src/game/rooms.js +++ b/src/game/rooms.js @@ -912,48 +912,15 @@ exports.make = function(_runtimeData, _intents, _register, _globals) { if(_.isUndefined(x) || _.isUndefined(y) || x < 0 || x > 49 || y < 0 || y > 49) { return C.ERR_INVALID_ARGS; } - if(_.size(globals.Game.flags) >= C.FLAGS_LIMIT) { - return C.ERR_FULL; - } if(_.isObject(firstArg)) { secondaryColor = color; color = name; name = secondArg; } - if(!color) { - color = C.COLOR_WHITE; - } - if(!secondaryColor) { - secondaryColor = color; - } - if(!_.contains(C.COLORS_ALL, color)) { - return C.ERR_INVALID_ARGS; - } - if(!_.contains(C.COLORS_ALL, secondaryColor)) { - return C.ERR_INVALID_ARGS; - } - if(!name) { - var cnt = 1; - do { - name = 'Flag'+cnt; - cnt++; - } - while(_.any(register.flags, {name}) || createdFlagNames.indexOf(name) != -1); - } - if(_.any(register.flags, {name}) || createdFlagNames.indexOf(name) != -1) { - return C.ERR_NAME_EXISTS; - } - if(name.length > 60) { - return C.ERR_INVALID_ARGS; - } - createdFlagNames.push(name); - - globals.Game.flags[name] = new globals.Flag(name, color, secondaryColor, this.name, x, y); + var pos = new globals.RoomPosition(x, y, this.name); - intents.pushByName('room', 'createFlag', {roomName: this.name, x, y, name, color, secondaryColor}); - - return name; + return pos.createFlag(name, color, secondaryColor); }); Room.prototype.createConstructionSite = register.wrapFn(function(firstArg, secondArg, structureType) { @@ -1398,11 +1365,45 @@ exports.makePos = function(_register) { }); RoomPosition.prototype.createFlag = register.wrapFn(function(name, color, secondaryColor) { - var room = register.rooms[this.roomName]; - if(!room) { - throw new Error(`Could not access room ${this.roomName}`); + if(_.size(globals.Game.flags) >= C.FLAGS_LIMIT) { + return C.ERR_FULL; + } + if(!color) { + color = C.COLOR_WHITE; + } + else if(!_.contains(C.COLORS_ALL, color)) { + return C.ERR_INVALID_ARGS; + } + if(!secondaryColor) { + secondaryColor = color; + } + else if(!_.contains(C.COLORS_ALL, secondaryColor)) { + return C.ERR_INVALID_ARGS; + } + if(!name) { + var cnt = 1; + do { + name = 'Flag'+cnt; + cnt++; + } + while(_.any(register.flags, {name}) || createdFlagNames.indexOf(name) != -1); } - return room.createFlag(this, name, color, secondaryColor); + else { + if(_.any(register.flags, {name}) || createdFlagNames.indexOf(name) != -1) { + return C.ERR_NAME_EXISTS; + } + if(name.length > 60) { + return C.ERR_INVALID_ARGS; + } + } + + createdFlagNames.push(name); + + globals.Game.flags[name] = new globals.Flag(name, color, secondaryColor, this.roomName, this.x, this.y); + + intents.pushByName('room', 'createFlag', {roomName: this.roomName, x: this.x, y: this.y, name, color, secondaryColor}); + + return name; }); RoomPosition.prototype.createConstructionSite = register.wrapFn(function(structureType) {