Skip to content

Commit 8a7d26e

Browse files
committed
feat(elixir:gsmlg): Add Chess module back.
1 parent b79c50d commit 8a7d26e

File tree

6 files changed

+311
-4
lines changed

6 files changed

+311
-4
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
defmodule GSMLG.Chess.ChessPieces do
2+
3+
def types() do
4+
[
5+
0, # 帅
6+
1, # 士
7+
2, # 相
8+
3, # 马
9+
4, # 车
10+
5, # 炮
11+
6, # 兵
12+
]
13+
end
14+
15+
def redPieces() do
16+
[
17+
%{
18+
name: "车",
19+
type: 4,
20+
position: %{x: 0, y: 0}
21+
},
22+
%{
23+
name: "马",
24+
type: 3,
25+
position: %{x: 1, y: 0}
26+
},
27+
%{
28+
name: "相",
29+
type: 2,
30+
position: %{x: 2, y: 0}
31+
},
32+
%{
33+
name: "士",
34+
type: 1,
35+
position: %{x: 3, y: 0}
36+
},
37+
%{
38+
name: "帅",
39+
type: 0,
40+
position: %{x: 4, y: 0}
41+
},
42+
%{
43+
name: "士",
44+
type: 1,
45+
position: %{x: 5, y: 0}
46+
},
47+
%{
48+
name: "相",
49+
type: 2,
50+
position: %{x: 6, y: 0}
51+
},
52+
%{
53+
name: "马",
54+
type: 3,
55+
position: %{x: 7, y: 0}
56+
},
57+
%{
58+
name: "车",
59+
type: 4,
60+
position: %{x: 8, y: 0}
61+
},
62+
%{
63+
name: "炮",
64+
type: 5,
65+
position: %{x: 1, y: 2}
66+
},
67+
%{
68+
name: "炮",
69+
type: 5,
70+
position: %{x: 7, y: 2}
71+
},
72+
%{
73+
name: "兵",
74+
type: 6,
75+
position: %{x: 0, y: 3}
76+
},
77+
%{
78+
name: "兵",
79+
type: 6,
80+
position: %{x: 2, y: 3}
81+
},
82+
%{
83+
name: "兵",
84+
type: 6,
85+
position: %{x: 4, y: 3}
86+
},
87+
%{
88+
name: "兵",
89+
type: 6,
90+
position: %{x: 6, y: 3}
91+
},
92+
%{
93+
name: "兵",
94+
type: 6,
95+
position: %{x: 8, y: 3}
96+
},
97+
]
98+
end
99+
100+
def blackPieces() do
101+
[
102+
%{
103+
name: "車",
104+
type: 4,
105+
position: %{x: 0, y: 9}
106+
},
107+
%{
108+
name: "馬",
109+
type: 3,
110+
position: %{x: 1, y: 9}
111+
},
112+
%{
113+
name: "象",
114+
type: 2,
115+
position: %{x: 2, y: 9}
116+
},
117+
%{
118+
name: "仕",
119+
type: 1,
120+
position: %{x: 3, y: 9}
121+
},
122+
%{
123+
name: "将",
124+
type: 0,
125+
position: %{x: 4, y: 9}
126+
},
127+
%{
128+
name: "仕",
129+
type: 1,
130+
position: %{x: 5, y: 9}
131+
},
132+
%{
133+
name: "象",
134+
type: 2,
135+
position: %{x: 6, y: 9}
136+
},
137+
%{
138+
name: "馬",
139+
type: 3,
140+
position: %{x: 7, y: 9}
141+
},
142+
%{
143+
name: "車",
144+
type: 4,
145+
position: %{x: 8, y: 9}
146+
},
147+
%{
148+
name: "砲",
149+
type: 5,
150+
position: %{x: 1, y: 7}
151+
},
152+
%{
153+
name: "砲",
154+
type: 5,
155+
position: %{x: 7, y: 7}
156+
},
157+
%{
158+
name: "卒",
159+
type: 6,
160+
position: %{x: 0, y: 6}
161+
},
162+
%{
163+
name: "卒",
164+
type: 6,
165+
position: %{x: 2, y: 6}
166+
},
167+
%{
168+
name: "卒",
169+
type: 6,
170+
position: %{x: 4, y: 6}
171+
},
172+
%{
173+
name: "卒",
174+
type: 6,
175+
position: %{x: 6, y: 6}
176+
},
177+
%{
178+
name: "卒",
179+
type: 6,
180+
position: %{x: 8, y: 6}
181+
},
182+
]
183+
end
184+
185+
def init_pieces() do
186+
red = redPieces() |> Enum.with_index |> Enum.map(fn({v, k}) -> v |> Map.put(:id, "r#{k}") |> Map.put(:color, "red") |> Map.put(:live, true) end)
187+
black = blackPieces() |> Enum.with_index |> Enum.map(fn({v, k}) -> v |> Map.put(:id, "b#{k}") |> Map.put(:color, "black") |> Map.put(:live, true) end)
188+
red ++ black
189+
end
190+
191+
end
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
defmodule GSMLG.Chess.Room do
2+
use GenServer
3+
alias GSMLG.Chess.ChessPieces
4+
5+
def start_link() do
6+
GenServer.start_link(__MODULE__, nil, name: __MODULE__)
7+
end
8+
9+
def start_room() do
10+
GenServer.call(__MODULE__, :start_room)
11+
end
12+
13+
def get_state() do
14+
GenServer.call(__MODULE__, :get_state)
15+
end
16+
17+
def move_chess(payload) do
18+
GenServer.call(__MODULE__, {:move_chess, payload})
19+
end
20+
21+
def init(_) do
22+
state = %{start?: false, done?: false, pieces: [], turn: nil}
23+
{:ok, state}
24+
end
25+
26+
def handle_call(:start_room, _from, state) do
27+
pieces = ChessPieces.init_pieces
28+
newState = state
29+
|> Map.put(:pieces, pieces)
30+
|> Map.put(:start?, true)
31+
|> Map.put(:turn, "red")
32+
{:reply, {:ok, pieces}, newState}
33+
end
34+
def handle_call(:get_state, _from, state) do
35+
{:reply, {:ok, state}, state}
36+
end
37+
def handle_call({:move_chess, %{"item" => %{"id" => id, "color" => color}, "position" => %{"x" => x, "y" => y}}}, _from, state) do
38+
pieces = state
39+
|> Map.fetch!(:pieces)
40+
|> Enum.map(fn(p) ->
41+
case p do
42+
%{id: ^id} -> Map.put(p, :position, %{x: x, y: y})
43+
%{position: %{x: ^x, y: ^y}} -> Map.put(p, :live, false)
44+
_ -> p
45+
end
46+
end)
47+
newState = state |> Map.put(:pieces, pieces) |> Map.put(:turn, if(color == "red", do: "black", else: "red"))
48+
{:reply, {:ok, pieces}, newState}
49+
end
50+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
defmodule GSMLG.Chess.Supervisor do
2+
use Supervisor
3+
alias GSMLG.Chess.Room
4+
5+
def start_link() do
6+
Supervisor.start_link(__MODULE__, :ok);
7+
end
8+
9+
def init(_) do
10+
children = [
11+
worker(Room, []),
12+
]
13+
supervise(children, strategy: :one_for_one)
14+
end
15+
16+
end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
defmodule GSMLGWeb.ChessChannel do
2+
use Phoenix.Channel
3+
alias Guardian.Phoenix.Socket
4+
alias Phoenix.Socket.Broadcast
5+
alias GSMLG.Chess.Room
6+
7+
def join("room:chess", msg, socket) do
8+
send(self(), {:after_join, msg})
9+
{:ok, socket}
10+
end
11+
12+
def handle_info({:after_join, _msg}, socket) do
13+
{:ok, %{pieces: pieces, turn: turn}} = Room.get_state
14+
push socket, "init_pieces", %{pieces: pieces, turn: turn}
15+
{:noreply, socket}
16+
end
17+
18+
def handle_info(%Broadcast{topic: _, event: event, payload: payload}, socket) do
19+
push socket, event, payload
20+
{:noreply, socket}
21+
end
22+
23+
def terminate(_reason, socket) do
24+
Socket.current_resource(socket)
25+
:ok
26+
end
27+
28+
def handle_in("start", _payload, socket) do
29+
{:ok, _} = Room.start_room
30+
{:ok, %{pieces: pieces, turn: turn}} = Room.get_state
31+
push socket, "init_pieces", %{pieces: pieces, turn: turn}
32+
broadcast!(socket, "init_pieces", %{pieces: pieces, turn: turn})
33+
{:noreply, socket}
34+
end
35+
def handle_in("move_chess", payload, socket) do
36+
{:ok, pieces} = Room.move_chess(payload)
37+
broadcast!(socket, "move_chess_remote", Map.put(payload, :pieces, pieces))
38+
{:noreply, socket}
39+
end
40+
def handle_in("new:msg", _msg, socket) do
41+
{:noreply, socket}
42+
end
43+
end

