Exemplo n.º 1
0
class GroupSettings(RBase):
    id = TextField(primary_key=True)
    welcome_text = TextField(default=welcome_text)
    invite_text = TextField(default=invite_text)
    group_patterns = JSONField(default=group_patterns)
    creators = ListField()
    mp_forward = JSONField(default=[])
    kick_quorum_n = IntegerField(default=5)
    kick_period = IntegerField(default=5)
    kick_text = TextField(default=kick_text)
Exemplo n.º 2
0
class Lobby(Model):
    __database__ = db
    name = TextField(primary_key=True)
    timestamp = IntegerField()
    mode = TextField()
    password = TextField()

    game = IntegerField(index=True)
    setup = IntegerField()
    owner = IntegerField()
Exemplo n.º 3
0
class User(Model):
    __database__ = db
    user_id = TextField(primary_key=True)
    nome = TextField(index=True, default=u"__NONE__")
    escola = TextField(index=True, default=u"__NONE__")
    genero = TextField(index=True, default=u"__NONE__")
    serie = IntegerField(index=True, default=0)
    idade = IntegerField(index=True, default=0)
    score = ListField(as_json=True)
    data = DateTimeField(default=datetime.datetime(1, 2, 1, 0, 0, 0, 0))
Exemplo n.º 4
0
class Player(walrus.Model):
    database = db
    name = TextField(primary_key=True)
    bank_roll = IntegerField(default=100)
    extra_bet = IntegerField(default=0)

    def __repr__(self):
        return u"%s: Bank$: %d" % (self.name, self.bank_roll)

    def win(self, bet):
        self.bank_roll += bet

    def lose(self, bet):
        self.bank_roll -= bet
Exemplo n.º 5
0
class File(Model):
    """User file object."""

    __database__ = BaseCache.model_db

    created_at = DateTimeField()

    file_id = TextField(primary_key=True, index=True)
    user_id = TextField(index=True)

    content_type = TextField()
    file_name = TextField()

    file_size = IntegerField()

    relative_path = TextField()
    is_archive = BooleanField()
    is_dir = BooleanField()
    unpack_archive = BooleanField()

    @property
    def abs_path(self):
        """Full path of cached file."""
        return CACHE_UPLOADS_PATH / self.user_id / self.relative_path

    def valid_file(self):
        """Ensure a file exists."""
        if self.abs_path.exists():
            self.is_dir = self.abs_path.is_dir()
            return True

        return False
Exemplo n.º 6
0
class Player(Model):
    __database__ = db
    name = TextField(
        primary_key=True)  ## combination of game, lobbyname and playername
    lobby = TextField(index=True)
    user = IntegerField()

    data = JSONField()
Exemplo n.º 7
0
class Score(Model):
    __database__ = db
    casa = TextField(index=True)
    carta = TextField(index=True)
    ponto = TextField(index=True)
    move = TextField(index=True)
    valor = IntegerField(index=True)
    data = DateTimeField()
Exemplo n.º 8
0
class Project(Model):
    """User project object."""

    __database__ = BaseCache.model_db

    created_at = DateTimeField()

    project_id = TextField(primary_key=True, index=True)
    user_id = TextField(index=True)

    clone_depth = IntegerField()
    git_url = TextField()

    name = TextField()
    fullname = TextField()
    email = TextField()
    owner = TextField()
    token = TextField()

    @property
    def abs_path(self):
        """Full path of cached project."""
        return CACHE_PROJECTS_PATH / self.user_id / self.owner / self.name

    def exists(self):
        """Ensure a project exists on file system."""
        return self.abs_path.exists()

    def ttl_expired(self, ttl=None):
        """Check if project time to live has expired."""
        if not self.created_at:
            # If record does not contain created_at,
            # it means its an old record, and
            # we should mark it for deletion.
            return True

        ttl = ttl or int(os.getenv('RENKU_SVC_CLEANUP_TTL_PROJECTS', 1800))

        created_at = (self.created_at -
                      datetime.utcfromtimestamp(0)).total_seconds() * 1e+3

        age = ((time.time() * 1e+3) - created_at) / 1e+3
        return self.exists() and age >= ttl

    def purge(self):
        """Removes project from file system and cache."""
        shutil.rmtree(str(self.abs_path))
        self.delete()

    def is_locked(self, jobs):
        """Check if file locked by given jobs."""
        return bool(
            next((job for job in jobs if self.project_id in job.locked),
                 False))
