Browse Source

clean index

Maxim Sokhatsky 11 years ago
parent
commit
95a2bf5622
2 changed files with 75 additions and 28 deletions
  1. 47 0
      apps/face/src/index.erl
  2. 28 28
      apps/face/src/n2o_game.erl

+ 47 - 0
apps/face/src/index.erl

@@ -0,0 +1,47 @@
+-module(index).
+-compile({parse_transform, shen}).
+-compile(export_all).
+-include_lib("n2o/include/wf.hrl").
+-include_lib("server/include/requests.hrl").
+-include_lib("server/include/settings.hrl").
+-jsmacro([take/2,attach/1,join/1]).
+
+join(Game) ->
+    ws:send(bert:encodebuf(bert:tuple(
+        bert:atom('client'),
+        bert:tuple(bert:atom("join_game"), Game)))).
+
+attach(Token) ->
+    ws:send(bert:encodebuf(bert:tuple(
+        bert:atom('client'),
+        bert:tuple(bert:atom("session_attach"), Token)))).
+
+take(GameId,Place) ->
+    ws:send(bert:encodebuf(bert:tuple(
+        bert:atom('client'),
+        bert:tuple(bert:atom("game_action"),GameId,bert:atom("okey_take"),[{pile,Place}])))).
+
+main() -> #dtl{file="index", bindings=[{title,<<"N2O">>},{body,body()}]}.
+
+body() ->
+    [ #panel{ id=history },
+      #dropdown { id=drop, value="2", postback=combo, source=[drop], options=[
+        #option { label= <<"Option 1">>, value= <<"1">> },
+        #option { label= <<"Option 2">>, value= <<"2">> },
+        #option { label= <<"Option 3">>, value= <<"3">> }
+     ]},
+      #button{ id = attach, body = <<"Attach">>, postback = attach},
+      #button{ id = join, body = <<"Join">>, postback = join},
+      #button{ id = take, body = <<"Take">>, postback = take},
+      #button{ id = discard, body = <<"Discard">>, postback = discard}
+    ].
+
+event(init) ->
+    {ok,GamePid} = game_session:start_link(self()),
+    put(game_session,GamePid);
+
+event(combo)  -> wf:info("Combo: ~p",[wf:q(drop)]);
+event(join)   -> wf:wire(join("1000001"));
+event(attach) -> wf:wire(attach("'"++?TEST_TOKEN++"'"));
+event(take)   -> wf:wire(take("1000001","0"));
+event(Event)  -> wf:info("Event: ~p", [Event]).

+ 28 - 28
apps/face/src/n2o_game.erl

@@ -23,6 +23,29 @@ init(_Transport, Req, _Opts, _Active) ->
     Req1 = wf:header(<<"Access-Control-Allow-Origin">>, <<"*">>, NewCtx#context.req),
     {ok, Req1, NewCtx}.
 
+html_events(Pro, State) ->
+    Pickled = proplists:get_value(pickle,Pro),
+    Linked = proplists:get_value(linked,Pro),
+    Depickled = wf:depickle(Pickled),
+    wf:info("Depickled: ~p",[Depickled]),
+    case Depickled of
+        #ev{module=Module,name=Function,payload=Parameter,trigger=Trigger} ->
+            case Function of 
+                control_event   -> lists:map(fun({K,V})-> put(K,V) end,Linked),
+                                   Module:Function(Trigger, Parameter);
+                api_event       -> Module:Function(Parameter,Linked,State);
+                event           -> lists:map(fun({K,V})-> put(K,V) end,Linked),
+                                   Module:Function(Parameter);
+                UserCustomEvent -> Module:Function(Parameter,Trigger,State) end;
+        _Ev -> wf:error("N2O allows only #ev{} events") end,
+    Actions = get(actions),
+    wf_context:clear_actions(),
+    Render = wf:render(Actions),
+    GenActions = get(actions),
+    RenderGenActions = wf:render(GenActions),
+    wf_context:clear_actions(),
+    [Render,RenderGenActions].
+
 stream(<<"ping">>, Req, State) ->
     wf:info("ping received~n"),
     {reply, <<"pong">>, Req, State};
@@ -31,37 +54,10 @@ stream({text,Data}, Req, State) ->
     self() ! Data,
     {ok, Req,State};
 stream({binary,Info}, Req, State) ->
-    wf:info("Binary Received: ~p",[Info]),
     Pro = binary_to_term(Info,[safe]),
-
-    wf:info("N2O Unknown Event: ~p",[Pro]),
     case Pro of
         {client,M} -> info({client,M},Req,State);
-        _ ->
-            Pickled = proplists:get_value(pickle,Pro),
-            Linked = proplists:get_value(linked,Pro),
-            Depickled = wf:depickle(Pickled),
-            wf:info("Depickled: ~p",[Depickled]),
-            case Depickled of
-                #ev{module=Module,name=Function,payload=Parameter,trigger=Trigger} ->
-                    case Function of 
-                        control_event   -> lists:map(fun({K,V})-> put(K,V) end,Linked),
-                                           Module:Function(Trigger, Parameter);
-                        api_event       -> Module:Function(Parameter,Linked,State);
-                        event           -> lists:map(fun({K,V})-> put(K,V) end,Linked),
-                                           Module:Function(Parameter);
-                        UserCustomEvent -> Module:Function(Parameter,Trigger,State) end;
-                _Ev -> wf:error("N2O allows only #ev{} events") end,
-
-            Actions = get(actions),
-            wf_context:clear_actions(),
-            Render = wf:render(Actions),
-
-            GenActions = get(actions),
-            RenderGenActions = wf:render(GenActions),
-            wf_context:clear_actions(),
-
-            {reply, [Render,RenderGenActions], Req, State} end;
+        _ -> {reply,html_events(Pro,State),Req,State} end;
 stream(Data, Req, State) ->
     wf:info("Data Received ~p",[Data]),
     self() ! Data,
@@ -77,11 +73,15 @@ render_actions(InitActions) ->
 info({client,Message}, Req, State) ->
     GamePid = get(game_session),
     game_session:process_request(GamePid, Message), 
+    Module = State#context.module,
+    Module:event(Message),
     wf:info("Client Message: ~p",[Message]),
     {reply,[],Req,State};
 
 info({send_message,Message}, Req, State) ->
     wf:info("Game Message: ~p",[Message]),
+    Module = State#context.module,
+    Module:event(Message),
     Ret = io_lib:format("~p",[Message]),
     T = wf:js_escape(Ret),
     {reply,io_lib:format("console.log('~s')",[T]),Req,State};