def rate(self, words, coef=None): ratings = [{word: self.ratings[word]} for word in words if self.ratings[word]] if len(ratings) > 1: rated = TRUESKILL_ENVIRONMENT.rate(ratings, partial_update=coef) for d in rated: word, rate = d.items()[0] self.ratings[word] = rate
def rate(self, words, coef=None): for lang in self.langs: ratings = [{word: self.ratings[word]} for word in words if self.ratings[word] and self.word_db[word].lang == lang] if len(ratings) > 1: rated = TRUESKILL_ENVIRONMENT.rate(ratings, partial_update=coef) for d in rated: word, rate = d.items()[0] self.ratings[word] = rate
def post(self): game_key = ndb.Key(urlsafe=self.request.get('game_key')) logging.info("Handling log of game {}".format(game_key.id())) if game_key.kind() not in ('GameLog', 'GameHistory'): self.abort(200) log_db = game_key.get() if log_db is None: logging.error("Can't find game log") self.abort(200) is_legacy = game_key.kind() == 'GameHistory' try: words_orig, seen_words_time, words_outcome, explained_at_once, explained_pair, players_count,\ start_timestamp, finish_timestamp = self.parse_history(log_db) if is_legacy else self.parse_log(log_db) if start_timestamp and finish_timestamp: self.update_game_len_prediction(players_count, 'game', finish_timestamp - start_timestamp) bad_words_count = 0 for k, v in seen_words_time.items(): if v < 2: bad_words_count += 1 if 2*len(seen_words_time) < len(words_orig): raise BadGameError('suspect_too_little_words') if 2*bad_words_count > len(seen_words_time): raise BadGameError('suspect_too_quick_explanation') for word in words_orig: self.check_word(word) word_db = [GlobalDictionaryWord.get(word) for word in words_orig] self.ratings = [TRUESKILL_ENVIRONMENT.create_rating(word.E, word.D) if word else None for word in word_db] d = defaultdict(list) for word in seen_words_time: if explained_at_once[word]: d[explained_pair[word]].append(word) for l in d.values(): l.sort(key=lambda item: (words_outcome[item] != 'failed', -seen_words_time[item])) self.rate(l) d.clear() d = defaultdict(list) for word in seen_words_time: if words_outcome[word] in ('guessed', 'failed'): d[explained_pair[word][0]].append(word) for l in d.values(): l.sort(key=lambda item: (words_outcome[item] != 'failed', -seen_words_time[item])) self.rate(l, coef=0.3) d.clear() for word in seen_words_time: if words_outcome[word] == 'guessed': d[explained_pair[word][1]].append(word) for l in d.values(): l.sort(key=lambda item: -seen_words_time[item]) self.rate(l, coef=0.8) words = [] for word in seen_words_time: if words_outcome[word] in ('guessed', 'failed'): words.append(word) words.sort(key=lambda item: (words_outcome[item] != 'failed', -seen_words_time[item])) self.rate(words, coef=0.6) for i in range(len(words_orig)): if i in seen_words_time: self.update_word(words_orig[i], words_outcome[i], seen_words_time[i], self.ratings[i], game_key) if start_timestamp: start_timestamp //= 1000 if finish_timestamp: finish_timestamp //= 1000 duration = finish_timestamp - start_timestamp else: duration = 0 game_date = get_date(start_timestamp) self.update_daily_statistics(game_date, len(seen_words_time), players_count, duration) self.update_total_statistics(len(seen_words_time), start_timestamp) if players_count: self.update_statistics_by_player_count(players_count) memcache.delete_multi(["danger_top", "words_top", "words_bottom", "used_words_count"]) except BadGameError as e: if isinstance(e, BadGameError): reason = e.reason else: reason = 'format-error' log_db.ignored = True log_db.reason = reason log_db.put() logging.warning("Did not handle and marked this game as ignored: {}".format(log_db.reason)) self.abort(200)