Exemplo n.º 9
0
class AutoBotSubmission(AutoBotBaseModel):
    submission_id = TextField(primary_key=True)
    author = TextField(index=True)
    submission_time = IntegerField()
    is_series = BooleanField()
    sent_series_pm = BooleanField()
    deleted = BooleanField()

    @classmethod
    def set_ttl(cls, submission, ttl=86400):
        submission.to_hash().expire(ttl=ttl)

    def set_index_ttls(self, ttl=86400):
        '''Kind of a hacky way to get index keys to expire since they
        are normally created without any TTL whatsoever.'''
        for mi in self._indexes:
            for index in mi.get_indexes():
                key = index.get_key(index.field_value(self)).key
                self.database.expire(key, ttl)
Exemplo n.º 10
0
class Project(Model):
    """User project object."""

    __database__ = BaseCache.model_db

    created_at = DateTimeField()

    project_id = TextField(primary_key=True, index=True)
    user_id = TextField(index=True)

    clone_depth = IntegerField()
    git_url = TextField()

    name = TextField()
    fullname = TextField()
    email = TextField()
    owner = TextField()
    token = TextField()

    @property
    def abs_path(self):
        """Full path of cached project."""
        return CACHE_PROJECTS_PATH / self.user_id / self.owner / self.name
Exemplo n.º 11
0
class UploadCache(Model):
    __database__ = redis_db

    _id = 0
    id = IntegerField(primary_key=True)
    cache = ListField()
    sequence_number = IntegerField(default=0)
    max_sequence_number = IntegerField(default=0)
    # store upload file information
    size = IntegerField(default=0)
    path = TextField(default='/')
    md5 = TextField()
    upload_date = DateTimeField()
    file_name = TextField()
    filetype = IntegerField()
    user_id = IntegerField()
    # store saving task information
    saving_task_start = BooleanField(default=False)

    @classmethod
    def generate_id(cls):
        rv = cls._id
        cls._id += 1
        return rv
