def test_get_and_set_score_list():
  iid = test_utils.make_instance_with_players()
  set_score(iid, firstpid, [100, 2])
  set_score(iid, players[1], [200, 1])
  assert get_score(iid, firstpid) == [100, 2]
  assert get_score(iid, players[1]) == [200, 1]
  assert get_score(iid, players[2]) == [0]
def test_get_score():
  iid = test_utils.make_instance_with_players()
  set_score(iid, firstpid, 100)
  set_score(iid, players[1], 200)
  assert get_score(iid, firstpid)[0] == 100
  assert get_score(iid, players[1])[0] == 200
  assert get_score(iid, players[2])[0] == 0
def test_get_and_set_score_list():
    iid = test_utils.make_instance_with_players()
    set_score(iid, firstpid, [100, 2])
    set_score(iid, players[1], [200, 1])
    assert get_score(iid, firstpid) == [100, 2]
    assert get_score(iid, players[1]) == [200, 1]
    assert get_score(iid, players[2]) == [0]
def test_get_cards_remaining():
    iid = test_utils.make_instance_with_players()
    response = test_utils.post_server_command(iid, 'crd_cards_left', [])
    assert response['contents'] == [-1]
    test_utils.post_server_command(iid, 'crd_draw_cards', [5, False])
    response = test_utils.post_server_command(iid, 'crd_cards_left', [])
    assert response['contents'] == [47]
def test_join_full_game():
    iid = test_utils.make_instance_with_players()
    playerid = "*****@*****.**"
    instance = test_utils.get_instance_model(iid)
    players = len(instance.players)

    # Set the maximum membership to the current number of players
    test_utils.post_server_command(iid, "sys_set_max_players", [players])

    # Invite someone new, confirm that they cannot join the instance.
    app.post("/invite", {"gid": gid, "iid": iid, "inv": playerid})
    instance = test_utils.get_instance_model(iid)
    assert instance.full
    assert playerid in instance.invited
    assert playerid not in instance.players
    response = app.post("/joininstance", {"gid": gid, "iid": iid, "pid": playerid}).json
    assert response["e"] is True
    instance = test_utils.get_instance_model(iid)
    assert instance.full
    assert playerid in instance.invited
    assert playerid not in instance.players

    # Increase the maximum membership by one, retry joining
    test_utils.post_server_command(iid, "sys_set_max_players", [players + 1])
    response = app.post("/joininstance", {"gid": gid, "iid": iid, "pid": playerid}).json
    instance = test_utils.get_instance_model(iid)
    assert instance.full
    assert playerid not in instance.invited
    assert playerid in instance.players
    test_utils.post_server_command(iid, "sys_set_max_players", [0])
    instance = test_utils.get_instance_model(iid)
    assert not instance.full
def test_pass_cards():
    iid = test_utils.make_instance_with_players()
    args = [7, True, True, False, players]
    response = test_utils.post_server_command(iid, 'crd_deal_cards', args)
    hands = get_hands(iid)

    # Player 2 passes four cards to player 1
    passing_player = check_playerid(players[1])
    initial_hand = hands[passing_player]
    args = [firstpid, initial_hand[3:]]
    response = test_utils.post_server_command(iid,
                                              'crd_pass_cards',
                                              args,
                                              pid=passing_player)
    assert response['contents'] == initial_hand[:3]
    hands = get_hands(iid)
    assert len(hands[firstpid]) == 11
    assert hands[firstpid][7:] == initial_hand[3:]

    # Player 1 passes all cards to player 3
    receiving_player = check_playerid(players[2])
    args = [receiving_player, hands[firstpid]]
    response = test_utils.post_server_command(iid, 'crd_pass_cards', args)
    assert response['contents'] == []
    hands = get_hands(iid)
    assert len(hands[firstpid]) == 0
    assert len(hands[receiving_player]) == 18
def test_get_cards_remaining():
  iid = test_utils.make_instance_with_players()
  response = test_utils.post_server_command(iid, 'crd_cards_left', [])
  assert response['contents'] == [-1]
  test_utils.post_server_command(iid, 'crd_draw_cards', [5, False])
  response = test_utils.post_server_command(iid, 'crd_cards_left', [])
  assert response['contents'] == [47]
