index.erl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. -module(index).
  2. -compile(export_all).
  3. -include_lib("n2o/include/wf.hrl").
  4. -include("../../server/include/requests.hrl").
  5. -include("../../server/include/settings.hrl").
  6. -include_lib("avz/include/avz.hrl").
  7. -include_lib("kvs/include/user.hrl").
  8. -define(GAMEID, 1000001).
  9. -record(okey_player,
  10. {
  11. player_id,
  12. label_id,
  13. player_info,
  14. right_pile_combo_id,
  15. right_pile = [],
  16. left_label_id
  17. }
  18. ).
  19. user() ->
  20. case wf:user() of undefined ->
  21. Imagionary = fake_users:imagionary_users(),
  22. {Id,Name,Surname} = lists:nth(crypto:rand_uniform(1,length(Imagionary)),Imagionary),
  23. X = #user{id = fake_users:fake_id(Id),
  24. names = Name,
  25. surnames = Surname},
  26. wf:user(X), X; U-> U end.
  27. color(Id,Color) -> wf:wire(wf:f("document.querySelector('#~s').style.color = \"~s\";",[Id,Color])).
  28. unselect(Id) -> color(Id,black).
  29. select(Id) -> color(Id,red).
  30. redraw_discard_combo(TilesList) ->
  31. redraw_tiles(TilesList, #dropdown{id = discard_combo, postback = combo, source = [discard_combo]}).
  32. redraw_tiles(undefined, _DropDown) -> [];
  33. redraw_tiles([] = _TilesList, DropDown = #dropdown{id = ElementId}) ->
  34. wf:update(ElementId, [DropDown#dropdown{value = [], options = []}]);
  35. redraw_tiles([{Tile, _}| _ ] = TilesList, DropDown = #dropdown{id = ElementId}) ->
  36. wf:update(ElementId, [DropDown#dropdown{value = Tile, options = [#option{label = CVBin, value = CVBin} || {CVBin, _} <- TilesList]}]).
  37. redraw_players(Players) ->
  38. User = user(),
  39. [ begin PN = player_name(PI),
  40. wf:update(LabelId, #label{ id = LabelId,
  41. style= case User#user.id == Id of
  42. true -> "font-weight: bold;";
  43. _ -> "" end, body = <<" ",PN/binary," ">>})
  44. end || #okey_player{label_id = LabelId, player_info = #'PlayerInfo'{id = Id} = PI} <- Players].
  45. update_players(UpdatedPlayer = #okey_player{label_id = LabelId}, Players) ->
  46. lists:sort(
  47. fun(#okey_player{label_id = E1}, #okey_player{label_id = E2}) -> E1 < E2 end,
  48. [UpdatedPlayer | lists:keydelete(LabelId, #okey_player.label_id, Players)]
  49. ).
  50. player_name(PI) -> auth_server:player_name(PI).
  51. main() -> #dtl{file="index", bindings=[{title,<<"N2O">>},{body,body()}]}.
  52. body() ->
  53. wf:wire(#api{name=plusLogin, tag=plus}),
  54. [ #panel{ id=history },
  55. #button{ id = plusloginbtn, body = <<"Login">>, postback=login_button},
  56. #label{ body = " Google"},#br{},#br{},
  57. #label{ id = gosterge, body="Gosterge: "}, #br{},#br{},
  58. #label{ id = player1, body = "Player 1"},
  59. #dropdown{ id = p1right_combo, options = []},#br{},
  60. #label{ id = player2, body = "Player 2"},
  61. #dropdown{ id = p2right_combo, options = []},#br{},
  62. #label{ id = player3, body = "Player 3"},
  63. #dropdown{ id = p3right_combo, options = []}, #br{},
  64. #label{ id = player4, body = "Player 4"},
  65. #dropdown{ id = p4right_combo, options = []},#br{},
  66. #br{},
  67. #button{ id = attach, body = <<"Attach">>, postback = attach},
  68. #button{ id = join, body = <<"Join">>, postback = join},
  69. #dropdown{ id= take_combo, value="0",
  70. options =
  71. [
  72. #option { label= <<"Table">>, value= <<"0">> },
  73. #option { label= <<"Left">>, value= <<"1">> }
  74. ]
  75. },
  76. #button{ id=take, body = <<"Take">>, postback = take, source=[take_combo]},
  77. #dropdown{ id=discard_combo, value="2", postback=combo, source=[discard_combo],
  78. options =
  79. [
  80. #option { label= <<"Option 1">>, value= <<"1">> },
  81. #option { label= <<"Option 2">>, value= <<"2">> },
  82. #option { label= <<"Option 3">>, value= <<"3">> }
  83. ]
  84. },
  85. #button{ id = discard, body = <<"Discard">>, postback = discard, source=[discard_combo]},
  86. #button{ id = reveal, body = <<"Reveal">>, postback = reveal, source = [discard_combo]},
  87. #button{ id = i_saw_okey, body = <<"I Saw Okey">>, postback = i_saw_okey},
  88. #button{ id = i_have_8_tashes, body = <<"I have 8 Tashes">>, postback = i_have_8_tashes},
  89. #button{ id = pause, body = <<"Pause">>, postback = pause},
  90. #button{ id = player_info, body = <<"PlayerInfo">>, postback = player_info}
  91. ].
  92. event(terminate) -> wf:info("terminate");
  93. event(init) -> event(attach), event(join);
  94. event(login_button) -> wf:wire(protocol:logout());
  95. event(join) -> wf:wire(protocol:join(wf:to_list(?GAMEID)));
  96. event(take) -> wf:wire(protocol:take(wf:to_list(?GAMEID), wf:q(take_combo)));
  97. event(player_info) ->
  98. User = user(),
  99. wf:wire(protocol:player_info(
  100. wf:f("'~s'",[wf:to_list(User#user.id)]),wf:f("'~s'",[game_okey])));
  101. event(attach) ->
  102. {ok,GamePid} = game_session:start_link(self()),
  103. wf:session(<<"game_pid">>,GamePid),
  104. User = user(),
  105. put(okey_im, User#user.id),
  106. wf:info("Session User: ~p",[User]),
  107. Token = auth_server:generate_token(?GAMEID,User),
  108. wf:wire(protocol:attach(wf:f("'~s'",[Token]))),
  109. ok;
  110. event(discard) ->
  111. TilesList = get(game_okey_tiles),
  112. DiscardCombo = wf:q(discard_combo),
  113. case lists:keyfind(erlang:list_to_binary(DiscardCombo), 1, TilesList) of
  114. {_, {C, V}} ->
  115. wf:wire(protocol:discard(wf:to_list(?GAMEID), wf:to_list(C), wf:to_list(V)));
  116. false -> wf:info("Discard Combo: ~p",[DiscardCombo]) end;
  117. event(reveal) ->
  118. TilesList = case get(game_okey_tiles) of undefined -> []; T -> T end,
  119. Discarded = wf:q(discard_combo),
  120. case lists:keyfind(wf:to_binary(Discarded), 1, TilesList) of
  121. {_, {CD, VD} = Key} ->
  122. Hand = [{C,V} || {_, {C, V}} <- lists:keydelete(Key, 2, TilesList) ],
  123. HandJS = "[[" ++ string:join([
  124. wf:f("tuple(atom('OkeyPiece'),~p,~p)",[C,V]) || {C,V} <- Hand],",") ++ "],[]]",
  125. RevealJS = protocol:reveal(wf:to_list(?GAMEID),wf:f("~p",[CD]),wf:f("~p",[VD]),HandJS),
  126. wf:info("RevealJS: ~p",[lists:flatten(RevealJS)]),
  127. wf:wire(RevealJS);
  128. _ ->
  129. wf:info("error discarded ~p", Discarded)
  130. end;
  131. event(i_saw_okey) ->
  132. wf:info("i_saw_okey!"),
  133. wf:wire(protocol:i_saw_okey(wf:to_list(?GAMEID)));
  134. event(i_have_8_tashes) ->
  135. wf:info("i_gave_8_tashes!"),
  136. wf:wire(protocol:i_have_8_tashes(wf:to_list(?GAMEID)));
  137. event(pause) ->
  138. Action =
  139. case get(game_okey_pause) of
  140. X when X == resume orelse X == undefined ->
  141. put(game_okey_pause, pause),
  142. wf:update(pause, [#button{id = pause, body = "Resume", postback = pause}]),
  143. "pause";
  144. pause ->
  145. put(game_okey_pause, resume),
  146. wf:update(pause, [#button{id = pause, body = <<"Pause">>, postback = pause}]),
  147. "resume"
  148. end,
  149. wf:wire(protocol:pause(wf:to_list(?GAMEID), wf:f("~p", [Action])));
  150. %event({binary,M}) -> {ok,<<"Hello">>};
  151. event({client,Message}) ->
  152. case wf:session(<<"game_pid">>) of
  153. undefined -> skip;
  154. GamePid -> game_session:process_request(GamePid, Message) end;
  155. event({server, {game_event, _, okey_game_started, Args}}) ->
  156. {_, Tiles} = lists:keyfind(tiles, 1, Args),
  157. TilesList = [{wf:to_binary([wf:to_list(C)," ",wf:to_list(V)]), {C, V}} || {_, C, V} <- Tiles],
  158. %%wf:info("tiles ~p", [TilesList]),
  159. case lists:keyfind(gosterge, 1, Args) of
  160. {_, {_, C, V}} ->
  161. wf:update(gosterge, #label{id = gosterge, body = wf:to_binary(["Gosterge: ", wf:to_list(C), " ", wf:to_list(V)])});
  162. _ ->
  163. ok
  164. end,
  165. put(game_okey_tiles, TilesList),
  166. put(game_okey_pause, resume),
  167. redraw_discard_combo(TilesList);
  168. event({server, {game_event, _, okey_game_player_state, Args}}) ->
  169. case lists:keyfind(whos_move, 1, Args) of
  170. {_, null} ->
  171. ok;
  172. {_, WhosMove} ->
  173. Players = get(okey_players),
  174. #okey_player{label_id = X} = lists:keyfind(WhosMove, #okey_player.player_id, Players),
  175. case X of
  176. null -> skip;
  177. false -> skip;
  178. X -> select(X), put(okey_turn_mark,X) end,
  179. {_, Tiles} = lists:keyfind(tiles, 1, Args),
  180. TilesList = [{wf:to_binary([wf:to_list(C)," ",wf:to_list(V)]),{C, V}}|| {_, C, V} <- Tiles],
  181. redraw_discard_combo(TilesList),
  182. put(game_okey_tiles, TilesList),
  183. {_, Piles} = lists:keyfind(piles, 1, Args),
  184. UpdatedPlayers =
  185. [
  186. begin
  187. Player = #okey_player{right_pile_combo_id = RightPileComboId} = lists:keyfind(PlayerId, #okey_player.player_id, Players),
  188. ConvertedPile = [{wf:to_binary([wf:to_list(C), " ", wf:to_list(V)]), {C, V}} || {_, C, V} <- Pile],
  189. redraw_tiles(ConvertedPile, #dropdown{id = RightPileComboId}),
  190. Player#okey_player{right_pile = ConvertedPile}
  191. end
  192. || {PlayerId, Pile} <- Piles
  193. ],
  194. put(okey_players, lists:sort(fun(#okey_player{label_id = E1}, #okey_player{label_id = E2}) -> E1 < E2 end, UpdatedPlayers));
  195. _ ->
  196. ok
  197. end;
  198. event({server, {game_event, _, okey_tile_taken, Args}}) ->
  199. Im = get(okey_im),
  200. {_, PlayerId} = lists:keyfind(player, 1, Args),
  201. case lists:keyfind(revealed, 1, Args) of
  202. {_, {_, C, V}} ->
  203. if
  204. Im == PlayerId ->
  205. TilesList = [{wf:to_binary([wf:to_list(C), " ", wf:to_list(V)]), {C, V}} | get(game_okey_tiles)],
  206. %%wf:info("Tiles: ~p",[TilesList]),
  207. put(game_okey_tiles, TilesList),
  208. redraw_discard_combo(TilesList);
  209. true ->
  210. ok
  211. end,
  212. case lists:keyfind(pile, 1, Args) of
  213. {_, 1} -> %% have taken from left
  214. Players = get(okey_players),
  215. #okey_player{left_label_id = LeftLabelId} = lists:keyfind(PlayerId, #okey_player.player_id, Players),
  216. LeftPlayer =
  217. #okey_player{right_pile_combo_id = RightPileComboId, right_pile = RightPile} =
  218. lists:keyfind(LeftLabelId, #okey_player.label_id, Players),
  219. UpdatedRightPile = lists:keydelete({C, V}, 2, RightPile),
  220. redraw_tiles(UpdatedRightPile, #dropdown{id = RightPileComboId}),
  221. UpdatedPlayers = update_players(LeftPlayer#okey_player{right_pile = UpdatedRightPile}, Players),
  222. put(okey_players, UpdatedPlayers);
  223. _ ->
  224. ok
  225. end;
  226. _ -> ok
  227. end;
  228. event({server, {game_event, _, okey_tile_discarded, Args}}) ->
  229. Im = get(okey_im),
  230. {_, PlayerId} = lists:keyfind(player, 1, Args),
  231. {_, {_, C, V}} = lists:keyfind(tile, 1, Args),
  232. if
  233. Im == PlayerId ->
  234. TilesListOld = get(game_okey_tiles),
  235. TilesList = lists:keydelete({C, V}, 2, TilesListOld),
  236. put(game_okey_tiles, TilesList),
  237. redraw_discard_combo(TilesList);
  238. true ->
  239. ok
  240. end,
  241. Players = get(okey_players),
  242. Player =
  243. #okey_player{right_pile_combo_id = RightPileComboId, right_pile = OldRightPile} =
  244. lists:keyfind(PlayerId, #okey_player.player_id, Players),
  245. NewRightPile = [{wf:to_binary([wf:to_list(C), " ", wf:to_list(V)]), {C, V}} | OldRightPile],
  246. redraw_tiles(NewRightPile, #dropdown{id = RightPileComboId}),
  247. UpdatedPlayer = Player#okey_player{right_pile = NewRightPile},
  248. UpdatedPlayers = update_players(UpdatedPlayer, Players),
  249. put(okey_players, UpdatedPlayers);
  250. event({server,{game_event, _Game, okey_turn_timeout, Args}}) ->
  251. wf:info("okey_turn_timeout ~p", [Args]);
  252. event({server, {game_event, _, okey_game_info, Args}}) ->
  253. %% wf:info("okay_game_info ~p", [Args]),
  254. {_, PlayersInfo} = lists:keyfind(players, 1, Args),
  255. PlayersTempl =
  256. [
  257. #okey_player{label_id = player1, right_pile_combo_id = p1right_combo, left_label_id = player4},
  258. #okey_player{label_id = player2, right_pile_combo_id = p2right_combo, left_label_id = player1},
  259. #okey_player{label_id = player3, right_pile_combo_id = p3right_combo, left_label_id = player2},
  260. #okey_player{label_id = player4, right_pile_combo_id = p4right_combo, left_label_id = player3}
  261. ],
  262. Players =
  263. lists:zipwith(
  264. fun(Players, #'PlayerInfo'{id = Id} = PI) ->
  265. Players#okey_player{player_id = Id, player_info = PI} end,
  266. PlayersTempl,
  267. PlayersInfo),
  268. put(okey_players, Players),
  269. % wf:info("players ~p", [Players]),
  270. redraw_players(Players);
  271. event({server,{game_event, _, player_left, Args}}) ->
  272. {_, OldPlayerId} = lists:keyfind(player, 1, Args),
  273. {_, PI} = lists:keyfind(replacement, 1, Args),
  274. #'PlayerInfo'{id = NewPlayerId} = PI,
  275. OldPlayers = get(okey_players),
  276. OldPlayer = lists:keyfind(OldPlayerId, #okey_player.player_id, OldPlayers),
  277. NewPlayers = update_players(OldPlayer#okey_player{player_id = NewPlayerId, player_info = PI}, OldPlayers),
  278. put(okey_players, NewPlayers),
  279. redraw_players(NewPlayers),
  280. case get(okey_turn_mark) of undefined -> ok; X -> select(X) end;
  281. event({server,{game_event, _, okey_next_turn, Args}}) ->
  282. {player, PlayerId} = lists:keyfind(player, 1, Args),
  283. % wf:info("im ~p next turn players ~p ~p", [get(okey_im), PlayerId, get(okey_players)]),
  284. #okey_player{label_id = LabelId} = lists:keyfind(PlayerId, #okey_player.player_id, get(okey_players)),
  285. case get(okey_turn_mark) of
  286. undefined -> ok;
  287. OldLabelId -> unselect(OldLabelId) end,
  288. select(LabelId),
  289. put(okey_turn_mark, LabelId);
  290. event({register,User}) -> wf:info("Register: ~p",[User]), kvs:add(User), wf:user(User);
  291. event({login,User}) -> wf:info("Login: ~p",[User]), kvs:put(User), wf:user(User), event(init);
  292. event(_Event) -> wf:info("Event: ~p", [_Event]).
  293. api_event(X,Y,Z) -> avz:api_event(X,Y,Z).