Browse Source

move accounting to KVS

Maxim Sokhatsky 11 years ago
parent
commit
8ab7f507d4

+ 0 - 118
apps/db/include/accounts.hrl

@@ -1,118 +0,0 @@
-%%----------------------------------------------------------------------
-%% @author Vladimir Baranov <vladimir.b.n.b@gmail.com>
-%% @copyright Paynet Internet ve Bilisim Hizmetleri A.S. All Rights Reserved.
-%% @doc
-%% Definitions file for accounts and transactions related records and macroses
-%% @end
-%%--------------------------------------------------------------------
-
--ifndef(ACCOUNTS_HRL).
--define(ACCOUNTS_HRL, "accounts_hrl").
-
--include_lib("kvs/include/kvs.hrl").
-
--type currency()         :: kakaush | quota | game_point | money.
-
-%% game events when points affected
--type game_event_type()  :: round_start | round_end | game_start | game_end | tour_start.
--type tournament_type()  :: standalone | elimination | pointing | lucky. %% FIXME: Copypasted from game_okey.hrl
--type game_name()        :: okey | tavla. %% TODO: add other game names
--type game_mode()        :: standard | evenodd | color | countdown | feellucky |
-                            esli | kakara. %%  TODO: add other game types
--type account_id()       :: {string(), currency()}. %% {username, currency}.
--type transaction_id()   :: string().
-
--define(SYSTEM_ACCOUNT_ID, system).
-
-%% common fields for all accounts
-
--record(account, {
-          ?ITERATOR(feed),
-          account_id          :: account_id(),
-          debet       :: integer(),
-          credit      :: integer(),
-          
-          %% can be negative. Store last change
-          %% to resolve conflicts just by applying
-          %% all changes to one of the conflicting
-          last_change :: integer()
-         }).
-
--define(ACC_ID(Account, Currency), {Account, Currency}). %% account id helper
-
--record(pointing_rule,
-		{
-          ?ITERATOR(feed),
-          pointing_rule_id,            %% {Game, GameType, Rounds} | {Game, GameType}
-          game,
-          game_type,
-          rounds,        %% rounds | points | undefined
-          kakush_winner, %% kakush points will be assigned for the winner of the game.
-          kakush_other,  %% kakush points will be assigned for the rest of the
-          
-          %% players other then winner, per player.
-          %% quota, no of quota will be charged to players.
-          quota,
-
-          %% game points will be assigned at the end of the game
-          %% to winner player
-          game_points    
-		}
-       ).
-
-%% Transaction info definitions. This records should be used as
-%% values in #transaction.info. 'ti' prefix = 'transaction info'.
-
--record(ti_game_event,
-        {
-          id                           :: integer(),         %% GameId
-          type                         :: game_event_type(),
-          game_name                    :: game_name(),
-          game_mode                    :: game_mode(),
-          double_points                :: integer(),
-          tournament_type = standalone :: tournament_type()
-        }
-       ).
-
--record(ti_payment,
-		{
-          id                         :: integer()
-		}).
-
--record(ti_admin_change,
-		{
-          reason                     :: binary()
-        }
-       ).
-
--record(ti_default_assignment,
-		{
-
-        }).
-
--type transaction_info() :: #ti_game_event{} | #ti_payment{} | #ti_admin_change{} | #ti_default_assignment{}.
-
--record(user_transaction, {user,top}).
-
--record(transaction,
-		{
-          id                         :: transaction_id(),
-          commit_time                :: erlang:now(),
-          amount                     :: integer(),    %% amount to move between accounts
-          remitter                   :: account_id(), %% accout that gives money/points
-          acceptor                   :: account_id(), %% account receive money/points
-          currency                   :: currency(),   %% some of the points or money
-          info                       :: transaction_info(),
-          next,
-          prev
-        }).
-
-%% Currencies
-
--define(CURRENCY_KAKUSH,               kakush).
--define(CURRENCY_KAKUSH_CURRENCY,      kakush_currency). %% used for gifts section, charged only when user buy package
--define(CURRENCY_MONEY,       money).
--define(CURRENCY_GAME_POINTS, game_point).
--define(CURRENCY_QUOTA,       quota).
-
--endif.

