From e4b0c7f50efbe0c42aa933cb58a86a44367c1140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Sat, 15 Aug 2015 14:50:43 +0200 Subject: Implemented module version of mcwrapper mcwrapper functionality split to modules. This is basic implementation of modules handling. Two module types are recognized. For server and commands for mcwrapper cli interface. This way can be implemented different command and server features simply without modifying main script. Interface between main script and modules is defined using service lists. Service list informs main script what function should be called in module. More detailed description should be written to README.md file. Or even separated file describing module interface. In this commit are implemented five different modules. Players and status are server modules. They are used only if mcwrapper is running instance of Minecraft server. Modules say and list-modules are implementing mcwrapper actions. And last module argmodules is implementing mcwrapper argument. For modules usage also added utils.py. This contains shared usable code that is used even by main mcwrapper script. --- modules/players.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 modules/players.py (limited to 'modules/players.py') diff --git a/modules/players.py b/modules/players.py new file mode 100644 index 0000000..91e50b6 --- /dev/null +++ b/modules/players.py @@ -0,0 +1,52 @@ +import os +import sys +import re +import utils +from utils import conf + +services = ( + utils.Service.config, + utils.Service.init, + utils.Service.clean, + utils.Service.parse + ) + +players = set() + +def config(conf): + conf.playersFile = conf.folder + '/players' + +def init(): + with open(conf.playersFile, 'w') as f: + pass + +def clean(): + os.remove(conf.playersFile) + +def parse(line): + if 'logged in with entity id' in line: + name = line[len('[00:00:00] [Server thread/INFO]: '):] + name = name[:name.index('[')] + __user_join__(name) + elif 'left the game' in line: + name = line[len('[00:00:00] [Server thread/INFO]: '):] + name = name[:name.index(' ')] + __user_leave__(name) + else: + return False + return True + + +def __user_join__(username): + print("User '" + username + "' joined server.") + with open(conf.playersFile, 'a') as f: + players.add(username) + f.write(username + '\n') + +def __user_leave__(username): + print("User '" + username + "' left server.") + players.remove(username) + with open(conf.playersFile, 'w') as f: + f.writelines(players) + if players: + f.write('\n') -- cgit v1.2.3