经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Erlang » 查看文章
简单Elixir游戏服设计-使table测试通过
来源:cnblogs  作者:damnit  时间:2018/9/25 20:32:10  对本文有异议

error_msg.ex  使用了点宏

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

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

  1. defmodule ErrorMsg do
  2. @msgs %{
  3. player_not_enough: "player_not_enough",
  4. can_not_start_when_playing: "can_not_start_when_playing",
  5. can_not_dismiss_when_playing: "can_not_dismiss_when_playing",
  6. just_creator_can_start: "just_creator_can_start",
  7. just_creator_can_dismiss: "just_creator_can_dismiss",
  8. can_not_join_when_playing: "can_not_join_when_playing",
  9. repeated_join: "repeated_join",
  10. can_not_quit_when_playing: "can_not_quit_when_playing",
  11. can_not_quit_when_creator: "can_not_quit_when_creator",
  12. can_not_make_up_when_not_playing: "can_not_makeup_when_not_playing",
  13. can_not_make_up_when_open: "can_not_make_up_when_open",
  14. can_not_make_up_when_full: "can_not_make_up_when_full",
  15. just_tian_gong_can_open: "just_tian_gong_can_open",
  16. repeated_open: "repeated_open"
  17. }
  18. for {tag, text} <- @msgs do
  19. def unquote(tag)() do
  20. unquote text
  21. end
  22. end
  23. end
error_msg.ex

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

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

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

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

  1. defmodule SimpleTable do
  2. @state_ready :ready
  3. @state_playing :playing
  4. @state_dismiss :dismiss
  5. def init() do
  6. %{
  7. id: 0,
  8. cards: nil,
  9. creator: nil,
  10. seat_map: %{},
  11. seat_order: [],
  12. state: @state_ready
  13. }
  14. end
  15. def is_playing?(table), do: table.state == @state_playing
  16. def is_dismiss?(table), do: table.state == @state_dismiss
  17. def is_ready?(table), do: table.state == @state_ready
  18. def set_playing(table), do: put_in(table.state, @state_playing)
  19. def set_ready(table), do: put_in(table.state, @state_ready)
  20. def set_dismiss(table), do: put_in(table.state, @state_dismiss)
  21. def set_cards(table, cards), do: put_in(table.cards, cards)
  22. def get_cards(table), do: table.cards
  23. def init_deal(table) do
  24. table.seat_order
  25. |> Enum.map(&(find_seat(table, &1)))
  26. |> Enum.reduce(table,
  27. fn seat, new_table ->
  28. new_table |> init_deal_one(seat)
  29. end)
  30. end
  31. def init_deal_one(table, seat) do
  32. {:ok, cards, left} = SimplePoker.init_deal(table.cards)
  33. seat = seat |> Seat.add_cards(cards)
  34. table |> update_seat(seat)
  35. |> set_cards(left)
  36. end
  37. def set_id(table, id), do: put_in(table.id, id)
  38. def get_id(table), do: table.id
  39. def set_creator(table, player), do: put_in(table.creator, player)
  40. def get_creator(table), do: table.creator
  41. def seat_count(table), do: table.seat_order |> Enum.count
  42. def seat_order(table), do: table.seat_order
  43. def find_seat(table, %{} = player), do: find_seat(table, player |> Player.get_id)
  44. def find_seat(table, player_id), do: table.seat_map[player_id]
  45. def add_seat(table, player) do
  46. seat = Seat.init(player)
  47. seat_id = seat |> Seat.get_id
  48. table = table |> update_seat(seat)
  49. add_to_order(table, seat_id)
  50. end
  51. def update_seat(table, seat), do: put_in(table.seat_map[seat |> Seat.get_id], seat)
  52. def add_to_order(table, seat_id), do: update_in(table.seat_order, &(&1 ++ [seat_id]))
  53. def remove_seat(table, %{} = player), do: remove_seat(table, player |> Player.get_id)
  54. def remove_seat(table, player_id) do
  55. table = update_in(table.seat_map, fn m -> Map.delete(m, player_id) end)
  56. update_in(table.seat_order, fn o -> List.delete(o, player_id) end)
  57. end
  58. def start(table, player) do
  59. cond do
  60. is_playing?(table) -> {:error, ErrorMsg.can_not_start_when_playing}
  61. seat_count(table) < 2 -> {:error, ErrorMsg.player_not_enough}
  62. not is_creator?(table, player) -> {:error, ErrorMsg.just_creator_can_start}
  63. true ->
  64. table = table |> set_playing
  65. {:ok, table}
  66. end
  67. end
  68. def quit(table, player) do
  69. cond do
  70. is_playing?(table) -> {:error, ErrorMsg.can_not_quit_when_playing}
  71. is_creator?(table, player) -> {:error, ErrorMsg.can_not_quit_when_creator}
  72. end
  73. end
  74. def dismiss(table, player) do
  75. cond do
  76. is_playing?(table) -> {:error, ErrorMsg.can_not_dismiss_when_playing}
  77. not is_creator?(table, player) -> {:error, ErrorMsg.just_creator_can_dismiss}
  78. true ->
  79. table = table |> set_dismiss
  80. {:ok, table}
  81. end
  82. end
  83. def make_up(table, player) do
  84. cond do
  85. is_ready?(table) -> {:error, ErrorMsg.can_not_make_up_when_not_playing}
  86. find_seat(table, player) |> Seat.is_open? -> {:error, ErrorMsg.can_not_make_up_when_open}
  87. find_seat(table, player) |> Seat.is_full? -> {:error, ErrorMsg.can_not_make_up_when_full}
  88. end
  89. end
  90. def join(table, player) do
  91. cond do
  92. is_playing?(table) -> {:error, ErrorMsg.can_not_join_when_playing}
  93. find_seat(table, player) -> {:error, ErrorMsg.repeated_join}
  94. true ->
  95. table = table |> add_seat(player)
  96. {:ok, table}
  97. end
  98. end
  99. def open(table, player) do
  100. cond do
  101. find_seat(table, player) |> Seat.is_open? -> {:error, ErrorMsg.repeated_open}
  102. not (find_seat(table, player) |> Seat.get_cards |> SimplePoker.can_be_tian_gong?) -> {:error, ErrorMsg.just_tian_gong_can_open}
  103. end
  104. end
  105. def is_creator?(table, player), do: table.creator |> Player.get_id == player |> Player.get_id
  106. end
simple_table.ex

测试果然发现需要调整,分解测试还是需要小心。

好在有测试的话,很容易就发现问题。这大概是测试的好处了吧。

还有其他的小改动, seat.ex  simple_poker.ex

具体看git吧

果然,写代码比写测试快得多, 看着测试一个一个通过,还是挺享受的。

另外,看代码 join 和 quit 的cond 语句少了true, 显然说明我们的测试还没覆盖到!!!!

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号