+ 18 - 0
apps/db/include/scoring.hrl

@@ -3,6 +3,14 @@
 
 -include_lib("kvs/include/kvs.hrl").
 
+-record(ti_game_event, {
+        id                           :: integer(),         %% GameId
+        type,
+        game_name,
+        game_mode,
+        double_points,
+        tournament_type = standalone }).
+
 -record(player_scoring, {
           ?ITERATOR(feed),
           temp, 
@@ -38,4 +46,14 @@
           average_time = 0
          }).
 
+-record(pointing_rule, {?ITERATOR(feed),
+        pointing_rule_id,            %% {Game, GameType, Rounds} | {Game, GameType}
+        game,
+        game_type,
+        rounds,        %% rounds | points | undefined
+        kakush_winner, %% kakush points will be assigned for the winner of the game.
+        kakush_other,  %% kakush points will be assigned for the rest of the
+        quota,
+        game_points}).
+
 -endif.

+ 0 - 9
apps/db/include/table.hrl

@@ -47,13 +47,4 @@
           robots_replacement_allowed = true :: boolean()
         }).
 
--record(save_game_table,
-        {
-          ?ITERATOR(feed),
-          uid :: username_type() | '_', %% Dialyzer and record MatchSpec warnings http://j.mp/vZ8670
-          name,
-          create_time,
-          settings
-         }
-       ).
 -endif.

+ 2 - 3
apps/db/src/db_accounts.erl

@@ -1,12 +1,11 @@
 -module(db_accounts).
 -include_lib("kvs/include/metainfo.hrl").
--include_lib("db/include/accounts.hrl").
+-include_lib("db/include/transaction.hrl").
 -compile(export_all).
 
 metainfo() ->
     #schema{name = kvs, tables = 
                 [
-                 #table{name = account, fields=record_info(fields, account)},
-                 #table{name = pointing_rule, fields=record_info(fields, pointing_rule)}
+                 #table{name = transaction, fields=record_info(fields, transaction)}
                 ]
            }.

+ 1 - 0
apps/db/src/db_scoring.erl

@@ -8,6 +8,7 @@ metainfo() ->
                 [
                  #table{name = player_scoring, fields=record_info(fields, player_scoring)},
                  #table{name = scoring_record, fields=record_info(fields, scoring_record)},
+                 #table{name = pointing_rule,  fields=record_info(fields, pointing_rule)},
                  #table{name = personal_score, fields=record_info(fields, personal_score)}
                 ]
            }.

+ 1 - 2
apps/db/src/db_table.erl

@@ -6,7 +6,6 @@
 metainfo() ->
     #schema{name = kvs, tables = 
                 [
-                 #table{name = game_table, fields=record_info(fields, game_table)},
-                 #table{name = save_game_table, fields=record_info(fields, save_game_table)}
+                 #table{name = game_table, fields=record_info(fields, game_table)}
                 ]
            }.

+ 0 - 13
apps/server/src/auth_server.erl

