     {socket, port}
   end
 
-  defp send_and_recv(socket, port, command) do
+  defp send_and_recv(command, socket, port) do
     :ok = :gen_udp.send(socket, '127.0.0.1', port, command)
     case :gen_udp.recv(socket, 0, @timeout) do
       {:ok, {_addr, _port, data}} -> data

  test "Player sends invalid command" do
    {startmsg, sockets, ports} = setup_game()
    {1, [player | _], [socket | _], [port | _]} = extract_player_seq(startmsg, sockets, ports)
    {:ok, {_ip, _port, msg}} = :gen_udp.recv(socket, 0, @timeout)
    [_, token] = Regex.run(~r/YOUR TURN;([0-9a-fA-F]{32})\n/, msg)
    :gen_udp.send(socket, 'localhost', port, "BLABLA;#{token}")
    check_player_lost_aftermath(player, sockets, "INVALID TURN")
  end

  test "Player rolls but fails to announce" do
    {startmsg, sockets, ports} = setup_game()
    {1, [player | _], [socket | _], [port | _]} = extract_player_seq(startmsg, sockets, ports)
    {:ok, {_ip, _port, msg}} = :gen_udp.recv(socket, 0, @timeout)
    [_, token] = Regex.run(~r/YOUR TURN;([0-9a-fA-F]{32})\n/, msg)
    :gen_udp.send(socket, 'localhost', port, "ROLL;#{token}")
    check_broadcast_message(sockets, "PLAYER ROLLS;#{player}\n")
    :gen_udp.recv(socket, 0, @timeout) # ROLLED;<dice>
    Process.sleep(@timeout)
    check_player_lost_aftermath(player, sockets, "DID NOT ANNOUNCE")
  end

  test "First player rolls and announces" do
    {startmsg, sockets, ports} = setup_game()
    {1, [player1, player2 | _], [socket1, socket2 | _], [port1, port2 | _]} = extract_player_seq(startmsg, sockets, ports)
    {:ok, {_ip, _port, msg}} = :gen_udp.recv(socket1, 0, @timeout)
    [_, token] = Regex.run(~r/YOUR TURN;([0-9a-fA-F]{32})\n/, msg)
    :gen_udp.send(socket1, 'localhost', port1, "ROLL;#{token}")
    check_broadcast_message(sockets, "PLAYER ROLLS;#{player1}\n")
    {:ok, {_ip, _port, msg}} = :gen_udp.recv(socket1, 0, @timeout)
    [_, token] = Regex.run(~r/ROLLED;[1-6],[1-6];([0-9a-fA-F]{32})/, msg)
    MiaServer.Game.testing_inject_dice(MiaServer.Dice.new(3,1))
    :gen_udp.send(socket1, 'localhost', port1, "ANNOUNCE;3,1;#{token}")
    check_broadcast_message(sockets, "ANNOUNCED;#{player1};3,1\n")
    # Second players turn
    assert {:ok, {_ip, _port, msg}} = :gen_udp.recv(socket2, 0, @timeout)
    assert msg =~ ~r/YOUR TURN;([0-9a-fA-F]{32})\n/
    [_, token] = Regex.run(~r/YOUR TURN;([0-9a-fA-F]{32})\n/, msg)
    :gen_udp.send(socket2, 'localhost', port2, "ROLL;#{token}")
    check_broadcast_message(sockets, "PLAYER ROLLS;#{player2}\n")
    {:ok, {_ip, _port, msg}} = :gen_udp.recv(socket2, 0, @timeout)
    [_, token] = Regex.run(~r/ROLLED;[1-6],[1-6];([0-9a-fA-F]{32})/, msg)
    MiaServer.Game.testing_inject_dice(MiaServer.Dice.new(3,2))
    :gen_udp.send(socket2, 'localhost', port2, "ANNOUNCE;4,1;#{token}")
    check_broadcast_message(sockets, "ANNOUNCED;#{player2};4,1\n")
    # Again first players turn
    assert {:ok, {_ip, _port, msg}} = :gen_udp.recv(socket1, 0, @timeout)
    assert msg =~ ~r/YOUR TURN;([0-9a-fA-F]{32})\n/

  end

end

    end
  end

  defp send_to_server(msg, socket, port) do
    assert :ok = :gen_udp.send(socket, 'localhost', port, msg)
  end

  defp receive_message(socket) do
    assert {:ok, {_ip, _port, msg}} = :gen_udp.recv(socket, 0, @timeout)
    msg
  end

  defp check_and_gettoken(message, type) do
    regex = case type do
      :roundstarting -> ~r/ROUND STARTING;([0-9a-fA-F]{32})\n/
      :yourturn      -> ~r/YOUR TURN;([0-9a-fA-F]{32})\n/
      :rolled        -> ~r/ROLLED;[1-6],[1-6];([0-9a-fA-F]{32})\n/
    end
    assert message =~ regex
    [_, token] = Regex.run(regex, message)
    token
  end

  defp setup_game() do
    {socket1, port1} = open_udp_socket({127,0,0,1})
    {socket2, port2} = open_udp_socket({127,0,0,2})
    {socket3, port3} = open_udp_socket({127,0,0,3})
    send_and_recv("REGISTER;player1", socket1, port1)
    send_and_recv("REGISTER;player2", socket2, port2)
    send_and_recv("REGISTER_SPECTATOR", socket3, port3)
    token1 = receive_message(socket1) |> check_and_gettoken(:roundstarting)
    token2 = receive_message(socket2) |> check_and_gettoken(:roundstarting)
    "ROUND STARTING\n" = receive_message(socket3)
    send_to_server("JOIN;#{token1}", socket1, port1)
    send_to_server("JOIN;#{token2}", socket2, port2)
    receive_message(socket1)
    receive_message(socket2)
    start = receive_message(socket3)
    {start, [socket1, socket2, socket3], [port1, port2, port3]}
  end