def test_pass_cards():
  iid = test_utils.make_instance_with_players()
  args = [7, True, True, False, players]
  response = test_utils.post_server_command(iid, 'crd_deal_cards', args)
  hands = get_hands(iid)

  # Player 2 passes four cards to player 1
  passing_player = check_playerid(players[1])
  initial_hand = hands[passing_player]
  args = [firstpid, initial_hand[3:]]
  response = test_utils.post_server_command(iid, 'crd_pass_cards', args,
                                            pid = passing_player)
  assert response['contents'] == initial_hand[:3]
  hands = get_hands(iid)
  assert len(hands[firstpid]) == 11
  assert hands[firstpid][7:] == initial_hand[3:]

  # Player 1 passes all cards to player 3
  receiving_player = check_playerid(players[2])
  args = [receiving_player, hands[firstpid]]
  response = test_utils.post_server_command(iid, 'crd_pass_cards', args)
  assert response['contents'] == []
  hands = get_hands(iid)
  assert len(hands[firstpid]) == 0
  assert len(hands[receiving_player]) == 18
def test_get_score():
    iid = test_utils.make_instance_with_players()
    set_score(iid, firstpid, 100)
    set_score(iid, players[1], 200)
    assert get_score(iid, firstpid)[0] == 100
    assert get_score(iid, players[1])[0] == 200
    assert get_score(iid, players[2])[0] == 0
def test_draw_cards():
  iid = test_utils.make_instance_with_players()
  test_utils.post_server_command(iid, 'crd_draw_cards', [5, False])
  hands = get_hands(iid)
  for player, hand in hands.items():
    assert len(hand) == (5 if player == firstpid else 0)
  assert hands[firstpid] == get_player_hand(iid, firstpid)
  assert get_cards_left(iid) == 47
def test_discard_cards_not_present():
  iid = test_utils.make_instance_with_players()
  args = [7, True, True, False, players]
  test_utils.post_server_command(iid, 'crd_deal_cards', args)
  initial_hand = get_hands(iid)[firstpid]
  to_discard = [initial_hand[0]] + ['%s' % x for x in xrange(12)]
  response = test_utils.post_server_command(iid, 'crd_discard', to_discard)
  assert response['contents'] == initial_hand[1:]
def test_discard():
    iid = test_utils.make_instance_with_players()
    args = [7, True, True, False, players]
    test_utils.post_server_command(iid, 'crd_deal_cards', args)
    initial_hand = get_hands(iid)[firstpid]
    response = test_utils.post_server_command(iid, 'crd_discard',
                                              initial_hand[:3])
    assert response['contents'] == initial_hand[3:]
def test_discard():
  iid = test_utils.make_instance_with_players()
  args = [7, True, True, False, players]
  test_utils.post_server_command(iid, 'crd_deal_cards', args)
  initial_hand = get_hands(iid)[firstpid]
  response = test_utils.post_server_command(iid, 'crd_discard',
                                            initial_hand[:3])
  assert response['contents'] == initial_hand[3:]
def test_set_score():
  iid = test_utils.make_instance_with_players()
  response = set_score(iid, firstpid, 100)
  assert [100, firstpid] == response['contents'][0]
  set_score(iid, firstpid, 200)
  response = set_score(iid, players[1], 400)
  assert [400, check_playerid(players[1])] == response['contents'][0]
  assert [200, firstpid] == response['contents'][1]
def test_set_score():
    iid = test_utils.make_instance_with_players()
    response = set_score(iid, firstpid, 100)
    assert [100, firstpid] == response['contents'][0]
    set_score(iid, firstpid, 200)
    response = set_score(iid, players[1], 400)
    assert [400, check_playerid(players[1])] == response['contents'][0]
    assert [200, firstpid] == response['contents'][1]
