       MiaServer.Registry.increase_score(name)
     end
   end
-
   defp update_score(playerno, :won) do
     {_ip, _port, name} = MiaServer.Playerlist.get_participating_player(playerno)
     MiaServer.Registry.increase_score(name)

    %MiaServer.Game{:round => data.round+1}
  end

  defp player_won_aftermath(state, reason) do
    players = for pn <- 0..MiaServer.Playerlist.get_participating_number()-1, pn != state.playerno do
      {_i, _p, name} = MiaServer.Playerlist.get_participating_player(pn)
      name
    end
    broadcast_message("PLAYER LOST;" <> Enum.join(players,",") <> ";#{reason}")
    update_score(state.playerno, :won)
    get_scoremsg() |> broadcast_message()
    %MiaServer.Game{:round => state.round+1}
  end

  defp handle_see(state) do
    {_ip, _port, name} = MiaServer.Playerlist.get_participating_player(state.playerno)
    broadcast_message("PLAYER WANTS TO SEE;#{name}")
    cond do
      state.dice == nil ->
        player_lost_aftermath(state, "SEE BEFORE FIRST ROLL")
      MiaServer.Dice.higher?(state.announced, state.dice) ->
        player_lost_aftermath(%{state | :playerno => prev_playerno(state)}, "CAUGHT BLUFFING")
      true ->
        player_lost_aftermath(state, "SEE FAILED")
    end
  end

  defp handle_announce(data, d1, d2) do
    case MiaServer.Dice.new(d1, d2) do
      :invalid ->
        {:wait_for_registrations, player_lost_aftermath(data, "INVALID TURN"), :check_registry}
      announced_dice ->
        {_ip, _port, name} = MiaServer.Playerlist.get_participating_player(data.playerno)
        broadcast_message("ANNOUNCED;#{name};#{announced_dice}")
        cond do
          data.announced != nil and MiaServer.Dice.higher?(data.announced, announced_dice) ->
            {:wait_for_registrations, player_lost_aftermath(data, "ANNOUNCED LOSING DICE"), :check_registry}
          not MiaServer.Dice.mia?(data.dice) and MiaServer.Dice.mia?(announced_dice) ->
            {:wait_for_registrations, player_lost_aftermath(data, "LIED ABOUT MIA"), :check_registry}
          MiaServer.Dice.mia?(data.dice) and MiaServer.Dice.mia?(announced_dice) ->
            {:wait_for_registrations, player_won_aftermath(data, "MIA"), :check_registry}
          true ->
            {:round,
            %{data | :playerno => next_playerno(data), :announced => announced_dice},
            :send_your_turn}
        end
    end
  end

        data = handle_roll(data)
        {:next_state, :wait_for_announce, data, {:state_timeout, @timeout, :check_for_announcement}}
      {:state_timeout, :check_action, :round} when currentaction == nil ->
        data = player_lost(data, "DID NOT TAKE TURN")
        {:next_state, :wait_for_registrations, data, {:state_timeout, @timeout, :check_registry}}
      {:state_timeout, :check_for_announcement, :wait_for_announce} when currentaction == nil ->
        data = player_lost(data, "DID NOT ANNOUNCE")
        {:next_state, :wait_for_registrations, data, {:state_timeout, @timeout, :check_registry}}

      other -> Logger.warn "MiaGame got #{inspect other}, ignoring"; :keep_state_and_data
