Exemple #1
0
def load_game(nick, against_nick, channel, moves):
    debug(f"Loading {nick}")
    game = botState.add_game(nick,
                             against_nick,
                             channel,
                             check_invitation=False)
    for m in moves:
        game.move(m)
Exemple #2
0
def delete_game(nick, against_nick, channel, game, nocheck=False):
    ongoing_games = load_ongoing_games()
    if nick in ongoing_games and channel in ongoing_games[
            nick] and against_nick in ongoing_games[nick][channel]:
        del ongoing_games[nick][channel][against_nick]
        save_ongoing_games(ongoing_games)
        debug("Removed ongoing game Data!!!!!")
        return True
    if nocheck:
        return
    delete_game(game.other(nick), nick, channel, game, nocheck=True)
Exemple #3
0
def update_game(nick, against_nick, channel, game, nocheck=False):
    ongoing_games = load_ongoing_games()
    debug(f"Searching for {nick=} {channel=} {ongoing_games=} ")
    if nick in ongoing_games and channel in ongoing_games[
            nick] and against_nick in ongoing_games[nick][channel]:
        ongoing_games[nick][channel][against_nick] = game.history
        save_ongoing_games(ongoing_games)
        debug("Updated ongoing game Data!!!!!")
        return True
    if nocheck:
        return
    update_game(game.other(nick), nick, channel, game, nocheck=True)
Exemple #4
0
def add_game(nick, against_nick, channel):
    ongoing_games = load_ongoing_games()
    if channel in ongoing_games and against_nick in ongoing_games[channel]:
        return
    if nick not in ongoing_games:
        ongoing_games[nick] = {}
    if channel not in ongoing_games:
        ongoing_games[nick][channel] = {}
    ongoing_games[nick][channel][against_nick] = []
    save_ongoing_games(ongoing_games)
    debug("Created ongoing game Data!!!!!")
    return True
Exemple #5
0
    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()
Exemple #6
0
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
Exemple #7
0
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())
Exemple #8
0
def save_ongoing_games(data):
    debug("Reading ongoing games json")
    with open(ONGOING_GAMES_STORE, 'w') as outfile:
        json.dump(data, outfile)