def test_draw_cards():
    iid = test_utils.make_instance_with_players()
    test_utils.post_server_command(iid, 'crd_draw_cards', [5, False])
    hands = get_hands(iid)
    for player, hand in hands.items():
        assert len(hand) == (5 if player == firstpid else 0)
    assert hands[firstpid] == get_player_hand(iid, firstpid)
    assert get_cards_left(iid) == 47
def test_discard_cards_not_present():
    iid = test_utils.make_instance_with_players()
    args = [7, True, True, False, players]
    test_utils.post_server_command(iid, 'crd_deal_cards', args)
    initial_hand = get_hands(iid)[firstpid]
    to_discard = [initial_hand[0]] + ['%s' % x for x in xrange(12)]
    response = test_utils.post_server_command(iid, 'crd_discard', to_discard)
    assert response['contents'] == initial_hand[1:]
def test_clear_and_get_scoreboard():
    iid = test_utils.make_instance_with_players()
    response = set_score(iid, firstpid, 100)
    response = test_utils.post_server_command(iid, 'scb_get_scoreboard', [])
    assert [100, firstpid] in response['contents']
    response = test_utils.post_server_command(iid, 'scb_clear_scoreboard', [])
    assert [0, firstpid] in response['contents']
    response = test_utils.post_server_command(iid, 'scb_get_scoreboard', [])
    assert [0, firstpid] in response['contents']
def test_deal_all_cards():
  iid = test_utils.make_instance_with_players()
  players_list = test_utils.add_player(iid, '*****@*****.**')
  args = [13, True, True, False, players_list]
  response = test_utils.post_server_command(iid, 'crd_deal_cards', args)
  assert len(response['contents']) == 13
  for hand in get_hands(iid).values():
    assert len(hand) == 13
  assert get_cards_left(iid) == 0
def test_clear_and_get_scoreboard():
  iid = test_utils.make_instance_with_players()
  response = set_score(iid, firstpid, 100)
  response = test_utils.post_server_command(iid, 'scb_get_scoreboard', [])
  assert [100, firstpid] in response['contents']
  response = test_utils.post_server_command(iid, 'scb_clear_scoreboard', [])
  assert [0, firstpid] in response['contents']
  response = test_utils.post_server_command(iid, 'scb_get_scoreboard', [])
  assert [0, firstpid] in response['contents']
def test_deal_all_cards():
    iid = test_utils.make_instance_with_players()
    players_list = test_utils.add_player(iid, '*****@*****.**')
    args = [13, True, True, False, players_list]
    response = test_utils.post_server_command(iid, 'crd_deal_cards', args)
    assert len(response['contents']) == 13
    for hand in get_hands(iid).values():
        assert len(hand) == 13
    assert get_cards_left(iid) == 0
def test_scoreboard_rejects_unknown_players():
  iid = test_utils.make_instance_with_players()

  #Try to add a scoreboard entry for a player not in the game, it should fail.
  fake_player = "*****@*****.**"
  args = [fake_player, 42]
  test_utils.post_server_command(iid, 'scb_add_to_score', args,
                                 error_expected = True, pid=fake_player)
  response = test_utils.post_server_command(iid, 'scb_get_scoreboard', [])
  assert [42, fake_player] not in response['contents']
def test_deal_twice():
    iid = test_utils.make_instance_with_players()
    args = [7, True, True, False, players]
    response = test_utils.post_server_command(iid, 'crd_deal_cards', args)
    args = [7, False, False, False, players]
    response = test_utils.post_server_command(iid, 'crd_deal_cards', args)
    assert len(response['contents']) == 14
    for hand in get_hands(iid).values():
        assert len(hand) == 14
    assert get_cards_left(iid) == 10
def test_deal_twice():
  iid = test_utils.make_instance_with_players()
  args = [7, True, True, False, players]
  response = test_utils.post_server_command(iid, 'crd_deal_cards', args)
  args = [7, False, False, False, players]
  response = test_utils.post_server_command(iid, 'crd_deal_cards', args)
  assert len(response['contents']) == 14
  for hand in get_hands(iid).values():
    assert len(hand) == 14
  assert get_cards_left(iid) == 10
