/core/engine/battleRecorder.erl

http://erlbattle.googlecode.com/ · Erlang · 129 lines · 87 code · 27 blank · 15 comment · 16 complexity · 3259f3d4ca90ef0bda4dbe5cf0421b75 MD5 · raw file

  1. -module(battleRecorder).
  2. -include("schema.hrl").
  3. -export([start/1]).
  4. start(Pid) ->
  5. process_flag(trap_exit, true),
  6. io:format("Battle Recorder Begin to Run ~n",[]),
  7. %% ????????????
  8. ets:new(battle_record,[ordered_set,named_table,private]),
  9. run(Pid,1).
  10. %% ????????
  11. run(Pid, Seq) ->
  12. receive
  13. %% ???????????
  14. {'EXIT', Pid , _Msg} ->
  15. recordBattle();
  16. {Pid,Record} ->
  17. ets:insert(battle_record, {Seq, Record} ),
  18. run(Pid, Seq + 1);
  19. _ ->
  20. run(Pid,Seq) %% ????????
  21. end.
  22. %% ???????????????
  23. recordBattle() ->
  24. %%????????
  25. Records = ets:tab2list(battle_record),
  26. ets:delete(battle_record),
  27. %% open file
  28. {_Ok, Io} = file:open(?EbBattleLogFile,[write]),
  29. %% ?????
  30. initBattleField(Io),
  31. %% ????????????????
  32. lists:foreach(
  33. fun(RawRecord) ->
  34. {_Seq, Record} = RawRecord,
  35. case Record of
  36. {?LogCmdAction, Time, Id, Action, Position, Facing, Hp}->
  37. {X,Y} = Position,
  38. io:fwrite(Io,"~p,~p,~p,~p,~p,~p,~p,0~n" , [Time, changeAction(Action), X, Y, uniqueId(Id), simpleDirection(Facing), Hp]);
  39. {?LogCmdPlan, Id, Action, ActionEffectTime} ->
  40. if
  41. Action == ?ActionWait ->
  42. io:fwrite(Io,"plan,~p,[]~n" , [uniqueId(Id)]);
  43. true ->
  44. io:fwrite(Io,"plan,~p,~p@~p~n" , [uniqueId(Id), changeAction(Action), ActionEffectTime])
  45. end;
  46. {?LogCmdStatus,Time, Id, Position, Facing, Hp, HpLost} ->
  47. {X,Y} = Position,
  48. io:fwrite(Io,"~p,~p,~p,~p,~p,~p,~p,~p~n" , [Time,'status', X,Y, uniqueId(Id), simpleDirection(Facing), Hp, HpLost]);
  49. {?LogCmdResult, Result}->
  50. io:fwrite(Io,"result,~p~n" , [list_to_atom(Result)]);
  51. _ ->
  52. none
  53. end
  54. end,
  55. Records),
  56. %% close file
  57. file:close(Io).
  58. %% ??????????????
  59. initBattleField(Io) ->
  60. Army = ?PreDef_army,
  61. %% ??????
  62. lists:foreach(
  63. fun(Id) ->
  64. io:fwrite(Io,"~p,~p,~p,~p,~p,~p,~p,~p~n" , [0,'stand', 0,1+Id, Id, 'e', 100, 0])
  65. end,
  66. Army),
  67. %% ??????
  68. lists:foreach(
  69. fun(Id) ->
  70. io:fwrite(Io,"~p,~p,~p,~p,~p,~p,~p,~p~n" , [0,'stand', 14,1+Id, Id+10, 'w', 100, 0])
  71. end,
  72. Army).
  73. %% record ?????????? id =10 ??
  74. uniqueId(Id) ->
  75. {Sid, Side} = Id,
  76. if
  77. Side == ?BlueSide ->
  78. Sid + 10;
  79. true ->
  80. Sid
  81. end.
  82. %% ????
  83. changeAction(Action) ->
  84. if
  85. Action == ?ActionMove -> 'walk';
  86. Action == ?ActionForward -> 'walk';
  87. Action == ?ActionAttack -> 'fight';
  88. Action == ?ActionBack -> 'back';
  89. Action == ?ActionTurnEast -> 'turnEast';
  90. Action == ?ActionTurnWest -> 'turnWest';
  91. Action == ?ActionTurnSouth -> 'turnSouth';
  92. Action == ?ActionTurnNorth -> 'turnNorth';
  93. true -> list_to_atom(Action)
  94. end.
  95. %% ????
  96. simpleDirection(Facing) ->
  97. if
  98. Facing == ?DirWest ->'w';
  99. Facing == ?DirEast ->'e';
  100. Facing == ?DirNorth ->'n';
  101. Facing == ?DirSouth ->'s'
  102. end.