def onDuelCommand(cn, args): if args == '': sbserver.playerMessage( cn, error('Usage: #duel <mapname> (mode) (cn) (cn)')) args = args.split(' ') players = sbserver.players() if len(players) != 2: sbserver.playerMessage( cn, error( 'There must be only two unspectated players to enter duel mode.' )) else: if len(args) == 2: map = args[0] mode = int(args[1]) elif len(args) == 1: map = args[0] mode = sbserver.gameMode() else: sbserver.playerMessage(cn, error('Usage: #duel <mapname> (mode)')) return duelers[0] = players[0] duelers[1] = players[1] prev_mastermode = sbserver.masterMode() sbserver.setMasterMode(2) sbserver.message(green('Duel begins in...')) duelCountdown(5, map, mode)
def onMapVote(cn, mapname, mapmode): p = player(cn) if sbserver.mapName() == '': setMap(mapname, mapmode) elif isAtLeastMaster(cn) and sbserver.masterMode() > 0: setMap(mapname, mapmode) elif mapmode != sbserver.gameMode() and (lock_mode or not allow_modevote): p.message(error('You cannot vote for a new game mode')) else: try: vote = player(cn).gamevars['mapvote'] allow_vote = vote[0] != mapname and vote[1] != mapmode except KeyError: allow_vote = True if allow_vote: sbserver.message( info( request_temp.substitute( colordict, user=p.name(), modename=sbserver.modeName(mapmode), mapname=mapname))) p.gamevars['mapvote'] = (mapname, mapmode) else: sbserver.playerMessage( cn, error('You have already requested this map.')) countVotes()
def onNextMapCmd(cn, args): '''@description Display next map @usage @public''' if args != '': sbserver.playerMessage(cn, error('Usage: #nextmap')) else: try: sbserver.playerMessage(cn, info(nextmap_response.substitute(colordict, mapname=getSuccessor(sbserver.gameMode(), sbserver.mapName())))) except (KeyError, ValueError): sbserver.playerMessage(cn, error('Could not determine next map'))
class CommandManager: def __init__(self): self.prefixes = '#!@' self.command_handlers = {} registerPolicyEventHandler('allow_message', self.onMsg) def register(self, command, func): if not self.command_handlers.has_key(command): self.command_handlers[command] = [] self.command_handlers[command].append(func) def trigger(self, cn, command, text): p = player(cn) if self.command_handlers.has_key(command): for func in self.command_handlers[command]: try: func(cn, text) except UsageError, e: try: usages = command_info[command].usages except KeyError: usages = [] p.message(error('Invalid Usage of #' + command + ' command. ' + str(e))) for usage in usages: p.message(info('Usage: ' + command + ' ' + usage)) except StateError, e: p.message(error(str(e))) except ArgumentValueError, e: p.message(error('Invalid argument. ' + str(e))) except ValueError: p.message(error('Value Error: Did you specify a valid cn?')) exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() logging.warn('Uncaught ValueError raised in command handler.') logging.warn(traceback.format_exc())
def namesCmd(cn, args): '''@description Display names used by client @usage cn @public''' if master_required and not isAtLeastMaster(cn): insufficientPermissions(cn) return if args == '': raise UsageError() return try: tcn = int(args) names = dbmanager.query(IpToNick).filter(IpToNick.ip==sbserver.playerIpLong(tcn)).all() if len(names) == 0: sbserver.playerMessage(cn, info('No names found')) return except NoResultFound: sbserver.playerMessage(cn, info('No names found')) return except ValueError: sbserver.playerMessage(cn, error('Invalid cn')) return namestr = 'Other known names: ' for name in names: namestr += name.nick + ' ' sbserver.playerMessage(cn, info(namestr))
def namesCmd(cn, args): '''@description Display names used by client @usage cn @public''' if master_required and not isAtLeastMaster(cn): insufficientPermissions(cn) return if args == '': raise UsageError() return try: tcn = int(args) names = dbmanager.query(IpToNick).filter( IpToNick.ip == sbserver.playerIpLong(tcn)).all() if len(names) == 0: sbserver.playerMessage(cn, info('No names found')) return except NoResultFound: sbserver.playerMessage(cn, info('No names found')) return except ValueError: sbserver.playerMessage(cn, error('Invalid cn')) return namestr = 'Other known names: ' for name in names: namestr += name.nick + ' ' sbserver.playerMessage(cn, info(namestr))
def onSetMaster(cn, givenhash): p = player(cn) adminhash = sbserver.hashPassword(cn, sbserver.adminPassword()) try: na = dbmanager.query(NickAccount).filter(NickAccount.nick==p.name()).one() except NoResultFound: if givenhash != adminhash: p.message(error('Your name is not assigned to any accounts')) except MultipleResultsFound: p.message(error('Multiple names linked to this account. Contact the system administrator.')) else: nickhash = sbserver.hashPassword(cn, na.user.password) if givenhash == nickhash: login(cn, na.user) else: if givenhash != adminhash: p.message(error('Invalid password'))
def onPlayerCommands(cn, args): if args != '': sbserver.playerMessage(cn, error('Usage: #playercommands')) else: msg = blue('Available commands: ') for command in command_info.keys(): msg += '#' + command + ' ' sbserver.playerMessage(cn, orange(msg))
def onNextMapCmd(cn, args): '''@description Display next map @usage @public''' if args != '': sbserver.playerMessage(cn, error('Usage: #nextmap')) else: try: sbserver.playerMessage( cn, info( nextmap_response.substitute(colordict, mapname=getSuccessor( sbserver.gameMode(), sbserver.mapName())))) except (KeyError, ValueError): sbserver.playerMessage(cn, error('Could not determine next map'))
def trigger(self, cn, command, text): p = player(cn) if self.command_handlers.has_key(command): for func in self.command_handlers[command]: try: func(cn, text) except UsageError, e: try: usages = command_info[command].usages except KeyError: usages = [] p.message(error('Invalid Usage of #' + command + ' command. ' + str(e))) for usage in usages: p.message(info('Usage: ' + command + ' ' + usage)) except StateError, e: p.message(error(str(e))) except ArgumentValueError, e: p.message(error('Invalid argument. ' + str(e)))
def onSwitchTeam(cn, team): p = player(cn) if isSafeTeam(team): execLater(p.suicide, ()) p.setTeam(team) else: p.message( error('You cannot join team \'%s\' in game mode %s' % (team, modeName(currentMode()))))
def onVoteKick(cn, args): '''@description Vote to kick a player from server @usage <cn>''' if args == '': sbserver.playerMessage(cn, error('Usage #votekick <cn>')) else: try: tcn = int(args) if player(cn).votekick == tcn: sbserver.playerMessage(cn, error('You have already voted to kick that player.')) allow_vote = False else: allow_vote = True except AttributeError: allow_vote = True if allow_vote: sbserver.message(info(vktemp.substitute(colordict, voter=sbserver.playerName(cn), victim=sbserver.playerName(tcn)))) player(cn).votekick = int(args) checkVotes(int(args))
def onSwitchTeam(cn, team): p = player(cn) if isSafeTeam(team): execLater(p.suicide, ()) p.setTeam(team) else: p.message(error('You cannot join team \'%s\' in game mode %s' % ( team, modeName(currentMode()) )))
def onMapSet(cn, mapname, mapmode): p = player(cn) if sbserver.mapName() == '': setMap(mapname, mapmode) elif mapreload[0]: setMap(mapname, mapmode) mapreload[0] = False elif isAtLeastMaster(cn) and sbserver.masterMode() > 0: sbserver.setMap(mapname, mapmode) elif mapmode != sbserver.gameMode() and (lock_mode or not allow_modevote): p.message(error('You cannot request a new game mode'))
def onReqUnspectate(cn, tcn): if tcn != cn: if isAtLeastMaster(cn): sbserver.unspectate(tcn) else: insufficientPermissions(cn) else: if sbserver.masterMode() > 1 and not isAtLeastMaster(cn): sbserver.playerMessage(cn, error('Master mode is locked. You cannot unspectate.')) else: sbserver.unspectate(tcn)
def onLoginCommand(cn, args): '''@description Login to server account @usage email password @public''' args = args.split(' ') if len(args) != 2: raise UsageError() user = userAuth(args[0], args[1]) if user: login(cn, user) else: sbserver.playerMessage(cn, error('Invalid login.'))
def onReqUnspectate(cn, tcn): if tcn != cn: if isAtLeastMaster(cn): sbserver.unspectate(tcn) else: insufficientPermissions(cn) else: if sbserver.masterMode() > 1 and not isAtLeastMaster(cn): sbserver.playerMessage( cn, error('Master mode is locked. You cannot unspectate.')) else: sbserver.unspectate(tcn)
def mapModifiedSpecCmd(cn, args): '''@description Enable or disable spectate clients with modified map @usage enable/disable''' p = player(cn) if args == 'disable': spectate_map_modified = False p.message(info('Spectate modified mapes disabled')) elif args == 'enable': spectate_map_modified = True p.message(info('Spectate modified mapes enabled')) else: p.message(error('Usage: #mapmodifiedspec (enable/disable)'))
def userPrivSetCmd(cn, tcn, args): user_id = player(tcn).user.id if args == 'user': try: if isUser(player(tcn).user.id): sbserver.playerMessage(cn, error('%s already has user permissions.' % sbserver.playerName(tcn))) return except (ValueError, AttributeError): pass else: try: user = loggedInAs(tcn) except AttributeError: sbserver.playerMessage(cn, error('%s is not logged in.' % sbserver.playerName(tcn))) else: dbmanager.query(UserPrivilege).filter(UserPrivilege.user_id==user_id).update({ 'privilege': None }) session.add(UserPrivilege(0, user.id)) session.commit() sbserver.playerMessage(cn, info('User privilege has been given to %s (%s)' % (sbserver.playerName(tcn), user.email))) elif args == 'master': try: if isUserMaster(player(tcn).user.id): sbserver.playerMessage(cn, error('%s already has master permissions.' % sbserver.playerName(tcn))) return except (ValueError, AttributeError): pass else: try: user = loggedInAs(tcn) except AttributeError: sbserver.playerMessage(cn, error('%s is not logged in.' % sbserver.playerName(tcn))) else: dbmanager.query(UserPrivilege).filter(UserPrivilege.user_id==user_id).update({ 'privilege': None }) session.add(UserPrivilege(1, user.id)) session.commit() sbserver.playerMessage(cn, info('Master privilege has been given to %s (%s)' % (sbserver.playerName(tcn), user.email))) elif args == 'admin': try: if isUserAdmin(player(tcn).user.id): sbserver.playerMessage(cn, error('%s already has admin permissions.' % sbserver.playerName(tcn))) return except (ValueError, AttributeError): pass else: try: user = loggedInAs(tcn) except AttributeError: sbserver.playerMessage(cn, error('%s is not logged in.' % sbserver.playerName(tcn))) else: dbmanager.query(UserPrivilege).filter(UserPrivilege.user_id==user_id).update({ 'privilege': None }) session.add(UserPrivilege(2, user.id)) session.commit() sbserver.playerMessage(cn, info('Admin privilege has been given to %s (%s)' % (sbserver.playerName(tcn), user.email))) else: sbserver.playerMessage(cn, error('Privilege level must be \'master\' to set master permissions and \'admin\' to set master or admin permissions'))
def onCommand(cn, args): '''@description Stats for the current match @usage (cn) @public''' cp = player(cn) if args != '': if require_master and not isPlayerMaster(cn): insufficientPermissions(cn) return try: p = player(int(args)) except ValueError: cp.message(error('Usage: #stats (cn)')) return else: p = cp if not p.name(): cp.message(error('You must use a valid cn')) return msg = template.substitute(colordict, name=p.name(), frags=p.frags(), deaths=p.deaths(), teamkills=p.teamkills(), shots=p.shots(), hits=p.hits(), accuracy=p.accuracy(), ktd=p.kpd(), score=p.score()) cp.message(msg)
def onDuelCommand(cn, args): if args == '': sbserver.playerMessage(cn, error('Usage: #duel <mapname> (mode) (cn) (cn)')) args = args.split(' ') players = sbserver.players() if len(players) != 2: sbserver.playerMessage(cn, error('There must be only two unspectated players to enter duel mode.')) else: if len(args) == 2: map = args[0] mode = int(args[1]) elif len(args) == 1: map = args[0] mode = sbserver.gameMode() else: sbserver.playerMessage(cn, error('Usage: #duel <mapname> (mode)')) return duelers[0] = players[0] duelers[1] = players[1] prev_mastermode = sbserver.masterMode() sbserver.setMasterMode(2) sbserver.message(green('Duel begins in...')) duelCountdown(5, map, mode)
def onSetTeam(tcn, cn, team): p = player(cn) r = player(tcn) if cn != tcn and not isAtLeastMaster(tcn): insufficientPermissions(tcn) return mode = currentMode() if isSafeTeam(team): execLater(p.suicide, ()) p.setTeam(team) else: r.message( error('You cannot join team \'%s\' in game mode %s' % (team, modeName(currentMode()))))
def onMapVote(cn, mapname, mapmode): p = player(cn) if sbserver.mapName() == '': setMap(mapname, mapmode) elif isAtLeastMaster(cn) and sbserver.masterMode() > 0: setMap(mapname, mapmode) elif mapmode != sbserver.gameMode() and (lock_mode or not allow_modevote): p.message(error('You cannot vote for a new game mode')) else: try: vote = player(cn).gamevars['mapvote'] allow_vote = vote[0] != mapname and vote[1] != mapmode except KeyError: allow_vote = True if allow_vote: sbserver.message(info(request_temp.substitute(colordict, user=p.name(), modename=sbserver.modeName(mapmode), mapname=mapname))) p.gamevars['mapvote'] = (mapname, mapmode) else: sbserver.playerMessage(cn, error('You have already requested this map.')) countVotes()
def onVoteKick(cn, args): '''@description Vote to kick a player from server @usage <cn>''' if args == '': sbserver.playerMessage(cn, error('Usage #votekick <cn>')) else: try: tcn = int(args) if player(cn).votekick == tcn: sbserver.playerMessage( cn, error('You have already voted to kick that player.')) allow_vote = False else: allow_vote = True except AttributeError: allow_vote = True if allow_vote: sbserver.message( info( vktemp.substitute(colordict, voter=sbserver.playerName(cn), victim=sbserver.playerName(tcn)))) player(cn).votekick = int(args) checkVotes(int(args))
def msgHelpText(cn, cmd): try: helpinfo = command_info[cmd] except KeyError: sbserver.playerMessage(cn, error('Command not found')) else: msgs = [] try: msgs.append(helpinfo.description) except AttributeError: pass for usage in helpinfo.usages: msgs.append(usage) for msg in msgs: sbserver.playerMessage(cn, info(msg))
def onSetTeam(tcn, cn, team): p = player(cn) r = player(tcn) if cn != tcn and not isAtLeastMaster(tcn): insufficientPermissions(tcn) return mode = currentMode() if isSafeTeam(team): execLater(p.suicide, ()) p.setTeam(team) else: r.message(error('You cannot join team \'%s\' in game mode %s' % ( team, modeName(currentMode()) )))
p.message(info('Usage: ' + command + ' ' + usage)) except StateError, e: p.message(error(str(e))) except ArgumentValueError, e: p.message(error('Invalid argument. ' + str(e))) except ValueError: p.message(error('Value Error: Did you specify a valid cn?')) exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() logging.warn('Uncaught ValueError raised in command handler.') logging.warn(traceback.format_exc()) except: exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() logging.warn('Uncaught exception occured in command handler.') logging.warn(traceback.format_exc()) else: sbserver.playerMessage(cn, error('Command not found')) def onMsg(self, cn, text): if len(text) > 0 and self.prefixes.find(text[0]) != -1: cmd = text[1:].split(' ')[0] self.trigger(cn, cmd, text[len(cmd)+2:]) return False return True commandmanager = CommandManager() def registerCommandHandler(command, func): xsbs.help.loadCommandInfo(command, func) commandmanager.register(command, func) class commandHandler(object): def __init__(self, name):
def onAuthSuccess(cn, name): if currentAdmin() != None: sbserver.playerMessage(cn, error('Admin is present')) return sbserver.setMaster(cn)