def test_get_public_instances():
    iid = test_utils.make_instance_with_players()
    response = test_utils.post_server_command(iid, "sys_get_public_instances", [])
    assert iid not in [i[0] for i in response["contents"]]

    test_utils.post_server_command(iid, "sys_set_public", [True])
    response = test_utils.post_server_command(iid, "sys_get_public_instances", [])
    assert iid in [i[0] for i in response["contents"]]

    test_utils.post_server_command(iid, "sys_set_public", [False])
    response = test_utils.post_server_command(iid, "sys_get_public_instances", [])
    assert iid not in [i[0] for i in response["contents"]]
def test_deal_too_many_cards():
  iid = test_utils.make_instance_with_players()
  args = [500, True, True, False, players]
  response = test_utils.post_server_command(iid, 'crd_deal_cards', args,
                                            error_expected = True)
  args = [500, True, True, True, players]
  response = test_utils.post_server_command(iid, 'crd_deal_cards', args,
                                            error_expected = False)
  assert len(response['contents']) == 18
  for player, hand in get_hands(iid).items():
    assert len(hand) == (18 if player == firstpid else 17)
  assert get_cards_left(iid) == 0
def test_set_public_with_strings():
    # Make sure new games are not public
    iid = test_utils.make_instance_with_players()

    # Set public to true with lowercase string
    test_utils.post_server_command(iid, "sys_set_public", ["true"])
    instance = test_utils.get_instance_model(iid)
    assert instance.public

    # Set public to false with lowercase string
    test_utils.post_server_command(iid, "sys_set_public", ["false"])
    instance = test_utils.get_instance_model(iid)
    assert not instance.public
def test_scoreboard_rejects_unknown_players():
    iid = test_utils.make_instance_with_players()

    #Try to add a scoreboard entry for a player not in the game, it should fail.
    fake_player = "*****@*****.**"
    args = [fake_player, 42]
    test_utils.post_server_command(iid,
                                   'scb_add_to_score',
                                   args,
                                   error_expected=True,
                                   pid=fake_player)
    response = test_utils.post_server_command(iid, 'scb_get_scoreboard', [])
    assert [42, fake_player] not in response['contents']
def test_set_deck():
  iid = test_utils.make_instance_with_players()
  deck = [[n, s] for n in range(14)[4:] for s in  ['Hearts','Spades']]
  missing_cards = [[n, s] for n in range(4)[1:] for s in ['Hearts', 'Spades']]
  response = test_utils.post_server_command(iid, 'crd_set_deck', deck)
  assert response['contents'] == [20]

  # Set deck again, should fail.
  test_utils.post_server_command(iid, 'crd_set_deck', deck,
                                 error_expected = True)
  # Draw all the cards
  response = test_utils.post_server_command(iid, 'crd_draw_cards', [40, True])
  for card in missing_cards:
    assert card not in response['contents']
def test_deal_new_hand():
  iid = test_utils.make_instance_with_players()
  args = [7, True, True, False, players]
  response = test_utils.post_server_command(iid, 'crd_deal_cards', args)
  assert len(response['contents']) == 7
  for player, hand in get_hands(iid).items():
    assert len(hand) == 7
    assert hand == get_player_hand(iid, player)
  assert get_cards_left(iid) == 31
  args = [7, False, True, False, players]
  response = test_utils.post_server_command(iid, 'crd_deal_cards', args)
  assert len(response['contents']) == 7
  for hand in get_hands(iid).values():
    assert len(hand) == 7
  assert get_cards_left(iid) == 10
def test_deal_new_hand():
    iid = test_utils.make_instance_with_players()
    args = [7, True, True, False, players]
    response = test_utils.post_server_command(iid, 'crd_deal_cards', args)
    assert len(response['contents']) == 7
    for player, hand in get_hands(iid).items():
        assert len(hand) == 7
        assert hand == get_player_hand(iid, player)
    assert get_cards_left(iid) == 31
    args = [7, False, True, False, players]
    response = test_utils.post_server_command(iid, 'crd_deal_cards', args)
    assert len(response['contents']) == 7
    for hand in get_hands(iid).values():
        assert len(hand) == 7
    assert get_cards_left(iid) == 10
