Beispiel #1
0
def players():
    conn, cursor = db.connect()
    p1 = Player(test_user_id, "erlend", 1000)
    p2 = Player(pingpongbot_id, "pingpong", 1000)
    db.create_player(cursor, p1)
    db.create_player(cursor, p2)
    conn.commit()
    conn.close()
    yield p1, p2
Beispiel #2
0
def join_game():
    session_code = request.form["session_code"]
    player_name = request.form["player"]
    if not db.get_session(session_code):
        flash("No game session found")
        return redirect(url_for('login'))
    if db.get_player(session_code, player_name):
        flash("Player %s already exists" % player_name)
        return redirect(url_for('login'))

    db.create_player(session_code, player_name)

    #response = make_response(render_template())
    return session_code + " - " + player_name
Beispiel #3
0
def join_game(access_code, player_name):
    """
    Find a game with the access code, and add the player to it
    """

    # find the game or error
    game = db_util.get_game(access_code)
    if game is None:
        send('game not found')
        return

    if game['state'] != 'waiting_for_players':
        send('game is in progress')
        return

    # create a player
    player = db_util.create_player(player_name)

    # add to the list of players
    db.games.update_one({'access_code': access_code},
                        {'$push': {
                            'players': player
                        }})

    join_room(access_code)
    emit('lobby_update',
         {'players': db_util.get_players_in_lobby(access_code)},
         room=access_code)
Beispiel #4
0
def handle(number, message, mtype='sms'):
	
	cmd_str = message.split()
	if cmd_str[0] == "create":
		return db.create_player(number)
	elif cmd_str[0] == "round":
		round_id = db.create_round(number)
		db.join_round(number, round_id)
		return "round id %s" % round_id
	elif cmd_str[0] == "join" and len(cmd_str) == 2:
		return db.join_round(number, cmd_str[1])
	elif cmd_str[0] == "score" and len(cmd_str) == 2:
		# need the round id, current hole, and total score to send to the new
		# score command
		return db.new_add_score(number, db.get_current_round(number),
								db.get_current_hole(number), cmd_str[1], 
								int(db.get_score(number)) + int(cmd_str[1]))

	elif cmd_str[0] == "score" and len(cmd_str) == 1:
		return db.get_score(number)
	elif cmd_str[0] == "name" and len(cmd_str) == 2:
		return db.add_name(number, cmd_str[1])
	elif cmd_str[0] == "name" and len(cmd_str) == 1:
		return db.get_name(number)
	elif cmd_str[0] == 'combine' and len(cmd_str) == 3:
		return db.combine(cmd_str[1], cmd_str[2])
	elif cmd_str[0] == 'compare' and len(cmd_str) == 3:
		res = db.compare(cmd_str[1], cmd_str[2])
		if res.player1 < res.player2:
			return "%s wins by %s" % (cmd_str[1], abs(res.player1 - res.player2))
		elif res.player2 < res.player1:
			return "%s wins by %s" % (cmd_str[2], abs(res.player2 - res.player1))
		else:
			return "Tie"
	elif cmd_str[0] == 'match' and len(cmd_str) == 5:
		for player in cmd_str[1:]:
			if not check_player_name(player):
				return "Player %s not in system" % player

		team1 = combine(cmd_str[1], cmd_str[2]).split()
		team2 = combine(cmd_str[3], cmd_str[4]).split()

		if team1[1] < team2[1]:
			return "%s and %s win" % (cmd_str[1], cmd_str[2])
		elif team2[1] < team1[1]:
			return "%s and %s win" % (cmd_str[3], cmd_str[4])
		else:
			return "Tie"

	elif cmd_str[0] == 'scores' and len(cmd_str) == 1:
		scores = db.scores(number)

		return ''.join('%s:%s, ' % (str(score.hole), str(score.score)) for score in scores)

	elif cmd_str[0] == 'front' and len(cmd_str) == 1:
		return db.front_nine(number)
	elif cmd_str[0] == 'back' and len(cmd_str) == 1:
		return db.back_nine(number)
	else:
		return "invalid command"
Beispiel #5
0
def create_game(player_name):
    """
    Generate a room access code and create a game with the creator as the first player
    """

    # create a player with player_name who is the moderator
    player = db_util.create_player(player_name, True)

    # generate an access code for the game
    while True:
        access_code = gl.generate_access_code()
        if db.games.count_documents({'access_code': access_code}) == 0:
            break

    # create a game with the player and access code
    game = db_util.create_game(access_code, player)
    db.games.insert_one(game)

    # send the room access code back to the creator
    send(access_code)
    join_room(access_code)
Beispiel #6
0
def add_new_player(id):
    player = Player(id, id, 1000)
    conn, cursor = db.connect()
    db.create_player(cursor, player)
    conn.commit()
    conn.close()