def create(scores): gameDate = date.today() Will = assignPoints('Will', scores) Dan = assignPoints('Dan', scores) Ben = assignPoints('Ben', scores) Sven = assignPoints('Sven', scores) print(gameDate) existing_record = None # Can we insert this person? if existing_record is None: # Create a person instance using the schema and the passed-in person schema = ScoresSchema() new_record = schema.load(scores, session=db.session).data # Add the person to the database newScores = Scores(gameDate=gameDate, Will=Will, Dan=Dan, Ben=Ben, Sven=Sven) db.session.add(newScores) db.session.commit() # Serialize and return the newly created person in the response return schema.dump(new_record).data, 201 # Otherwise, nope, person exists already else: abort(409, f'Record exists already')
def play(): """ - Entering the player's name - Creating a player object - level = 1 - Creating an enemy object - calls the attack and defense methods of the player object in an endless loop - when an exception occurs, EnemyDown raises the game level, creates a new Enemy object with a new level, adds +5 points to the player.""" level = 1 player = Player(Inputs.input_player_name(), LIVES) Inputs.input_start() enemy = Enemy(level) while True: try: player.attack(enemy) player.defence(enemy) except EnemyDown: level += 1 enemy = Enemy(level) player.score += ENEMY_DOWN_SCORE print(cli_color(f'{ENEMY_DOWN_STRING} {player.score}', 'c')) print(LIVES_STRING, player.lives) except GameOver: scores = Scores() # read scores from file try: scores.read_from_file(SCORE_FILE) except FileNotFoundError: pass # write score to scores file scores.new_result(player, SCORE_FILE) raise GameOver
def countscore(r, mathobj, checkscore, attckscore): print('统计 %d 轮分数' % r) #统计check #队伍数量 team_count = Teams.query.count() #被扣分的container.id和typename checked = Round.query.join(containers, containers.id == Round.containerid).filter( Round.rounds == r, Round.attackteamid == 0).with_entities( containers.typename, containers.id).all() checked_score_sum = {} for i in checked: if checked_score_sum.get(i[0]): checked_score_sum[i[0]]['score'] += checkscore checked_score_sum[i[0]]['containers'].append(i[1]) else: checked_score_sum[i[0]] = {} checked_score_sum[i[0]]['containers'] = [] checked_score_sum[i[0]]['score'] = checkscore checked_score_sum[i[0]]['containers'].append(i[1]) for i in checked_score_sum: if team_count > len(checked_score_sum[i]['containers']): checked_score_sum[i][ 'avgscore'] = checked_score_sum[i]['score'] / ( team_count - len(checked_score_sum[i]['containers']) + 0.0) else: checked_score_sum[i]['avgscore'] = 0 print '=============================' print 'check情况' print '=============================' print checked_score_sum for i in mathobj: for c in checked_score_sum: if i.db_containers.typename == c: print c, i.db_containers.id #print i.teamid print i.db_containers.score if i.db_containers.id in checked_score_sum[c]['containers']: i.db_containers.score -= decimal.Decimal(checkscore) else: i.db_containers.score += decimal.Decimal( checked_score_sum[c]['avgscore']).quantize( decimal.Decimal('0.00')) print i.db_containers.score i.update_score() #统计丢分 checked = Round.query.join(containers, containers.id == Round.containerid).filter( Round.rounds == r, Round.attackteamid != 0).with_entities( containers.typename, containers.id, Round.attackteamid).all() #print checked checked_score_sum = {} for i in checked: #print 'Key ERROR?',i if checked_score_sum.get(i[0]): if checked_score_sum[i[0]].get(i[1]): checked_score_sum[i[0]][i[1]]['attackteams'].append(i[2]) else: checked_score_sum[i[0]][i[1]] = {} checked_score_sum[i[0]][i[1]]['attackteams'] = [] checked_score_sum[i[0]][i[1]]['attackteams'].append(i[2]) else: checked_score_sum[i[0]] = {} checked_score_sum[i[0]][i[1]] = {} checked_score_sum[i[0]][i[1]]['attackteams'] = [] checked_score_sum[i[0]][i[1]]['attackteams'].append(i[2]) print '=============================' print '丢分情况' print '=============================' for i in checked_score_sum: for j in checked_score_sum[i]: checked_score_sum[i][j]['avgscore'] = attckscore / ( len(checked_score_sum[i][j]['attackteams']) + 0.0) print '=============================' print checked_score_sum print '=============================' for i in checked_score_sum: for cid in checked_score_sum[i]: for x in Round.query.filter(Round.rounds == r, Round.containerid == cid).all(): x.score = decimal.Decimal( checked_score_sum[i][cid]['avgscore']) db.session.commit() for c in checked_score_sum: for i in mathobj: if i.db_containers.typename == c: for i1 in checked_score_sum[c]: #print c,i.db_containers.id,i.db_containers.score if i.db_containers.id == i1: i.db_containers.score -= decimal.Decimal(attckscore) #print c,i.db_containers.id,i.db_containers.score #i.update_score() #break if i.teamid in checked_score_sum[c][i1]['attackteams']: i.db_containers.score += decimal.Decimal( checked_score_sum[c][i1]['avgscore']).quantize( decimal.Decimal('0.00')) #print c,i.db_containers.id,i.db_containers.score #i.update_score() #continue #break #i.session.commit() i.update_score() # 记录每轮的分数 teams = Teams.query.all() for i in teams: #team_score = i.score() #r db.session.add(Scores(i.id, i.score(), r)) db.session.commit() return