Skip to content

WIP move createFlag code to RoomPosition #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 40 additions & 39 deletions src/game/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down