wslでelixir その45

# 概要
wsl(wsl2じゃない)で、elixirやってみた。
練習問題、やってみた。

# 練習問題
neosvrで、小遣い帳アプリを操作せよ。

# 写真

![2023-08-23 01.39.33.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/31b167c9-9ae6-c844-02a5-335aa3633559.jpeg)


# サンプルコード

```
defmodule Kozu do
	def insert(a, b, c, d) do
		e = 0
		f = 0
		if c == "+" do
			{e, _} = Integer.parse(d)
			p = %Friends.Kozu{adate: Date.from_iso8601!(a), memo: b, inp: e, oup: f}
			Friends.Repo.insert(p)
		else
			{f, _} = Integer.parse(d)
			p = %Friends.Kozu{adate: Date.from_iso8601!(a), memo: b, inp: e, oup: f}
			Friends.Repo.insert(p)
		end
	end
	def update(n, a, b, c, d) do
		{id, _} = Integer.parse(n)
		p = Friends.Repo.get(Friends.Kozu, id)
		e = 0
		f = 0
		if c == "+" do
			{e, _} = Integer.parse(d)
			o = Ecto.Changeset.change(p, %{adate: Date.from_iso8601!(a), memo: b, inp: e, oup: f})
			Friends.Repo.update(o)
		else
			{f, _} = Integer.parse(d)
			o = Ecto.Changeset.change(p, %{adate: Date.from_iso8601!(a), memo: b, inp: e, oup: f})
			Friends.Repo.update(o)
		end
	end
	def delete(n) do
		{id, _} = Integer.parse(n)
		p = Friends.Repo.get(Friends.Kozu, id)
		Friends.Repo.delete(p)
	end
	def list() do
		import Ecto.Query, only: [order_by: 2]
		IO.puts(" id  date 内訳　貰ったお金　使ったお金　残金")
		Friends.Kozu
		|> Ecto.Query.order_by(asc: :adate)
		|> Friends.Repo.all()
		|> Enum.reduce(0, fn v, acc ->
			acc = v.inp - v.oup + acc
			IO.puts(" #{v.id}  #{v.adate}  #{v.memo}  #{v.inp}  #{v.oup}  #{acc}")
			acc
		end)
	end
	def run() do
		l = IO.gets("> ")
		l = String.replace(l, "\n", "")
		s = String.split(l, " ")
		cond do
		Enum.at(s, 0) == "in" ->
			Kozu.insert(Enum.at(s, 1), Enum.at(s, 2), Enum.at(s, 3), Enum.at(s, 4))
		Enum.at(s, 0) == "del" ->
			Kozu.delete(Enum.at(s, 1))
		Enum.at(s, 0) == "up" ->
			Kozu.update(Enum.at(s, 1), Enum.at(s, 2), Enum.at(s, 3), Enum.at(s, 4), Enum.at(s, 5))
		Enum.at(s, 0) == "disp" ->
			Kozu.list()
		true ->
			IO.inspect(l)
		end
		run()
	end
	def disp() do
		import Ecto.Query, only: [order_by: 2]
		Str.cls
		Str.add(" id  date 内訳　貰ったお金　使ったお金　残金\n")
		Friends.Kozu
		|> Ecto.Query.order_by(asc: :adate)
		|> Friends.Repo.all()
		|> Enum.reduce(0, fn v, acc ->
			acc = v.inp - v.oup + acc
			Str.add(" #{v.id}  #{v.adate}  #{v.memo}  #{v.inp}  #{v.oup}  #{acc}\n")
			acc
		end)
		Str.get
	end
	def say(l) do
		l = String.replace(l, "\n", "")
		s = String.split(l, " ")
		cond do
		Enum.at(s, 0) == "in" ->
			Kozu.insert(Enum.at(s, 1), Enum.at(s, 2), Enum.at(s, 3), Enum.at(s, 4))
			"ok"
		Enum.at(s, 0) == "del" ->
			Kozu.delete(Enum.at(s, 1))
			"ok"
		Enum.at(s, 0) == "up" ->
			Kozu.update(Enum.at(s, 1), Enum.at(s, 2), Enum.at(s, 3), Enum.at(s, 4), Enum.at(s, 5))
			"ok"
		Enum.at(s, 0) == "disp" ->
			Kozu.disp()
		true ->
			IO.inspect(l)
			"ng0"
		end
	end
end

defmodule Kozu.Server do
	alias ElixirSocketTesting.ClientAgent
	def start(port \\ 50002) do
		ClientAgent.start_link
		Str.start_link
		server = Socket.Web.listen! port
		loop(server)
	end
	def loop(server) do
		client = server
			|> Socket.Web.accept!
		client
		|> Socket.Web.accept!
		ClientAgent.add(client)
		Task.async(fn ->
			recv(client)
		end)
		loop(server)
	end
	def recv(client) do
		case client |> Socket.Web.recv! do
		{:text, message} ->
			res = Kozu.say(message)
			broadcast({:text, res})
			recv(client)
		{:close, _, _} ->
			ClientAgent.remove(client)
		other ->
			IO.inspect other
		end
	end
	def broadcast(packet) do
		ClientAgent.all
		|> Enum.each(fn client ->
			Socket.Web.send(client, packet)
		end)
	end
end


```

以上。