def test_set_public():
    # Make sure new games are not public
    iid = test_utils.make_instance_with_players()
    instance = test_utils.get_instance_model(iid)
    assert not instance.public

    # Set public to True
    test_utils.post_server_command(iid, "sys_set_public", [True])
    instance = test_utils.get_instance_model(iid)
    assert instance.public

    # Set public to False
    test_utils.post_server_command(iid, "sys_set_public", [False])
    instance = test_utils.get_instance_model(iid)
    assert not instance.public
def test_deal_too_many_cards():
    iid = test_utils.make_instance_with_players()
    args = [500, True, True, False, players]
    response = test_utils.post_server_command(iid,
                                              'crd_deal_cards',
                                              args,
                                              error_expected=True)
    args = [500, True, True, True, players]
    response = test_utils.post_server_command(iid,
                                              'crd_deal_cards',
                                              args,
                                              error_expected=False)
    assert len(response['contents']) == 18
    for player, hand in get_hands(iid).items():
        assert len(hand) == (18 if player == firstpid else 17)
    assert get_cards_left(iid) == 0
def test_add_to_score():
  iid = test_utils.make_instance_with_players()
  score = 100

  # Add the first player to the scoreboard
  change_response = add_to_score(iid, firstpid, score)
  response = test_utils.post_server_command(iid, 'scb_get_scoreboard', [])
  assert response['contents']==change_response['contents']
  assert [score, firstpid] in response['contents']

  # Increment first players score and add another player
  add_to_score(iid, firstpid, score)
  add_to_score(iid, players[1], score)
  response = test_utils.post_server_command(iid, 'scb_get_scoreboard', [])
  assert [score * 2, players[0]] in response['contents']
  assert [score, check_playerid(players[1])] in response['contents']
def test_add_to_score():
    iid = test_utils.make_instance_with_players()
    score = 100

    # Add the first player to the scoreboard
    change_response = add_to_score(iid, firstpid, score)
    response = test_utils.post_server_command(iid, 'scb_get_scoreboard', [])
    assert response['contents'] == change_response['contents']
    assert [score, firstpid] in response['contents']

    # Increment first players score and add another player
    add_to_score(iid, firstpid, score)
    add_to_score(iid, players[1], score)
    response = test_utils.post_server_command(iid, 'scb_get_scoreboard', [])
    assert [score * 2, players[0]] in response['contents']
    assert [score, check_playerid(players[1])] in response['contents']
def test_draw_cards_beyond_deck():
  iid = test_utils.make_instance_with_players()
  # Draw too many cards with ignore empty deck set to false.
  # There should be no effect.
  test_utils.post_server_command(iid, 'crd_draw_cards', [75, False],
                                 error_expected = True)
  hands = get_hands(iid)
  for hand in hands.values():
    assert len(hand) == 0
  assert get_cards_left(iid) == -1

  # Draw too many cards with ignore empty deck, should deal all cards.
  test_utils.post_server_command(iid, 'crd_draw_cards', [75, True])
  hands = get_hands(iid)
  for player, hand in hands.items():
    assert len(hand) == (52 if player == firstpid else 0)
  assert get_cards_left(iid) == 0
def test_join_public():
    iid = test_utils.make_instance_with_players()

    # Make sure that an uninvited player cannot join.
    playerid = "*****@*****.**"
    response = app.post("/joininstance", {"gid": gid, "iid": iid, "pid": playerid}).json
    assert response["e"] is True
    assert "not invited" in response["response"]

    # Set the game to public and confirm that uninvited players can join.
    test_utils.post_server_command(iid, "sys_set_public", [True])
    response = app.post("/joininstance", {"gid": gid, "iid": iid, "pid": playerid}).json
    assert response["e"] is False

    state = test_utils.get_invited_and_joined_instances(iid, playerid)
    assert iid not in state["invited"]
    assert iid in state["joined"]
