Browse Source

game stats

Maxim Sokhatsky 11 years ago
parent
commit
9483e9e2d8
3 changed files with 16 additions and 13 deletions
  1. 2 8
      apps/db/include/game_log.hrl
  2. 2 2
      apps/db/src/db_game_log.erl
  3. 12 3
      apps/server/src/game_log.erl

+ 2 - 8
apps/db/include/game_log.hrl

@@ -3,18 +3,12 @@
 
 
 -include_lib("kvs/include/kvs.hrl").
 -include_lib("kvs/include/kvs.hrl").
 
 
--record(event_log, {?ITERATOR(feed),
+-record(game_log,  {?CONTAINER, protocol_stat=[] }).
+-record(event_log, {?ITERATOR(game_log),
     game_id,
     game_id,
     timestamp,
     timestamp,
     user,
     user,
     event,
     event,
     game_event}).
     game_event}).
 
 
--record(game_log, {?ITERATOR(feed),
-    game_type,
-    game_id,
-    user,
-    aggregations % [{attach,Count},{join,Count},{}]
-    }).
-
 -endif.
 -endif.

+ 2 - 2
apps/db/src/db_game_log.erl

@@ -5,6 +5,6 @@
 
 
 metainfo() ->
 metainfo() ->
     #schema{name=kvs,tables=[
     #schema{name=kvs,tables=[
-        #table{name=game_log,fields=record_info(fields,game_log)},
-        #table{name=event_log,fields=record_info(fields,event_log)}
+        #table{name=game_log,container=true,fields=record_info(fields,game_log)},
+        #table{name=event_log,container=game_log,fields=record_info(fields,event_log)}
     ]}.
     ]}.

+ 12 - 3
apps/server/src/game_log.erl

@@ -26,7 +26,7 @@ handle_call(_Request, _From, State) ->
     {reply, Reply, State}.
     {reply, Reply, State}.
 handle_cast({log_event, PI, #game_event{game = GameId, event = EventName, args = Args} = Event, GameState}, #state{history = History} = State) ->
 handle_cast({log_event, PI, #game_event{game = GameId, event = EventName, args = Args} = Event, GameState}, #state{history = History} = State) ->
 
 
-    Container = 
+    EventLogEntry = 
         #event_log{
         #event_log{
            feed_id = {GameId, PI#'PlayerInfo'.id},
            feed_id = {GameId, PI#'PlayerInfo'.id},
            id = {timestamp(), GameId, PI#'PlayerInfo'.id},
            id = {timestamp(), GameId, PI#'PlayerInfo'.id},
@@ -35,8 +35,17 @@ handle_cast({log_event, PI, #game_event{game = GameId, event = EventName, args =
            timestamp = calendar:now_to_universal_time(erlang:now()),
            timestamp = calendar:now_to_universal_time(erlang:now()),
            game_event = Event},
            game_event = Event},
 
 
-    gas:info(?MODULE, "Event Log: ~p", [Container]),
-    kvs:add(Container),
+    gas:info(?MODULE, "Event Log: ~p", [EventLogEntry]),
+    kvs:add(EventLogEntry),
+
+    {ok,GL} = kvs:get(game_log,{GameId, PI#'PlayerInfo'.id}),
+    ProtocolStats = GL#game_log.protocol_stat,
+    PS = case is_list(ProtocolStats) of true -> ProtocolStats; _ -> [] end,
+    Stats = case lists:keyfind(EventName,1,PS) of
+        {EventName,Count} -> lists:keyreplace(EventName,1,PS,{EventName,Count+1});
+        false -> [{EventName,1}|PS] end,
+    kvs:put(GL#game_log{protocol_stat=Stats}),
+
     {noreply, State#state{history = [Event | History]}};
     {noreply, State#state{history = [Event | History]}};
 handle_cast(clear_history, State) -> {noreply, State#state{history = []}};
 handle_cast(clear_history, State) -> {noreply, State#state{history = []}};
 handle_cast(_Msg, State) -> gas:info(?MODULE, "Event Log: cast message ~p", [_Msg]), {noreply, State}.
 handle_cast(_Msg, State) -> gas:info(?MODULE, "Event Log: cast message ~p", [_Msg]), {noreply, State}.