Skip to content

Commit

Permalink
- Added 'CPU Battle'.
Browse files Browse the repository at this point in the history
- Fixed a bug with game expirations.
  • Loading branch information
blixt committed Jul 16, 2008
1 parent 8a0fbbb commit 2619aa1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
6 changes: 3 additions & 3 deletions index.yaml
Expand Up @@ -10,22 +10,22 @@ indexes:
# automatically uploaded to the admin console when you next deploy
# your application using appcfg.py.

# Used 187 times in query history.
# Used 16 times in query history.
- kind: Game
properties:
- name: players
- name: state
- name: last_update
direction: desc

# Used 127 times in query history.
# Used 16 times in query history.
- kind: Game
properties:
- name: state
- name: last_update
direction: desc

# Used 659 times in query history.
# Used 409 times in query history.
- kind: Player
properties:
- name: session
Expand Down
20 changes: 19 additions & 1 deletion js/monkey.js
Expand Up @@ -92,6 +92,10 @@ var MonkeyService = new Class({
this.call('change_nickname', { nickname: newNick }, onSuccess, onError);
},

cpuBattle: function (ruleSetId, onSuccess, onError) {
this.call('cpu_battle', { rule_set: ruleSetId }, onSuccess, onError);
},

createGame: function (ruleSetId, onSuccess, onError) {
this.call('create_game', { rule_set: ruleSetId }, onSuccess, onError);
},
Expand Down Expand Up @@ -179,6 +183,15 @@ var MonkeyClient = new Class({
}
},
text: 'Create game'
}),
mc.html.cpuBattle = new Element('button', {
events: {
click: function () {
this.disabled = true;
mc.cpuBattle(parseInt(ruleSets.value));
}
},
text: 'CPU battle'
})
),
new Element('ul').adopt(
Expand Down Expand Up @@ -250,6 +263,10 @@ var MonkeyClient = new Class({
this.service.addCpuPlayer(this.gameId, this.refresh.bind(this));
},

cpuBattle: function (ruleSetId) {
this.service.cpuBattle(ruleSetId, this.goToGame.bind(this));
},

createGame: function (ruleSetId) {
this.service.createGame(ruleSetId, this.goToGame.bind(this));
},
Expand Down Expand Up @@ -473,7 +490,7 @@ var MonkeyClient = new Class({
} else if (mc.game.state == 'waiting') {
mc.timer = mc.refresh.delay(5000, mc);
} else if (mc.game.state == 'playing') {
mc.timer = mc.refresh.delay(1000, mc);
mc.timer = mc.refresh.delay(750, mc);
}
},

Expand Down Expand Up @@ -629,6 +646,7 @@ var MonkeyClient = new Class({
this.game = null;
this.gameId = null;

this.html.cpuBattle.disabled = false;
this.html.createGame.disabled = false;
this.html.main.set('class', 'monkey in-lobby');
this.html.lobby.inject(this.html.main);
Expand Down
18 changes: 17 additions & 1 deletion main.py
Expand Up @@ -61,6 +61,22 @@ def change_nickname(self, nickname):
player = monkey.Player.get_current(self)
player.rename(nickname)
return self.get_player_info()

def cpu_battle(self, rule_set):
"""Creates a new game with only CPU players.
"""
if not isinstance(rule_set, monkey.RuleSet):
rule_set = monkey.RuleSet.get_by_id(rule_set)
if not rule_set: raise ValueError('Invalid rule set id.')

game = monkey.Game(rule_set = rule_set)
game.put()

for i in xrange(rule_set.num_players):
cpu = monkey.CpuPlayer()
cpu.join(game)

return game.key().id()

def create_game(self, rule_set):
"""Creates a new game.
Expand Down Expand Up @@ -164,7 +180,7 @@ def get_games(self, mode = 'play'):
# after six hours.
# - Games that are in play are considered abandoned after 48 hours.
age = now - game.last_update
age = age.seconds / 3600.0 + age.days / 24.0
age = age.seconds / 3600.0 + age.days * 24.0
if ((game.state == 'waiting' and age > 6) or
(game.state == 'playing' and age > 48)):
# Abort the game to speed up future queries.
Expand Down
2 changes: 1 addition & 1 deletion monkey.py
Expand Up @@ -409,7 +409,7 @@ def register(nickname, password, handler = None):

@staticmethod
def validate(nickname):
"""Validates a nickname and throws an extension if it's invalid.
"""Validates a nickname and throws an exception if it's invalid.
"""
if nickname in ('Anonymous', 'CPU'):
raise PlayerNameError(nickname + ' is a reserved nickname.')
Expand Down

0 comments on commit 2619aa1

Please sign in to comment.