def test_set_deck():
    iid = test_utils.make_instance_with_players()
    deck = [[n, s] for n in range(14)[4:] for s in ['Hearts', 'Spades']]
    missing_cards = [[n, s] for n in range(4)[1:]
                     for s in ['Hearts', 'Spades']]
    response = test_utils.post_server_command(iid, 'crd_set_deck', deck)
    assert response['contents'] == [20]

    # Set deck again, should fail.
    test_utils.post_server_command(iid,
                                   'crd_set_deck',
                                   deck,
                                   error_expected=True)
    # Draw all the cards
    response = test_utils.post_server_command(iid, 'crd_draw_cards',
                                              [40, True])
    for card in missing_cards:
        assert card not in response['contents']
def test_draw_cards_beyond_deck():
    iid = test_utils.make_instance_with_players()
    # Draw too many cards with ignore empty deck set to false.
    # There should be no effect.
    test_utils.post_server_command(iid,
                                   'crd_draw_cards', [75, False],
                                   error_expected=True)
    hands = get_hands(iid)
    for hand in hands.values():
        assert len(hand) == 0
    assert get_cards_left(iid) == -1

    # Draw too many cards with ignore empty deck, should deal all cards.
    test_utils.post_server_command(iid, 'crd_draw_cards', [75, True])
    hands = get_hands(iid)
    for player, hand in hands.items():
        assert len(hand) == (52 if player == firstpid else 0)
    assert get_cards_left(iid) == 0
def test_get_public_instances_with_player_counts():
    test_utils.clear_data_store()
    iid = test_utils.make_instance_with_players()
    test_utils.post_server_command(iid, "sys_set_public", [True])

    instance = test_utils.get_instance_model(iid)
    players = len(instance.players)

    response = test_utils.post_server_command("", "sys_get_public_instances", [])
    assert [iid, 3, 0] in response["contents"]

    test_utils.post_server_command(iid, "sys_set_max_players", [players + 1])
    response = test_utils.post_server_command(iid, "sys_get_public_instances", [])
    assert [iid, players, players + 1] in response["contents"]

    test_utils.post_server_command(iid, "sys_set_max_players", [0])
    response = test_utils.post_server_command(iid, "sys_get_public_instances", [])
    assert [iid, players, 0] in response["contents"]
def test_pass_cards_not_present():
  iid = test_utils.make_instance_with_players()
  args = [7, True, True, False, players]
  response = test_utils.post_server_command(iid, 'crd_deal_cards', args)
  hands = get_hands(iid)

  # Player 2 passes four cards to player 1 that she has and two cards that
  # she doesn't have.
  passing_player = check_playerid(players[1])
  initial_hand = hands[passing_player]
  args = [firstpid,
          ['fake_card_1'] + initial_hand[3:] + ['fake_card_2']]
  response = test_utils.post_server_command(iid, 'crd_pass_cards', args,
                                            pid = passing_player)
  assert response['contents'] == initial_hand[:3]
  hands = get_hands(iid)
  assert len(hands[firstpid]) == 11
  assert hands[firstpid][7:] == initial_hand[3:]
def test_pass_cards_not_present():
    iid = test_utils.make_instance_with_players()
    args = [7, True, True, False, players]
    response = test_utils.post_server_command(iid, 'crd_deal_cards', args)
    hands = get_hands(iid)

    # Player 2 passes four cards to player 1 that she has and two cards that
    # she doesn't have.
    passing_player = check_playerid(players[1])
    initial_hand = hands[passing_player]
    args = [firstpid, ['fake_card_1'] + initial_hand[3:] + ['fake_card_2']]
    response = test_utils.post_server_command(iid,
                                              'crd_pass_cards',
                                              args,
                                              pid=passing_player)
    assert response['contents'] == initial_hand[:3]
    hands = get_hands(iid)
    assert len(hands[firstpid]) == 11
    assert hands[firstpid][7:] == initial_hand[3:]
def test_unknown_command():
    iid = test_utils.make_instance_with_players()
    test_utils.post_server_command(iid, "bogus_command", [], error_expected=True)