def index():
    redis.zincrby("counters", hostname)
    counters = redis.zrevrange("counters", 0, -1, withscores=True)
    counters = [ (s.decode(), int(i)) for (s,i) in counters ]
    thiscount = int(redis.zscore("counters", hostname))
    totalcount = sum(i for (s,i) in counters)
    return render_template( "index.html",
        hostname=hostname, counters=counters,
        thiscount=thiscount, totalcount=totalcount)
Пример #2
0
def article_vote(redis, user, article):
    #2018-04-11 01:23:59.118211 - 7 days, 0:00:00
    cutoff = datetime.datetime.now() - datetime.timedelta(
        seconds=ONE_WEEK_IN_SECONDS)
    # print(cutoff)
    #
    if not datetime.datetime.fromtimestamp(redis.zscore('time:',
                                                        article)) < cutoff:
        article_id = article.split(':')[-1]
        if redis.sadd('voted:' + article_id, user):
            redis.zincrby(name='score:', value=article, amount=VOTE_SCORE)
            redis.hincrby(name=article, key='votes', amount=1)
Пример #3
0
def article_switch_vote(redis, user, from_article, to_article):
    # HOMEWORK 2 Part I
    # allows user to switch vote from one article to the next by modifying appropriate data structures
    article_id1 = from_article.split(':')[-1]
    article_id2 = to_article.split(':')[-1]
    if (redis.smove('voted:' + article_id1, 'voted:' + article_id2,
                    user) == 1):
        redis.zincrby(name='score:', value=from_article, amount=-VOTE_SCORE)
        redis.hincrby(name=from_article, key='votes', amount=-1)
        redis.zincrby(name='score:', value=to_article, amount=VOTE_SCORE)
        redis.hincrby(name=to_article, key='votes', amount=1)

    pass
Пример #4
0
def article_switch_vote(redis, user, from_article, to_article):

    to_article_id = to_article.split(':')[-1]
    from_article_id = from_article.split(':')[-1]

    if redis.sismember(
            'voted:' + from_article_id,
            user) and redis.sismember('voted:' + to_article_id, user) is False:
        if redis.smove('voted:' + from_article_id, 'voted:' + to_article_id,
                       user):
            redis.zincrby(name='score:', value=to_article, amount=VOTE_SCORE)
            redis.hincrby(name=to_article, key='votes', amount=1)
            redis.zincrby(name='score:',
                          value=from_article,
                          amount=-VOTE_SCORE)
            redis.hincrby(name=from_article, key='votes', amount=-1)
Пример #5
0
    def _handle_message(self, msg):
        """
        Forwards messages from Redis to the players directly connected to this
        instance.
        """
        self._debug('RECEIVED - {}', extra=[str(msg)])
        if msg['type'] != 'message' or msg['channel'] != self.topic:
            return  # Ignore messages we don't need

        msg = json_loads(msg['data'])

        # If we have the artist, we're responsible for adjusting game state
        must_pass = False
        artist = self._get_artist()
        score = None
        guesser = None
        if self._has_artist(artist):
            if msg.verb == 'GUESSED':
                if msg.player_name == artist:
                    self._error('artist ({}) submitted a guess',
                                extra=[artist])
                else:
                    word = redis.get(self.word_key)
                    # TODO: Correct is only set for clients connected to this instance.
                    msg.correct = word.lower() == msg.word.lower()
                    if msg.correct:
                        guesser = msg.player_name
                        score = redis.zincrby(self.players_key,
                                              msg.player_name, 1)
                        word_won(word)
                        must_pass = True
            elif msg.verb == 'SKIPPED':
                if msg.player_name == artist:
                    self._error('artist ({}) voted to skip', extra=[artist])
                else:
                    voted = redis.scard(self.skip_key)
                    total = redis.zcard(self.players_key) - 1

                    if voted * 2 > total:
                        must_pass = True
            elif msg.verb == 'ENDED':
                if msg.player_name == artist:
                    must_pass = True
                # TODO: Player name will be sent on other instances
                del msg.player_name

        # Repeat the message to all players
        for p in self.players:
            if msg.verb == 'PASSED' and msg.player_name == p.name:
                # Add the word to the passed message for the correct player
                special = Message(msg)
                special.word = redis.get(self.word_key)
                gevent.spawn(p.send, special)
            else:
                gevent.spawn(p.send, msg)

        if must_pass:
            self._pass_turn(artist, guesser=guesser, score=score)
