Skip to content

Commit

Permalink
Updated code to support Python 3.6.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Appelpitje committed Feb 3, 2018
1 parent 10745af commit ea545f8
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions MOHAA-PTC.py
Expand Up @@ -4,20 +4,22 @@
import socket
import requests
import ctypes
import ConfigParser
import configparser
from datetime import timedelta
from functools import update_wrapper

from flask import Flask, request, jsonify, make_response, current_app

app = Flask(__name__)


import logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)


def readConfig():
config = ConfigParser.RawConfigParser()
config = configparser.RawConfigParser()
try:
config.read('settings.cfg')
settings["MOHPTC_port"] = config.get('MOHPTC-server', 'port')
Expand All @@ -27,7 +29,8 @@ def readConfig():
settings["server_rconpassword"] = config.get('MOH-server', 'rconpassword')
handleGame()
except:
print 'Error with the config, please check if the config file exists and is in correct format.'
print('Error with the config, please check if the config file exists and is in correct format.')


def handleGame():
settings["server_windows"] = ""
Expand All @@ -42,9 +45,9 @@ def crossdomain(origin=None, methods=None, headers=None,
automatic_options=True):
if methods is not None:
methods = ', '.join(sorted(x.upper() for x in methods))
if headers is not None and not isinstance(headers, basestring):
if headers is not None and not isinstance(headers, str):
headers = ', '.join(x.upper() for x in headers)
if not isinstance(origin, basestring):
if not isinstance(origin, str):
origin = ', '.join(origin)
if isinstance(max_age, timedelta):
max_age = max_age.total_seconds()
Expand Down Expand Up @@ -91,23 +94,24 @@ def read_console():

buffer = win32gui.PyMakeBuffer(25555)
length = win32gui.SendMessage(console, win32con.WM_GETTEXT, 25555, buffer)
result = buffer[:length]
result = buffer[0:length * 2].tobytes().decode("UTF-16")

#Click the clear button
win32gui.SendMessage(clearBtn, win32con.WM_LBUTTONDOWN, 0, 0)
win32gui.SendMessage(clearBtn, win32con.WM_LBUTTONUP, 0, 0)

#split on newline
splitted = result.splitlines()
print(splitted)
cmd = ""

kills = []
how = ["was rifled by ", "was machine-gunned by ", "was bashed by ", "was gunned down by ", "was crushed by ", "was clubbed by ", "was sniped by ", "was perforated by ", "pumped full of buckshot by "]
how = [" was rifled by ", " was machine-gunned by ", " was bashed by ", " was gunned down by ", " was sniped by ", " was crushed by ", " was clubbed by ", " was sniped by ", " was perforated by ", " pumped full of buckshot by "]
for index, value in enumerate(splitted):
if any(x in value for x in how):
kills.append(value.decode("latin1"))
kills.append(value)

print kills
print(kills)

# Split kills
# for index, value in enumerate(kills):
Expand All @@ -129,9 +133,10 @@ def read_console():
cmd = "say [Joke] " + joke()
rcon(settings["server_ip"], settings["server_port"], settings["server_rconpassword"], cmd)

except win32gui.error, e:
except Exception as e:
print("Error: " + str(e))


def rcon(ip, port, password, command):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
Expand All @@ -157,7 +162,7 @@ def joke():
r = requests.get('https://icanhazdadjoke.com/', headers={'Accept': 'application/json'})
jokedata = r.json()
try:
return str(jokedata['joke'].decode("latin1"))
return str(jokedata['joke'])
except:
return str("Oops something went wrong! Try again!")

Expand All @@ -177,8 +182,8 @@ def main():
global settings
settings = {}
readConfig()
ctypes.windll.kernel32.SetConsoleTitleA("MOHAA Python Total Control [port " + str(settings["MOHPTC_port"]) + "]")
print("Starting MOHPTC on port: " + str(settings["MOHPTC_port"]))
ctypes.windll.kernel32.SetConsoleTitleA("MOHAA Python Total Control [port " + settings["MOHPTC_port"] + "]")
print("Starting MOHPTC on port: " + settings["MOHPTC_port"])
read_console()
consoleMessages()
app.run(threaded=True,debug=False,use_evalex=False,host='0.0.0.0',port=settings["MOHPTC_port"])
app.run(threaded=True, debug=False, use_evalex=False, host='0.0.0.0', port=int(settings["MOHPTC_port"]))

0 comments on commit ea545f8

Please sign in to comment.