Exemplo n.º 12
0
class Game(walrus.Model):
    database = db
    id = IntegerField(primary_key=True, default=1)
    players = ListField()
    minimum_bet = IntegerField(default=10)
    game_point = IntegerField(default=0)
    rolling_position = IntegerField(default=1)
    rolls = IntegerField(default=0)
    player_passes = SetField()
    player_do_not_passes = SetField()

    def __str__(self):
        players_str = "*players:* " + ",".join(self.players)
        come_in_str = ""
        if self.rolls < 1:
            come_in_str = "*_COME_IN_*"
        if self.rolls >= 1:
            come_in_str += "\n*game point:* " + str(self.game_point)
        roller_str = "*roller:* " + self.roller()
        player_passes_str = "*pass bets:* " + ",".join(self.player_passes)
        player_do_not_passes_str = "*do not pass bets:* " + ",".join(
            self.player_do_not_passes)

        ret = [
            players_str, come_in_str, roller_str, player_passes_str,
            player_do_not_passes_str
        ]
        return "\n".join(ret)

    def roller(self):
        return self.players[0]

    def add_player(self, player_name):
        if player_name in self.players:
            return "%s already in the game" % player_name
        try:
            player = Player.load(player_name)
        except KeyError:
            player = Player(name=player_name, extra_bet=0)
            player.save()

        self.players.append(player_name)
        return "%s added to game" % player_name

    def remove_player(self, player_name):
        def redis_pop_element(redis_list, element):
            new_list = list()
            for i in range(len(redis_list)):
                x = redis_list.popleft()
                if x != element:
                    new_list.append(x)

            for i in new_list:
                redis_list.append(i)

        if player_name not in self.players:
            return "%s was not in the game" % player_name

        if player_name == self.roller() and self.game_point is not None \
            and self.game_point > 0:
            return "%s cannot leave the game while an active roller" % player_name

        self.player_passes.remove(player_name)
        self.player_do_not_passes.remove(player_name)
        redis_pop_element(self.players, player_name)

        # remove the Player's extra bets
        player = Player.load(player_name)
        player.extra_bet = 0
        player.save()

        if self.roller() == player_name:
            self.reset_game(new_roller=True, clear_bets=False)
        return "%s removed from the game" % player_name

    def player_choice(self, player_name, choice, extra_bet=None):
        add_msg = None

        if player_name not in self.players:
            return "@{pn} please join the game to make a bet".format(
                pn=player_name)

        if extra_bet:
            player = Player.load(player_name)
            player_current_bets = self.minimum_bet + player.extra_bet + extra_bet
            if player_current_bets > player.bank_roll:
                return "{pn} cannot bet {x}, {pn} only has {bank}".format(
                    pn=player_name, x=extra_bet, bank=player.bank_roll)
            else:
                player.extra_bet += extra_bet
                player.save()
                add_msg = "{pn} added {x} to their bet".format(pn=player_name,
                                                               x=extra_bet)

        def _player_choice(player_name, choice, add_msg=None):
            already_made_that_bet = "{pn} already made a {bet} bet"
            first_bet = "{pn} made the first {bet} bet"
            choices = {
                "!p": "*pass*",
                "!d": "*do not pass*",
            }
            bet = choices[choice]
            if bet == "*pass*":
                bet_in = self.player_passes
                cannot_bet_in = self.player_do_not_passes
            if bet == "*do not pass*":
                bet_in = self.player_do_not_passes
                cannot_bet_in = self.player_passes

            if player_name not in cannot_bet_in:

                if player_name in bet_in and not extra_bet:
                    return already_made_that_bet.format(pn=player_name,
                                                        bet=bet)

                if len(bet_in) == 0:
                    msg = first_bet.format(pn=player_name, bet=bet)
                else:
                    other_bets = [b for b in list(bet_in) if b != player_name]
                    if len(other_bets) == 0:
                        msg = ""
                    elif len(other_bets) == 1:
                        msg = "{pn} joined {m} in making a {bet} bet".format(
                            pn=player_name, m=other_bets[0], bet=bet)
                    else:
                        msg = "{pn} joined ({m}) in making a {bet} bet".format(
                            pn=player_name, m=",".join(other_bets), bet=bet)
                if bet == "*pass*":
                    self.player_passes.add(player_name)
                if bet == "*do not pass*":
                    self.player_do_not_passes.add(player_name)

                if add_msg and msg:
                    msg += "\n" + "and " + add_msg
                if add_msg and not msg:
                    msg = add_msg
                return msg

            else:
                return "{pn} already chose {bet}".format(pn=player_name,
                                                         bet=bet)

        return _player_choice(player_name, choice, add_msg=add_msg)

    def reset_game(self, new_roller=False, clear_bets=True):
        print "RESETTING GAME"
        self.game_point = 0
        self.rolls = 0

        # reset all players
        if clear_bets:
            for p in self.players:
                player = Player.load(p)
                player.extra_bet = 0
                print "__PLAYER__"
                print player
                player.save()

                if p in self.player_passes:
                    self.player_passes.remove(p)
                if p in self.player_do_not_passes:
                    self.player_do_not_passes.remove(p)

        if new_roller:
            last_roller = self.players.popleft()

            print "last roller: %s" % last_roller
            print "players left: %s" % self.players

            # add last roller to end of the list
            self.players.append(last_roller)
            print "players now: %s" % self.players
            new_roller = self.players[0]

            msg = u"{u} is the new roller\n{u} type `!roll` when ready to start a new round.".format(
                u=new_roller)
            return msg

        return "new round"

    def crapped(self):
        crap_message = u"\n {}".format(OUTCOMES["CRAPPED"])
        crap_message += u"\n"
        crap_message += self.reset_game(new_roller=True)
        return crap_message

    def rolled_point(self):
        point_message = u"\n {}".format(OUTCOMES["WIN"])
        point_message += u"\n"
        point_message += self.reset_game(new_roller=False)
        return point_message

    #def player_check(self):
    #    if self.player.bank_roll < self.minimum_bet:
    #        print "Sorry, but you lost all of your monies"
    #        sys.exit(5)

    def winners_losers(self, true_pass=True):
        full_reply = u"\n"

        for player_name in self.players:
            player_name = str(player_name)
            player = Player.load(player_name)
            ret = None
            reply = str(player) + "{operation}" + str(self.minimum_bet)

            if true_pass:
                if player.name in self.player_passes:
                    reply = reply.format(operation="+")
                    if player.extra_bet > 0:
                        player.win(self.minimum_bet + player.extra_bet)
                        ret = reply + " +" + str(player.extra_bet)
                    else:
                        player.win(self.minimum_bet)
                        ret = reply

                if player.name in self.player_do_not_passes:
                    reply = reply.format(operation="-")
                    if player.extra_bet > 0:
                        player.lose(self.minimum_bet + player.extra_bet)
                        ret = reply + " -" + str(player.extra_bet)
                    else:
                        player.lose(self.minimum_bet)
                        ret = reply

            if true_pass == False:
                if player.name in self.player_do_not_passes:
                    reply = reply.format(operation="+")
                    if player.extra_bet > 0:
                        player.win(self.minimum_bet + player.extra_bet)
                        ret = reply + " +" + str(player.extra_bet)
                    else:
                        player.win(self.minimum_bet)
                        ret = reply

                if player.name in self.player_passes:
                    reply = reply.format(operation="-")
                    if player.extra_bet > 0:
                        player.lose(self.minimum_bet + player.extra_bet)
                        ret = reply + " -" + str(player.extra_bet)
                    else:
                        player.lose(self.minimum_bet)
                        ret = reply

            # round if over set any extra bets to 0
            player.extra_bet = 0
            player.save()
            if ret:
                full_reply += ret + "\n"

        return full_reply

    def first_roll(self, debug_roll=None):
        reply = u""

        if self.rolls == 0:
            if not debug_roll:
                roll = Roll()
            else:
                roll = debug_roll
            self.rolls += 1
            reply += str(roll).decode("utf8")

            if roll.point in PASS:
                reply += self.winners_losers()
                reply += self.reset_game()

            elif roll.point in DO_NOT_PASS:
                reply += self.winners_losers(true_pass=False)
                reply += self.reset_game(new_roller=True)

            else:
                self.game_point = int(roll.point)
                reply += u"\nPOINT SET"

            self.save()
            return reply
        else:
            raise Exception("not first roll")

    def keep_goin(self, debug_roll=None):
        reply = u""
        if not debug_roll:
            roll = Roll()
        else:
            roll = debug_roll
        self.rolls += 1
        reply += str(roll).decode("utf8")

        reply += u"\nROLLING FOR %s" % NUMBERS[self.game_point]

        if roll.point == 7:
            reply += self.winners_losers(true_pass=False)
            reply += self.crapped()
            return reply

        elif roll.point == self.game_point:
            reply += self.winners_losers()
            reply += self.rolled_point()
            return reply
        else:
            return reply
