diff --git a/RoomManager.js b/RoomManager.js index 87ed3ce..f85bb76 100644 --- a/RoomManager.js +++ b/RoomManager.js @@ -75,6 +75,7 @@ RoomManager.prototype.createClient = function (socket,id) { socket.on('getReplayList',this.getReplayList.bind(this,client)); socket.on('getReplayContent',this.getReplayContent.bind(this,client)); socket.on('watchLive',this.watchLive.bind(this,client)); + socket.on('renameRoom',this.renameRoom.bind(this,client)); socket.on('ready',client.ready.bind(client)); socket.on('unready',client.unready.bind(client)); @@ -83,7 +84,6 @@ RoomManager.prototype.createClient = function (socket,id) { socket.on('surrender',client.surrender.bind(client)); socket.on('drop',client.drop.bind(client)); socket.on('tick',client.tick.bind(client)); - // socket.on('reloadCardInfo',this.reloadCardInfo.bind(this)); socket.emit('version',this.VERSION); @@ -211,6 +211,36 @@ RoomManager.prototype.checkLiveIP = function (client,room) { if (address === guestAddress) return 'IP_BANNED'; }; +RoomManager.prototype.renameRoom = function (client,cfg) { + var errMsg; + if (!isObj(cfg) || !isStr(cfg.roomName)) { + errMsg = 'INVALID_CONFIG'; + } + var oldRoomName = client.room.name; + var newRoomName = cfg.roomName; + if (!errMsg) { + errMsg = this.checkRoomName(newRoomName); + } + var room; + if (!errMsg) { + room = this.roomMap[oldRoomName]; + if (!room) { + errMsg = 'ROOM_DOES_NOT_EXIST'; + } else if (client.getPosition() !== 'host') { + errMsg = 'YOU_ARE_NOT_ROOM_HOST'; + } + } + if (errMsg) { + client.socket.emit('error message',errMsg); + return; + } + + room.name = newRoomName; + renameProperty(this.roomMap, oldRoomName, newRoomName); + room.update(); + this.updateRoomList(); +}; + RoomManager.prototype.createRoom = function (client,cfg) { var errMsg; if (!isObj(cfg) || !isStr(cfg.roomName) || !isStr(cfg.nickname)) { diff --git a/util.js b/util.js index 4b9f2f1..53c99bb 100644 --- a/util.js +++ b/util.js @@ -33,4 +33,14 @@ global.isFunc = function (v) { // function nextTick (callback) { // setTimeout(callback,0); -// } \ No newline at end of file +// } + +global.renameProperty = function (obj, oldName, newName) { + if (!obj.hasOwnProperty(oldName) || obj.hasOwnProperty(newName)) { + return false; + } else if (oldName === newName) { + return true; + } + obj[newName] = obj[oldName]; + delete obj[oldName]; +}; \ No newline at end of file diff --git a/webxoss-client b/webxoss-client index c6bfbf1..51a710f 160000 --- a/webxoss-client +++ b/webxoss-client @@ -1 +1 @@ -Subproject commit c6bfbf136d077c19ad88b3abd2af41d684014d05 +Subproject commit 51a710f84c2931e55ca71009ad469468b5662863