简单Elixir游戏服设计- TableServer的测试

怀念了产,TableServer还是来硌东西而测的,至少我们得测试API接口是否正常。

还起另的有些改变, seat.ex  simple_poker.ex

 

自身就当博客里生描绘了方案从excel直接扭转

代码已经付出。

测试果然发现用调,分解测试或要小心。

达成磨道到,游戏逻辑交由Table去测试了,TableServer似乎没有什么而测的了。

(不过本休建议了,该方案依赖的excel处理库不敷好, 建议改成为csv,再起csv生成重好)

API并增加又上平等叠的API供统一行使或者会见另行好点,这样毫无每个需要之地方还开。

不当信息呢足以由表文件生成,只是系列本身要演示基础服务器出,因此即使未更加了。

另外顺便取一下,如果在一个节点才所以1单Registry提供Player/Table等多注册的话,最好分装几独Global注册之

defmodule SimpleTable do
    @state_ready  :ready
    @state_playing :playing
    @state_dismiss :dismiss
    def init() do
        %{
            id: 0,
            cards: nil,
            creator: nil,
            seat_map: %{},
            seat_order: [],
            state: @state_ready
        }
    end

    def is_playing?(table), do: table.state == @state_playing
    def is_dismiss?(table), do: table.state == @state_dismiss
    def is_ready?(table), do: table.state == @state_ready

    def set_playing(table), do: put_in(table.state, @state_playing)
    def set_ready(table), do: put_in(table.state, @state_ready)
    def set_dismiss(table), do: put_in(table.state, @state_dismiss)


    def set_cards(table, cards), do: put_in(table.cards, cards)
    def get_cards(table), do: table.cards

    def init_deal(table) do
        table.seat_order
        |> Enum.map(&(find_seat(table, &1)))
        |> Enum.reduce(table, 
            fn seat, new_table ->
                new_table |> init_deal_one(seat)
            end)
    end

    def init_deal_one(table, seat) do
        {:ok, cards, left} = SimplePoker.init_deal(table.cards)
        seat = seat |> Seat.add_cards(cards)
        table |> update_seat(seat)
              |> set_cards(left)
    end

    def set_id(table, id), do: put_in(table.id, id)
    def get_id(table), do: table.id

    def set_creator(table, player), do: put_in(table.creator, player)
    def get_creator(table), do: table.creator

    def seat_count(table), do: table.seat_order |> Enum.count
    def seat_order(table), do: table.seat_order

    def find_seat(table, %{} = player), do: find_seat(table, player |> Player.get_id)
    def find_seat(table, player_id), do: table.seat_map[player_id]


    def add_seat(table, player) do
        seat = Seat.init(player)
        seat_id = seat |> Seat.get_id
        table = table |> update_seat(seat)
        add_to_order(table, seat_id)
    end

    def update_seat(table, seat), do: put_in(table.seat_map[seat |> Seat.get_id], seat)

    def add_to_order(table, seat_id), do: update_in(table.seat_order, &(&1 ++ [seat_id]))

    def remove_seat(table, %{} = player), do: remove_seat(table, player |> Player.get_id)
    def remove_seat(table, player_id) do
        table = update_in(table.seat_map, fn m -> Map.delete(m, player_id) end)
        update_in(table.seat_order, fn o -> List.delete(o, player_id) end)
    end

    def start(table, player) do
        cond do
            is_playing?(table) -> {:error, ErrorMsg.can_not_start_when_playing}
            seat_count(table) < 2 -> {:error, ErrorMsg.player_not_enough}
            not is_creator?(table, player) -> {:error, ErrorMsg.just_creator_can_start}
            true ->
                table = table |> set_playing
                {:ok, table}
        end
    end

    def quit(table, player) do
        cond do
            is_playing?(table) -> {:error, ErrorMsg.can_not_quit_when_playing}
            is_creator?(table, player) -> {:error, ErrorMsg.can_not_quit_when_creator}
        end
    end

    def dismiss(table, player) do
        cond do
            is_playing?(table) -> {:error, ErrorMsg.can_not_dismiss_when_playing}
            not is_creator?(table, player) -> {:error, ErrorMsg.just_creator_can_dismiss}
            true ->
                table = table |> set_dismiss
                {:ok, table}
        end
    end

    def make_up(table, player) do
        cond do
            is_ready?(table) -> {:error, ErrorMsg.can_not_make_up_when_not_playing}
            find_seat(table, player) |> Seat.is_open? -> {:error, ErrorMsg.can_not_make_up_when_open}
            find_seat(table, player) |> Seat.is_full? -> {:error, ErrorMsg.can_not_make_up_when_full}
        end
    end

    def join(table, player) do
        cond do
            is_playing?(table) -> {:error, ErrorMsg.can_not_join_when_playing}
            find_seat(table, player) -> {:error, ErrorMsg.repeated_join}
            true -> 
                table = table |> add_seat(player)
                {:ok, table}
        end
    end

    def open(table, player) do
        cond do
            find_seat(table, player) |> Seat.is_open? -> {:error, ErrorMsg.repeated_open}
            not (find_seat(table, player) |> Seat.get_cards |> SimplePoker.can_be_tian_gong?) -> {:error, ErrorMsg.just_tian_gong_can_open}
        end
    end


    def is_creator?(table, player), do: table.creator |> Player.get_id == player |> Player.get_id

end

因而现在补了点TableServer的测试。代码也开了点修改。

simple_table.ex

即时一部分我们处理,因为其实不难。

error_msg.ex  使用了点宏

simple_table.ex 新增的假设测试通过的代码

(废了接触时间,一致在尝抹初那段for,想一直定义个工具宏, 由于生了未曾能够好快成,

好以发测试的语,很易就意识问题。这大概是测试的补了吧。

除此以外,看代码 join 和 quit 的cond 语词少了true, 显然说明我们的测试还从来不挂至!!!!

测试使代码更便于编写,代码又助发现测试不足。相辅相成。

defmodule ErrorMsg do
    @msgs %{
        player_not_enough: "player_not_enough",
        can_not_start_when_playing: "can_not_start_when_playing",
        can_not_dismiss_when_playing: "can_not_dismiss_when_playing",
        just_creator_can_start: "just_creator_can_start",
        just_creator_can_dismiss: "just_creator_can_dismiss",
        can_not_join_when_playing: "can_not_join_when_playing",
        repeated_join: "repeated_join",
        can_not_quit_when_playing: "can_not_quit_when_playing",
        can_not_quit_when_creator: "can_not_quit_when_creator",
        can_not_make_up_when_not_playing: "can_not_makeup_when_not_playing",
        can_not_make_up_when_open: "can_not_make_up_when_open",
        can_not_make_up_when_full: "can_not_make_up_when_full",
        just_tian_gong_can_open: "just_tian_gong_can_open",
        repeated_open: "repeated_open"
    }

    for {tag, text} <- @msgs do
        def unquote(tag)() do
            unquote text
        end
    end


end

果,写代码比写测试快得差不多, 看在测试一个一个经,还是那个享受的。

好以for的代码也杀简单,而实质上从csv生成的口舌,也是只要举行巡回工作,算是安慰)

具体看git吧

error_msg.ex