@@ -55,7 +55,6 @@ robot_credentials() -> gen_server:call(?SERVER, {robot_credentials}).
 
 init([]) ->
     Tokens = ets:new(tokens, [private, ordered_set, {keypos, #authtoken.token}]),
-    wf:reg(system),
     store_token(0,Tokens, <<?TEST_TOKEN>>, "maxim"),
     store_token(0,Tokens, <<?TEST_TOKEN2>>, "alice"),
     {ok, #state{tokens = Tokens}}.
@@ -104,22 +103,10 @@ handle_call({fake_credentials}, _From, #state{spare = Spare} = State) -> H = fak
 handle_call({robot_credentials}, _From, #state{spare = Spare} = State) -> H = fake_credentials0(Spare), {reply, H#'PlayerInfo'{robot = true}, State};
 handle_call(_Request, _From, State) -> Reply = ok, {reply, Reply, State}.
 handle_cast(_Msg, State) -> {noreply, State}.
-handle_info({system,Route,Message}, State) ->
-    handle_system(Route,Message),
-    {noreply, State};
 handle_info(_Info, State) -> {noreply, State}.
 terminate(_Reason, _State) -> ok.
 code_change(_OldVsn, State, _Extra) -> {ok, State}.
 
-handle_system([tournament,T,cancel],Message) -> ok;
-handle_system([tournament,T,activate],Message) -> ok;
-handle_system([personal_score,user,U,add],Message) -> ok;
-handle_system([system,game_end_note,U,add],Message) -> ok;
-handle_system([system,tournament_tour_note,T],Message) -> ok;
-handle_system([system,tournament_ends_note,T],Message) -> ok;
-handle_system([system,game_ends_note,T],Message) -> ok;
-handle_system(Route,Message) -> ok.
-
 fake_credentials0(Spare) ->
     Pos = crypto:rand_uniform(1, length(Spare)),
     H0 = lists:nth(Pos, Spare),

+ 1 - 1
apps/server/src/game_manager.erl

@@ -9,7 +9,7 @@
 -include_lib("db/include/tournaments.hrl").
 -include_lib("server/include/log.hrl").
 -include_lib("stdlib/include/qlc.hrl").
--include_lib("db/include/accounts.hrl").
+-include_lib("db/include/scoring.hrl").
 -record(state, { game_tavla = 0, game_okey = 0 }).
 
 destroy_game(Pid,Sup) -> game_sup:stop_game(Sup,Pid).

+ 35 - 15
apps/server/src/game_stats.erl

@@ -3,14 +3,14 @@
 
 -export([start_link/0, add_game/1, get_skill/1, get_game_points/2, get_player_stats/1,
          init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3,
-         assign_points/2, is_feel_lucky/1, game_info_to_ti/1, charge_quota/1]).
+         assign_points/2, is_feel_lucky/1, charge_quota/1]).
 
 -include_lib("server/include/basic_types.hrl").
 -include_lib("server/include/game_okey.hrl").
 -include_lib("server/include/game_tavla.hrl").
 -include_lib("server/include/log.hrl").
 -include_lib("server/include/games.hrl").
--include_lib("db/include/accounts.hrl").
+-include_lib("db/include/transaction.hrl").
 -include_lib("db/include/scoring.hrl").
 
 
@@ -49,7 +49,9 @@ get_player_stats(UserId) -> {ok, [{total_games,crypto:rand_uniform(1,10)},
                                   {overal_success_ratio,crypto:rand_uniform(1,100)},
                                   {average_play_time,crypto:rand_uniform(1000,5000)}]}.
 
-init([]) -> {ok, no_state}.
+init([]) ->
+    wf:ref(stats),
+    {ok, no_state}.
 
 handle_call(Request, From, State) ->
     error_logger:error_msg("unknown call ~p ~p ~n", [Request, From]),
@@ -57,19 +59,33 @@ handle_call(Request, From, State) ->
 
 handle_cast({add_game, Game}, State) -> {noreply, State};
 handle_cast(Msg, State) -> error_logger:error_msg("unknown cast ~p ~n", [Msg]), {noreply, State}.
+handle_info({stats,Route,Message}, State) ->
+    handle_stats(Route,Message),
+    {noreply, State};
+
 handle_info(Info, State) -> error_logger:error_msg("unknown info ~p~n", [Info]), {noreply, State}.
+
+handle_stats([tournament,T,cancel],Message) -> ok;
+handle_stats([tournament,T,activate],Message) -> ok;
+handle_stats([personal_score,user,U,add],Message) -> ok;
+handle_stats([system,game_end_note,U,add],Message) -> ok;
+handle_stats([system,tournament_tour_note,T],Message) -> ok;
+handle_stats([system,tournament_ends_note,T],Message) -> ok;
+handle_stats([system,game_ends_note,T],Message) -> ok;
+handle_stats(Route,Message) -> ok.
+
 terminate(_Reason, _State) -> ok.
 code_change(_OldVsn, State, _Extra) -> {ok, State}.
 
 is_feel_lucky(GameInfo) ->
     proplists:get_value(lucky, GameInfo,false).
 
-game_info_to_ti(GameInfo) ->
-    #ti_game_event{game_name = okey,
-                   game_mode = proplists:get_value(mode, GameInfo),
-                   id = proplists:get_value(id, GameInfo),
-                   double_points = proplists:get_value(double_points, GameInfo)
-                  }.
+%game_info_to_ti(GameInfo) ->
+%    #ti_game_event{game_name = okey,
+%                   game_mode = proplists:get_value(mode, GameInfo),
+%                   id = proplists:get_value(id, GameInfo),
+%                   double_points = proplists:get_value(double_points, GameInfo)
+%                  }.
 
 
 charge_quota(GameInfo) ->
@@ -77,7 +93,7 @@ charge_quota(GameInfo) ->
     PRLucky   = proplists:get_value(pointing_rules_lucky, GameInfo),
     Players   = proplists:get_value(initial_players, GameInfo),
     Double    = proplists:get_value(double_points, GameInfo),
-    TI = game_info_to_ti(GameInfo),
+%    TI = game_info_to_ti(GameInfo),
     PR = pointing_rules:double_points(PR0, Double),
     [begin
          UId = user_id_to_string(U#'PlayerInfo'.id),
@@ -85,7 +101,9 @@ charge_quota(GameInfo) ->
                       true -> PRLucky#pointing_rule.quota;
                       _ -> PR#pointing_rule.quota
                   end,
-        ok = nsm_accounts:transaction(UId, ?CURRENCY_QUOTA, -Amount, TI#ti_game_event{type = game_start})
+
+        kvs:add(#transaction{id=kvs:next_id(transaction,1),feed_id={quota,UId},comment=game_start})
+
      end || U  <- Players].
 
 assign_points(#'TavlaGameResults'{players = Results}, GameInfo) ->
@@ -105,7 +123,7 @@ assign_points(RawResults, GameInfo) ->
     PRLucky  = proplists:get_value(pointing_rules_lucky, GameInfo),
     Players  = proplists:get_value(initial_players, GameInfo),
     Double   = proplists:get_value(double_points, GameInfo),
-    TI = game_info_to_ti(GameInfo),
+%    TI = game_info_to_ti(GameInfo),
 
     PR1 = pointing_rules:double_points(PR0, Double),
 
@@ -164,11 +182,13 @@ assign_points(RawResults, GameInfo) ->
          end,
          if not Robot ->
                 if KakushPoints /= 0 ->
-                       ok = nsm_accounts:transaction(UserId, ?CURRENCY_KAKUSH, KakushPoints, TI#ti_game_event{type = game_end});
+        kvs:add(#transaction{id=kvs:next_id(transaction,1),feed_id={kakush,UserId},comment=game_end});
+%                       ok = nsm_accounts:transaction(UserId, ?CURRENCY_KAKUSH, KakushPoints, TI#ti_game_event{type = game_end});
                    true -> ok
                 end,
                 if GamePoints /= 0 ->
-                        ok = nsm_accounts:transaction(UserId, ?CURRENCY_GAME_POINTS, GamePoints, TI#ti_game_event{type = game_end});
+        kvs:add(#transaction{id=kvs:next_id(transaction,1),feed_id={game_points,UserId},comment=game_end});
+%                        ok = nsm_accounts:transaction(UserId, ?CURRENCY_GAME_POINTS, GamePoints, TI#ti_game_event{type = game_end});
                    true -> ok
                 end;
             true -> do_nothing %% no points for robots
@@ -190,7 +210,7 @@ is_bot(UserId, Players) ->
         _ -> true % If UserId is not found then the player is a replaced bot. 
     end.
 
-is_paid(UserId) -> nsm_accounts:user_paid(UserId).
+is_paid(UserId) -> true. %nsm_accounts:user_paid(UserId).
 
 user_id_to_string(UserId) -> binary_to_list(UserId).
 

+ 9 - 2
apps/server/src/nsg_trn_elimination.erl

@@ -24,7 +24,8 @@
 -include_lib("server/include/log.hrl").
 -include_lib("server/include/basic_types.hrl").
 -include_lib("db/include/table.hrl").
--include_lib("db/include/accounts.hrl").
+-include_lib("db/include/transaction.hrl").
+-include_lib("db/include/scoring.hrl").
 
 %% --------------------------------------------------------------------
 %% External exports
@@ -704,7 +705,13 @@ deduct_quota(GameId, GameType, GameMode, Amount, UsersIds) ->
     TI = #ti_game_event{game_name = GameType, game_mode = GameMode,
                         id = GameId, double_points = 1,
                         type = start_tour, tournament_type = ?TOURNAMENT_TYPE},
-    [nsm_accounts:transaction(binary_to_list(UserId), ?CURRENCY_QUOTA, -Amount, TI)
+    [
+%        nsm_accounts:transaction(binary_to_list(UserId), ?CURRENCY_QUOTA, -Amount, TI)
+    kvs:add(#transaction{
+        id=kvs:next_id(transaction,1),
+        feed_id={quota,binary_to_list(UserId)},
+        comment=TI})
+
        || UserId <- UsersIds],
     ok.
 

+ 26 - 4
apps/server/src/nsg_trn_standalone.erl

@@ -24,7 +24,8 @@
 -include_lib("server/include/log.hrl").
 -include_lib("server/include/basic_types.hrl").
 -include_lib("db/include/table.hrl").
--include_lib("db/include/accounts.hrl").
+-include_lib("db/include/scoring.hrl").
+-include_lib("db/include/transaction.hrl").
 -include_lib("eunit/include/eunit.hrl").
 
 %% --------------------------------------------------------------------
@@ -1026,7 +1027,14 @@ deduct_quota(GameId, GameType, GameMode, Amount, MulFactor, UsersIds) ->
          TI = #ti_game_event{game_name = GameType, game_mode = GameMode,
                              id = GameId, double_points = MulFactor,
                              type = start_round, tournament_type = ?TOURNAMENT_TYPE},
-         nsm_accounts:transaction(binary_to_list(UserId), ?CURRENCY_QUOTA, -RealAmount, TI)
+        kvs:add(#transaction{
+            id=kvs:next_id(transaction,1),
+            feed_id={quota,binary_to_list(UserId)},
+            amount=-RealAmount,
+            comment=TI})
+
+%         nsm_accounts:transaction(binary_to_list(UserId), ?CURRENCY_QUOTA, -RealAmount, TI)
+
      end || UserId <- UsersIds],
     ok.
 
@@ -1039,11 +1047,25 @@ add_points_to_accounts(Points, GameId, GameType, GameMode, MulFactor) ->
                         type = game_end, tournament_type = ?TOURNAMENT_TYPE},
     [begin
          if KakushPoints =/= 0 ->
-                ok = nsm_accounts:transaction(UserId, ?CURRENCY_KAKUSH, KakushPoints, TI);
+
+        kvs:add(#transaction{
+            id=kvs:next_id(transaction,1),
+            feed_id={kakush,UserId},
+            amount=KakushPoints,
+            comment=TI});
+
+%                ok = nsm_accounts:transaction(UserId, ?CURRENCY_KAKUSH, KakushPoints, TI);
             true -> do_nothing
          end,
          if GamePoints =/= 0 ->
-                ok = nsm_accounts:transaction(UserId, ?CURRENCY_GAME_POINTS, GamePoints, TI);
+
+        kvs:add(#transaction{
+            id=kvs:next_id(transaction,1),
+            feed_id={game_points,UserId},
+            amount=GamePoints,
+            comment=TI});
+
+%                ok = nsm_accounts:transaction(UserId, ?CURRENCY_GAME_POINTS, GamePoints, TI);
             true -> do_nothing
          end
      end || {UserId, KakushPoints, GamePoints} <- Points],

+ 6 - 13
apps/server/src/nsm_auth.erl

@@ -2,7 +2,7 @@
 -compile(export_all).
 -include_lib("db/include/config.hrl").
 -include_lib("kvs/include/user.hrl").
--include_lib("db/include/user.hrl").
+-include_lib("db/include/user_info.hrl").
 -include_lib("server/include/log.hrl").
 
 login(Data) ->
@@ -71,19 +71,12 @@ build_user_info(#user{username = UserName,
                avatar_url = get_avatar(User, small),
                sex = Sex}.
 
-get_avatar(#user{avatar = Avatar}, Size) ->
-    get_avatar(Avatar, Size);
+get_avatar(#user{avatar = Avatar}, Size) -> get_avatar(Avatar, Size);
 get_avatar(Avatar, Size) ->
-    case Avatar of
-        #avatar{big = Big} when Size =:= big -> Big;
-        #avatar{small = Small} when Size =:= small -> Small;
-        #avatar{tiny = Tiny} when Size =:= tiny -> Tiny;
-        _ -> case Size of
-                 big -> "/images/no_avatar_big.jpg";
-                 small -> "/images/no_avatar_small.jpg";
-                 tiny -> "/images/no_avatar_tiny.jpg"
-             end
-    end.
+    case Size of
+         big -> "/images/no_avatar_big.jpg";
+         small -> "/images/no_avatar_small.jpg";
+         tiny -> "/images/no_avatar_tiny.jpg" end.
 
 names() ->
    ["pinar","betul","eda","lale","ilgin","alp","ayberk","vural","ozan","doruk",

+ 11 - 4
apps/server/src/tavla/game_tavla_ng_trn_paired.erl

@@ -24,7 +24,8 @@
 -include_lib("server/include/log.hrl").
 -include_lib("server/include/basic_types.hrl").
 -include_lib("db/include/table.hrl").
--include_lib("db/include/accounts.hrl").
+-include_lib("db/include/transaction.hrl").
+-include_lib("db/include/scoring.hrl").
 -include_lib("server/include/game_tavla.hrl").
 
 %% --------------------------------------------------------------------
@@ -765,7 +766,9 @@ deduct_quota(GameId, GameType, GameMode, Amount, MulFactor, UsersIds) ->
          TI = #ti_game_event{game_name = GameType, game_mode = GameMode,
                              id = GameId, double_points = MulFactor,
                              type = start_round, tournament_type = ?TOURNAMENT_TYPE},
-         nsm_accounts:transaction(binary_to_list(UserId), ?CURRENCY_QUOTA, -RealAmount, TI)
+        kvs:add(#transaction{id=kvs:next_id(transaction,1),
+            feed_id={quota,binary_to_list(UserId)},amount=-RealAmount,comment=TI})
+%         nsm_accounts:transaction(binary_to_list(UserId), ?CURRENCY_QUOTA, -RealAmount, TI)
      end || UserId <- UsersIds],
     ok.
 
@@ -824,11 +827,15 @@ add_points_to_accounts(Points, GameId, GameType, GameMode, MulFactor) ->
                         type = game_end, tournament_type = ?TOURNAMENT_TYPE},
     [begin
          if KakushPoints =/= 0 ->
-                ok = nsm_accounts:transaction(UserId, ?CURRENCY_KAKUSH, KakushPoints, TI);
+        kvs:add(#transaction{id=kvs:next_id(transaction,1),
+            feed_id={kakush,UserId},amount=KakushPoints,comment=TI});
+%                ok = nsm_accounts:transaction(UserId, ?CURRENCY_KAKUSH, KakushPoints, TI);
             true -> do_nothing
          end,
          if GamePoints =/= 0 ->
-                ok = nsm_accounts:transaction(UserId, ?CURRENCY_GAME_POINTS, GamePoints, TI);
+        kvs:add(#transaction{id=kvs:next_id(transaction,1),
+            feed_id={game_points,UserId},amount=GamePoints,comment=TI});
+%                ok = nsm_accounts:transaction(UserId, ?CURRENCY_GAME_POINTS, GamePoints, TI);
             true -> do_nothing
          end
      end || {UserId, KakushPoints, GamePoints} <- Points],