diff options
| author | Fulgen301 <tokmajigeorge@gmailc.om> | 2016-12-08 15:16:38 +0100 |
|---|---|---|
| committer | Fulgen301 <tokmajigeorge@gmailc.om> | 2016-12-08 15:16:38 +0100 |
| commit | e00f82325d0e8d6c6fdfc2decd340aa1dfccc00f (patch) | |
| tree | 45e2773bad070f4192dc14e3f81f03eaa28498be /pycrctrl.pyp | |
| parent | 1c9184599f38f88496595fea9fd8b633016028d7 (diff) | |
| download | pycrctrl-e00f82325d0e8d6c6fdfc2decd340aa1dfccc00f.tar.gz pycrctrl-e00f82325d0e8d6c6fdfc2decd340aa1dfccc00f.zip | |
Using local config file; addScenario(self, link) added.
Diffstat (limited to 'pycrctrl.pyp')
| -rw-r--r-- | pycrctrl.pyp | 125 |
1 files changed, 85 insertions, 40 deletions
diff --git a/pycrctrl.pyp b/pycrctrl.pyp index 7dc4a5b..b0568ea 100644 --- a/pycrctrl.pyp +++ b/pycrctrl.pyp @@ -22,17 +22,20 @@ if sys.version_info < (3,6): import socket import subprocess import urllib.request +import urllib.error +import urllib.parse + from _thread import start_new_thread import inspect import queue import traceback import os -import PyQt5 import re import signal +import json import supybot.ircmsgs as ircmsgs -from pickle import Unpickler +from pickle import Pickler, Unpickler from PyQt5.QtCore import * try: from iostream import * @@ -99,43 +102,42 @@ class PyCRCtrl(object): clonk = None thread_started = False stopped = False - autohost = False + config = {} scenario = "" - prefix = "@" + #prefix = "@" commands = {} - engine = "clonk" - codec = QTextCodec.codecForName("Windows-1252") - commandline = "/fullscreen /lobby:300 /config:config.txt /record /faircrew" + #engine = "clonk" + codec = None + #commandline = "/fullscreen /lobby:300 /config:config.txt /record /faircrew" - path = QString() + #path = QString() scenlist = QStringList() league_scenlist = QStringList() - channels = {} + #channels = {} topic = "Kein laufendes Spiel." league = True __state = "Lobby" - RegExps = { - "lobbyStartExp" : "((?:Los geht's!|Action go!)\s*)", - "startExp" : r"^Start!\s*$", - "joinExp" : r"^Client (.+) (?:verbunden|connected)\.\s*$", - "leaveExp" : "^Client (.+) (?:entfernt|removed)(.*)", - "shutdownExp" : r"^Internetspiel ausgewertet(.*)" - } + #RegExps = { + # "lobbyStartExp" : "((?:Los geht's!|Action go!)\s*)", + # "startExp" : r"^Start!\s*$", + # "joinExp" : r"^Client (.+) (?:verbunden|connected)\.\s*$", + # "leaveExp" : "^Client (.+) (?:entfernt|removed)(.*)", + # "shutdownExp" : r"^Internetspiel ausgewertet(.*)" + # } - def __init__(self, **kwargs): + def __init__(self, irc=None, path=None): if sys.platform == "win32": raise NotImplementedError("{} wird nicht unterstützt!".format(sys.platform)) - self.autohost = bool(kwargs["autohost"]) - self.irc = kwargs["irc"] - self.path = QString(kwargs["path"]) - self.channels["ingame"] = QString(kwargs["ingamechannel"]) - self.channels["parent"] = QString(kwargs["channel"]) + self.irc = irc + self.path = path + self.loadConfigFile() self.loadScenarioList() + self.codec = codec = QTextCodec.codecForName(self.getConfigEntry("encoding")) self.queue = queue.Queue(5) def __ostream__(self, ostream): @@ -162,10 +164,43 @@ class PyCRCtrl(object): self.league_scenlist.strlist = Unpickler(fobj).load() return True + + def loadConfigFile(self): + if self.path == None: + return False + + self.config = json.load(open(os.path.join(self.path, "pycrctrl.conf"),"r")) + return True + + def getConfigEntry(self, entry=""): + if type(self.config) != dict: + return + + try: + return self.config[entry] + except KeyError: + return + + def setConfigEntry(self, entry="", value=None): + if type(self.config) != dict: + return + + try: + self.config[entry] = value + finally: + json.dump(self.config, open(os.path.join(self.path, "pycrctrl.conf"), "w")) + + return True def host(self, scenario=None) -> str: if scenario == None: - return (CmdResult.SyntaxFail, "Bitte gib einen Szenarionamen an!") + return (CmdResult.SyntaxFail, "Bitte gib einen Szenarionamen an!")"RegExps" : { + "lobbyStartExp" : "((?:Los geht\'s!|Action go!)\\\\s*)", + "startExp" : "^Start!\\\\s*$", + "joinExp" : "^Client (.+) (?:verbunden|connected)\\.\\s*$", + "leaveExp" : "^Client (.+) (?:entfernt|removed)(.*)", + "shutdownExp" : "^Internetspiel ausgewertet(.*)" + } if type(scenario) in [bytes, QByteArray]: try: scenario = self.codec.toUnicode(scenario) @@ -200,7 +235,7 @@ class PyCRCtrl(object): else: self.scenario = self.scenlist[randint(0,len(self.scenlist))-1] self.clonk = subprocess.Popen( - './{} {} "{}"'.format(self.engine, self.commandline + (" --" if type(self).__name__ == "PyOCCtrl" else " /") + ("league" if self.scenario in self.league_scenlist() else "noleague"),self.scenario), + './{} {} "{}"'.format(self.getConfigEntry("engine"), self.getConfigEntry("commandline") + (" --" if type(self).__name__ == "PyOCCtrl" else " /") + ("league" if self.scenario in self.league_scenlist() else "noleague"),self.scenario), 0, None, subprocess.PIPE, @@ -227,7 +262,7 @@ class PyCRCtrl(object): try: output = self.clonk.stdout.readline() - if bool(re.match(self.RegExps["shutdownExp"], self.codec.toUnicode(output))): + if bool(re.match(self.getConfigEntry("RegExps")["shutdownExp"], self.codec.toUnicode(output))): self.clonk.stdin.close() elif output == b"" and self.clonk.poll() is not None: if self.clonk: @@ -262,10 +297,10 @@ class PyCRCtrl(object): if type(x) == tuple and x[1] != "": self.writeToServer(x[1]) del x - if bool(re.match(self.RegExps["lobbyStartExp"], output)): + if bool(re.match(self.getConfigEntry("RegExps")["lobbyStartExp"], output)): self.state = "Lädt" - elif bool(re.match(self.RegExps["startExp"], output)): + elif bool(re.match(self.getConfigEntry("RegExps")["startExp"], output)): self.state = "Läuft" try: cout << output << endl @@ -277,11 +312,11 @@ class PyCRCtrl(object): if output.find("<" + self.irc.nick + ">") == -1: if bool(re.match(r"^<.*>", output) and output.find("[IRC]") == -1) and \ bool(output.find(self.prefix) == -1): - self.irc.reply("[Clonk]{}".format(output), to=self.channels["ingame"]) + self.irc.reply("[Clonk]{}".format(output), to=self.getConfigEntry("channels")["ingame"]) - elif bool(re.match(self.RegExps["joinExp"], output)) or \ - bool(re.match(self.RegExps["leaveExp"], output)): - self.irc.reply(output, to=self.channels["ingame"]) + elif bool(re.match(self.getConfigEntry("RegExps")["joinExp"], output)) or \ + bool(re.match(self.getConfigEntry("RegExps")["leaveExp"], output)): + self.irc.reply(output, to=self.getConfigEntry("channels")["ingame"]) except KeyboardInterrupt: @@ -310,7 +345,7 @@ class PyCRCtrl(object): if not self.irc: return for channel in msg.args[0].split(","): - if channel == self.channels["ingame"] and msg.nick != self.irc.nick: + if channel == self.getConfigEntry("channels")["ingame"] and msg.nick != self.irc.nick: self.writeToServer("[IRC]<{}> {}".format(msg.nick, msg.args[1])) @@ -343,7 +378,7 @@ class PyCRCtrl(object): else: self.topic = text - return bool(self.irc.sendMsg(ircmsgs.topic(self.channels["ingame"], text))) + return bool(self.irc.sendMsg(ircmsgs.topic(self.getConfigEntry("channels")["ingame"], text))) def start(self, time=None): self.stopped = False @@ -414,18 +449,28 @@ class PyCRCtrl(object): def addCommand(self, function, text): self.commands[text.split(" ")[0]] = function return self + + def addScenario(self, link): + name = "" + fname = urllib.parse.urlsplit(link).split("/") + for item in fname: + if re.match(r"(.*)\.[oc][c4]s",item): + name = item + break + + site = urllib.request.urlopen(link).read() #WARNING: Raises an error if the link is invalid! + with open(os.path.join(self.path, name),"wb") as fobj: + fobj.write(site) + + self.scenlist().append(name) + Pickler(open(os.path.join(self.path, "scenlist"))).dump(self.scenlist()) + return self class PyOCCtrl(PyCRCtrl): - RegExps = { - "lobbyStartExp" : "((?:Los geht's!|Action go!)\s*)", - "startExp" : r"^Spiel gestartet.", - "joinExp" : r"^Client (.+) (?:verbunden|connected)\.\s*$", - "leaveExp" : "^Client (.+) (?:entfernt|removed)(.*)", - "shutdownExp" : r"^Spiel ausgewertet(.*)" - } + engine = "openclonk-server" codec = QTextCodec.codecForName("UTF-8") |
