@@ -24,55 +24,79 @@ defmodule Get5ApiWeb.MatchLive.Show do
2424 match = socket . assigns . entity || Matches . get_match! ( id )
2525 map_stats = Stats . get_by_match ( id )
2626 player_stats = Stats . player_stats_by_match ( id )
27+ game_server = socket . assigns . entity . game_server
2728
2829 { :noreply ,
2930 socket
3031 |> assign ( :page_title , page_title ( socket . assigns . live_action ) )
3132 |> assign ( :match , match )
3233 |> assign ( :map_stats , map_stats )
3334 |> assign ( :player_stats , player_stats )
34- |> start_async ( :get_status , fn -> Get5Client . status ( socket . assigns . entity . game_server ) end ) }
35+ |> start_async ( :get_status , fn -> Get5Client . status ( game_server ) end ) }
3536 end
3637
3738 @ impl true
3839 def handle_event ( "get_status" , _params , socket ) do
40+ game_server = socket . assigns . match . game_server
41+
3942 { :noreply ,
4043 socket
4144 |> assign ( status: AsyncResult . loading ( ) )
42- |> start_async ( :get_status , fn -> Get5Client . status ( socket . assigns . match . game_server ) end ) }
45+ |> start_async ( :get_status , fn -> Get5Client . status ( game_server ) end ) }
4346 end
4447
4548 @ impl true
4649 def handle_event ( "start_match" , _params , socket ) do
4750 if socket . assigns . match . user_id == socket . assigns . current_user . id do
51+ match = socket . assigns . match
52+
4853 { :noreply ,
4954 socket
50- |> start_async ( :start_match , fn -> Get5Client . start_match ( socket . assigns . match ) end ) }
55+ |> start_async ( :start_match , fn -> Get5Client . start_match ( match ) end ) }
5156 else
5257 { :noreply ,
5358 socket
5459 |> put_flash ( :error , gettext ( "You are not allowed to start this match" ) ) }
5560 end
5661 end
5762
63+ @ impl true
64+ def handle_event ( "end_match" , _params , socket ) do
65+ if socket . assigns . match . user_id == socket . assigns . current_user . id do
66+ match = socket . assigns . match
67+
68+ { :noreply ,
69+ socket
70+ |> start_async ( :end_match , fn -> Get5Client . end_match ( match ) end ) }
71+ else
72+ { :noreply ,
73+ socket
74+ |> put_flash ( :error , gettext ( "You are not allowed to end this match" ) ) }
75+ end
76+ end
77+
5878 @ impl true
5979 def handle_async ( :start_match , { :ok , { :ok , _resp } } , socket ) do
80+ game_server = socket . assigns . match . game_server
81+
6082 { :noreply ,
6183 socket
6284 |> start_async ( :get_status , fn ->
63- Get5Client . status ( socket . assigns . match . game_server )
85+ Get5Client . status ( game_server )
6486 end )
6587 |> put_flash ( :info , gettext ( "Match sendt to server" ) ) }
6688 end
6789
6890 @ impl true
6991 def handle_async ( :start_match , { :ok , { :error , error } } , socket ) do
92+ game_server = socket . assigns . match . game_server
93+
7094 case error do
7195 :nxdomain ->
7296 { :noreply ,
7397 socket
7498 |> start_async ( :get_status , fn ->
75- Get5Client . status ( socket . assigns . match . game_server )
99+ Get5Client . status ( game_server )
76100 end )
77101 |> put_flash (
78102 :error ,
@@ -85,42 +109,31 @@ defmodule Get5ApiWeb.MatchLive.Show do
85109 { :noreply ,
86110 socket
87111 |> start_async ( :get_status , fn ->
88- Get5Client . status ( socket . assigns . match . game_server )
112+ Get5Client . status ( game_server )
89113 end )
90114 |> put_flash (
91115 :error ,
92116 gettext ( "A match is already loaded on the server" )
93117 ) }
94118
95- err ->
119+ _err ->
96120 { :noreply ,
97121 socket
98122 |> start_async ( :get_status , fn ->
99- Get5Client . status ( socket . assigns . match . game_server )
123+ Get5Client . status ( game_server )
100124 end )
101125 |> put_flash ( :error , gettext ( "Failed to start match" ) ) }
102126 end
103127 end
104128
105129 @ impl true
106- def handle_event ( "end_match" , _params , socket ) do
107- if socket . assigns . match . user_id == socket . assigns . current_user . id do
108- { :noreply ,
109- socket
110- |> start_async ( :end_match , fn -> Get5Client . end_match ( socket . assigns . match ) end ) }
111- else
112- { :noreply ,
113- socket
114- |> put_flash ( :error , gettext ( "You are not allowed to end this match" ) ) }
115- end
116- end
130+ def handle_async ( :end_match , { :ok , { :ok , _msg } } , socket ) do
131+ game_server = socket . assigns . match . game_server
117132
118- @ impl true
119- def handle_async ( :end_match , { :ok , { :ok , msg } } , socket ) do
120133 { :noreply ,
121134 socket
122135 |> start_async ( :get_status , fn ->
123- Get5Client . status ( socket . assigns . match . game_server )
136+ Get5Client . status ( game_server )
124137 end )
125138 |> put_flash ( :info , gettext ( "Match ended" ) ) }
126139 end
0 commit comments