Skip to content

Commit

Permalink
Added unit banning support to server interface
Browse files Browse the repository at this point in the history
  • Loading branch information
bzar committed Jan 12, 2013
1 parent bd5ec73 commit 7753722
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
4 changes: 1 addition & 3 deletions client/src/skeleton.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ Skeleton::playerJoined = (gameId, playerNumber, playerName, isMe) ->

Skeleton::playerLeft = (gameId, playerNumber) ->

Skeleton::unitBanned = (unitType) ->

Skeleton::unitUnbanned = (unitType) ->
Skeleton::bannedUnits = (unitTypes) ->

Skeleton::gameStarted = (gameId) ->

Expand Down
4 changes: 1 addition & 3 deletions client/www/skeleton.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions server/game/management.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,26 @@ GameManagement.prototype.myGames = function(userId, callback) {
}
});
}

GameManagement.prototype.setBannedUnits = function(gameId, bannedUnits, userId, callback) {
var database = this.database;
database.game(gameId, function(result) {
if(!result.success) {
callback({success: false, reason: result.reason});
} else if(result.game.authorId != userId) {
callback({success: false, reason: "Not the game author!"});
} else if(result.game.state != result.game.STATE_PREGAME) {
callback({success: false, reason: "Banned units can only be set during pregame!"});
} else {
var game = result.game;
game.settings.bannedUnits = bannedUnits;
database.updateGame(game, function(result) {
if(result.success) {
callback({success: true});
} else {
callback({success: false, reason: result.reason});
}
});
}
});
}
7 changes: 7 additions & 0 deletions server/messenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ Messenger.prototype.sendGameEvents = function(gameId, events) {
}, "game-" + gameId);
}

Messenger.prototype.sendBannedUnits = function(gameId, bannedUnits) {
this.subscriptions.forSubscribers(function(sub) {
if(sub.client.stub.bannedUnits)
sub.client.stub.bannedUnits(gameId, bannedUnits);
}, "game-" + gameId);
}

Messenger.prototype.sendPlayerJoined = function(gameId, playerNumber, username, userId) {
this.subscriptions.forSubscribers(function(sub) {
var isMe = userId == sub.session.userId;
Expand Down
26 changes: 26 additions & 0 deletions server/skeleton.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,32 @@ Skeleton.prototype.gameRules = function(gameId) {
}
}

Skeleton.prototype.setBannedUnits = function(gameId, bannedUnits) {
if(!requireArgs([gameId, bannedUnits])) return {success: false, reason: "Missing method arguments!"};

if(this.sessionId === null)
return {success: false, reason: "Not logged in"}

var requestId = this.client.requestId;
var this_ = this;
var userId = this.session.userId;

var mutex = this.server.gameMutex(gameId);
mutex.lock(function() {
var timer = new utils.Timer("Skeleton.setBannedUnits");
this_.server.gameManagement.setBannedUnits(gameId, bannedUnits, userId, function(result) {
if(result.success) {
this_.server.messenger.sendBannedUnits(gameId, bannedUnits);
this_.client.sendResponse(requestId, {success: true});
timer.end();
} else {
this_.client.sendResponse(requestId, {success: false, reason: result.reason});
}
mutex.release();
});
});
}

Skeleton.prototype.gameData = function(gameId) {
if(!requireArgs([gameId])) return {success: false, reason: "Missing method arguments!"};
var timer = new utils.Timer("Skeleton.gameData");
Expand Down

0 comments on commit 7753722

Please sign in to comment.