def test_purchase_multiple_sufficient(self): nyan = stickers.details_for(STORE_ITEM) user = create_user() user.redis.user_kv.hset('sticker:7:count', 1000) result = self.api_post('/api/store/buy', {'item_type': 'sticker', 'item_id': nyan.type_id, 'quantity': '2'}, user=user) self.assertEqual(result, {'success': True, 'new_balance': 1000-(2*nyan.cost) })
def _log_key(comment, s=1, r=1): details = comment.details() sticker_score = sum(stickers.details_for(k).value * v for k, v in details.sticker_counts.items()) # sticker_core is roughly -5 to 100 reply_count = details.reply_count # reply_count is roughly 0 to 100 (in 24 hours) score = sticker_score *s + reply_count * r """ boost is defined as: post with score 1000 should be above a stickerless post two days later >>> (2 * 24 * 60 * 60) / math.log(1000, 2) 17339.327750245317 """ C = 17339.327750245317 # not actually the speed of light if score: sign = score / abs(score) boost = sign * math.log(abs(score), 2) * C else: boost = 0 rank = details.timestamp + boost return -rank
def handle(self, start, *args, **options): start = int(start) comments = Comment.objects.filter(id__gte=start).exclude(author=None) print "Remaining", len(comments) for comment in comments: if comment.id % 100 == 0: print "Updating", comment.id details = comment.details() sticker_scores = [ stickers.details_for(k).value * v for k, v in details['sticker_counts'].items() ] top_score = sum(sticker_scores) visibility = details.get('visibility') if visibility in Visibility.invisible_choices: top_score = -1 # Some old comments have no author personal_top = (comment.author.redis.top_anonymous_posts if comment.anonymous else comment.author.redis.top_posts) personal_top.bump(comment.id, top_score)
def purchase_stickers(user, type_id, quantity): """ Execute the actual purchase. type_id: A Sticker or a sticker type id. """ sticker = stickers.details_for(type_id) cost = sticker.cost if not cost: raise InvalidPurchase('Invalid item_id.') cost *= quantity # Is this a limited inventory sticker? if sticker.is_limited_inventory(): if user.kv.stickers.did_purchase(sticker): raise InvalidPurchase("You've already bought this limited availability sticker.") # Sticker ID of the #1 sticker. result = user.kv.stickers.currency.increment_ifsufficient(-cost) #TODO This is nasty, increment_ifsufficient should raise an exception instead of returning status code. if result['success']: user.kv.stickers.get(type_id).increment(quantity) else: raise InvalidPurchase('Insufficient balance.') if sticker.is_limited_inventory: user.kv.stickers.mark_sticker_purchased(sticker) sticker.decrement_inventory() realtime_update_sticker_counts(user) return result['remaining']
def test_purchase_multiple_insufficient(self): nyan = stickers.details_for(STORE_ITEM) user = create_user() user.redis.user_kv.hset('sticker:7:count', 100) result = self.api_post('/api/store/buy', {'item_type': 'sticker', 'item_id': nyan.type_id, 'quantity': '1000'}, user=user) self.assertEqual(result, {'success': False, 'reason': 'Insufficient balance.'})
def test_purchase_insufficientbalance(self): nyan = stickers.details_for(STORE_ITEM) result = self.api_post('/api/store/buy', { 'item_type': 'sticker', 'item_id': nyan.type_id }) self.assertEqual(result, { 'success': False, 'reason': 'Insufficient balance.' })
def test_purchase_inventory_empty(self): nyan = stickers.details_for(STORE_ITEM) user = create_user() self.assertEqual(stickers.get_inventory(user), []) user.kv.stickers[7].set(100) result = self.api_post('/api/store/buy', {'item_type': 'sticker', 'item_id': nyan.type_id}, user=user) user.kv.update() user_inventory = stickers.get_inventory(user) self.assertEqual(user_inventory, [nyan])
def sorted_sticker_counts(self): counts = dict([(stickers.details_for(type_id), count) for type_id, count in self._d['sticker_counts'].items() ]) sorted_counts = stickers.sorted_counts(counts) count_json = [{ 'type_id': sticker.type_id, 'count': count, 'name': sticker.name, } for sticker, count in sorted_counts] return count_json
def credit_received_sticker(user, type_id): sticker = stickers.details_for(type_id) # Stars get their own handling inside the stars app. if not sticker.is_star(): cost = sticker.cost or 1 # temp andrew wk stuff if type_id == 111: cost = 50 user.kv.stickers_received.increment(cost) user.kv.sticker_inbox.increment(cost)
def sorted_sticker_counts(self): counts = dict([(stickers.details_for(type_id), count) for type_id, count in self._d['sticker_counts'].items()]) sorted_counts = stickers.sorted_counts(counts) count_json = [ { 'type_id': sticker.type_id, 'count': count, 'name': sticker.name, } for sticker, count in sorted_counts ] return count_json
def test_purchase_sufficientbalance(self): nyan = stickers.details_for(STORE_ITEM) user = create_user() user.redis.user_kv.hset('sticker:7:count', 100) result = self.api_post('/api/store/buy', { 'item_type': 'sticker', 'item_id': nyan.type_id }, user=user) self.assertEqual(result, { 'success': True, 'new_balance': 100 - nyan.cost })
def test_purchase_multiple_insufficient(self): nyan = stickers.details_for(STORE_ITEM) user = create_user() user.redis.user_kv.hset('sticker:7:count', 100) result = self.api_post('/api/store/buy', { 'item_type': 'sticker', 'item_id': nyan.type_id, 'quantity': '1000' }, user=user) self.assertEqual(result, { 'success': False, 'reason': 'Insufficient balance.' })
def test_purchase_inventory_empty(self): nyan = stickers.details_for(STORE_ITEM) user = create_user() self.assertEqual(stickers.get_inventory(user), []) user.kv.stickers[7].set(100) result = self.api_post('/api/store/buy', { 'item_type': 'sticker', 'item_id': nyan.type_id }, user=user) user.kv.update() user_inventory = stickers.get_inventory(user) self.assertEqual(user_inventory, [nyan])
def handle(self, start, *args, **options): start = int(start) comments = Comment.objects.filter(id__gte=start).exclude(author=None) print "Remaining", len(comments) for comment in comments: if comment.id % 100 == 0: print "Updating", comment.id details = comment.details() sticker_scores = [stickers.details_for(k).value * v for k, v in details['sticker_counts'].items()] top_score = sum(sticker_scores) visibility = details.get('visibility') if visibility in Visibility.invisible_choices: top_score = -1 # Some old comments have no author personal_top = (comment.author.redis.top_anonymous_posts if comment.anonymous else comment.author.redis.top_posts) personal_top.bump(comment.id, top_score)
def test_purchase_insufficientbalance(self): nyan = stickers.details_for(STORE_ITEM) result = self.api_post('/api/store/buy', {'item_type': 'sticker', 'item_id': nyan.type_id}) self.assertEqual(result, {'success': False, 'reason': 'Insufficient balance.'})