Commit 43efd410 authored by Trevor Cappallo's avatar Trevor Cappallo
Browse files

deleted comments, fixed spamming game started bug

parent 3bcd2619
import os
import urllib.request
import discord
import re
import pprint
......@@ -8,37 +7,12 @@ import platform
from discord.ext import tasks, commands
from discord.ext.commands import Bot
# line=b'* 0 0 0 37 1E9C4 0 MG-on\n'
# line=b'0 10E1 "POLAND vs Mystery" B180D21 12C 691 91 "" "0/1 -- •15:30" 900 2 42 6178368D 108.20.79.156 10E1 "Kate Kirby" B180B21 96 3B9 9 "test msg" "..." 0 16 32 61780D39 108.20.79.156 10E1 "Mario" B180B21 96 3B9 9 "test msg" "..." 0 16 32 61780D39 * 0 0 0 37 1E9C4 0 MG-on\n'
# line=b'0 10E1 "POLAND vs Mystery" B180D21 12C 691 91 "" "0/2 -- 30:30•" 900 2 42 6178368D 108.20.79.156 10E1 "Kate Kirby" B180B21 96 3B9 9 "test msg" "..." 0 16 32 6178382F * 1 0 0 C 1E9D4 0 MG-on\n'
# line=line.decode('utf-8')
reGame = re.compile(''
'\s*([0-9.]+)'
'\s+([0-9a-fA-F]+)'
'\s+"(.*?)"'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s+"(.*?)"'
'\s+"(.*?)"'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s*',
re.MULTILINE)
reGame = re.compile('\s*([0-9.]+)\s+([0-9a-fA-F]+)\s+"(.*?)"'
'\s+([0-9a-fA-F]+)\s+([0-9a-fA-F]+)\s+([0-9a-fA-F]+)\s+([0-9a-fA-F]+)\s+"(.*?)"\s+"(.*?)"'
'\s+([0-9a-fA-F]+)\s+([0-9a-fA-F]+)\s+([0-9a-fA-F]+)\s+([0-9a-fA-F]+)\s*',re.MULTILINE)
reSummary = re.compile('^'
'\s*(.*)\*'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s+([0-9a-fA-F]+)'
'\s+MG\-on.*$',
re.MULTILINE)
reSummary = re.compile('^\s*(.*)\*\s+([0-9a-fA-F]+)\s+([0-9a-fA-F]+)\s+([0-9a-fA-F]+)\s+'
'([0-9a-fA-F]+)\s+([0-9a-fA-F]+)\s+([0-9a-fA-F]+)\s+MG\-on.*$',re.MULTILINE)
oldGames=[]
newGames=[]
......@@ -49,56 +23,38 @@ def parse_server_poll(data):
(listing, summary) = (m.groups()[0], m.groups()[1:])
m = reGame.findall(listing)
r = {'games':
[{
'ip': a[0],
'port': int(a[1], 16),
'name': a[2],
# 'game_info': [{
# 'trial': x>>0 & 1<<2-1,
# 'player_cfg': x>>2 & 1<<3-1,
# 'nb_set': x>>5 & 1<<2-1,
# 'skill_mode': x>>7 & 1<<2-1,
# 'court': x>>9 & 1<<9-1,
# 'game_per_set': x>>18 & 1<<3-1,
# 'unused': x>>21 & 1<<1-1,
# 'control_mode': x>>22 & 1<<2-1,
# 'preview': x>>24 & 1<<3-1,
# 'tiredness': x>>27 & 1<<1-1,
# } for x in [int(a[3], 16)]][0],
'game_info': [{
'tiredness': (x>>27) & ((1<<1)-1),
'preview': (x>>24) & ((1<<3)-1),
'control_mode': (x>>22) & ((1<<2)-1),
'unused': (x>>21) & ((1<<1)-1),
'game_per_set': (x>>18) & ((1<<3)-1),
'court': (x>>9) & ((1<<9)-1),
'skill_mode': (x>>7) & ((1<<2)-1),
'nb_set': (x>>5) & ((1<<2)-1),
'player_cfg': (x>>2) & ((1<<3)-1),
'trial': (x>>0) & ((1<<2)-1),
} for x in [int(a[3], 16)]][0],
'max_ping': int(a[4], 16),
'elo': int(a[5], 16),
'nb_game': int(a[6], 16),
'tagline': a[7],
'score': a[8],
'other_elo': int(a[9], 16),
'give_up_rate': int(a[10], 16),
'reputation': int(a[11], 16),
'creation_time': int(a[12], 16),
} for a in m],
r = {'games': [
{
'ip': a[0],
'port': int(a[1], 16),
'name': a[2],
'game_info': [
{
'tiredness': (x>>27) & ((1<<1)-1),
'preview': (x>>24) & ((1<<3)-1),
'control_mode': (x>>22) & ((1<<2)-1),
'unused': (x>>21) & ((1<<1)-1),
'game_per_set': (x>>18) & ((1<<3)-1),
'court': (x>>9) & ((1<<9)-1),
'skill_mode': (x>>7) & ((1<<2)-1),
'nb_set': (x>>5) & ((1<<2)-1),
'player_cfg': (x>>2) & ((1<<3)-1),
'trial': (x>>0) & ((1<<2)-1),
} for x in [int(a[3], 16)]][0],
'max_ping': int(a[4], 16),
'elo': int(a[5], 16),
'nb_game': int(a[6], 16),
'tagline': a[7],
'score': a[8],
'other_elo': int(a[9], 16),
'give_up_rate': int(a[10], 16),
'reputation': int(a[11], 16),
'creation_time': int(a[12], 16),
} for a in m],
'summary': summary}
return(r)
# pp = pprint.PrettyPrinter(indent=4)
# pp.pprint(r)
# print(parse_server_poll(line))
#stats = [0,0,0]
TOKEN = open("token.txt", "r").readline()
intents = discord.Intents.default()
......@@ -108,11 +64,8 @@ intents.presences = False
intents.messages = True
ch = None
# client = discord.Client(intents = intents)
# client = commands.Bot(command_prefix='!')
bot = Bot(command_prefix='!', intents=intents)
# The code in this even is executed when the bot is ready
@bot.event
async def on_ready():
global ch
......@@ -121,85 +74,36 @@ async def on_ready():
print(f"Python version: {platform.python_version()}")
print(f"Running on: {platform.system()} {platform.release()} ({os.name})")
print("-------------------")
channels = [c for g in bot.guilds for c in g.text_channels] #getting all the text channels bot can see
channels = [c for g in bot.guilds for c in g.text_channels]
ch = discord.utils.get(channels, name="find-a-game")
# await ch.send("hello world!")
status_task.start()
# Setup the game status task of the bot
@tasks.loop(seconds=15.0)
async def status_task():
global oldGames, newGames
data = requests.get("https://www.managames.com/tennis/online/TE4_ServerList.php?Poll=1")
# print(data.text)
# print('-----')
info = parse_server_poll(data.text.replace('\n',' '))
# info2 = parse_server_poll(data.content.decode('utf-8'))
# pprint.pprint(info2)
newGames = [x for x in info['games'] if x not in oldGames]
newGames = [{k:v for (k,v) in x.items() if k!= 'score'} for x in info['games']]
newGames = [x for x in newGames if x not in oldGames]
if len(newGames)>0:
out=[]
for game in newGames:
if game['ip']=='0':
s = f"{game['name']} started their match."
else:
s = f"{game['name']} (elo: {game['elo']}, rep: {game['reputation']}) wants to play a {game['game_info']['nb_set']}-set match. Comment: {game['tagline']}"
s = f"{game['name']} (elo: {game['elo']}, rep: {game['reputation']}%) wants to play a {game['game_info']['nb_set']}-set match."
if len(game['tagline'])>0:
s += f" Comment: {game['tagline']}"
out += [s]
pprint.pprint(out)
await ch.send("\n".join(out))
oldGames=newGames
# else:
# await ch.send("No current games found.")
# else:
# out = [info['summary']]
# await bot.change_presence(activity=discord.Game(random.choice(statuses)))
# @client.event
# async def on_ready():
# print('Logged in as {0.user}'.format(client))
# # channel = discord.utils.get(client.guilds[0].text_channels, name="General")
# channels = [c for g in client.guilds for c in g.text_channels] #getting all the text channels bot can see
# ch = discord.utils.get(channels, name="general")
# await ch.send("hello world!")
# @client.event
# async def on_message(message):
# if message.author == client.user:
# return
# if message.content.startswith('!games'):
# # data = urllib.request.urlopen("https://www.managames.com/tennis/online/TE4_ServerList.php?Poll=1").read()
# # data = data.decode('utf-8')
# data = requests.get("https://www.managames.com/tennis/online/TE4_ServerList.php?Poll=1")
# print(data.text)
# print('-----')
# info = parse_server_poll(data.text.replace('\n',' '))
# pprint.pprint(info)
# # info2 = parse_server_poll(data.content.decode('utf-8'))
# # pprint.pprint(info2)
# if len(info['games'])>0:
# out = [pprint.pformat(x) for x in info['games']]
# print(out)
# await message.channel.send("\n".join(out))
# else:
# await message.channel.send("No current games found.")
# # else:
# # out = [info['summary']]
bot.run(TOKEN)
# client.run(TOKEN)
# 108.20.79.156 10E1 "Kate Kirby" B180B21 96 3B9 9 "test msg" "..." 0 16 32 61780D39 * 0 0 0 37 1E9C4 0 MG-on
# 123.45.67.89 10E1 "Kate Kirby" B180B21 96 3B9 9 "test msg" "..." 0 16 32 61780D39 * 0 0 0 37 1E9C4 0 MG-on
# * 0 0 0 37 1E9C4 0 MG-on
# For each server, you'll see this :
......@@ -219,41 +123,4 @@ bot.run(TOKEN)
# - <unused> : 1
# - ControlMode : 2
# - Preview : 3
# - Tiredness : 1
# {
# 'trial': x>>0 & 1<<2-1,
# 'player_cfg': x>>2 & 1<<3-1,
# 'nb_set': x>>5 & 1<<2-1,
# 'skill_mode': x>>7 & 1<<2-1,
# 'court': x>>9 & 1<<9-1,
# 'game_per_set': x>>18 & 1<<3-1,
# 'unused': x>>21 & 1<<1-1,
# 'control_mode': x>>22 & 1<<2-1,
# 'preview': x>>24 & 1<<3-1,
# 'tiredness': x>>27 & 1<<1-1,
# }
# {
# 'tiredness': x & 1,
# 'preview': x>>1 & 1<<3-1,
# 'control_mode': x>>4 & 1<<2-1,
# 'unused': x>>5 & 1<<1-1,
# 'game_per_set': x>>6 & 1<<3-1,
# 'court': x>>9 & 1<<9-1,
# 'skill_mode': x>>18 & 1<<2-1,
# 'nb_set': x>>20 & 1<<2-1,
# 'player_cfg': x>>22 & 1<<3-1,
# 'trial': x>>25 & 1<<2-1,
# }
# bot = commands.Bot(command_prefix='!')
# initial_extensions = ['cogs.mycog'] #cog.(filename)
# if __name__ == '__main__':
# for extension in initial_extensions:
# bot.load_extension(extension)
# @client.event
# async def on_ready():
# print('Bot is ready')
# bot.run(TOKEN)
\ No newline at end of file
# - Tiredness : 1
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment