def loadDB(self): print("Initializing OpeningsLoader") file_data = self.preProcessGamesData() print("Current games folder data size:") print(len(file_data)) # To do: remove truncation for gameMap in file_data[0:3]: game = Game(event=gameMap['Event'], site=gameMap['Site'], date=gameMap['Date'], stage=gameMap['Round'], white=gameMap['White'], black=gameMap['Black'], result=gameMap['Result'], black_elo=gameMap['BlackElo'], blackfide_id=gameMap['BlackFideId'], black_title=gameMap['BlackTitle'], eco=gameMap['ECO'], event_date=gameMap['EventDate'], opening=gameMap['Opening'], variation=gameMap['Variation'], white_elo=gameMap['WhiteElo'], whitefide_id=gameMap['WhiteFideId'], white_title=gameMap['WhiteTitle'], moves=gameMap['Moves']) game.save() print("Database loaded successfully!")
def create_game(payload): body = request.get_json() # If the request doesn't contain the below keys return 400 if not ('name' in body and 'age_rating' in body and 'category_id' in body and 'developer_id' in body and 'image_link' in body): abort(400) try: # Create a game instance game = Game( name=body.get('name'), age_rating=body.get('age_rating'), category_id=body.get('category_id'), developer_id=body.get('developer_id'), image_link=body.get('image_link') ) # Create the record game.insert() return jsonify({ "success": True, "game": game.format() }) except BaseException: abort(422)
def create_game_submission(): try: # Query both category & developer based on name category = Category.query.filter_by( name=request.form["category"].lower()).first() developer = Developer.query.filter_by( name=request.form["developer"].lower()).first() # Create a game instance game = Game(name=request.form["name"].lower(), age_rating=request.form["age_rating"], category_id=category.id, developer_id=developer.id, image_link=request.form["image_link"]) # Create the record game.insert() flash(request.form['name'] + ' game was successfully listed!') return redirect(url_for('web_app.get_games')) except BaseException: abort(422)
def post(self): body = request.get_json() game = Game(**body).save() id = game.id return {'id': str(id)}, 200
def update(): logger = getLogger("cronjob.pastats") logger.info("Updating PAStats Winners database...") # calculate duration, at most seconds since start with shelfopen(environ["PASTATS_SHELF"], "c") as shelf: last = shelf.get("last", 0) reset = int(environ["PASTATS_RESET"]) start = max(last, reset) reference = datetime(1970, 1, 1) + timedelta(seconds=start) dur_delta = datetime.utcnow() - reference duration = int(dur_delta.total_seconds()) # create url we need for the query params = {"start": start, "duration": duration} winners_url = "http://pastats.com/report/winners" url = "{0}?{1}".format(winners_url, urlencode(params)) logger.debug("PAStats URL: %s", url) # update start for next query start += duration # set it back again three hours so we can be sure to get all matches start -= (60 * 60 * 3) # save it for the next run with shelfopen(environ["PASTATS_SHELF"], "c") as shelf: shelf["last"] = start request = Request(url, headers={"Accept-Encoding": "gzip"}) with urlopen(request) as response: data = response.read() if response.info().get("Content-Encoding") == "gzip": data = decompress(data) data = str(data, "utf-8") logger.info("Updating game database...") games = loads(data) session = Session() known_games = list(chain(*session.query(Game.gid).all())) for game in games: game_id = game["gameId"] # check only games we haven't seen yet if game_id in known_games: continue # for now only add games with exactly 2 teams and 1 player per team teams = game["teams"] if (len(teams) != 2 or len(teams[0]["players"]) != 1 or len(teams[1]["players"]) != 1): continue # start time is stored in ms game_start = game["startTime"] // 1000 game_time = datetime(1970, 1, 1) + timedelta(seconds=game_start) p1 = teams[0]["players"][0] p2 = teams[1]["players"][0] p1_id, p1_name = p1["playerId"], p1["playerName"] p2_id, p2_name = p2["playerId"], p2["playerName"] del p1, p2 # ignore matches vs anonymous players for now if p1_id == -1 or p2_id == -1: continue try: p1 = session.query(Player).filter(Player.pid == p1_id).one() except NoResultFound: p1 = Player(p1_id, p1_name, trueskill.Rating(), game_time) session.add(p1) logger.debug("Created new player: {0}".format(p1)) if p1.name != p1_name: p1.name = p1_name session.add(p1) if p1.updated < game_time: p1.updated = game_time session.add(p1) try: p2 = session.query(Player).filter(Player.pid == p2_id).one() except NoResultFound: p2 = Player(p2_id, p2_name, trueskill.Rating(), game_time) session.add(p2) logger.debug("Created new player: {0}".format(p2)) if p2.name != p2_name: p2.name = p2_name session.add(p2) if p2.updated < game_time: p2.updated = game_time session.add(p2) # check for winner or draw and update ratings if game["winner"] == -1: winner = None p1.skill, p2.skill = trueskill.rate_1vs1(p1.skill, p2.skill, drawn=True) elif teams[0]["teamId"] == game["winner"]: winner = p1 p1.skill, p2.skill = trueskill.rate_1vs1(p1.skill, p2.skill) else: winner = p2 p2.skill, p1.skill = trueskill.rate_1vs1(p2.skill, p1.skill) # add match session.add(Game(game_id, winner, p1, p2)) # for the unlikely case we receive the same match twice: known_games.append(game_id) session.commit() session.close() logger.info("Received and parsed all new data, " "seen {0} matches".format(len(known_games)))