def main(): """Runs the RCON client.""" args = get_args() log_level = DEBUG if args.debug else INFO basicConfig(level=log_level, format=LOG_FORMAT) host, port = get_credentials(args.server) try: with Client(host, port, timeout=args.timeout) as client: if args.action == 'basic-stats': print(get_basic_stats(client, args), flush=True) elif args.action == 'full-stats': print(get_full_stats(client, args), flush=True) elif args.action == 'check': exit(0 if check(client, args) else 1) except KeyboardInterrupt: print() LOGGER.error('Aborted by user.') exit(1) except ConnectionRefusedError: LOGGER.error('Connection refused.') exit(3) except timeout: LOGGER.error('Connection timeout.') exit(4)
def get_basic_stats(client: Client, args: Namespace): # pylint: disable=R0911 """Handles basic stats queries.""" basic_stats = client.stats(full=False) if not args.field: return dumps(basic_stats.to_json(), indent=args.indent) if args.field == 'motd': return basic_stats.motd if args.field == 'game-type': return basic_stats.game_type if args.field == 'map': return basic_stats.map if args.field == 'num-players': return basic_stats.num_players if args.field == 'max-players': return basic_stats.max_players if args.field == 'host-port': return basic_stats.host_port if args.field == 'host-ip': return basic_stats.host_ip raise ValueError('Invalid action.')
def main(): """Runs the RCON client.""" args = get_args() log_level = DEBUG if args.debug else INFO basicConfig(level=log_level, format=LOG_FORMAT) host, port = get_credentials(args.server) try: with Client(host, port, timeout=args.timeout) as client: if args.action == 'basic-stats': print(basic_stats(client, args), flush=True) elif args.action == 'full-stats': print(full_stats(client, args), flush=True) except timeout: LOGGER.error('Connection timeout.') exit(3)
def get_full_stats(client: Client, args: Namespace): # pylint: disable=R0911 """Handles full stats queries.""" full_stats = client.stats(full=True) if not args.field: return dumps(full_stats.to_json(), indent=args.indent) if args.field == 'hostname': return full_stats.host_name if args.field == 'game-type': return full_stats.game_type if args.field == 'game-id': return full_stats.game_id if args.field == 'version': return full_stats.version if args.field == 'plugins': return dumps(full_stats.plugins, indent=args.indent) if args.field == 'map': return full_stats.map if args.field == 'num-players': return full_stats.num_players if args.field == 'max-players': return full_stats.max_players if args.field == 'host-port': return full_stats.host_port if args.field == 'host-ip': return full_stats.host_ip if args.field == 'players': return dumps(full_stats.players, indent=args.indent) raise ValueError('Invalid action.')
def check(client: Client, args: Namespace) -> bool: """Checks whether the specified conditions are met.""" stats = client.stats(full=True) if args.max_players is not None and stats.num_players > args.may_players: return False if args.min_players is not None and stats.num_players < args.min_players: return False if args.players_online is not None and any( player not in stats.players for player in args.players_online): return False if args.players_offline is not None and any( player in stats.players for player in args.players_offline): return False return True