示例#1
0
def update_answer(answer):
  db = get_db()

  # WARNING!!!!
  # AS THIS STANDS, USER CAN CHANGE THEIR ANSWER AFTER SUBMISSION
  # WITH A SECOND POST CALL.
  player_name = session['username']
  
  db = get_db()
  db.execute(
    'UPDATE players'
    ' SET answer = ?'
    ' WHERE name = ?',
    (answer, player_name,))
示例#2
0
文件: ready.py 项目: rmbern/cah-clone
def ready():
    if 'username' in session:
        player_name = session['username']
        db = get_db()

        db.execute(
            'UPDATE players'
            ' SET ready = 1,'
            '     answer = NULL'
            ' WHERE name = ?', (player_name, ))

        # Ideally, we would want to select a judge at initialization to
        # keep all of our "one and done" code in a single spot. However,
        # This is the first point at which the database is aware of all
        # the players in the game.
        if all_ready():
            update_judge()
            update_question()
        db.commit()

        return render_template('wait-for-ready.html', player=player_name)
    else:
        current_app.logger.warning("Player with no username in session "
                                   "sent a ready signal!")

        return "You don't exist!"
示例#3
0
文件: ready.py 项目: rmbern/cah-clone
def update_question():
    db = get_db()

    # First, delete the question we just used.
    # TODO: We have nothing handling the exhaustion
    #       of questions. This will most likely
    #       result in a crash.

    # There may be desirable scenarios where we don't
    # delete whatever we find in current_question,
    # notably during the first round where a bogus init
    # value is placed into the database.
    current_question = db.execute(
        'SELECT question FROM current_question').fetchone()['question']

    db.execute('DELETE FROM questions'
               ' WHERE question = ?', (current_question, ))

    question_records = db.execute('SELECT question FROM questions').fetchall()

    r = random.Random()
    q_index = r.randrange(0, len(question_records))
    new_question = question_records[q_index]['question']

    db.execute('UPDATE current_question' ' SET question = ?', (new_question, ))
示例#4
0
def submit_answer():
  with current_app.app_context():
    db = get_db()
    answer = request.form['answer']
    update_answer(answer)
    db.commit()

  return render_template('answer-wait.html', answer=answer)
示例#5
0
def all_answered():
  db = get_db()

  answer_records = db.execute(
    'SELECT answer FROM players'
  ).fetchall()

  return not None in [ x['answer'] for x in answer_records ]
示例#6
0
文件: judge.py 项目: rmbern/cah-clone
def judge_answered():
    db = get_db()

    record = db.execute('SELECT answer FROM players'
                        ' WHERE judge = 1').fetchone()

    if record['answer'] == None:
        return "JUDGE DID NOT ANSWER"
    else:
        return "JUDGE ANSWERED"
示例#7
0
def scoreboard():
    db = get_db()

    score_records = db.execute('SELECT name, score FROM players').fetchall()

    winning_answer = db.execute('SELECT answer FROM players'
                                ' WHERE judge = 1').fetchone()

    return render_template('scoreboard.html',
                           records=score_records,
                           winning_answer=winning_answer[0])
示例#8
0
def get_answers():
  with current_app.app_context():
    # Construct and return a json object of all answers
    # excluding nulls and the current player's answer.
    db = get_db()
    answer_records = db.execute(
                         'SELECT answer FROM players'
                         ' WHERE name != ?'
                         ' AND answer IS NOT NULL',(session['username'],)
                        ).fetchall()
    
    # TODO: Can the map iterable be passed to json.dumps?
    answers = [ x['answer'] for x in answer_records ]
    return json.dumps(answers)
示例#9
0
文件: judge.py 项目: rmbern/cah-clone
def select_winner():
    # Look up score of player(s) submitting winning answer,
    # and increment it.

    db = get_db()

    # In what should be a rare case of 2+ players submitting
    # the same answer, and one of those answers being chosen,
    # we will increment the score of both players, except in
    # the case where one of those players is a judge.

    # The judge should have a blank answer whereas all players
    # should have populated answers at this point, but we
    # explicity make sure we are not updating a judge's score
    # for defensive purposes.
    db.execute(
        'UPDATE players'
        ' SET score = score + 1'
        ' WHERE answer = ?'
        ' AND judge = 0', (request.form['answer-button'], ))

    # After setting the players' scores, set the judge's answer
    # to whatever they selected from the other players. Now we
    # know in other functions what the judge answered.
    change_diff = db.total_changes
    db.execute(
        'UPDATE players'
        ' SET answer = ?'
        ' WHERE judge = 1'
        ' AND name = ?', (
            request.form['answer-button'],
            session['username'],
        ))
    # TODO: My gut said to check that an update actually occured
    #       for this one sql statement, but maybe it belongs to
    #       most of them? I can't think of a situation in this
    #       enviornment where an execute shouldn't make a change.
    change_diff = db.total_changes - change_diff

    if change_diff == 0:
        current_app.logger.warning("Judge's answer was not updated!")

    # Remove all ready flags. Players will re-submit a ready request
    # on the scoreboard page.
    db.execute('UPDATE players' ' SET ready = 0')

    db.commit()

    return redirect('/scoreboard')
示例#10
0
def serve_question():
  db = get_db()

  question = db.execute(
    'SELECT question FROM current_question'
  ).fetchone()['question']

  player_record = db.execute(
    'SELECT judge FROM players'
    ' WHERE name = ?', (session['username'],)
  ).fetchone()

  if player_record['judge']:
    return render_template('judge-menu.html', question=question)
  else:
    return render_template('round.html', question=question)
示例#11
0
文件: ready.py 项目: rmbern/cah-clone
def update_judge():
    db = get_db()

    # We are going to select a judge randomly,
    # excluding the player who is currently the
    # judge.
    judge_records = db.execute('SELECT name FROM players'
                               ' WHERE judge != 1').fetchall()
    r = random.Random()
    j_index = r.randrange(0, len(judge_records))
    new_judge = judge_records[j_index]['name']

    db.execute('UPDATE players' ' SET judge = 0')

    db.execute('UPDATE players'
               ' SET judge = 1'
               ' WHERE name = ?', (new_judge, ))
示例#12
0
文件: ready.py 项目: rmbern/cah-clone
def all_ready():
    db = get_db()
    records = db.execute('SELECT ready FROM players').fetchall()
    return not 0 in [x['ready'] for x in records]