/core/engine/battleRecorder.erl
http://erlbattle.googlecode.com/ · Erlang · 129 lines · 87 code · 27 blank · 15 comment · 16 complexity · 3259f3d4ca90ef0bda4dbe5cf0421b75 MD5 · raw file
- -module(battleRecorder).
- -include("schema.hrl").
- -export([start/1]).
-
- start(Pid) ->
-
- process_flag(trap_exit, true),
-
- io:format("Battle Recorder Begin to Run ~n",[]),
-
- %% ????????????
- ets:new(battle_record,[ordered_set,named_table,private]),
-
- run(Pid,1).
-
- %% ????????
- run(Pid, Seq) ->
-
- receive
- %% ???????????
- {'EXIT', Pid , _Msg} ->
- recordBattle();
- {Pid,Record} ->
- ets:insert(battle_record, {Seq, Record} ),
- run(Pid, Seq + 1);
- _ ->
- run(Pid,Seq) %% ????????
- end.
-
- %% ???????????????
- recordBattle() ->
-
- %%????????
- Records = ets:tab2list(battle_record),
- ets:delete(battle_record),
-
- %% open file
- {_Ok, Io} = file:open(?EbBattleLogFile,[write]),
-
- %% ?????
- initBattleField(Io),
-
- %% ????????????????
- lists:foreach(
- fun(RawRecord) ->
- {_Seq, Record} = RawRecord,
- case Record of
- {?LogCmdAction, Time, Id, Action, Position, Facing, Hp}->
- {X,Y} = Position,
-
- io:fwrite(Io,"~p,~p,~p,~p,~p,~p,~p,0~n" , [Time, changeAction(Action), X, Y, uniqueId(Id), simpleDirection(Facing), Hp]);
- {?LogCmdPlan, Id, Action, ActionEffectTime} ->
- if
- Action == ?ActionWait ->
- io:fwrite(Io,"plan,~p,[]~n" , [uniqueId(Id)]);
- true ->
- io:fwrite(Io,"plan,~p,~p@~p~n" , [uniqueId(Id), changeAction(Action), ActionEffectTime])
- end;
- {?LogCmdStatus,Time, Id, Position, Facing, Hp, HpLost} ->
- {X,Y} = Position,
- io:fwrite(Io,"~p,~p,~p,~p,~p,~p,~p,~p~n" , [Time,'status', X,Y, uniqueId(Id), simpleDirection(Facing), Hp, HpLost]);
- {?LogCmdResult, Result}->
- io:fwrite(Io,"result,~p~n" , [list_to_atom(Result)]);
- _ ->
- none
- end
- end,
- Records),
-
- %% close file
- file:close(Io).
-
- %% ??????????????
- initBattleField(Io) ->
-
- Army = ?PreDef_army,
-
- %% ??????
- lists:foreach(
- fun(Id) ->
- io:fwrite(Io,"~p,~p,~p,~p,~p,~p,~p,~p~n" , [0,'stand', 0,1+Id, Id, 'e', 100, 0])
- end,
- Army),
-
- %% ??????
- lists:foreach(
- fun(Id) ->
- io:fwrite(Io,"~p,~p,~p,~p,~p,~p,~p,~p~n" , [0,'stand', 14,1+Id, Id+10, 'w', 100, 0])
- end,
- Army).
-
-
- %% record ?????????? id =10 ??
- uniqueId(Id) ->
-
- {Sid, Side} = Id,
-
- if
- Side == ?BlueSide ->
- Sid + 10;
- true ->
- Sid
- end.
-
- %% ????
- changeAction(Action) ->
- if
- Action == ?ActionMove -> 'walk';
- Action == ?ActionForward -> 'walk';
- Action == ?ActionAttack -> 'fight';
- Action == ?ActionBack -> 'back';
- Action == ?ActionTurnEast -> 'turnEast';
- Action == ?ActionTurnWest -> 'turnWest';
- Action == ?ActionTurnSouth -> 'turnSouth';
- Action == ?ActionTurnNorth -> 'turnNorth';
- true -> list_to_atom(Action)
- end.
-
-
- %% ????
- simpleDirection(Facing) ->
-
- if
- Facing == ?DirWest ->'w';
- Facing == ?DirEast ->'e';
- Facing == ?DirNorth ->'n';
- Facing == ?DirSouth ->'s'
- end.