index.erl 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. -module(index).
  2. -compile({parse_transform, shen}).
  3. -compile(export_all).
  4. -include_lib("n2o/include/wf.hrl").
  5. -include_lib("server/include/requests.hrl").
  6. -include_lib("server/include/settings.hrl").
  7. -jsmacro([take/2]).
  8. take(GameId,Place) ->
  9. ws:send(bert:encodebuf(bert:tuple(bert:atom('client'),
  10. bert:tuple(bert:atom("game_action"),GameId,bert:atom("okey_take"),[{pile,Place}])))).
  11. main() ->
  12. case wf:user() of
  13. undefined -> wf:redirect("/login"), #dtl{file="index",app=n2o_sample,bindings=[{title,""},{body,""}]};
  14. _ -> #dtl{file = "index", app=n2o_sample,bindings=[{title,<<"N2O">>},{body,body()}]}
  15. end.
  16. body() ->
  17. [ #panel{ id=history },
  18. #dropdown { id=drop, value="2", postback=combo, source=[drop], options=[
  19. #option { label= <<"Option 1">>, value= <<"1">> },
  20. #option { label= <<"Option 2">>, value= <<"2">> },
  21. #option { label= <<"Option 3">>, value= <<"3">> }
  22. ]},
  23. #button{ id = attach, body = <<"Attach">>, postback = attach},
  24. #button{ id = join, body = <<"Join">>, postback = join},
  25. #button{ id = take, body = <<"Take">>, postback = take},
  26. #button{ id = discard, body = <<"Discard">>, postback = discard}
  27. ].
  28. event(init) ->
  29. {ok,GamePid} = game_session:start_link(self()),
  30. put(game_session,GamePid);
  31. event(combo) ->
  32. wf:info("Combo: ~p",[wf:q(drop)]);
  33. event(attach) ->
  34. Msg = "ws.send(Bert.encodebuf(Bert.tuple(Bert.atom('client'), Bert.tuple(Bert.atom('session_attach'), '" ++ ?TEST_TOKEN ++ "'))));",
  35. wf:wire(Msg);
  36. event(join) ->
  37. Msg = "ws.send(Bert.encodebuf(Bert.tuple(Bert.atom('client'), Bert.tuple(Bert.atom('join_game'), 1000001))));",
  38. wf:wire(Msg);
  39. %%-record(game_action, {
  40. %% game :: 'GameId'(),
  41. %% action :: string(),
  42. %% args = [] :: proplist()
  43. %% }).
  44. %%-record(okey_take, {
  45. %% pile :: integer() %% 0 or 1
  46. %% }).
  47. event(take) ->
  48. Msg = "ws.send(Bert.encodebuf(Bert.tuple(Bert.atom('client'),"
  49. " Bert.tuple(Bert.atom('game_action'), 1000001, Bert.atom('okey_take'), {pile: 0} ) )));",
  50. wf:wire(take("1000001","0"));
  51. %%-record('OkeyPiece', {
  52. %% color = -1 :: integer(), %% 1..4
  53. %% value = -1 :: integer() %% 1..13
  54. %% %% color set to 1 and value set to zero mean that this is false okey
  55. %% }).
  56. %%-record(okey_discard, {
  57. %% tile :: #'OkeyPiece'{}
  58. %% }).
  59. event(Event) -> wf:info("Event: ~p", [Event]).