TOP

简单Elixir游戏服设计-使table测试通过(一)
2017-10-16 18:18:42 】 浏览:6091
Tags:简单 Elixir 游戏 设计 table 测试 通过

error_msg.ex  使用了点宏

(废了点时间,一致在尝试抹初那段for,想直接定义个工具宏, 由于生疏了没能很快成功,

好在for的代码也很简短,而实际上从csv生成的话,也是要做循环工作,算是安慰)

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
error_msg.ex

错误消息也可以从外部文件生成,只是系列本身主要演示基础服务器开发,因此就不进一步了。

我曾经在博客里有写过方案从excel直接生成

(不过现在不建议了,该方案依赖的excel处理库不够好, 建议转成csv,再从csv生成更好)

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

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, &  
		
简单Elixir游戏服设计-使table测试通过(一) https://www.cppentry.com/bencandy.php?fid=88&id=164833

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇java线程锁基础 下一篇简单Elixir游戏服设计-测试驱动?

kafka-
kafka   Partit
解决android studio
Kafka史上最详细原理
Error while fetchin
【Kafka】安装与快速
    &
flume读取日志数据写
Authentication plug
Flume 自定义source
flume   三大核
ICC副本>>>
愚公移山  
Hbase架构   Hb
5 hbase-shell + &
Hbase   MapRed
MetaException(messa
Exception in thread
HIVE metastore Dupl
-->