Created
January 10, 2012 17:22
-
-
Save tony-brewerio/1590108 to your computer and use it in GitHub Desktop.
Example actions for Garena bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from gbot.action import Action | |
from gbot.exceptions import BotError | |
from gbot import param | |
from gbot.models import Group, Player, Membership | |
class PlayerInfo(Action): | |
# Query the bot about info on a certain player, giving back rank and groups player is member of | |
# command users needs to enter to execute this action | |
# actions gets auto-wired in routes thanks to metaclass | |
# for this action example of user command would be ".i some_player_name" | |
# accepts multiple command names, those act as aliases | |
expose = '.i', | |
# param.* are special classes that try to convert a string into something | |
# they will also respond with meaninful errors on failure | |
# in this case param.Player tries to convert some string into a Player instance ( Django ORM model ) | |
# lambda passed is a "default", it can be a value or a callable | |
# it wont be used unless user does not provide some values at all, wrong values wont fallback to default | |
# in case of callable it will be passed current action instance | |
# in PlayerInfo case, we use "player" action instance variable as a default | |
# action.player is set to the player that entered the command by the superclass | |
params = ['target', param.Player(lambda action: action.player)] | |
# method "action" is called on action classes | |
# nothing gets passed into it, since everything is accessed as instance variables/method | |
# and yes, all instace variables are exploded into a template :( | |
# that was a bad choice actually, and i can't really remember my reasoning behind this | |
def action(self): | |
# usual Django ORM stuff | |
self.stats = self.target.roomstats.get(room = self.room) | |
self.place = Membership.filter(active = True, room = self.room, | |
player__roomstats__room = self.room, | |
player__roomstats__score__gt = self.stats.score).count() + 1 | |
self.total = Membership.filter(active = True, room = self.room).count() | |
memberships = self.target.memberships.filter(active = True, room = self.room).\ | |
select_related('group') | |
self.groups = ["%s(%s)" % (m.group.name, m.by or "`BOT`") for m in memberships] | |
# There are two methods that allow bot to respond with something | |
# .private() will send a private message to some player | |
# by default it'll use action.player as a target, but a different player can be passed as a param | |
# template is picked based on controller name, but also can be given as a parameter | |
# there's also .announce() that will send message in chat, to be seen by everybody | |
self.private() | |
class Top(Action): | |
# Display top ten players, along with their scores | |
expose = '.top', | |
def action(self): | |
self.top = self.room.roomstats.filter(player__memberships__active = True, | |
player__memberships__group__name = 'player', | |
player__memberships__room = self.room).\ | |
order_by('-score').select_related('player')[:10] | |
self.top = ["%s(%s)" % (rs.player.login, rs.score) for rs in self.top] | |
self.private() | |
class Bot(Action): | |
# Display bottom ten players, along with their scores | |
expose = ".bot", | |
def action(self): | |
self.bot = self.room.roomstats.filter(player__memberships__active = True, | |
player__memberships__group__name = 'player', | |
player__memberships__room = self.room).\ | |
order_by('score').select_related('player')[:10] | |
self.bot = ["%s(%s)" % (rs.player.login, rs.score) for rs in self.bot] | |
self.private() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment