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