Exemplo n.º 13
0
class File(Model):
    """User file object."""

    __database__ = BaseCache.model_db

    created_at = DateTimeField()

    file_id = TextField(primary_key=True, index=True)
    user_id = TextField(index=True)

    content_type = TextField()
    file_name = TextField()

    file_size = IntegerField()

    relative_path = TextField()
    is_archive = BooleanField()
    is_dir = BooleanField()
    unpack_archive = BooleanField()

    @property
    def abs_path(self):
        """Full path of cached file."""
        return CACHE_UPLOADS_PATH / self.user_id / self.relative_path

    def exists(self):
        """Ensure a file exists on file system."""
        if self.abs_path.exists():
            self.is_dir = self.abs_path.is_dir()
            return True

        return False

    def ttl_expired(self, ttl=None):
        """Check if file time to live has expired."""
        if not self.created_at:
            # If record does not contain created_at,
            # it means its an old record, and
            # we should mark it for deletion.
            return True

        ttl = ttl or int(os.getenv("RENKU_SVC_CLEANUP_TTL_FILES", 1800))

        created_at = (self.created_at -
                      datetime.utcfromtimestamp(0)).total_seconds() * 1e3

        age = ((time.time() * 1e3) - created_at) / 1e3
        return self.exists() and age >= ttl

    def purge(self):
        """Removes file from file system and cache."""
        if self.abs_path.is_file():
            self.abs_path.unlink()

        if self.abs_path.is_dir():
            shutil.rmtree(str(self.abs_path))

        self.delete()

    def is_locked(self, jobs):
        """Check if file locked by given jobs."""
        return bool(
            next((job for job in jobs if self.file_id in job.locked), False))
Exemplo n.º 14
0
class CountdownTracker(Model):
    __database__ = db
    id = TextField(primary_key=True)
    seconds = IntegerField()
    desc = TextField()
    created_at = DateTimeField(default=datetime.now)