index.erl 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #button{ id = attach, body = <<"Attach">>, postback = attach},
  19. #button{ id = join, body = <<"Join">>, postback = join},
  20. #button{ id = take, body = <<"Take">>, postback = take},
  21. #textbox{ id = tbcolor }, #textbox{ id = tbvalue }, #button{ id = discard, body = <<"Discard">>, postback = discard, source = [tbcolor, tbvalue]}
  22. ].
  23. event(init) ->
  24. {ok,GamePid} = game_session:start_link(self()),
  25. put(game_session,GamePid);
  26. event(attach) ->
  27. Msg = "ws.send(Bert.encodebuf(Bert.tuple(Bert.atom('client'), Bert.tuple(Bert.atom('session_attach'), '" ++ ?TEST_TOKEN ++ "'))));",
  28. wf:wire(Msg);
  29. event(join) ->
  30. Msg = "ws.send(Bert.encodebuf(Bert.tuple(Bert.atom('client'), Bert.tuple(Bert.atom('join_game'), 1000001))));",
  31. wf:wire(Msg);
  32. %%-record(game_action, {
  33. %% game :: 'GameId'(),
  34. %% action :: string(),
  35. %% args = [] :: proplist()
  36. %% }).
  37. %%-record(okey_take, {
  38. %% pile :: integer() %% 0 or 1
  39. %% }).
  40. event(take) ->
  41. Msg = "ws.send(Bert.encodebuf(Bert.tuple(Bert.atom('client'),"
  42. " Bert.tuple(Bert.atom('game_action'), 1000001, Bert.atom('okey_take'), {pile: 0} ) )));",
  43. wf:wire(take("1000001","0"));
  44. %%-record('OkeyPiece', {
  45. %% color = -1 :: integer(), %% 1..4
  46. %% value = -1 :: integer() %% 1..13
  47. %% %% color set to 1 and value set to zero mean that this is false okey
  48. %% }).
  49. %%-record(okey_discard, {
  50. %% tile :: #'OkeyPiece'{}
  51. %% }).
  52. event(discard) ->
  53. Color = wf:q(tbcolor),
  54. Value = wf:q(tbvalue),
  55. Msg = "ws.send(Bert.encodebuf(Bert.tuple(Bert.atom('client'),"
  56. " Bert.tuple(Bert.atom('game_action'), 1000001, Bert.atom('okey_discard'),"
  57. " [Bert.tuple(Bert.atom('tile'), Bert.tuple(Bert.atom('OkeyPiece'), " ++ Color ++ ", " ++ Value ++ "))]) )));",
  58. wf:wire(Msg);
  59. event(Event) -> wf:info("Event: ~p", [Event]).