我编写了一些代码,用于读取CS1.6控制台上rcon users命令的输出。然后它会通知我任何不在我的whitelist.csv中或在我的黑名单中的球员。该程序需要3个文件作为输入:
rcon users命令的原始输出的文本文件。#!/usr/bin/env python3
import csv
import re
class WhiteListVerifier:
"""
Class to check if non-white-listed players are playing CS.
"""
def __init__(self, users_filepath, whitelist_filepath, blacklist_filepath):
"""
Get filepaths and load the following files: users file , whitelist file and blacklist file
users_filepath: Path to a text file which contains the raw output of the 'rcon users' in the in-game console.
whitelist_filepath: Path to a csv file which contains Steam IDs and player-names of the white-listed players.
blacklist_filepath: Path to a csv file which contains Steam IDs, player-names and alert-message for the black-listed players.
"""
with open(users_filepath) as f:
self.users_rawcontents = f.read()
self.whitelist = list(csv.DictReader(open(whitelist_filepath)))
self.blacklist = list(csv.DictReader(open(blacklist_filepath)))
def get_playername(self, steamid, player_records):
"""
Function to return name of a person if steamid exits in given records
and return 'Unknown' otherwise.
"""
for player in player_records:
if steamid == player['Steam_ID']:
return player['Player_Name']
return 'Unknown'
def get_active_users_steamids(self):
"""
Function to extract the Steam IDs using output of 'rcon users' command.
"""
active_users_steamids = re.findall(r'STEAM_[0-5]:[01]:\d+', self.users_rawcontents)
return active_users_steamids
def verify_steamids(self):
"""
Function to alert if people other than the ones mentioned in the whitelist are playing.
"""
active_users_steamids = self.get_active_users_steamids()
num_nonwhitelisted_users = 0
for active_user in active_users_steamids:
player = self.get_playername(active_user, self.whitelist)
if player is 'Unknown':
num_nonwhitelisted_users += 1
nonwhitelisted_playername = self.get_playername(active_user, self.blacklist)
print('-- nonwhitelisted player: '+str(nonwhitelisted_playername))
print('>> Total number of non-whitelisted players: '+str(num_nonwhitelisted_users))
if __name__ == '__main__':
checker = WhiteListVerifier('data/users.txt', 'data/whitelist.csv', 'data/blacklist.csv')
checker.verify_steamids()发布于 2020-06-04 19:02:02
这是可行的,但本质上是O(N**2);通过将您的白名单/黑名单转换为dicts,您可以相当容易地使其成为O(N):
def __init__(self, users_filepath, whitelist_filepath, blacklist_filepath):
"""
Get filepaths and load the following files: users file , whitelist file and blacklist file
users_filepath: Path to a text file which contains the raw output of the 'rcon users' in the in-game console.
whitelist_filepath: Path to a csv file which contains Steam IDs and player-names of the white-listed players.
blacklist_filepath: Path to a csv file which contains Steam IDs, player-names and alert-message for the black-listed players.
"""
with open(users_filepath) as f:
self.users_rawcontents = f.read()
raw_whitelist = list(csv.DictReader(open(whitelist_filepath)))
raw_blacklist = list(csv.DictReader(open(blacklist_filepath)))
self.whitelist = { p['Steam_ID']: p for p in raw_whitelist }
self.blacklist = { p['Steam_ID']: p for p in raw_blacklist }
def get_playername(self, steamid, player_records):
"""
Function to return name of a person if steamid exits in given records
and return 'Unknown' otherwise.
"""
return player_records.get(steamid, 'Unknown')因此,在每次查找某人时,它都不会遍历整个player_records列表,而是在启动时对您的白名单和黑名单进行一次迭代,从而使它们在启动时成为O(1),然后使用steamid进行搜索(这是您所做过的唯一一种搜索)。
https://codereview.stackexchange.com/questions/243355
复制相似问题