Add support for specific games

This commit is contained in:
Rampoina 2021-07-11 19:44:45 +02:00
parent f327a1000a
commit 64ac0d5798
1 changed files with 62 additions and 39 deletions

101
bot.py
View File

@ -4,69 +4,92 @@ from irctokens import build, Line
from ircrobots import Bot as BaseBot
from ircrobots import Server as BaseServer
from ircrobots import ConnectionParams
import pickle
servers = []
match_players = {}
connections = {}
def update_file(name):
file = open(f"playerlist-{name}.txt", "w")
file.seek(0)
file.truncate()
for player in match_players[name]:
file.write(player + '\n')
with open(f"playerlist-{name}.txt", "wb") as file:
pickle.dump(match_players, file)
def read_file(name):
file = open(f"playerlist-{name}.txt", "r")
for player in file:
match_players[name].add(player[:-1]) #Remove newline
file.close()
try:
with open(f"playerlist-{name}.txt", "rb") as file:
temp = pickle.load(file)
match_players[name] = temp[name]
except EOFError:
match_players[name] = {}
print("Empty")
class Server(BaseServer):
async def line_read(self, line: Line):
print(f"{self.name} < {line.format()}")
if "PRIVMSG" in line.command and any(channel in line.params[0] for channel in self.params.autojoin):
chan = self.channels[line.params[0]].name
if line.params[1].startswith(".matchmake"):
channel = self.channels[line.params[0]]
ping = ""
for player in match_players[self.name + '-' + chan]:
pfold = self.casefold(player)
if pfold in channel.users:
ping += f"{channel.users[pfold].nickname} "
if len(line.params[1].split(" ")) < 2:
await self.send(build("PRIVMSG", [chan, "ERROR: game not specified"]))
else:
game = " ".join(line.params[1].split(" ")[1:]).lower()
ping = ""
if game not in match_players[self.name + '-' + chan]:
await self.send(build("PRIVMSG", [chan, "ERROR: no players in "+ game]))
else:
for player in match_players[self.name + '-' + chan][game]:
pfold = self.casefold(player)
if pfold in channel.users:
ping += f"{channel.users[pfold].nickname} "
await self.send(build("PRIVMSG", [chan, f"Pinging users: {ping}"]))
if connections[self.name]:
for connection,address in connections[self.name].items():
if connection in line.params[1]:
if ':' in address:
realaddress = address.split(':')[0]
port = address.split(':')[1]
self.send(build("PRIVMSG", [chan, f"Connect using nc {realaddress} {port}"]))
else:
self.send(build("PRIVMSG", [chan, f"Connect using nc {address} 1234"]))
await self.send(build("PRIVMSG", [chan, "Anyone ready for " + game + f" : {ping} ?"]))
if connections[self.name]:
for connection,address in connections[self.name].items():
if connection in line.params[1]:
if ':' in address:
realaddress = address.split(':')[0]
port = address.split(':')[1]
self.send(build("PRIVMSG", [chan, f"Connect using nc {realaddress} {port}"]))
else:
self.send(build("PRIVMSG", [chan, f"Connect using nc {address} 1234"]))
elif line.params[1].startswith(".matchadd"):
user = line.source.split('!')[0]
if user in match_players[self.name + '-' + chan]:
await self.send(build("PRIVMSG", [chan, "ERROR: player already in list."]))
if len(line.params[1].split(" ")) < 2:
await self.send(build("PRIVMSG", [chan, "ERROR: game not specified"]))
else:
match_players[self.name + '-' + chan].add(user)
update_file(self.name + '-' + chan)
await self.send(build("PRIVMSG", [chan, "Added to the match list."]))
game = " ".join(line.params[1].split(" ")[1:]).lower()
if game not in match_players[self.name + '-' + chan]:
match_players[self.name + '-' + chan][game] = set()
if user in match_players[self.name + '-' + chan][game]:
await self.send(build("PRIVMSG", [chan, "ERROR: player already in " + game + " list."]))
else:
match_players[self.name + '-' + chan][game].add(user)
update_file(self.name + '-' + chan)
await self.send(build("PRIVMSG", [chan, "Added " + user + " to the " + game + " match list."]))
elif line.params[1].startswith(".matchdel"):
user = line.source.split('!')[0]
if user not in match_players[self.name + '-' + chan]:
await self.send(build("PRIVMSG", [chan, "ERROR: player isn't in list."]))
if len(line.params[1].split(" ")) < 2:
await self.send(build("PRIVMSG", [chan, "ERROR: game not specified"]))
else:
match_players[self.name + '-' + chan].remove(user)
update_file(self.name + '-' + chan)
await self.send(build("PRIVMSG", [chan, "Removed from match list."]))
game = " ".join(line.params[1].split(" ")[1:]).lower()
if game not in match_players[self.name + '-' + chan]:
await self.send(build("PRIVMSG", [chan, "ERROR: no players in "+ game]))
else:
if user not in match_players[self.name + '-' + chan][game]:
await self.send(build("PRIVMSG", [chan, "ERROR: player isn't in list."]))
else:
match_players[self.name + '-' + chan][game].remove(user)
if len(match_players[self.name + '-' + chan][game]) == 0:
del match_players[self.name + '-' + chan][game]
update_file(self.name + '-' + chan)
await self.send(build("PRIVMSG", [chan, "Removed " + user + " from the " + game + " match list."]))
elif line.params[1].startswith(".help"):
await self.send(build("PRIVMSG", [chan, " .matchadd: Add to list; .matchdel: Remove from list; .matchmake: Ping everyone on list"]))
await self.send(build("PRIVMSG", [chan, " .matchadd game: Add to list; .matchdel game: Remove from list; .matchmake game: Ping everyone on list"]))
async def line_send(self, line: Line):
print(f"{self.name} > {line.format()}")
@ -102,9 +125,6 @@ async def main():
connections[section].update({config_connections[i]:config_connections[i+1]})
servers.append({'name':section, 'opts':{'server':server, 'nickname':nickname, 'channels':channels}})
for channel in channels:
match_players.update([(section + '-' + channel, set())])
# read_file()
bot = Bot()
for entry in servers:
@ -116,9 +136,12 @@ async def main():
params = ConnectionParams(botnick, server, 6697, True)
for channel in channels:
params.autojoin.append(channel)
print("Reading channel", channel)
read_file(name + '-' + channel)
print(match_players)
await bot.add_server(name, params)
print("Match players is: ", match_players)
await bot.run()
if __name__ == "__main__":