async def onRun(bot: IrcBot): log("Loading ongoing games...") load_games() log("Done loading ongoing games!") if isinstance(bot.channels, list): for channel in bot.channels: await bot.send_message( Color("CHESS BOT INITIALIZED", Color.light_green, Color.black).str, channel, ) else: await bot.send_message( Color("CHESS BOT INITIALIZED", Color.light_green, Color.black).str) while True: for nick in botState.invites: for channel in botState.invites[nick]: for against_nick in deepcopy(botState.invites[nick][channel]): now = datetime.now().timestamp() if (now - botState.invites[nick][channel][against_nick] > EXPIRE_INVITE_IN): botState.invites[nick][channel].pop(against_nick) await bot.send_message( f"<{against_nick}> Your game request to {nick} has expired!", channel, ) await bot.sleep(0.5)
def download(self, filepath, progress_callback=None, buffsize=BUFFSIZE): """Performs the download of the offered file. This will be performed blocking, use bot.dcc_get() otherwise. :param filepath: String absolute file path :type str: :param progress_callback: Optinal function to call as each chunk of the file is received, the progress callback: pcb(total_received: int, progress: float) ---> total_received is the number of bytes received so far """ if not self.is_passive: with socket.socket() as e: s.connect((self.ip, self.port)) with open(filepath, "wb") as f: s_list = get_chunk_sizes_list(self.size, buffsize) for i, bsize in enumerate(s_list): bytes_read = s.recv(bsize) if not bytes_read: break f.write(bytes_read) self.download_progress = i / len(s_list) if callable(progress_callback): progress_callback(i * buffsize, self.download_progress) self.download_progress = 1 if callable(progress_callback): progress_callback(i * buffsize, self.download_progress) return log("PASSIVE BLOCKING DCC SEND DOWNLOAD!!!") with socket.socket() as s: s.bind((self.ip, self.port)) s.listen(1) debug(f"Listening on to: {self.ip} {self.port}") c, a = s.accept() with open(filepath, "wb") as f: s_list = get_chunk_sizes_list(self.size, buffsize) for i, bsize in enumerate(s_list): bytes_read = c.recv(bsize) if not bytes_read: break f.write(bytes_read) self.download_progress = i / len(s_list) if callable(progress_callback): progress_callback(i * buffsize, self.download_progress) c.shutdown(2) c.close()
def load_ongoing_games(): debug("Trying to read ongoing games json") data = {} try: with open(ONGOING_GAMES_STORE) as json_file: data = json.load(json_file) except FileNotFoundError: debug("Read failed. Creating new file json") save_ongoing_games({}) return load_ongoing_games() except json.decoder.JSONDecodeError: log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") log("Recreating json store. Reason: File is corrupt") data = {} with open(ONGOING_GAMES_STORE, 'w') as outfile: json.dump(data, outfile) return data
def increment_data(nick, column): data = get_data(nick) if data is None: data = { "nick": nick, "checkmates": 0, "stalemates": 0, "draws": 0, "games": 0, "losses": 0, "prefs": json.dumps(DEFAULT_PREF), "ongoing_games": "{}", # {channel: against_nick: [b2b4,c3c6..]} } data.update({column: 1}) log("Creating player data") db_handler.push(data) return data else: log("incrementing player data") data.update({column: data[column] + 1}) db_handler.update(data["id"], data)
class Config: nick: str display_progress: bool = True quota: int = 100 # MB def asdict(self) -> dict: return asdict(self) @classmethod def _get_data_by_nick(cls, nick): return configs.db.getByKeyWithId(ConfigOptions.nick.name, nick) def save(self): if user := self._get_data_by_nick(self.nick): configs.update(user["id"], self.asdict()) return log(f"Creating new config for user: {self.nick}") debug(f"{self.asdict()=}") configs.push(self.asdict())
async def on_run(bot: IrcBot): log("STARTED!")
def on_dcc_reject(**m): log(f"Rejected!!! {m=}")
async def not_found(bot: IrcBot, m, msg): log(f"{msg.text=}") return reply( msg, f'{red("Unknown command or invalid syntax")}. Try "/msg {NICK} help" for help', )