From 35d2ae9a115a17df2267cf0673d59419f4fd2a2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Sun, 30 Oct 2016 22:44:34 +0100 Subject: Split status and players to separate classes --- mcwrapper/players.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 mcwrapper/players.py (limited to 'mcwrapper/players.py') diff --git a/mcwrapper/players.py b/mcwrapper/players.py new file mode 100644 index 0000000..c3dabb4 --- /dev/null +++ b/mcwrapper/players.py @@ -0,0 +1,49 @@ +# vim: expandtab ft=python ts=4 sw=4 sts=4: +import os + +from . import prints + +__PLAYERSFILE__ = 'players' + + +class MCPlayers: + "Tracks online players" + def __init__(self, wrapper, file_export=False): + self.players = set() + self.wrapper = wrapper + wrapper.hook_start(self.__reset__) + wrapper.hook_stop(self.__reset__) + wrapper.hook_line('logged in with entity id', self.__user_join__) + wrapper.hook_line('left the game', self.__user_leave__) + self.file_export = file_export + self.__reset__() + + def clean(self): + try: + os.remove(__PLAYERSFILE__) + except FileNotFoundError: + pass + + def __reset__(self): + if self.file_export: + open(__PLAYERSFILE__, 'w') # Just create empty file + + def __user_join__(self, line): + username = line[len('[00:00:00] [Server thread/INFO]: '):] + username = username[:username.index('[')] + prints.info("User '" + username + "' joined server.") + self.players.add(username) + if self.file_export: + with open(__PLAYERSFILE__, 'a') as file: + file.write(username + '\n') + + def __user_leave__(self, line): + username = line[len('[00:00:00] [Server thread/INFO]: '):] + username = username[:username.index(' ')] + prints.info("User '" + username + "' left server.") + self.players.remove(username) + if self.file_export: + with open(__PLAYERSFILE__, 'w') as file: + file.write('\n'.join(self.players)) + if self.players: + file.write('\n') -- cgit v1.2.3