307 lines
16 KiB
Erlang
307 lines
16 KiB
Erlang
%%----------------------------------------------------
|
|
%% 数据配置文件
|
|
%% @author whjing2011@gmail.com
|
|
%%----------------------------------------------------
|
|
-module(attr_data).
|
|
-export([cfg/0, handle/4]).
|
|
-include("data_config.hrl").
|
|
-include("common.hrl").
|
|
cfg() ->
|
|
#data_cfg{
|
|
name = attr_data
|
|
,lua_name = attr_data
|
|
,desc = "attr配置数据"
|
|
,source = "attr_data.xml"
|
|
,target = "attr_data.erl"
|
|
,inc = ["common.hrl", "attr.hrl"]
|
|
,callback = [
|
|
{{do_sub_list,4, val(do_sub_list)}, ?MODULE, handle, {write_string, function_head(do_sub_list)}}
|
|
,{{do_sub,4,val(do_sub)}, cb_fun, handle, {row_fun, ["DATA"], fun row_do_sub/1}}
|
|
,{{do_sub1,4,val(do_sub1)}, cb_fun, handle, {row_fun, ["属性批"], fun row_do_sub1/1}}
|
|
,{{get_attr_val,2, "get_attr_val(_, _) -> 0."}, cb_fun, handle, {row_fun, ["DATA"], fun do_row/1}}
|
|
,{{add_attr_val,3,"add_attr_val(Attr, _, _) -> Attr."}, cb_fun, handle, {row_fun, ["DATA"], fun row_add_attr_val/1}}
|
|
,{{attr2per,undefined}, cb_fun, handle, {row_fun, ["DATA"], fun row_attr2per/1}}
|
|
,{{attr2atom_kv,1,def}, ?MODULE, handle, {attr2atom_kv, ["DATA"]}}
|
|
,{{mix_attr_and_attr,2,def}, ?MODULE, handle, {mix_attr_and_attr, ["DATA"]}}
|
|
,{{mix_attr_and_ratio,2,def}, ?MODULE, handle, {mix_attr_and_ratio, ["DATA"]}}
|
|
,{{attr_ratio,1,def}, ?MODULE, handle, {attr_ratio, ["DATA"]}}
|
|
,{{attr_round,1,def}, ?MODULE, handle, {attr_round, ["DATA"]}}
|
|
,{{attr_uround,1,def}, ?MODULE, handle, {attr_uround, ["DATA"]}}
|
|
,{{attr_atom2power,0}, cb_fun, handle, {row_fun, ["DATA"], fun row_attr_atom2power/1}}
|
|
,{{attr_atom2partner_power,0}, cb_fun, handle, {row_fun, ["DATA"], fun row_attr_atom2partner_power/1}}
|
|
,{{attr_atom2id,0}, cb_fun, handle, {row_fun, ["DATA"], fun row_attr_atom2id/1}}
|
|
,{{attr_atom2name,"<<>>"}, cb_fun, handle, {row_fun, ["DATA"], fun row_attr_atom2name/1}}
|
|
,{{base_power,1,def}, ?MODULE, handle, {base_power, ["DATA"]}}
|
|
,{{partner_base_power,1,def}, ?MODULE, handle, {partner_base_power, ["DATA"]}}
|
|
,{{print_attr,1,def}, ?MODULE, handle, {print_attr, ["DATA"]}}
|
|
,{{atom_to_int, 1, val(atom_to_int)}, cb_fun, handle, {get_tuple, ["DATA"], atom, id}}
|
|
,{{int_to_atom, 1, val(int_to_atom)}, cb_fun, handle, {get_tuple, ["DATA"], id, atom}}
|
|
,{{attr_addition,2,def}, ?MODULE, handle, {attr_addition, ["DATA"]}}
|
|
,{all_attr, cb_fun, handle, {tuple_list, ["DATA"], atom, false}}
|
|
]
|
|
,callback_cli = [
|
|
{id_to_key, cb_fun_lua, handle, {key_val, ["DATA"], id, atom}}
|
|
,{key_to_id, cb_fun_lua, handle, {key_val, ["DATA"], atom, id}}
|
|
,{id_to_name, cb_fun_lua, handle, {key_val, ["DATA"], id, name}}
|
|
,{key_to_name, cb_fun_lua, handle, {key_val, ["DATA"], atom, name}}
|
|
,{power, cb_fun_lua, handle, {key_val, ["DATA"], atom, [power, not_to_power]}}
|
|
,{type, cb_fun_lua, handle, {key_val, ["DATA"], atom, type}}
|
|
,{partner_power, cb_fun_lua, handle, {key_val, ["DATA"], atom, partner_power}}
|
|
,{is_show, cb_fun_lua, handle, {key_val, ["DATA"], atom, is_show}, fun("DATA", Row) ->
|
|
cb_fun:get_val(is_show, Row) =:= 1;
|
|
(_,_) -> false
|
|
end}
|
|
]
|
|
,callback_json = [
|
|
{id_to_key, cb_fun_json, handle, {key_val, ["DATA"], id, atom}}
|
|
,{key_to_id, cb_fun_json, handle, {key_val, ["DATA"], atom, id}}
|
|
,{id_to_name, cb_fun_json, handle, {key_val, ["DATA"], id, name}}
|
|
,{key_to_name, cb_fun_json, handle, {key_val, ["DATA"], atom, name}}
|
|
,{power, cb_fun_json, handle, {key_val, ["DATA"], atom, power}}
|
|
,{type, cb_fun_json, handle, {key_val, ["DATA"], atom, type}}
|
|
,{partner_power, cb_fun_json, handle, {key_val, ["DATA"], atom, partner_power}}
|
|
,{is_show, cb_fun_json, handle, {key_val, ["DATA"], atom, is_show}, fun("DATA", Row) ->
|
|
cb_fun:get_val(is_show, Row) =:= 1;
|
|
(_,_) -> false
|
|
end}
|
|
]
|
|
,sheet = [
|
|
{["DATA"], [
|
|
#f_column{type = int, name = id, desc = "编号", primary = true}
|
|
,#f_column{type = bstr, name = name, desc = "名称"}
|
|
,#f_column{type = bstr, name = desc, desc = "描述"}
|
|
,#f_column{type = atom, name = atom, desc = "原子"}
|
|
,#f_column{type = int, name = power, desc = "战力"}
|
|
,#f_column{type = int, name = partner_power, desc = "伙伴战力"}
|
|
,#f_column{type = int, name = not_to_power, desc = "不计算战力"}
|
|
,#f_column{type = kv, name = is_show, desc = "是否显示", kv_type=bool}
|
|
,#f_column{type = kv, name = type, desc = "单位", kv_type = [{"数值", 1}, {"百分比", 2}]}
|
|
,#f_column{type = kv, name = is_base_power, desc = "是否基础战力", kv_type=bool}
|
|
,#f_column{type = kv, name = is_use, desc = "是否可配置属性", kv_type=bool}
|
|
]}
|
|
,{["属性批"], [
|
|
#f_column{type = atom, name = atom, desc = "原始属性", primary = true}
|
|
,#f_column{type = kv, name = type, desc = "类型", kv_type = [{"val",1},{"per",2},{"all",3}], primary = true}
|
|
,#f_column{type = term, name = attr_list, desc = "属性批"}
|
|
]}
|
|
]
|
|
}.
|
|
|
|
handle(_DataCfg, SheetDatas, _Label, {attr2atom_kv, Sheets}) ->
|
|
AllRows = gen_data_lib:all_rows(SheetDatas, Sheets),
|
|
L = lists:filtermap(fun(Row) ->
|
|
Atom = cb_fun:get_val(atom, Row),
|
|
case cb_fun:get_val(is_use, Row) of
|
|
1 -> {true, ?S("\t{~-25s, Attr#attr.~-25s}", [Atom, Atom])};
|
|
_ -> false
|
|
end
|
|
end, AllRows),
|
|
lists:concat(["attr2atom_kv(Attr) ->[\n", string:join(L, ",\n"), "\n]."]);
|
|
|
|
handle(_DataCfg, SheetDatas, _Label, {mix_attr_and_attr, Sheets}) ->
|
|
AllRows = gen_data_lib:all_rows(SheetDatas, Sheets),
|
|
L = lists:filtermap(fun(Row) ->
|
|
Atom = cb_fun:get_val(atom, Row),
|
|
Flag = lists:member(Atom, ["cure", "be_cure", "res", "res_p", "res_s"]),
|
|
case cb_fun:get_val(is_use, Row) of
|
|
1 when Flag -> {true, ?S("\t~-25s= util:round(A1#attr.~-25s+ A2#attr.~-25s)", [Atom, Atom, Atom])};
|
|
1 -> {true, ?S("\t~-25s= max(0, util:round(A1#attr.~-25s+ A2#attr.~-25s))", [Atom, Atom, Atom])};
|
|
_ -> false
|
|
end
|
|
end, AllRows),
|
|
lists:concat(["mix_attr_and_attr(A1, A2) -> #attr{\n", string:join(L, ",\n"), "\n}."]);
|
|
|
|
handle(_DataCfg, SheetDatas, _Label, {mix_attr_and_ratio, Sheets}) ->
|
|
AllRows = gen_data_lib:all_rows(SheetDatas, Sheets),
|
|
L = lists:filtermap(fun(Row) ->
|
|
Atom = cb_fun:get_val(atom, Row),
|
|
case cb_fun:get_val(is_use, Row) of
|
|
1 -> {true, ?S("\t~-25s= util:round(A1#attr.~-25s* R1#attr.~-25s/1000)", [Atom, Atom, Atom])};
|
|
_ -> false
|
|
end
|
|
end, AllRows),
|
|
lists:concat(["mix_attr_and_ratio(A1, R1) -> #attr{\n", string:join(L, ",\n"), "\n}."]);
|
|
|
|
handle(_DataCfg, SheetDatas, _Label, {attr_ratio, Sheets}) ->
|
|
AllRows = gen_data_lib:all_rows(SheetDatas, Sheets),
|
|
L = lists:filtermap(fun(Row) ->
|
|
Atom = cb_fun:get_val(atom, Row),
|
|
case cb_fun:get_val(is_use, Row) of
|
|
1 -> {true, ?S("\t~-25s= util:round(Attr#attr.~-20s * Ratio / 1000)", [Atom, Atom])};
|
|
_ -> false
|
|
end
|
|
end, AllRows),
|
|
lists:concat(["attr_ratio({1000, Attr}) -> Attr;\nattr_ratio({Ratio, Attr}) ->Attr#attr{\n", string:join(L, ",\n"), "\n}."]);
|
|
|
|
handle(_DataCfg, SheetDatas, _Label, {attr_round, Sheets}) ->
|
|
AllRows = gen_data_lib:all_rows(SheetDatas, Sheets),
|
|
L = lists:filtermap(fun(Row) ->
|
|
Atom = cb_fun:get_val(atom, Row),
|
|
case cb_fun:get_val(is_use, Row) of
|
|
1 -> {true, ?S("\t~-25s= util:round(Attr#attr.~-25s)", [Atom, Atom])};
|
|
_ -> false
|
|
end
|
|
end, AllRows),
|
|
lists:concat(["attr_round(Attr) ->Attr#attr{\n", string:join(L, ",\n"), "\n}."]);
|
|
|
|
handle(_DataCfg, SheetDatas, _Label, {attr_addition, Sheets}) ->
|
|
AllRows = gen_data_lib:all_rows(SheetDatas, Sheets),
|
|
L = lists:filtermap(fun(Row) ->
|
|
Atom = cb_fun:get_val(atom, Row),
|
|
case cb_fun:get_val(is_use, Row) of
|
|
1 when Atom =:= "hp_max" -> {true, ?S("\t~-25s= max(util:uround(Attr#attr.~-25s * Param), 1)", [Atom, Atom])};
|
|
1 -> {true, ?S("\t~-25s= util:uround(Attr#attr.~-25s * Param)", [Atom, Atom])};
|
|
_ -> false
|
|
end
|
|
end, AllRows),
|
|
lists:concat(["attr_addition(Attr, Param) ->Attr#attr{\n", string:join(L, ",\n"), "\n}."]);
|
|
|
|
handle(_DataCfg, SheetDatas, _Label, {attr_uround, Sheets}) ->
|
|
AllRows = gen_data_lib:all_rows(SheetDatas, Sheets),
|
|
L = lists:filtermap(fun(Row) ->
|
|
Atom = cb_fun:get_val(atom, Row),
|
|
case cb_fun:get_val(is_use, Row) of
|
|
1 when Atom =:= "hp_max" -> {true, ?S("\t~-25s= max(util:uround(Attr#attr.~-25s), 1)", [Atom, Atom])};
|
|
1 -> {true, ?S("\t~-25s= util:uround(Attr#attr.~-25s)", [Atom, Atom])};
|
|
_ -> false
|
|
end
|
|
end, AllRows),
|
|
lists:concat(["attr_uround(Attr) ->Attr#attr{\n", string:join(L, ",\n"), "\n}."]);
|
|
|
|
handle(_DataCfg, SheetDatas, _Label, {base_power, Sheets}) ->
|
|
AllRows = gen_data_lib:all_rows(SheetDatas, Sheets),
|
|
L = lists:filtermap(fun(Row) ->
|
|
Atom = cb_fun:get_val(atom, Row),
|
|
case cb_fun:get_val(is_use, Row) of
|
|
1 -> {true, ?S("\tattr_atom2power(~-25s) * Attr#attr.~s", [Atom, Atom])};
|
|
_ -> false
|
|
end
|
|
end, AllRows),
|
|
lists:concat(["base_power(Attr) -> \n", string:join(L, " + \n"), "\n."]);
|
|
|
|
handle(_DataCfg, SheetDatas, _Label, {partner_base_power, Sheets}) ->
|
|
AllRows = gen_data_lib:all_rows(SheetDatas, Sheets),
|
|
L = lists:filtermap(fun(Row) ->
|
|
Atom = cb_fun:get_val(atom, Row),
|
|
NotToPower = cb_fun:get_val(not_to_power, Row),
|
|
case cb_fun:get_val(is_use, Row) of
|
|
1 -> {true, ?S("\tattr_atom2partner_power(~-25s) * max(Attr#attr.~s - ~s, 0)", [Atom, Atom, NotToPower])};
|
|
_ -> false
|
|
end
|
|
end, AllRows),
|
|
lists:concat(["partner_base_power(Attr) -> \n", string:join(L, " + \n"), "\n."]);
|
|
|
|
handle(_DataCfg, SheetDatas, _Label, {print_attr, Sheets}) ->
|
|
AllRows = gen_data_lib:all_rows(SheetDatas, Sheets),
|
|
L = lists:filtermap(fun(Row) ->
|
|
Atom = cb_fun:get_val(atom, Row),
|
|
case cb_fun:get_val(is_use, Row) of
|
|
1 -> {true, ?S("\t?INFO(\"~-25s=~s\", [Attr#attr.~-25s]),", [Atom, "~w", Atom])};
|
|
_ -> false
|
|
end
|
|
end, AllRows),
|
|
lists:concat(["print_attr(Attr) -> \n", string:join(L, "\n"), "\n\tok."]);
|
|
|
|
handle(_DataCfg, _SheetDatas, _Label, {write_string, String}) -> %% 配置不到
|
|
String;
|
|
|
|
handle(_DataCfg, _SheetDatas, _Label, _Args) -> %% 配置不到
|
|
?P("匹配不到相关处理方法:~w", [_Args]),
|
|
error.
|
|
|
|
row_attr2per(Row) ->
|
|
Atom = cb_fun:get_val(atom, Row),
|
|
Desc = cb_fun:get_val(desc, Row),
|
|
case cb_fun:get_val(is_use, Row) of
|
|
1 -> {true, ?S("attr2per(~25s) -> ~-25s; %% ~ts", [Atom, lists:concat([Atom,"_per"]), Desc])};
|
|
_ -> false
|
|
end.
|
|
|
|
row_attr_atom2power(Row) ->
|
|
Atom = cb_fun:get_val(atom, Row),
|
|
Power = cb_fun:get_val(power, Row),
|
|
{true, ?S("attr_atom2power(~-25s) -> ~s;", [Atom, Power])}.
|
|
|
|
row_attr_atom2partner_power(Row) ->
|
|
Atom = cb_fun:get_val(atom, Row),
|
|
Power = cb_fun:get_val(partner_power, Row),
|
|
{true, ?S("attr_atom2partner_power(~-25s) -> ~s;", [Atom, Power])}.
|
|
|
|
row_attr_atom2id(Row) ->
|
|
Atom = cb_fun:get_val(atom, Row),
|
|
Id = cb_fun:get_val(id, Row),
|
|
{true, ?S("attr_atom2id(~-25s) -> ~s;", [Atom, Id])}.
|
|
|
|
row_attr_atom2name(Row) ->
|
|
Atom = cb_fun:get_val(atom, Row),
|
|
Name = cb_fun:get_val(name, Row),
|
|
{true, ?S("attr_atom2name(~-25s) -> ~ts;", [Atom, Name])}.
|
|
|
|
row_add_attr_val(Row) ->
|
|
Atom = cb_fun:get_val(atom, Row),
|
|
Desc = cb_fun:get_val(desc, Row),
|
|
case cb_fun:get_val(is_use, Row) of
|
|
1 -> {true, ?S("add_attr_val(Attr = #attr{~s = Val1}, ~s, Val) -> Attr#attr{~s = Val1 + Val}; %% ~ts", [Atom, Atom, Atom, Desc])};
|
|
_ -> false
|
|
end.
|
|
|
|
do_row(Row) ->
|
|
Atom = cb_fun:get_val(atom, Row),
|
|
Desc = cb_fun:get_val(desc, Row),
|
|
case cb_fun:get_val(is_use, Row) of
|
|
1 -> {true, ?S("get_attr_val(#attr{~25s = Val}, ~25s) -> Val; %% ~ts", [Atom, Atom, Desc])};
|
|
_ -> false
|
|
end.
|
|
|
|
row_do_sub1(Row) ->
|
|
Atom = cb_fun:get_val(atom, Row),
|
|
Type = cb_fun:get_val(type, Row),
|
|
Str = cb_fun:get_val(attr_list, Row),
|
|
case util:string_to_term(Str) of
|
|
{ok, L} ->
|
|
F1 = fun(I,Rec) ->
|
|
?S("~w=~s~w+Val", [I,Rec,I])
|
|
end,
|
|
case Type of
|
|
1 ->
|
|
{true, ?S("do_sub1({~s, Val}, ?attr_calc_val, _RType, AC = #attr_calc{attr = Attr = #attr{}}) -> {ok, AC#attr_calc{attr = Attr#attr{~s}}};\n", [Atom, string:join([F1(I,"Attr#attr.")||I<-L],",")])};
|
|
2 ->
|
|
{true, ?S("do_sub1({~s_per, Val}, _AType, ?attr_calc_ratio, AC = #attr_calc{ratio = Attr = #attr{}}) -> {ok, AC#attr_calc{ratio = Attr#attr{~s}}};\n", [Atom, string:join([F1(I,"Attr#attr.")||I<-L],",")])};
|
|
_ ->
|
|
{true, ?S("do_sub1({~s, Val}, ?attr_calc_val, ?attr_calc_ratio, AC = #attr_calc{attr = Attr, ratio = Ratio}) -> {ok, AC#attr_calc{attr = Attr#attr{~s}, ratio = Ratio#attr{~s}}};\n", [Atom,string:join([F1(I,"Attr#attr.")||I<-L],","), string:join([F1(I,"Ratio#attr.")||I<-L],",")])}
|
|
end;
|
|
_ -> false
|
|
end.
|
|
|
|
row_do_sub(Row) ->
|
|
Atom = cb_fun:get_val(atom, Row),
|
|
Desc = cb_fun:get_val(desc, Row),
|
|
case cb_fun:get_val(is_use, Row) of
|
|
1 ->
|
|
S1 = ?S("%% ~ts\ndo_sub({~s, Val}, ?attr_calc_val, _RType, AC = #attr_calc{attr = Attr = #attr{~s = V}}) -> {ok, AC#attr_calc{attr = Attr#attr{~s = V + Val}}};\n", [Desc, Atom, Atom, Atom]),
|
|
S2 = ?S("do_sub({~s, Val}, ?attr_calc_val_no_ratio, _RType, AC = #attr_calc{attr_no_ratio = Attr = #attr{~s = V}}) -> {ok, AC#attr_calc{attr_no_ratio = Attr#attr{~s = V + Val}}};\n", [Atom, Atom, Atom]),
|
|
S3 = ?S("do_sub({~s, Val}, ?attr_calc_val_no_fc, _RType, AC = #attr_calc{attr_no_fc = Attr = #attr{~s = V}}) -> {ok, AC#attr_calc{attr_no_fc = Attr#attr{~s = V + Val}}};\n", [Atom, Atom, Atom]),
|
|
S4 = ?S("do_sub({~s_per, Val}, _AType, ?attr_calc_ratio, AC = #attr_calc{ratio = Attr = #attr{~s = V}}) -> {ok, AC#attr_calc{ratio = Attr#attr{~s = V + Val}}};\n", [Atom, Atom, Atom]),
|
|
S5 = ?S("do_sub({~s_per, Val}, _AType, ?attr_calc_ratio_no_fc, AC = #attr_calc{ratio_no_fc = Attr = #attr{~s = V}}) -> {ok, AC#attr_calc{ratio_no_fc = Attr#attr{~s = V + Val}}};\n", [Atom, Atom, Atom]),
|
|
{true,lists:concat([S1,S2,S3,S4,S5])};
|
|
_ -> false
|
|
end.
|
|
|
|
val(atom_to_int) -> <<"atom_to_int(Attr) ->
|
|
?ERR(\" atom to int error ~w\", [Attr]),
|
|
undefined.">>;
|
|
val(int_to_atom) -> <<"int_to_atom(Id) ->
|
|
?ERR(\" int to atom error ~w\", [Id]),
|
|
undefined.">>;
|
|
val(do_sub) -> <<"do_sub(Sub, _AType, _RType, AC) ->
|
|
?ERR(\" 未知属性错误 ~w,~w\", [Sub, util:get_stacktrace()]),
|
|
{ok, AC}."/utf8>>;
|
|
val(do_sub1) -> <<"do_sub1(Sub, _AType, _RType, AC) ->
|
|
?ERR(\" 未知属性错误 ~w,~w\", [Sub, util:get_stacktrace()]),
|
|
{ok, AC}."/utf8>>;
|
|
val(do_sub_list) -> "do_sub_list(Sub, _AType, _RType, AC) ->
|
|
?ERR(\"do sub list error ~w,~w\", [Sub, util:get_stacktrace()]),
|
|
{ok, AC}.".
|
|
|
|
function_head(do_sub_list) -> "do_sub_list([], _AType, _RType, AC) -> {ok, AC};\ndo_sub_list([H | T], AType, RType, AC) ->\n\t {ok, AC1} = do_sub(H, AType, RType, AC), \n\t do_sub_list(T, AType, RType, AC1);".
|