     assert invites == ["ROUND STARTING\n"]
   end
 
+  defp make_roll_msg(token), do: "ROLL;#{token}"
+  defp make_nonsense_msg(token), do: "BLABLA;#{token}"
+  defp make_announcement_msg(token, d1, d2), do: "ANNOUNCE;#{d1},#{d2};#{token}"
+
   test "First player receives Your Turn" do
     {startmsg, sockets, ports} = setup_game()
     {turn, _players, [socket | _], _ports} = extract_player_seq(startmsg, sockets, ports)
     assert turn == 1
-    assert {:ok, {_ip, _port, msg}} = :gen_udp.recv(socket, 0, @timeout)
-    assert msg =~ ~r/YOUR TURN;([0-9a-fA-F]{32})\n/
+    socket
+      |> receive_message()
+      |> check_and_gettoken(:yourturn)
   end
 
   test "First player rolls and receives dice" 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}")
+    socket
+      |> receive_message()
+      |> check_and_gettoken(:yourturn)
+      |> make_roll_msg()
+      |> send_to_server(socket, port)
     check_broadcast_message(sockets, "PLAYER ROLLS;#{player}\n")
-    {:ok, {_ip, _port, msg}} = :gen_udp.recv(socket, 0, @timeout)
-    assert msg =~ ~r/ROLLED;[1-6],[1-6];[0-9a-fA-F]{32}/
+    socket
+      |> receive_message()
+      |> check_and_gettoken(:rolled)
   end
 
   test "First player does nothing" do
     {startmsg, sockets, ports} = setup_game()
     {1, [player | _], [socket | _], _} = extract_player_seq(startmsg, sockets, ports)
-    :gen_udp.recv(socket, 0, @timeout)
+    receive_message(socket)
     Process.sleep(@timeout)
     check_player_lost_aftermath(player, sockets, "DID NOT TAKE TURN")
   end

    end
  end

  defp setup_game() do
    token = ~r/ROUND STARTING;([0-9a-fA-F]{32})\n/
    {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(socket1, port1, "REGISTER;player1")
    send_and_recv(socket2, port2, "REGISTER;player2")
    send_and_recv(socket3, port3, "REGISTER_SPECTATOR")
    {:ok, {_ip, _port, invitation1}} = :gen_udp.recv(socket1, 0, @timeout)
    {:ok, {_ip, _port, invitation2}} = :gen_udp.recv(socket2, 0, @timeout)
    {:ok, {_ip, _port, "ROUND STARTING\n"}} = :gen_udp.recv(socket3, 0, @timeout)
    [^invitation1, mytoken1] = Regex.run(token, invitation1)
    [^invitation2, mytoken2] = Regex.run(token, invitation2)
    :gen_udp.send(socket1, 'localhost', port1, "JOIN;"<>mytoken1)
    :gen_udp.send(socket2, 'localhost', port2, "JOIN;"<>mytoken2)
    {:ok, {_ip, _port, _start1}} = :gen_udp.recv(socket1, 0, @timeout)
    {:ok, {_ip, _port, _start2}} = :gen_udp.recv(socket2, 0, @timeout)
    {:ok, {_i, _p, start}} = :gen_udp.recv(socket3, 0, @timeout)
    {start, [socket1, socket2, socket3], [port1, port2, port3]}
  end


    {socket, port}
  end

  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
