Browse Source

game_stats really don't needed

Maxim Sokhatsky 11 years ago
parent
commit
471bc1d5c1

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

@@ -11,41 +11,6 @@
         double_points,
         double_points,
         tournament_type = standalone }).
         tournament_type = standalone }).
 
 
--record(player_scoring, {
-          ?ITERATOR(feed),
-          temp, 
-          permanent, %% top of Permanent Scoring record linked list
-          %%-- aggregated score for all game types is is a list
-          %%  [{game_okey, 15},{game_tavla, 12},...]
-          agregated_score 
-         }).
-
--record(scoring_record, {
-          ?ITERATOR(feed),
-          game_id,     %% game id for rematching and lost connections
-          who,         %% player
-          all_players, %% with other players
-          game_type,   %% okey, tavla, batak 
-          game_kind,   %% chanak, standard, even-odd
-          condition,   %% reveal with even tashes, color okey reveal, show gosterge, batak 3 aces, tavla mars.
-          score_points,       %% result score points for player
-          score_kakaush,      %% result score kakuş for player
-          custom,      %% erlang record for a specific game
-          timestamp    %% now() of the record
-    }).
-
-%% total count of everything
--record(personal_score, { 
-          ?ITERATOR(feed),
-          uid,
-          games = 0,  
-          wins = 0,
-          loses = 0,
-          disconnects = 0,
-          points = 0,
-          average_time = 0
-         }).
-
 -record(pointing_rule, {?ITERATOR(feed),
 -record(pointing_rule, {?ITERATOR(feed),
         pointing_rule_id,            %% {Game, GameType, Rounds} | {Game, GameType}
         pointing_rule_id,            %% {Game, GameType, Rounds} | {Game, GameType}
         game,
         game,

+ 67 - 8
apps/db/src/db_scoring.erl

@@ -4,11 +4,70 @@
 -compile(export_all).
 -compile(export_all).
 
 
 metainfo() ->
 metainfo() ->
-    #schema{name = kvs, tables = 
-                [
-                 #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)}
-                ]
-           }.
+    #schema{name = kvs, tables = [
+        #table{name = pointing_rule,  fields=record_info(fields, pointing_rule)}]}.
+
+setup() ->
+
+    %% OKEY
+
+    Rounds5 = #pointing_rule{rounds = 5, game = okey, kakush_winner = 15, kakush_other = 3, quota = 5},
+    kvs:put(Rounds5#pointing_rule{id = {okey, standard, 5}, game_type = standard, game_points = 1}),
+    kvs:put(Rounds5#pointing_rule{id = {okey, evenodd,  5}, game_type = evenodd,  game_points = 5}),
+    kvs:put(Rounds5#pointing_rule{id = {okey, color,    5}, game_type = color,    game_points = 7}),
+
+    Rounds10 = #pointing_rule{rounds = 10, game = okey, kakush_winner = 15, kakush_other = 3, quota = 10},
+    kvs:put(Rounds10#pointing_rule{id = {okey, standard, 10}, game_type = standard, game_points = 7}),
+    kvs:put(Rounds10#pointing_rule{id = {okey, evenodd,  10}, game_type = evenodd,  game_points = 10}),
+    kvs:put(Rounds10#pointing_rule{id = {okey, color,    10}, game_type = color,    game_points = 15}),
+
+    Rounds20 = #pointing_rule{rounds = 20, game = okey, kakush_winner = 15, kakush_other = 3, quota = 20},
+    kvs:put(Rounds20#pointing_rule{id = {okey, standard, 20}, game_type = standard, game_points = 15}),
+    kvs:put(Rounds20#pointing_rule{id = {okey, evenodd,  20}, game_type = evenodd,  game_points = 20}),
+    kvs:put(Rounds20#pointing_rule{id = {okey, color,    20}, game_type = color,    game_points = 30}),
+
+    Rounds40 = #pointing_rule{rounds = 40, game = okey, kakush_winner = 32, kakush_other = 7, quota = 40},
+    kvs:put(Rounds40#pointing_rule{id = {okey, standard, 40}, game_type = standard, game_points = 30}),
+    kvs:put(Rounds40#pointing_rule{id = {okey, evenodd,  40}, game_type = evenodd,  game_points = 40}),
+    kvs:put(Rounds40#pointing_rule{id = {okey, color,    40}, game_type = color,    game_points = 60}),
+
+    Rounds60 = #pointing_rule{rounds = 60, game = okey, kakush_winner = 32, kakush_other = 7, quota = 40},
+    kvs:put(Rounds60#pointing_rule{id = {okey, standard, 60}, game_type = standard, game_points = 60}),
+    kvs:put(Rounds60#pointing_rule{id = {okey, evenodd,  60}, game_type = evenodd,  game_points = 80}),
+    kvs:put(Rounds60#pointing_rule{id = {okey, color,    60}, game_type = color,    game_points = 120}),
+
+    Rounds80 = #pointing_rule{rounds = 80, game = okey, kakush_winner = 70, kakush_other = 15, quota = 80},
+    kvs:put(Rounds80#pointing_rule{id = {okey, standard, 80}, game_type = standard, game_points = 120}),
+    kvs:put(Rounds80#pointing_rule{id = {okey, evenodd,  80}, game_type = evenodd,  game_points = 160}),
+    kvs:put(Rounds80#pointing_rule{id = {okey, color,    80}, game_type = color,    game_points = 240}),
+
+    %% Countdown 10
+    kvs:put(#pointing_rule{id = {okey, countdown}, game = okey, game_type = countdown,
+         kakush_winner = 6, kakush_other = 1, quota = 8, game_points = 10}),
+
+    %% Feel lucky
+    kvs:put(#pointing_rule{id = {okey, feellucky}, game = okey, game_type = feellucky,
+         kakush_winner = 0, kakush_other = 0, quota = 0, game_points = 0}),
+
+    %% TAVLA
+
+    GameTypes = [standard, evenodd],
+    %% here we store points in rounds field
+    [kvs:put(#pointing_rule{id = {tavla, GT, 3}, rounds = 3, game = tavla, game_type = GT,
+          kakush_winner = 1, kakush_other = 1, quota = 3, game_points = 5})
+       || GT <- GameTypes],
+
+
+    [kvs:put(#pointing_rule{id = {tavla, GT, 5}, rounds = 5, game = tavla, game_type = GT,
+          kakush_winner = 2, kakush_other = 1, quota = 4, game_points = 5})
+       || GT <- GameTypes],
+
+    [kvs:put(#pointing_rule{id = {tavla, GT, 7}, rounds = 7, game = tavla, game_type = GT,
+          kakush_winner = 3, kakush_other = 1, quota = 5, game_points = 7})
+       || GT <- GameTypes],
+
+    %% Kakara
+
+    kvs:put(#pointing_rule{id = {tavla, kakara}, game = tavla, game_type = kakara,
+         kakush_winner = 0, kakush_other = 0, quota = 1, game_points = 0}).
+

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

@@ -20,9 +20,8 @@ init([]) ->
     IdGen = {id_generator, {id_generator, start_link, []},  Restart, Shutdown, worker, [id_generator]},
     IdGen = {id_generator, {id_generator, start_link, []},  Restart, Shutdown, worker, [id_generator]},
     AuthServer = {auth_server, {auth_server, start_link, []}, Restart, Shutdown, worker, [auth_server]},
     AuthServer = {auth_server, {auth_server, start_link, []}, Restart, Shutdown, worker, [auth_server]},
     GameLog = {game_log, {game_log, start_link, []}, Restart, Shutdown, worker, [game_log]},
     GameLog = {game_log, {game_log, start_link, []}, Restart, Shutdown, worker, [game_log]},
-    GameStats = {game_stats, {game_stats, start_link, []}, Restart, Shutdown, worker, [game_stats]},
     TavlaSup = {tavla_sup, {tavla_sup, start_link, []}, Restart, Shutdown, supervisor, [tavla_sup]},
     TavlaSup = {tavla_sup, {tavla_sup, start_link, []}, Restart, Shutdown, supervisor, [tavla_sup]},
     OkeySup = {okey_sup, {okey_sup, start_link, []}, Restart, Shutdown, supervisor, [okey_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]},
     LuckySup = {lucky_sup, {lucky_sup, start_link, []}, Restart, Shutdown, supervisor, [lucky_sup]},
-    {ok, {SupFlags, [IdGen,LuckySup,AuthServer,GameLog,GameStats,TavlaSup,OkeySup]}}.
+    {ok, {SupFlags, [IdGen,LuckySup,AuthServer,GameLog,TavlaSup,OkeySup]}}.
 
 

+ 2 - 183
apps/server/src/sup/game_stats.erl

@@ -1,9 +1,7 @@
 -module(game_stats).
 -module(game_stats).
 -behaviour(gen_server).
 -behaviour(gen_server).
-
--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, charge_quota/1]).
+-compile(export_all).
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
 
 
 -include_lib("server/include/basic_types.hrl").
 -include_lib("server/include/basic_types.hrl").
 -include_lib("server/include/game_okey.hrl").
 -include_lib("server/include/game_okey.hrl").
@@ -11,41 +9,7 @@
 -include_lib("db/include/transaction.hrl").
 -include_lib("db/include/transaction.hrl").
 -include_lib("db/include/scoring.hrl").
 -include_lib("db/include/scoring.hrl").
 
 
-
--record(raw_result,
-        {player_id :: binary(),
-         winner    :: boolean(),
-         score     :: integer(),
-         pos       :: integer()
-        }).
-
--record(result,
-        {player_id :: string(),
-         robot     :: boolean(),
-         winner    :: boolean(),
-         score     :: integer(),
-         pos       :: integer(),
-         kakush_points :: integer(),
-         game_points :: integer()
-        }).
-
 start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
 start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
-add_game(Game) -> gen_server:cast(?MODULE, {add_game, Game}).
-
-get_skill(UserId) when is_binary(UserId) -> get_skill(binary_to_list(UserId));
-get_skill(UserId) -> {ok, crypto:rand_uniform(1,1000)}.
-
-get_game_points(GameType, UserId) when is_binary(UserId) -> get_game_points(GameType, binary_to_list(UserId));
-get_game_points(GameType, UserId) -> {ok, [{game_points,crypto:rand_uniform(1,1000)},
-                                           {finished_with_okey,crypto:rand_uniform(1,1000)},
-                                           {finished_with_8_tashes,crypto:rand_uniform(1,1000)}]}.
-
-get_player_stats(UserId) when is_binary(UserId) -> get_player_stats(binary_to_list(UserId));
-get_player_stats(UserId) -> {ok, [{total_games,crypto:rand_uniform(1,10)},
-                                  {total_wins,crypto:rand_uniform(1,10)},
-                                  {total_loses,crypto:rand_uniform(1,10)},
-                                  {overal_success_ratio,crypto:rand_uniform(1,100)},
-                                  {average_play_time,crypto:rand_uniform(1000,5000)}]}.
 
 
 init([]) ->
 init([]) ->
     wf:reg(stats),
     wf:reg(stats),
@@ -55,7 +19,6 @@ handle_call(Request, From, State) ->
     error_logger:error_msg("unknown call ~p ~p ~n", [Request, From]),
     error_logger:error_msg("unknown call ~p ~p ~n", [Request, From]),
     {noreply, State}.
     {noreply, 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_cast(Msg, State) -> error_logger:error_msg("unknown cast ~p ~n", [Msg]), {noreply, State}.
 handle_info({stats,Route,Message}, State) ->
 handle_info({stats,Route,Message}, State) ->
     wf:info("Stats: Route: ~p Message: ~p~n",[Route,Message]),
     wf:info("Stats: Route: ~p Message: ~p~n",[Route,Message]),
@@ -76,147 +39,3 @@ handle_stats(Route,Message) -> ok.
 terminate(_Reason, _State) -> ok.
 terminate(_Reason, _State) -> ok.
 code_change(_OldVsn, State, _Extra) -> {ok, State}.
 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)
-%                  }.
-
-
-charge_quota(GameInfo) ->
-    PR0       = proplists:get_value(pointing_rules, 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),
-    PR = pointing_rules:double_points(PR0, Double),
-    [begin
-         UId = user_id_to_string(U#'PlayerInfo'.id),
-         Amount = case is_feel_lucky(GameInfo) of
-                      true -> PRLucky#pointing_rule.quota;
-                      _ -> PR#pointing_rule.quota
-                  end,
-
-        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) ->
-    ConvertedResults = [ #raw_result{winner = Winner == <<"true">>, player_id = PlayerId, score = Score,
-                                     pos = if Winner == <<"true">> -> 1; true -> 2 end}
-                        || #'TavlaPlayerScore'{winner = Winner, player_id = PlayerId, score = Score} <- Results],
-    assign_points(ConvertedResults, GameInfo);
-
-assign_points(#'OkeyGameResults'{series_results = Results}, GameInfo) ->
-    ConvertedResults = [ #raw_result{winner = Winner == <<"true">>, player_id = PlayerId, score = Score, pos = Pos}
-                        || #'OkeySeriesResult'{winner = Winner, player_id = PlayerId, score = Score, place = Pos} <- Results],
-    assign_points(ConvertedResults, GameInfo);
-
-assign_points(RawResults, GameInfo) ->
-    GameId   = proplists:get_value(id, GameInfo),
-    PR0      = proplists:get_value(pointing_rules, 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),
-
-    PR1 = pointing_rules:double_points(PR0, Double),
-
-    PlayersIds = [if Robot -> user_id_to_string(UserId);
-                     true -> "(robot)"
-                  end || #'PlayerInfo'{id = UserId, robot = Robot} <- Players],
-
-    #pointing_rule{
-                   kakush_winner = KakushWinner,
-                   kakush_other = KakushOther,
-                   game_points = WinGamePoints
-                  } = case is_feel_lucky(GameInfo) of true -> PRLucky; false -> PR1 end,
-
-    Bots    = [UserId || #raw_result{player_id = UserId} <- RawResults, is_bot(UserId, Players)],
-    Paids   = [UserId || #raw_result{player_id = UserId} <- RawResults, is_paid(UserId)],
-    Winners = [UserId || #raw_result{player_id = UserId, winner = true} <- RawResults],
-    TotalNum = length(RawResults),
-    PaidsNum = length(Paids),
-    WinnersNum = length(Winners),
-    KakushPerWinner = round((KakushWinner * PaidsNum div TotalNum) / WinnersNum),
-    KakushPerLoser = KakushOther * PaidsNum div TotalNum,
-    gas:info(?MODULE,"GAME_STATS <~p> KakushWinner: ~p KakushOther: ~p", [GameId, KakushWinner, KakushOther]),
-    gas:info(?MODULE,"GAME_STATS <~p> KakushPerWinner: ~p KakushPerLoser: ~p", [GameId, KakushPerWinner, KakushPerLoser]),
-    Results = [begin
-                   Robot = lists:member(UserId, Bots),
-                   Paid = lists:member(UserId, Paids),
-                   {KakushPoints, GamePoints} = calc_points(KakushPerWinner, KakushPerLoser,
-                                                            WinGamePoints, Paid, Robot, Winner),
-                   #result{player_id = user_id_to_string(UserId), robot = Robot, winner = Winner,
-                           pos = Pos, kakush_points = KakushPoints, game_points = GamePoints}
-               end || #raw_result{player_id = UserId, winner = Winner, pos = Pos} <- RawResults],
-    gas:info(?MODULE,"GAME_STATS <~p> Results: ~p", [GameId, Results]),
-    [begin
-         if not Robot ->
-                SR = #scoring_record{
-                                     game_id = proplists:get_value(id, GameInfo),
-                                     who = UserId,
-                                     all_players = PlayersIds,
-                                     game_type = PR0#pointing_rule.game_type,
-                                     game_kind = PR0#pointing_rule.game,
-%                                     condition, % where'd I get that?
-                                     score_points = GamePoints,
-                                     score_kakaush = KakushPoints,
-%                                     custom,    % no idea what to put here
-                                     timestamp = erlang:now()
-                                    },
-                Route = [feed, user, UserId, scores, 0, add],  % maybe it would require separate worker for this
-                wf:send(Route, [SR]),
-                {Wins, Loses} = if Winner-> {1, 0};
-                                   true -> {0, 1}
-                                end,
-                % haven't found a way to properly get average time
-                wf:send([personal_score, user, UserId, add],
-                               {_Games = 1, Wins, Loses, _Disconnects = 0, GamePoints, 0});
-            true -> do_nothing  % no statistics for robots
-         end,
-         if not Robot ->
-                if KakushPoints /= 0 -> 
-        kvs:add(#transaction{id=kvs:next_id(transaction,1),feed_id={kakush,UserId},comment=game_end});
-                   true -> ok
-                end,
-                if GamePoints /= 0 ->
-        kvs:add(#transaction{id=kvs:next_id(transaction,1),feed_id={game_points,UserId},comment=game_end});
-                   true -> ok
-                end;
-            true -> do_nothing %% no points for robots
-         end
-     end || #result{player_id = UserId, robot = Robot, winner = Winner,
-                    kakush_points = KakushPoints, game_points = GamePoints}  <- Results],
-
-    GameName = proplists:get_value(name, GameInfo, ""),
-    GameType = proplists:get_value(game_type, GameInfo),
-    GameEndRes = [{if Robot -> "robot"; true -> UserId end, Robot, Pos, KPoints, GPoints}
-                  || #result{player_id = UserId, robot = Robot, pos = Pos,
-                             kakush_points = KPoints, game_points = GPoints} <- Results],
-    gas:info(?MODULE,"GAME_STATS <~p> Notificaton: ~p", [GameId, {{GameName, GameType}, GameEndRes}]),
-    wf:send(["system", "game_ends_note"], {{GameName, GameType}, GameEndRes}).
-
-is_bot(UserId, Players) ->
-    case lists:keyfind(UserId, #'PlayerInfo'.id, Players) of
-        #'PlayerInfo'{robot = Robot} -> Robot;
-        _ -> true % If UserId is not found then the player is a replaced bot. 
-    end.
-
-is_paid(UserId) -> true. %nsm_accounts:user_paid(UserId).
-
-user_id_to_string(UserId) -> binary_to_list(UserId).
-
-calc_points(KakushPerWinner, KakushPerLoser, WinGamePoints, Paid, Robot, Winner) ->
-    if Robot -> {0, 0};
-       not Paid andalso Winner -> {0, WinGamePoints};
-       not Paid -> {0, 0};
-       Paid andalso Winner -> {KakushPerWinner, WinGamePoints};
-       Paid -> {KakushPerLoser, 0}
-    end.
-
-get_player_stats() -> ok.