Elixir Game Costume Design Six

Source: Internet
Author: User

Next chapter, I built a new app to include the table model, Tableserver and so on. Table Table code is temporarily as follows, some states are useless

defmodule Table do @state_accept 0 #准备接入玩家 @state_ready1 #开局准备?defdelegate [Fetch (t, key), Get_and_update (t, key, list)], to:map defstruct [: config,: seats,: State]defNew (config) do%table{config:config, seats:%{}, state: @state_accept} enddefis_full? (table) do Cur_count=Enum.count (table.seats) Cur_count>=Table.config.allow_count EnddefHas_player? (table, player_id) do table.seats[player_id] Enddefcheck_in (table, player) do update_in (Table.seats,& (Map.put (&1, player.base_info.id, player))) Enddefcheck_out (table, player_id) do update_in (Table.seats,& (Map.delete (&1, player_id))) EndEnd

We need the relevant configuration Table_config.txt

ID,     allow_count,   base_gold,   need_gold,     descint,    int,           int,         int,        string 1,      4,             ten,          480,         Novice field 2,      4,,         4800,        Intermediate field 3,      4,        48000,       premium field

This txt can be generated by Excel through the Xslx2csv tool. Then we use Table_config.txt to generate code to configure Table_config.ex.

Of course we can generate directly from Excel files in Tableconfig, which is more convenient.

defmodule tableconfig do module.register_attribute__module__,: Column_names, [] Module.register_attribute__module__,: Column_types, [] Module.register_attribute__module__,: config, [] line_with_index= file.stream! (Path.join ([__dir__,"Table_config.txt"]), [],: line)|>Stream.with_index for{line, index} <-line_with_index Do items= Line |> String.Split (",") |> Stream.map (&string.strip (&1)) Case index do 0-@column_names Items|> Enum.map (&string.to_atom (&1))            1-@column_types Items|>Stream.with_index|> Enum.map (fn {v, i}{i, String.to_atom (v)} end)|>Io.inspect|> Enum.into (%{})            _ -New_items=Items|>Stream.with_index|> Stream.map (& (Typeconverter.convert (&1, @column_types)) ) Zip=Enum.zip (@column_names, New_items) @config enum.into (Zip,%{}) Io.inspect @config
# The following functions took me a bit of time and finally had to be done through the module properties, I don't know if there's any other way
# The early versions are the same
# config = enum.into (zip,%{})
# Def get (unquote (config.id)) do
# unquote (config) # here will be an error, baffled its solution, in the errormsg I was so used, no problem. I don't know what the difference is between 2 people.
# End
defGet (unquote (@config. id)) Do @config end end EndEnd

Last point test code TABLE_TEST.EXS

defmodule tabeltest do use Exunit.case#Import Pipeheresetup do config= Tableconfig.get (1) Table=table.new (config) {: OK, table:table} end Test"table is full", %{table:table} do new_table= 1.. Table.config.allow_count|> Stream.map (&AMP;PLAYER.NEW/1)        |> enum.reduce (table, FN p, acc-table.check_in (ACC, p) end)assertNew_table |>table.is_full? End Test"table has player", %{table:table} do p1= Player.new (1) P2= Player.new (2) new_table=table.check_in (Table, p1)assertTable.has_player? (new_table, p1.base_info.id) refute Table.has_player? (table, p2.base_info.id) end test"Table Check_in_and_out", %{table:table} do p1= Player.new (1) new_table=table.check_in (Table, p1) check_out_table=table.check_out (new_table, p1.base_info.id) refute Table.has_player? (Check_out_table, p1.base_info.id) endend

The next section starts with the hand, then Tableserver, then lets it run.

Elixir Game Costume Design Six

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.