It's my turn to process the table. The table process throws away the message and is basically a call to the table.
Meaningless test drive first or write code first, anyway, how to do the smooth.
defmodule tableserver Do use Genserver, restart:: temporary, start: {__module__,: Start_link, []}defStart_link (table) do Genserver.start_link (__module__, table, name:register_name (table)) Enddefinit (table) do {: OK, table} enddefRegister_name (%{} = table), Do:register_name (table |>simpletable.get_id)defregister_name (ID), do: {: Via, Registry, {localregistry, {Table, id}}}defexist? (table) do key= {table, table |>simpletable.get_id} case registry.lookup (Localregistry, key) do [{_pid, _}]-true []-false End EnddefCreate (player) do table=Simpletable.init|> simpletable.set_id (Player |>player.get_id)|>simpletable.set_creator (player)|>simpletable.add_seat (player) tablesupervisor.start_table (table) EnddefJoin (table, player), do:GenServer.cast (table, {: Join, player:player})defQuit (table, player), do:GenServer.cast (table, {: Quit, player:player})defDismiss (table, player), do:GenServer.cast (table, {:d Ismiss, player:player})defStart (table, player), do:GenServer.cast (table, {: Start, player:player})defOpen (table, player), do:GenServer.cast (table, {: Open, player:player})defMakeup (table, player), do:GenServer.cast (table, {: makeup, player:player})defhandle_cast (request, table) do {: OK, table}=inner_handle_cast (request, table) {: noreply, table} enddefSend_error (_player, _error) do enddefinner_handle_cast ({: Join, player:player}, table) do with {: OK, table}<-Table |>simpletable.join (player) do seat=simpletable.find_seat (table, player) broadcast_join (table, seat)Else{: error, error}-send_error (player, error) end {: OK, table} enddefInner_handle_cast ({: Quit, player:player}, table) do with {: OK, table}<-Table |>simpletable.quit (player) do broadcast_quit (table, player)Else{: error, error}-send_error (player, error) end {: OK, table} enddefinner_handle_cast ({:d Ismiss, player:player}, table) do with {: OK, table}<-Table |>Simpletable.dismiss (player) do broadcast_dismiss (table)Else{: error, error}-send_error (player, error) end {: OK, table} enddefinner_handle_cast ({: Start, player:player}, table) do with {: OK, table}<-Table |>Simpletable.start (player) do broadcast_start (table)Else{: error, error}-send_error (player, error) end {: OK, table} enddefinner_handle_cast ({: Open, player:player}, table) do with {: OK, table}<-Table |>Simpletable.open (player) do send_open (table, player)Else{: error, error}-send_error (player, error) end {: OK, table} enddefinner_handle_cast ({: Makeup, player:player}, table) do with {: OK, table}<-Table |>simpletable.make_up (player) do send_makeup (table, player)Else{: error, error}-send_error (player, error) end {: OK, table} enddefBroadcast_join (_table, _seat) do enddefbroadcast_quit (_table, _player) do enddefBroadcast_dismiss (_table) do enddefBroadcast_start (_table) do enddefSend_open (_table, _player) do enddefsend_makeup (_table, _player) do endend
Table_server.ex
Although Table_server is very simple, I still spend some time on it.
The following questions are mainly considered:
1. To simplify the API interface with the Exactor library
Later no use, Exactor is still suitable for the use of fast-error mode, while the game we usually want to try catch, if you want to use, need to wrap Exactor macros, trouble.
Of course, if the table is stored in the ETS, it can be more convenient crash recovery, perhaps this is more suitable for use exactor.
2. Inner_handle_cast should be what kind of interface to facilitate the modification
Want to go, with {cmd, keyword_list} more convenient, intuitive and easy to modify
3. How to send messages easily and intuitively
The first attempt is to broadcast_table, and the temptation is to invoke the interface whenever a message is sent.
But there are 2 flaws in the apparent sense of
One is too large (the result is that there are different branches in the function, for example)
An inability to visualize the specific impact of each operation
So finally change to, need to send what, get an API to send what, this has broadcast_join broadcast_quit and so on,
The feeling is clear a lot, natural many, and the granularity is small sends the information also to be few.
Next, add the relevant tests and code.
Simple Elixir Game suit design-Enrich table process