コード例 #1
0
 def update_elo(self, p_a, p_b, score_a, score_b):
     Ra = redis.zscore('elo', p_a) or START_ELO
     Rb = redis.zscore('elo', p_b) or START_ELO
     Ea = 1.0 / (1 + 10.0**((Rb - Ra) / 400.0))
     Eb = 1.0 / (1 + 10.0**((Ra - Rb) / 400.0))
     Ra_new = Ra + 10 * (score_a - Ea)
     Rb_new = Rb + 10 * (score_b - Eb)
     redis.zadd('elo', {p_a: Ra_new})
     redis.zadd('elo', {p_b: Rb_new})
     return Ra_new, Rb_new, Ra_new - Ra
コード例 #2
0
def article_vote(redis, user, article):
    cutoff = datetime.datetime.now() - datetime.timedelta(seconds=ONE_WEEK_IN_SECONDS)

    if not datetime.datetime.fromtimestamp(redis.zscore('time:', article)) < cutoff:
        article_id = article.split(':')[-1]
        if redis.sadd('voted:' + artical_id, user):
            redis.zincrby('score:', VOTE_SCORE, article)
            reids.hincrby(article, 'votes', 1)
コード例 #3
0
ファイル: homework_2.py プロジェクト: Lalksy/nosql-databases
def article_vote(redis, user, article):
    cutoff = datetime.datetime.now() - datetime.timedelta(seconds=ONE_WEEK_IN_SECONDS)

    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)
コード例 #4
0
def index():
    redis.zincrby("counters", 1, 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)
コード例 #5
0
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)
コード例 #6
0
ファイル: homework_2.py プロジェクト: feelpnw/nosql-databases
def article_switch_vote(redis, user, from_article, to_article):
    # HOMEWORK 2 Part I
    from_article_id = from_article.split(':')[-1]
    to_article_id = to_article.split(':')[-1]

    redis.srem('voted:' + from_article_id, user)
    redis.sadd('voted:' + to_article_id, user)
    # or redis.smove('voted:' + from_article_id, 'voted:' + to_article_id, user)

    from_article_score = redis.zscore('score:', 'article:' + from_article_id)
    changed_from_article_score = from_article_score - 432
    to_article_score = redis.zscore('score:', 'article:' + to_article_id)
    changed_to_article_score = to_article_score + 432

    redis.zrem('score:', 'article:' + from_article_id)
    redis.zadd('score:', changed_from_article_score,
               'article:' + from_article_id)
    redis.zrem('score:', 'article:' + to_article_id)
    redis.zadd('score:', changed_to_article_score, 'article:' + to_article_id)
コード例 #7
0
ファイル: sketch.py プロジェクト: tecywiz121/sketchwithus
    def join(self, player):
        if player.table == self:
            return  # Already part of this table.

        if player.table is not None:
            player.table.leave(player)  # Player has to leave old table

        msg = Message('JOINED')  # Tell all the other players
        msg.player_name = player.name  # that a new player has joined.
        self.send(msg)

        player.table = self  # Register the new player with
        self.players.append(player)  # this table.

        # Get a list of all the other players on this table
        others = redis.zrange(self.players_key, 0, -1)

        # Check if the player exists (race condition I guess)
        score = redis.zscore(self.players_key, player.name)
        if score is None:
            # Add new player to the player list
            redis.zadd(self.players_key, player.name, 0)

            # Add player to the turn list if he/she wasn't already there
            redis.zadd(self.turns_key, player.name, time.time())

        # Prepare joined messages for all existing players
        msgs = []
        for other in others:
            if other == player.name:
                continue
            msg = Message('JOINED')
            msg.player_name = other
            msgs.append(msg)

        # Prepare passed message to set correct turn
        current = self._get_artist()
        msg = Message('PASSED', player_name=current)
        if player.name == current:
            msg.word = redis.get(self.word_key)

            end_time = time.time() + 120
            if not redis.setnx(self.end_key, end_time):
                # Clock's already started!
                end_time = redis.get(self.end_key)
        else:
            end_time = redis.get(self.end_key)
            assert (end_time is not None)
        msg.end_time = end_time
        msgs.append(msg)

        # Send all the prepared messages
        gevent.joinall([gevent.spawn(player.send, x) for x in msgs])
コード例 #8
0
ファイル: sketch.py プロジェクト: tecywiz121/sketchwithus
    def join(self, player):
        if player.table == self:
            return                              # Already part of this table.

        if player.table is not None:
            player.table.leave(player)          # Player has to leave old table

        msg = Message('JOINED')                 # Tell all the other players
        msg.player_name = player.name           # that a new player has joined.
        self.send(msg)

        player.table = self                     # Register the new player with
        self.players.append(player)             # this table.

        # Get a list of all the other players on this table
        others = redis.zrange(self.players_key, 0, -1)

        # Check if the player exists (race condition I guess)
        score = redis.zscore(self.players_key, player.name)
        if score is None:
            # Add new player to the player list
            redis.zadd(self.players_key, player.name, 0)

            # Add player to the turn list if he/she wasn't already there
            redis.zadd(self.turns_key, player.name, time.time())

        # Prepare joined messages for all existing players
        msgs = []
        for other in others:
            if other == player.name:
                continue
            msg = Message('JOINED')
            msg.player_name = other
            msgs.append(msg)

        # Prepare passed message to set correct turn
        current = self._get_artist()
        msg = Message('PASSED', player_name=current)
        if player.name == current:
            msg.word = redis.get(self.word_key)

            end_time = time.time() + 120
            if not redis.setnx(self.end_key, end_time):
                # Clock's already started!
                end_time = redis.get(self.end_key)
        else:
            end_time = redis.get(self.end_key)
            assert(end_time is not None)
        msg.end_time = end_time
        msgs.append(msg)

        # Send all the prepared messages
        gevent.joinall([gevent.spawn(player.send, x) for x in msgs])
コード例 #9
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)
コード例 #10
0
ファイル: __init__.py プロジェクト: fanout/pysmartfeed
	def _get_spec_parts(self, redis, key_index, spec):
		if spec.type == 'id':
			return (int(redis.zscore(key_index, spec.value)), None, None)
		elif spec.type == 'time':
			return (calendar.timegm(datetime.strptime(spec.value, '%Y-%m-%dT%H:%M:%S').utctimetuple()), None, None)
		else: # cursor
			if not spec.value:
				return (0, None, None)
			try:
				ts, offset, cs = spec.value.split('_')
				ts = int(ts)
				offset = int(offset)
			except:
				raise ValueError('bad cursor format')
			return (ts, offset, cs)
コード例 #11
0
 def get_score(self, player):
     return redis.zscore('elo', player) or START_ELO