okey.erl 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. -module(okey).
  2. -compile(export_all).
  3. -include_lib("n2o/include/wf.hrl").
  4. -include_lib("server/include/requests.hrl").
  5. -include_lib("server/include/settings.hrl").
  6. -include_lib("avz/include/avz.hrl").
  7. -include_lib("kvs/include/user.hrl").
  8. -include_lib("db/include/journal.hrl").
  9. -define(GAMEID, game_form()).
  10. game_form() ->
  11. case wf:qs(<<"game">>) of
  12. undefined -> 1000001;
  13. X -> wf:to_integer(X) end.
  14. new_user() ->
  15. Imagionary = anonymous:imagionary_users(),
  16. {Id,Name,Surname} = lists:nth(crypto:rand_uniform(1,length(Imagionary)),Imagionary),
  17. FakeId = anonymous:fake_id(Id),
  18. X = #user{
  19. id = FakeId,
  20. tokens=[{n2o,get(session_id)}],
  21. names = Name,
  22. surnames = Surname},
  23. wf:wire(wf:f("document.cookie='~s=~s; path=/; expires=~s';",
  24. ["n2o-name",wf:to_list(FakeId),js_session:cookie_expire(js_session:ttl())])),
  25. kvs:put(X),
  26. X.
  27. new_facebook_user(User) ->
  28. ExistingUser = user(),
  29. Score = proplists:get_value(score,ExistingUser#user.tokens,0),
  30. Skill = case kvs:get(reveal_log, User#user.id) of
  31. {ok,RL} -> RL#reveal_log.skill;
  32. _ -> 0 end,
  33. U1 = User#user{tokens=game:plist_setkey(score,1,User#user.tokens,{score,Score})},
  34. U2 = U1#user{tokens=game:plist_setkey(n2o,1,U1#user.tokens,{n2o,get(session_id)})},
  35. U3 = U2#user{tokens=game:plist_setkey(skill,1,U2#user.tokens,{skill,Skill})},
  36. kvs:put(U3),
  37. send_auth_cookies(U3),
  38. U3.
  39. send_auth_cookies(User) ->
  40. wf:wire(wf:f("document.cookie='~s=~s; path=/; expires=~s';",
  41. ["n2o-name",wf:to_list(User#user.id),
  42. js_session:cookie_expire(js_session:ttl())])),
  43. wf:wire(wf:f("document.cookie='~s=~s; path=/; expires=~s';",
  44. ["n2o-sid",wf:to_list(proplists:get_value(n2o,User#user.tokens)),
  45. js_session:cookie_expire(js_session:ttl())])),
  46. wf:user(User),
  47. ok.
  48. user() ->
  49. case wf:user() of
  50. undefined ->
  51. SessionUser = wf:cookie_req(<<"n2o-name">>,?REQ),
  52. SessionId = get(session_id),
  53. wf:info(?MODULE,"Auth User: ~p",[SessionUser]),
  54. wf:info(?MODULE,"Auth Id: ~p",[SessionId]),
  55. X = case kvs:get(user,SessionUser) of
  56. {ok,User} ->
  57. SS = lists:keyfind(n2o,1,User#user.tokens),
  58. case SS of
  59. {n2o,SessionId} -> User;
  60. _ -> new_user() end;
  61. _ -> new_user() end,
  62. wf:user(X),
  63. X;
  64. U-> U end.
  65. player_name(PI) -> auth_server:player_name(PI).
  66. main() -> #dtl{file="index", bindings=[{title,<<"N2O">>},{body,[]}]}.
  67. patch_users() ->
  68. [ begin
  69. Score = score_journal(User),
  70. kvs:put(User#user{tokens=game:plist_setkey(score,1,Tokens,{score,Score})})
  71. end|| User=#user{tokens=Tokens} <- kvs:all(user), Tokens /= [], Tokens /= undefined].
  72. send_roster(Pid) ->
  73. % X = [ send_roster_item(User) || User=#user{tokens=Tokens} <- kvs:all(user), Tokens /= [], Tokens /= undefined],
  74. X = [ begin
  75. {User#user.id,User#user.names,User#user.surnames,score(User)}
  76. end || User=#user{tokens=Tokens} <- kvs:all(user), Tokens /= [], Tokens /= undefined, proplists:get_value(score,Tokens,0) /= 0],
  77. XS = lists:sort(fun({_,_,_,S1},{_,_,_,S2}) -> S1 > S2 end,X),
  78. Lists = [lists:sublist(XS,100)], %split(170,XS,[]),
  79. [ send_roster_group(Pid,List) || List <- Lists],
  80. Pid ! {server,{roster_end,length(Lists)}},
  81. wf:info(?MODULE,"Users: ~p",[length(X)]).
  82. split(N,[],Result) -> Result;
  83. split(N,List,Result) when length(List) < N -> Result ++ [List];
  84. split(N,List,Result) -> {A,B}=lists:split(N,List), Result ++ [A] ++ split(N,B,Result).
  85. score(User) -> proplists:get_value(score,User#user.tokens,0).
  86. score_journal(User) ->
  87. Score = case kvs:get(reveal_log,User#user.id) of
  88. {ok,#reveal_log{score=S}} -> S;
  89. _ -> wf:info(?MODULE,"Score not found for User ~p",[User#user.id]), 0 end.
  90. already_online(Pid) ->
  91. [ Pid ! {user_online,User} || {_,_,{_,User}} <- game:online() ].
  92. send_roster_item(Pid,User) ->
  93. Pid ! {server,{roster_item,User#user.id,User#user.names,User#user.surnames,0}}.
  94. send_roster_group(Pid,List) ->
  95. wf:info(?MODULE,"User Group: ~p",[List]),
  96. Pid ! {server,{roster_group,List}}.
  97. event(terminate) ->
  98. User = user(),
  99. wf:send(broadcast,{user_offline,User}),
  100. wf:info(?MODULE,"EXTerminate",[]);
  101. event(init) ->
  102. js_session:ensure_sid([],?CTX),
  103. event(attach),
  104. event(join);
  105. event(join) ->
  106. GameId = get(okey_game_id),
  107. wf:wire(#api{name=fbLogin, tag=fb}),
  108. wf:wire(protocol:join(wf:to_list(GameId)));
  109. event(attach) ->
  110. {ok,GamePid} = game_session:start_link(self()),
  111. wf:session(<<"game_pid">>,GamePid),
  112. User = user(),
  113. wf:reg(User#user.id),
  114. wf:info(?MODULE,"User Attach: ~p",[User]),
  115. gproc:set_value({p,l,broadcast},{wf:peer(?REQ),User}),
  116. wf:info(?MODULE,"Games Online: ~p",[game:online()]),
  117. put(okey_im, User#user.id),
  118. wf:wire(wf:f("document.user = '~s';document.names = '~s';document.surnames = '~s';",
  119. [User#user.id,User#user.names,User#user.surnames])),
  120. wf:info(?MODULE,"Session User: ~p",[User]),
  121. GameId = case wf:q(games_ids) of undefined -> ?GAMEID; Res -> Res end,
  122. put(okey_game_id, GameId),
  123. Token = auth_server:generate_token(GameId,User#user.id),
  124. wf:info(?MODULE,"Game Token: ~p",[Token]),
  125. wf:wire(protocol:attach(wf:f("'~s'",[Token]))),
  126. Pid = self(),
  127. spawn(fun() ->
  128. send_roster(Pid),
  129. already_online(Pid),
  130. wf:send(broadcast,{user_online,User})
  131. end),
  132. ok;
  133. event({client,{message,From,Name,To,Message}}) ->
  134. wf:info(?MODULE,"Online Chat Message from ~p(~p) to ~p:~n ~p~n",[From,Name,To,Message]),
  135. wf:send(To,{server,{chat_message,{From,Name},To,wf:to_binary(Message)}}),
  136. ok;
  137. event({client,Message}) ->
  138. wf:info(?MODULE,"Client: ~p", [Message]),
  139. case wf:session(<<"game_pid">>) of
  140. undefined -> skip;
  141. GamePid -> SyncRes = game_session:process_request(GamePid, Message),
  142. wf:info(?MODULE,"Sync Result: ~p",[SyncRes]) end;
  143. event({server,{roster_group,List}}) -> skip;
  144. event({server,terminate}) -> event(terminate);
  145. event({server,{update_score,Score}}) ->
  146. User = user(),
  147. NewUser = User#user{tokens=game:plist_setkey(score,1,User#user.tokens,{score,Score})},
  148. wf:user(NewUser),
  149. gproc:set_value({p,l,broadcast},{wf:peer(?REQ),NewUser}),
  150. wf:info(?MODULE,"User Process Updated Score ~p ~p",[User#user.id,Score]),
  151. wf:send(broadcast,{user_online,NewUser}),
  152. ok;
  153. event({counter,Res}) -> Pid = self(), spawn(fun() -> Pid ! {server,{online_number,length(game:online())}} end);
  154. event({user_online,User}) -> wf:info(?MODULE,"User ~p goes Online",[User#user.id]), self() ! {server,{online,User#user.id,User#user.names,User#user.surnames,score(User)}};
  155. event({user_offline,User}) -> self() ! {server,{offline,User#user.id,User#user.names,User#user.surnames,score(User)}};
  156. event({register,User}) -> wf:info(?MODULE,"Register: ~p",[User]), new_facebook_user(User), wf:wire("window.location='https://kakaranet.com'");
  157. event({login,User}) -> wf:info(?MODULE,"Login: ~p",[User]), send_auth_cookies(User), wf:wire("window.location='https://kakaranet.com'");
  158. event(_Event) -> wf:info(?MODULE,"Unknown Event: ~p", [_Event]).
  159. api_event(X,Y,Z) -> avz:api_event(X,Y,Z).