Browse Source

Grand Refactoring

Maxim Sokhatsky 11 years ago
parent
commit
8cd1f15499

+ 10 - 56
apps/server/include/requests.hrl

@@ -1,64 +1,18 @@
 -include("basic_types.hrl").
 -include("types.hrl").
 
--record(session_attach, {
-          token :: string() %% shared secret, stored in auth_server
-         }).
-
--record(session_attach_debug, {
-          token :: string(), %% shared secret, stored in auth_server
-          id :: string()
-         }).
-
--record(login, {
-          username :: string(),
-          password :: string()
-         }).
-
+-record(session_attach, {token}).
+-record(login, {username, password}).
 -record(logout, {}).
+-record(join_game, {game}).
+-record(get_game_info, {game}).
+-record(get_player_info, {player_id :: 'PlayerId'() | 0}).
+-record(get_player_stats, {player_id :: 'PlayerId'() | 0, game_type :: binary()}).
+-record(subscribe_player_rels, {players :: list()}).
+-record(unsubscribe_player_rels, {players :: list()}).
+-record(chat, {chat_id :: 'GameId'(),message :: string()}).
+-record(game_action, { game :: 'GameId'(), action :: any(), args = [] :: proplist()}).
 
--record(join_game, {
-          game :: 'GameId'()
-         }).
-
--record(rematch, {
-          game :: 'GameId'()
-         }).
-
--record(get_game_info, {
-          game :: 'GameId'()
-         }).
-
--record(get_player_info, {
-          player_id :: 'PlayerId'() | 0 %% 0 stands for currently logged in user
-         }).
-
--record(get_player_stats, {
-          player_id :: 'PlayerId'() | 0, %% 0 stands for currently logged in user
-          game_type :: binary()
-         }).
-
--record(subscribe_player_rels, {
-          players :: list()
-         }).
-
--record(unsubscribe_player_rels, {
-          players :: list()
-         }).
-
--record(chat, {
-          chat_id :: 'GameId'(),
-          message :: string()
-         }).
-
--record(game_action, {
-          game      :: 'GameId'(),
-          action    :: any(),
-          args = [] :: proplist()
-         }).
-%%%
-%%% Events, passed via #KakaMessage
-%%%
 -record(game_event, {
           game      :: 'GameId'(),
           event     :: any(),

+ 0 - 0
apps/server/src/auth_server.erl → apps/server/src/auth/auth_server.erl


+ 0 - 0
apps/server/src/fake_users.erl → apps/server/src/auth/fake_users.erl


+ 8 - 28
apps/server/src/game_manager.erl → apps/server/src/game.erl

@@ -1,14 +1,11 @@
--module(game_manager).
--behaviour(gen_server).
+-module(game).
 -author('Maxim Sokhatsky <maxim@synrc.com>').
 -compile(export_all).
--export([init/1, start/0, start_link/0, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
 -include_lib("server/include/requests.hrl").
 -include_lib("db/include/table.hrl").
 -include_lib("db/include/tournaments.hrl").
 -include_lib("stdlib/include/qlc.hrl").
 -include_lib("db/include/scoring.hrl").
--record(state, { game_tavla = 0, game_okey = 0 }).
 
 online() -> [X||X<-qlc:e(gproc:table()),element(1,X)=={p,l,broadcast}].
 
@@ -54,33 +51,16 @@ counter(Game) -> PL = supervisor:count_children(case Game of game_okey -> okey_s
                       game_tavla -> Res;
                       _ -> 0 end.
 
-start() -> gen_server:start({local, ?MODULE}, ?MODULE, [], []).
-start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
-stop(Ref) -> gen_server:cast(Ref, stop).
-
-init([]) -> {ok,#state{}}.
-
-handle_call({get_relay, Topic}, _From, State) -> Res = get_relay_pid(Topic), {reply, Res, State};
-handle_call({game_counter, FSM}, _From, State) ->
-    {reply, case FSM of game_tavla -> State#state.game_tavla; game_okey -> State#state.game_okey; _ -> 0 end, State};
-handle_call(Event, From, State) -> {stop, {unknown_call, Event, From}, State}.
-handle_cast(stop, State) -> {stop, normal, State};
-handle_cast(Event, State) -> {stop, {unknown_cast, Event}, State}.
-handle_info({'DOWN', _, process, Pid, Reason}, State) -> {noreply, State};
-handle_info(Info, State) -> {stop, {unknown_info, Info}, State}.
-terminate(_Reason, _State) -> ok.
-code_change(_OldVsn, State, _Extra) -> {ok, State}.
-
 game_sup_domain(Module, Params) ->
     case Module of
         game_tavla_ng_trn_paired -> tavla_sup;
-        nsg_trn_standalone ->
+        standalone ->
             case proplists:get_value(game, Params) of
                 game_okey -> okey_sup;
                 game_tavla -> tavla_sup;
                 _ -> game_sup
             end;
-        nsg_trn_elimination ->
+        elimination ->
             case proplists:get_value(game_type, Params) of
                 game_okey -> okey_sup;
                 game_tavla -> tavla_sup;
@@ -194,7 +174,7 @@ create_standalone_game(Game, Params, Users) ->
                            {gosterge_finish_allowed, GostergeFinishAllowed}
                          ],
 
-            create_game(GameId, nsg_trn_standalone,
+            create_game(GameId, standalone,
                          [{game, Game},
                           {game_mode, GameMode},
                           {game_name, TableName},
@@ -251,7 +231,7 @@ create_standalone_game(Game, Params, Users) ->
                            {tables_num, 1}
                          ],
 
-            create_game(GameId, nsg_trn_standalone,
+            create_game(GameId, standalone,
                          [{game, Game},
                           {game_mode, GameMode},
                           {game_name, TableName},
@@ -349,7 +329,7 @@ create_elimination_trn(GameType, Params, Registrants) ->
            exit(wrong_registrants_number);
        true -> do_nothing
     end,
-    {ok, Plan} = nsg_matrix_elimination:get_plan(GameType, QuotaPerRound, PlayersNumber, Tours),
+    {ok, Plan} = matrix:get_plan(GameType, QuotaPerRound, PlayersNumber, Tours),
     case GameType of
         game_okey ->
             Rounds = 10,
@@ -371,7 +351,7 @@ create_elimination_trn(GameType, Params, Registrants) ->
                            {social_actions_enabled, false},
                            {mult_factor, 1}
                          ],
-            create_game(TrnId, nsg_trn_elimination,
+            create_game(TrnId, elimination,
                         [{game_type, GameType},
                          {game_mode, GameMode},
                          {registrants, Registrants},
@@ -407,7 +387,7 @@ create_elimination_trn(GameType, Params, Registrants) ->
                            {tables_num, 1}
                          ],
 
-            create_game(TrnId, nsg_trn_elimination,
+            create_game(TrnId, elimination,
                         [{game_type, GameType},
                          {game_mode, GameMode},
                          {registrants, Registrants},

+ 1 - 1
apps/server/src/nsg_games_app.erl → apps/server/src/game_app.erl

@@ -1,4 +1,4 @@
--module(nsg_games_app).
+-module(game_app).
 -behaviour(application).
 -export([start/2, start/0, stop/0, stop/1]).
 -include_lib("eunit/include/eunit.hrl").

+ 2 - 2
apps/server/src/game_sup.erl

@@ -21,9 +21,9 @@ init([]) ->
     AuthServer = {auth_server, {auth_server, start_link, []}, Restart, Shutdown, worker, [auth_server]},
     GameLog = {game_log, {game_log, start_link, []}, Restart, Shutdown, worker, [game_log]},
     GameStats = {game_stats, {game_stats, start_link, []}, Restart, Shutdown, worker, [game_stats]},
-    GameManager = {game_manager, {game_manager, start_link, []}, Restart, Shutdown, worker, [game_manager]},
+    %GameManager = {game_manager, {game_manager, start_link, []}, Restart, Shutdown, worker, [game_manager]},
     TavlaSup = {tavla_sup, {tavla_sup, start_link, []}, Restart, Shutdown, supervisor, [tavla_sup]},
     OkeySup = {okey_sup, {okey_sup, start_link, []}, Restart, Shutdown, supervisor, [okey_sup]},
     LuckySup = {lucky_sup, {lucky_sup, start_link, []}, Restart, Shutdown, supervisor, [lucky_sup]},
-    {ok, {SupFlags, [IdGen,GameManager,LuckySup,AuthServer,GameLog,GameStats,TavlaSup,OkeySup]}}.
+    {ok, {SupFlags, [IdGen,LuckySup,AuthServer,GameLog,GameStats,TavlaSup,OkeySup]}}.
 

+ 0 - 0
apps/server/src/deck.erl → apps/server/src/lib/deck.erl


+ 0 - 0
apps/server/src/gas.erl → apps/server/src/lib/gas.erl


+ 0 - 1
apps/server/src/known_records.erl → apps/server/src/lib/known_records.erl

@@ -33,7 +33,6 @@ fields(game_paused) ->             record_info(fields, game_paused);
 fields(disconnect) ->              record_info(fields, disconnect);
 fields(player_left) ->             record_info(fields, player_left);
 fields(okey_debug) ->              record_info(fields, okey_debug);
-fields(session_attach_debug) ->    record_info(fields, session_attach_debug);
 fields('OkeyPiece') ->             record_info(fields, 'OkeyPiece');
 fields('OkeySeriesResult') ->      record_info(fields, 'OkeySeriesResult');
 fields('OkeyGameResults') ->       record_info(fields, 'OkeyGameResults');

+ 0 - 0
apps/server/src/midict.erl → apps/server/src/lib/midict.erl


+ 0 - 0
apps/server/src/nsg_crowd_lib.erl → apps/server/src/lib/nsg_crowd_lib.erl


+ 1 - 1
apps/server/src/nsg_trn_elimination.erl → apps/server/src/modules/elimination.erl

@@ -15,7 +15,7 @@
 %%%          to refer to a table directly - without pointing to a tournament.
 %%%          Type: integer()
 
--module(nsg_trn_elimination).
+-module(elimination).
 
 -behaviour(gen_fsm).
 %% --------------------------------------------------------------------

+ 9 - 9
apps/server/src/nsg_trn_lucky.erl → apps/server/src/modules/lucky.erl

@@ -15,7 +15,7 @@
 %%%          to refer to a table directly - without pointing to a tournament.
 %%%          Type: integer()
 
--module(nsg_trn_lucky).
+-module(lucky).
 
 -behaviour(gen_fsm).
 %% --------------------------------------------------------------------
@@ -365,8 +365,8 @@ handle_table_message(TableId, {round_finished, NewScoringState, RoundScore, _Tot
                      #state{game_id = GameId, tables = Tables, table_module = TableModule,
                            game_mode = GameMode, game = GameType, players = Players} = StateData)
   when is_integer(TableId) ->
-    gas:info(?MODULE,"TRN_LUCKY <~p> The <round_finished> notification received from table: ~p.",
-          [GameId, TableId]),
+    gas:info(?MODULE,"TRN_LUCKY <~p> The <round_finished> received from table: ~p~nScore: ~p.",
+          [GameId, TableId,RoundScore]),
 
     %% Add score per round
     UsersPoints = lists:flatten(
@@ -766,13 +766,13 @@ add_points_to_accounts(Points, GameId, GameType, GameMode) ->
                         id = GameId, double_points = 1,
                         type = game_end, tournament_type = ?TOURNAMENT_TYPE},    
     [begin
-         if GamePoints =/= 0 -> ok;
+         if GamePoints =/= 0 ->
 
-%                 kvs:add(#transaction{
-%                            id=kvs:next_id(transaction,1),
-%                            feed_id={game_points,UserId},
-%                            amount=GamePoints,
-%                            comment=TI});
+                 kvs:add(#transaction{
+                            id=kvs:next_id(transaction,1),
+                            feed_id={game_points,UserId},
+                            amount=GamePoints,
+                            comment=TI});
 
             true -> do_nothing
          end

+ 1 - 1
apps/server/src/nsg_matrix_elimination.erl → apps/server/src/modules/matrix.erl

@@ -5,7 +5,7 @@
 %%% Created : Feb 15, 2013
 %%% -------------------------------------------------------------------
 
--module(nsg_matrix_elimination).
+-module(matrix).
 
 -export([get_plan/4,
          get_prize_fund/4,

+ 0 - 0
apps/server/src/relay_ng.erl → apps/server/src/modules/relay_ng.erl


+ 1 - 1
apps/server/src/nsg_trn_standalone.erl → apps/server/src/modules/standalone.erl

@@ -15,7 +15,7 @@
 %%%          to refer to a table directly - without pointing to a tournament/game.
 %%%          Type: integer()
 
--module(nsg_trn_standalone).
+-module(standalone).
 
 -behaviour(gen_fsm).
 %% --------------------------------------------------------------------

+ 0 - 20
apps/server/src/okey/game_okey.erl

@@ -1,20 +0,0 @@
--module(game_okey).
--export([get_player_stats/1]).
--include_lib("server/include/basic_types.hrl").
--include_lib("server/include/game_okey.hrl").
-
-get_player_stats(PlayerId) ->
-    {ok, GameStats} = game_stats:get_game_points(okey, PlayerId),
-    {ok, Skill} = game_stats:get_skill(PlayerId),
-    {ok, PlayerStats} = game_stats:get_player_stats(PlayerId),
-    #'PlayerOkeyStats'{playerId = PlayerId,
-                       level = Skill,
-                       score = proplists:get_value(game_points, GameStats),
-                       numberOkey = proplists:get_value(finished_with_okey, GameStats),
-                       number8Tashes = proplists:get_value(finished_with_8_tashes, GameStats),
-                       totalWins = proplists:get_value(total_wins, PlayerStats),
-                       totalLose = proplists:get_value(total_loses, PlayerStats),
-                       totalDisconnects = proplists:get_value(total_disconnects, PlayerStats),
-                       overalSuccessRatio = proplists:get_value(overall_success_ratio, PlayerStats),
-                       averagePlayDuration = proplists:get_value(average_play_time, PlayerStats)
-                      }.

+ 0 - 9
apps/server/src/okey/game_okey_bot.erl

@@ -239,7 +239,6 @@ okey_client_loop(State) ->
                     okey_client_loop(State#state{hand = Hand1, gosterge = Gosterge});
                 {_, game_finished} ->
                     gas:info(?MODULE,"init bot: finished", []),
-                     okey_client_rematch(State),
                     okey_client_loop(State);
                 {_, do_okey_ready} ->
                     gas:info(?MODULE,"init bot: ready", []),
@@ -284,14 +283,6 @@ okey_client_round(<<"next_round">>, State) ->
 %    State2 = get_hand(State),
     okey_client_loop(State).
 
-okey_client_rematch(State) ->
-    S = State#state.conn,
-    GameId = State#state.gid,
-    gas:info(?MODULE,"sending rematch", []),
-    A = call_rpc(S, #rematch{game = GameId}),
-    gas:info(?MODULE,"rematch result: ~p", [A]),
-    ok = A,
-    okey_client_rematch2(State).
 
 okey_client_rematch2(State) ->
     S = State#state.conn,

+ 2 - 2
apps/server/src/okey/test_okey.erl

@@ -278,7 +278,7 @@ init_with_join_game(Owner, Host, Port, GameId, OwnId, Rematch, Mode) ->
     log(started),
     S1 = ?TCM:connect(Host, Port),
     TT = ?TEST_TOKEN,
-    #'PlayerInfo'{id = Id} = ?TCM:call_rpc(S1, #session_attach_debug{token = TT, id = OwnId}) ,
+    #'PlayerInfo'{id = Id} = ?TCM:call_rpc(S1, #session_attach{token = TT}) ,
     log(connected),
     Stats = ?TCM:call_rpc(S1, #get_player_stats{game_type = <<"okey">>, player_id = Id}) ,
     #'PlayerOkeyStats'{} = Stats,
@@ -295,7 +295,7 @@ init_with_join_game_observe(_Owner, Host, Port, GameId, OwnId, Mode, EJoinResult
     log(started),
     S1 = ?TCM:connect(Host, Port),
     TT = ?TEST_TOKEN,
-    #'PlayerInfo'{id = Id} = ?TCM:call_rpc(S1, #session_attach_debug{token = TT, id = OwnId}) ,
+    #'PlayerInfo'{id = Id} = ?TCM:call_rpc(S1, #session_attach{token = TT}) ,
     log(connected),
     Stats = ?TCM:call_rpc(S1, #get_player_stats{game_type = <<"okey">>, player_id = Id}) ,
     #'PlayerOkeyStats'{} = Stats,

+ 2 - 6
apps/server/src/server.app.src

@@ -3,11 +3,7 @@
   {description, "KKN Game Server"},
   {vsn, "1"},
   {registered, []},
-  {applications, [
-                  kernel,
-                  gproc,
-                  stdlib,db
-                 ]},
-  {mod, { nsg_games_app, []}},
+  {applications, [kernel,gproc,stdlib,db]},
+  {mod, { game_app, []}},
   {env, []}
  ]}.

+ 0 - 0
apps/server/src/game_log.erl → apps/server/src/sup/game_log.erl


+ 0 - 0
apps/server/src/game_stats.erl → apps/server/src/sup/game_stats.erl


+ 0 - 0
apps/server/src/id_generator.erl → apps/server/src/sup/id_generator.erl


+ 4 - 7
apps/server/src/lucky_sup.erl → apps/server/src/sup/lucky_sup.erl

@@ -44,8 +44,8 @@ init([]) ->
                   {bot_module, game_okey_bot},
                   {table_params, OkeyTableParams}
                  ],
-    OkeySpec = {okey_lucky, {nsg_trn_lucky, start_link, [OkeyGameId, OkeyParams]},
-                  Restart, Shutdown, worker, [nsg_trn_lucky]},
+    OkeySpec = {okey_lucky, {lucky, start_link, [OkeyGameId, OkeyParams]},
+                  Restart, Shutdown, worker, [lucky]},
 
 
     TavlaTableParams = [{mult_factor, 1},
@@ -76,11 +76,8 @@ init([]) ->
                    {bot_module, game_tavla_bot},
                    {table_params, TavlaTableParams}
                   ],
-    TavlaSpec = {tavla_lucky, {nsg_trn_lucky, start_link, [TavlaGameId, TavlaParams]},
-                  Restart, Shutdown, worker, [nsg_trn_lucky]},
-
-%%     TavlaSpec = {tavla_lucky, {fl_lucky, start_link, [TavlaGameId,[{game_type, game_tavla}, {mode, normal}]]}, 
-%%                   Restart, Shutdown, worker, [fl_lucky]},
+    TavlaSpec = {tavla_lucky, {lucky, start_link, [TavlaGameId, TavlaParams]},
+                  Restart, Shutdown, worker, [lucky]},
 
     {ok, { SupFlags, [OkeySpec, TavlaSpec]} }.
 

+ 2 - 2
apps/server/src/okey_sup.erl → apps/server/src/sup/okey_sup.erl

@@ -79,8 +79,8 @@ okey_standalone_specs(GamesNum, VirtUsersPerTable) ->
                                  {common_params, CommonParams} %% This data will used for the gproc register
                                 ],
                        {GameId,
-                        {nsg_trn_standalone, start_link, [GameId, Params]},
-                        _Restart = permanent, _Shutdown = 2000, worker, [nsg_trn_standalone]}
+                        {standalone, start_link, [GameId, Params]},
+                        _Restart = permanent, _Shutdown = 2000, worker, [standalone]}
                end,
            lists:map(F, lists:seq(1, GamesNum))
     end.

+ 2 - 2
apps/server/src/tavla_sup.erl → apps/server/src/sup/tavla_sup.erl

@@ -79,8 +79,8 @@ tavla_standalone_specs(GamesNum, VirtUsersPerTable) ->
                                  {common_params, CommonParams}
                                 ],
                        {GameId,
-                        {nsg_trn_standalone, start_link, [GameId, Params]},
-                        _Restart = permanent, _Shutdown = 2000, worker, [nsg_trn_standalone]}
+                        {standalone, start_link, [GameId, Params]},
+                        _Restart = permanent, _Shutdown = 2000, worker, [standalone]}
                end,
            lists:map(F, lists:seq(1, GamesNum))
     end.

+ 0 - 18
apps/server/src/tavla/game_tavla.erl

@@ -1,18 +0,0 @@
--module(game_tavla).
--export([get_player_stats/1]).
--include_lib("server/include/basic_types.hrl").
--include_lib("server/include/game_tavla.hrl").
-
-get_player_stats(PlayerId) ->
-    {ok, GameStats} = game_stats:get_game_points(okey, PlayerId),
-    {ok, Skill} = game_stats:get_skill(PlayerId),
-    {ok, PlayerStats} = game_stats:get_player_stats(PlayerId),
-    #'PlayerTavlaStats'{playerId = PlayerId,
-                       level = Skill,
-                       score = proplists:get_value(game_points, GameStats),
-                       totalWins = proplists:get_value(total_wins, PlayerStats),
-                       totalLose = proplists:get_value(total_loses, PlayerStats),
-                       totalDisconnects = proplists:get_value(total_disconnects, PlayerStats),
-                       overalSuccessRatio = proplists:get_value(overall_success_ratio, PlayerStats),
-                       averagePlayDuration = proplists:get_value(average_play_time, PlayerStats)
-                      }.

+ 1 - 7
apps/server/src/tavla/game_tavla_bot.erl

@@ -3,8 +3,7 @@
 -behaviour(gen_server).
 
 -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
--export([start/3, start_link/3, robot_init/1, init_state/2, join_game/1, get_session/1,
-         send_message/2, call_rpc/2, do_skip/2, do_rematch/1, first_move_table/0, follow_board/3 ]).
+-compile(export_all).
 
 -include_lib("server/include/basic_types.hrl").
 -include_lib("server/include/requests.hrl").
@@ -552,11 +551,6 @@ find_move(Color, OppColor, Die, Tactic, Board, [{Cell, No} | Rest]) ->
 
 % actions
 
-do_rematch(State) ->
-    S = State#state.conn,
-    GameId = State#state.gid,
-    ok = call_rpc(S, #rematch{game = GameId}).
-
 say_ready(State,TableId) ->
     S = State#state.conn,
     GameId = State#state.gid,

+ 1 - 1
apps/server/src/tavla/test_tavla.erl

@@ -67,7 +67,7 @@ attach_and_join(Owner, Host, Port, GameId, OwnId, Rematch, Mode) ->
             <<"maxim">> -> ?TEST_TOKEN;
             <<"alice">> -> ?TEST_TOKEN2
     end,
-    #'PlayerInfo'{id = Id} = ?TCM:call_rpc(S1, #session_attach_debug{token = TT, id = OwnId}) ,
+    #'PlayerInfo'{id = Id} = ?TCM:call_rpc(S1, #session_attach{token = TT}) ,
     log(connected),
     #'TableInfo'{game = _Atom} = ?TCM:call_rpc(S1, #join_game{game = GameId}) ,
     State = #state{conn = S1, gid = GameId, uid = Id, acker_fun = standard_acker(Owner)},