elixir/gsmlg_umbrella/apps/gsmlg_web/lib/gsmlg_web/channels/user_socket.ex

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ defmodule GSMLGWeb.UserSocket do
88

99
## Channels
1010

11-
channel "node:*", GSMLGWeb.NodeChannel
11+
channel "node:lobby", GSMLGWeb.NodeChannel
12+
channel "room:chess", GSMLGWeb.ChessChannel
1213

1314
# Socket params are passed from the client and can
1415
# be used to verify and authenticate a user. After
@@ -22,7 +23,13 @@ defmodule GSMLGWeb.UserSocket do
2223
# See `Phoenix.Token` documentation for examples in
2324
# performing token verification on connect.
2425
@impl true
25-
def connect(_params, socket, _connect_info) do
26+
def connect(params, socket, connect_info) do
27+
IO.puts "Params: "
28+
IO.inspect params
29+
IO.puts "Socket: "
30+
IO.inspect socket
31+
IO.puts "Connect Info: "
32+
IO.inspect connect_info
2633
{:ok, socket}
2734
end
2835

elixir/gsmlg_umbrella/apps/gsmlg_web/lib/gsmlg_web/controllers/page_controller.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ defmodule GSMLGWeb.PageController do
2929
path = if path == "/", do: "/index", else: path
3030

3131
cond do
32-
File.exists?(file_path = Path.join(:code.priv_dir(:gsmlg_web), "static" <> path <> ".html")) ->
32+
File.exists?(file_path = Path.join(:code.priv_dir(:gsmlg_web), "static", path <> ".html")) ->
3333
{:html, file_path}
3434

3535
File.exists?(
36-
file_path = Path.join(:code.priv_dir(:gsmlg_web), "static" <> path <> "/index.html")
36+
file_path = Path.join(:code.priv_dir(:gsmlg_web), "static", path, "/index.html")
3737
) ->
3838
{:html, file_path}
3939

0 commit comments

Comments
 (0)