Пример #6
0
    def _handle_message(self, msg):
        """
        Forwards messages from Redis to the players directly connected to this
        instance.
        """
        self._debug('RECEIVED - {}', extra=[str(msg)])
        if msg['type'] != 'message' or msg['channel'] != self.topic:
            return                              # Ignore messages we don't need

        msg = json_loads(msg['data'])

        # If we have the artist, we're responsible for adjusting game state
        must_pass = False
        artist = self._get_artist()
        score = None
        guesser = None
        if self._has_artist(artist):
            if msg.verb == 'GUESSED':
                if msg.player_name == artist:
                    self._error('artist ({}) submitted a guess', extra=[artist])
                else:
                    word = redis.get(self.word_key)
                    # TODO: Correct is only set for clients connected to this instance.
                    msg.correct = word.lower() == msg.word.lower()
                    if msg.correct:
                        guesser = msg.player_name
                        score = redis.zincrby(self.players_key, msg.player_name, 1)
                        word_won(word)
                        must_pass = True
            elif msg.verb == 'SKIPPED':
                if msg.player_name == artist:
                    self._error('artist ({}) voted to skip', extra=[artist])
                else:
                    voted = redis.scard(self.skip_key)
                    total = redis.zcard(self.players_key) - 1

                    if voted * 2 > total:
                        must_pass = True
            elif msg.verb == 'ENDED':
                if msg.player_name == artist:
                    must_pass = True
                # TODO: Player name will be sent on other instances
                del msg.player_name

        # Repeat the message to all players
        for p in self.players:
            if msg.verb == 'PASSED' and msg.player_name == p.name:
                # Add the word to the passed message for the correct player
                special = Message(msg)
                special.word = redis.get(self.word_key)
                gevent.spawn(p.send, special)
            else:
                gevent.spawn(p.send, msg)

        if must_pass:
            self._pass_turn(artist, guesser=guesser, score=score)
Пример #7
0
def article_switch_vote(redis, user, from_article, to_article):
    cutoff = datetime.datetime.now() - datetime.timedelta(
        seconds=ONE_WEEK_IN_SECONDS)
    to_ = (not datetime.datetime.fromtimestamp(
        redis.zscore('time:', to_article)) < cutoff)
    from_ = (not datetime.datetime.fromtimestamp(
        redis.zscore('time:', from_article)) < cutoff)
    #if both articles are within the cutoff
    if to_ and from_:
        vrem = 'voted:' + from_article.split(':')[-1]
        vadd = 'voted:' + to_article.split(':')[-1]
        #verify user voting status on articles
        if redis.sismember(vrem, user) and not redis.sismember(vadd, user):
            #remove vote from from_article
            redis.srem(vrem, user)
            redis.zincrby(name='score:',
                          value=from_article,
                          amount=-1 * VOTE_SCORE)
            redis.hincrby(name=from_article, key='votes', amount=-1)
            #add vote to to_article
            redis.sadd(vadd, user)
            redis.zincrby(name='score:', value=to_article, amount=VOTE_SCORE)
            redis.hincrby(name=to_article, key='votes', amount=1)
Пример #8
0
def die(user, point):
    redis.zincrby("leaderboard", user, int(point))
    return user + " got " + point + " new point"
Пример #9
0
 def product_bought(self, products):
     product_ids = [product.id for product in products]
     for product_id in product_ids:
         for with_id in product_ids:
             if product_id != with_id:
                 redis.zincrby(self.get_product_key(product_id), value=with_id, amount=1)