首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CS 1.6蒸汽白名单检查器

CS 1.6蒸汽白名单检查器
EN

Code Review用户
提问于 2020-06-04 02:20:27
回答 1查看 98关注 0票数 2

我编写了一些代码,用于读取CS1.6控制台上rcon users命令的输出。然后它会通知我任何不在我的whitelist.csv中或在我的黑名单中的球员。该程序需要3个文件作为输入:

  1. 包含CS1.6控制台上rcon users命令的原始输出的文本文件。
  2. 一个CSV文件,包含SteamID和白名单球员的名称.
  3. 包含SteamID和黑名单玩家名字的CSV文件.
代码语言:javascript
复制
#!/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()
EN

回答 1

Code Review用户

回答已采纳

发布于 2020-06-04 19:02:02

这是可行的,但本质上是O(N**2);通过将您的白名单/黑名单转换为dicts,您可以相当容易地使其成为O(N):

代码语言:javascript
复制
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进行搜索(这是您所做过的唯一一种搜索)。

票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/243355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档