From d8e295f1e271e0b63dc76b10401016e88fdb8fa4 Mon Sep 17 00:00:00 2001 From: chino_desu <33673786+kafuuchino-desu@users.noreply.github.com> Date: Tue, 6 Nov 2018 22:43:01 +0800 Subject: [PATCH] add basic features --- mcdlog.py | 8 +++++--- mcgit.py | 50 +++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + server.py | 30 +++++++++++++++++++++------ serverinfoparser.py | 34 ++++++++++++++++++++++++++++++ 5 files changed, 114 insertions(+), 9 deletions(-) create mode 100644 mcgit.py create mode 100644 requirements.txt create mode 100644 serverinfoparser.py diff --git a/mcdlog.py b/mcdlog.py index 166e3ec..1b172e2 100644 --- a/mcdlog.py +++ b/mcdlog.py @@ -8,13 +8,15 @@ def errlog(data): print(PREFIX + data) traceback.print_exc() -def exitlog(data, crit=0, traceback=0): +def exitlog(data, crit=0, traceback=''): if crit == 0: print(PREFIX + data) print(PREFIX + 'Exiting...') if crit == 1: print(PREFIX + 'Critical Error Occured') print(PREFIX + 'Reason:' + data) - if traceback == 1: - traceback.print_exc() + if traceback != '': + print(traceback) + print(PREFIX + 'Exiting...') + diff --git a/mcgit.py b/mcgit.py new file mode 100644 index 0000000..8152832 --- /dev/null +++ b/mcgit.py @@ -0,0 +1,50 @@ +#!/bin/bash +# -*- coding: utf-8 -*- +#this file includes the mcgit object + +import configparser +import json +from gittle import Gittle +from mcdlog import * + +helpmsg = '''------thanks for use mcgit------ +those are basic functions: +!!mcg help -show this help message +!!mcg commit list -show a list of commits available +!!mcg commit [info] -reboot the server and commit current world +!!mcg rollback [commit] -reboot the server and rollback the world to a commit +!!mcg branch now -show which branch the server is on now +!!mcg branch list -show all branches avaliable +!!mcg branch change [branch] -reboot and change the server to another branch +(the world now will commit with info 'changebranch') +!!mcg branch delete [branch] -delete a branch +(if you are deleting the branch server is running on,the server will load master branch after reload) +!!mcg merge [branch] -merge the branch to master +--------------------------------''' + +class mcgit(object): + def __init__(self): + conf = configparser.ConfigParser() + try: + conf.read('mcgit.properties') + except: + log('mcgit failed to read a config file.Creating a new one') + conf.add_section('savePath') + conf.set('savePath', 'path', './mcgit') + #try: + #saveRepo = Gittle.init(conf.get('savePath','path')) + #except: + #exitlog('failed to init world repo...', 1, traceback.format_exc()) + + def onServerInfo(self, server, info): + if (info.isPlayer == 0): + pass + else: + if info.content.startswith('!!mcg'): + args = info.content.split(' ') + if (args[1] == 'help'): + for line in helpmsg.splitlines(): + server.tell(info.player, line) + + + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5c81838 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +gittle==0.5.0 diff --git a/server.py b/server.py index 8748c30..05a4e01 100644 --- a/server.py +++ b/server.py @@ -1,14 +1,17 @@ #!/bin/bash # -*- coding: utf-8 -*- # this file includes basic minecraft server class functions + from subprocess import Popen, PIPE import select import fcntl, os import time import sys +import traceback #import mcdplugin #still in develop -from mcdlog import * import mcgit +from mcdlog import * +import serverinfoparser def notice(): print('thanks for using MCDaemon,it\'s open source and u can find it here:') @@ -35,7 +38,8 @@ def tick(self): if line[11:].startswith('[Server Watchdog/FATAL]: A single server tick took 60.00 seconds (should be max 0.05)'): exitlog('single tick took too long for server and watchdog forced the server off', 1) sys.exit(0) - mcgithandler.onServerInfo() + result = serverinfoparser.parse(line) + mcgithandler.onServerInfo(server,result) ''' for singleplugin in plugins.plugins(): singleplugin.onServerInfo() @@ -77,10 +81,16 @@ def stop(self): self.cmdstop() try: self.forcestop() - print('forced server to stop') + log('forced server to stop') except: pass + def say(self, data): + self.execute('say ' + data) + + def tell(self, player, data): + self.execute('tellraw '+ player + ' {"text":"' + data + '"}') + if __name__ == "__main__": notice() @@ -94,9 +104,17 @@ def stop(self): errlog('error initalizing plugins,printing traceback') sys.exit(0) ''' - - server = Server() - mcgithandler= mcgit() + try: + server = Server() + except: + exitlog('failed to initalize the server.', 1, traceback.format_exc()) + sys.exit(0) + try: + mcgithandler= mcgit.mcgit() + except: + exitlog('failed to initalize plugins.', 1, traceback.format_exc()) + server.stop() + sys.exit(0) while True: try: server.tick() diff --git a/serverinfoparser.py b/serverinfoparser.py new file mode 100644 index 0000000..60ca59a --- /dev/null +++ b/serverinfoparser.py @@ -0,0 +1,34 @@ +#!/bin/bash +# -*- coding: utf-8 -*- +#this file is for handleing server info and parsing those info into a object + +import re + +class parseResult(object): + def __init__(self): + self.hour = 0 + self.min = 0 + self.sec = 0 + self.sourceProcess = '' + self.isPlayer = 0 + self.player = '' + self.message = '' + +def parse(line): + result = parseResult() + result.hour = line[1:3] + result.min = line[4:6] + result.sec = line[7:9] + result.sourceProcess = re.search(r'[[](.*?)[]]', line[11:]).group()[1:-1] + if (result.sourceProcess == 'Server thread/INFO') and (line[33:].startswith('<')): + player = re.search(r'[<](.*?)[>]', line[33:]).group()[1:-1] + if player != '': + result.isPlayer = 1 + result.player = player + content = line[33:].replace('[' + result.sourceProcess + ']: ', '' , 1) + result.content = content.replace('<' + result.player + '> ', '', 1) + else: + result.isPlayer = 0 + result.player = '' + result.content = line[11].replace('[' + result.sourceProcess + ']: ' , '' , 1) + return result