Browse Source

代码整理

master
SisMaker 4 years ago
parent
commit
e0842a748d
2 changed files with 14 additions and 233 deletions
  1. +0
    -65
      src/comMisc/util.erl
  2. +14
    -168
      src/comMisc/util1.erl

+ 0
- 65
src/comMisc/util.erl View File

@ -14,60 +14,6 @@
]).
-compile(export_all).
%% @doc reductions计数, PID
sort_red() ->
[Pid | T] = erlang:processes(),
{_, MaxVal} = erlang:process_info(Pid, reductions),
Pid1 = find_max(MaxVal, T, Pid),
io:format("max_reductions_pid:~p~n", [erlang:process_info(Pid1)]).
find_max(MaxVal, [TemPid | T], Pid) ->
{_, TemMaxVal} = erlang:process_info(TemPid, reductions),
case TemMaxVal > MaxVal of
true ->
find_max(TemMaxVal, T, TemPid);
_ ->
find_max(MaxVal, T, Pid)
end;
find_max(_, [], Pid) ->
Pid.
%% @doc
insert_last(List, Info) when is_list(List) ->
Other = lists:reverse(List),
Other1 = [Info | Other],
lists:reverse(Other1);
insert_last(_, _) ->
[].
%% @doc ()
upset_list(List) when is_list(List) ->
Len = length(List),
case List of
[_ | _] ->
upset_list_loop(Len, List, []);
_ ->
List
end;
upset_list(_) ->
[].
upset_list_loop(_, [], L) ->
L;
upset_list_loop(Len, List, L) when Len > 0 ->
Index = util:rand(1, Len),
Tuple = lists:nth(Index, List),
Other = lists:delete(Tuple, List),
upset_list_loop(Len - 1, Other, [Tuple | L]);
upset_list_loop(_, _, L) ->
L.
%% @doc
rm_str_lr_space(String) ->
String1 = rm_str_lr_space1(String),
@ -382,17 +328,6 @@ check_keyword(Text) ->
true
end.
%% @doc
%% @spec check_sen_keyword(Type, Text) -> true|false
%% where Type::base or base_other ; true false
check_sen_keyword(base, Text) when is_list(Text) ->
svr_chat:word_base(Text);
check_sen_keyword(base_other, Text) when is_list(Text) ->
svr_chat:word_base_other(Text);
check_sen_keyword(_, _) ->
true.
%%
check_length(Item, LenLimit) ->
check_length(Item, 1, LenLimit).

+ 14
- 168
src/comMisc/util1.erl View File

@ -1,179 +1,25 @@
-module(util1).
-compile(export_all).
%% List中的每两个元素之间插入一个分隔符
implode(_S, []) ->
[<<>>];
implode(S, L) when is_list(L) ->
implode(S, L, []).
implode(_S, [H], NList) ->
lists:reverse([thing_to_list(H) | NList]);
implode(S, [H | T], NList) ->
L = [thing_to_list(H) | NList],
implode(S, T, [S | L]).
%%X在[Min, Max]
minmax(X, Min, Max) ->
min(max(X, Min), Max).
%% ->
explode(S, B) ->
re:split(B, S, [{return, list}]).
explode(S, B, int) ->
[list_to_integer(Str) || Str <- explode(S, B), length(Str) > 0].
thing_to_list(X) when is_integer(X) -> integer_to_list(X);
thing_to_list(X) when is_float(X) -> float_to_list(X);
thing_to_list(X) when is_atom(X) -> atom_to_list(X);
thing_to_list(X) when is_binary(X) -> binary_to_list(X);
thing_to_list(X) when is_list(X) -> X.
%%
log(T, F, A, Mod, Line) ->
{ok, Fl} = file:open("logs/error_log.txt", [write, append]),
Format = list_to_binary("#" ++ T ++ " ~s[~w:~w] " ++ F ++ "\r\n~n"),
{{Y, M, D}, {H, I, S}} = erlang:localtime(),
Date = list_to_binary([integer_to_list(Y), "-", integer_to_list(M), "-", integer_to_list(D), " ", integer_to_list(H), ":", integer_to_list(I), ":", integer_to_list(S)]),
file:close(Fl).
%% unix时间戳
unixtime() ->
{M, S, _} = game_timer:now(),%%os:timestamp()
M * 1000000 + S.
longunixtime() ->
{M, S, Ms} = game_timer:now(),
(M * 1000000000000 + S * 1000000 + Ms) div 1000.
%% HEX格式的md5
md5(S) ->
lists:flatten([io_lib:format("~2.16.0b", [N]) || N <- binary_to_list(erlang:md5(S))]).
%% Min到Max之间的随机整数
rand(Same, Same) -> Same;
rand(Min, Max) ->
M = Min - 1,
if
Max - M =< 0 ->
0;
true ->
%%
case get("rand_seed") of
undefined ->
RandSeed = mod_rand:get_seed(),
random:seed(RandSeed),
put("rand_seed", RandSeed);
_ -> skip
end,
RanNum = random:uniform(Max - M) + M,
RanNum
end.
%%length(List) >= Num
%%[1,2,3,4,5,6,7,8,9][1,2,4]
get_random_list(List, Num) ->
ListSize = length(List),
F = fun(N, List1) ->
Random = rand(1, (ListSize - N + 1)),
Elem = lists:nth(Random, List1),
List2 = lists:delete(Elem, List1),
List2
end,
Result = lists:foldl(F, List, lists:seq(1, Num)),
List -- Result.
%%[{a,7},{b,8},{c,90},{d,100}],a为7%,
%%Num不能大于lenth(List)
get_random_list_probability(List, Num) ->
F = fun(Elem, ProbabilityValue) ->
lists:duplicate(ProbabilityValue, Elem)
end,
Result1 = lists:flatten([F(Elem, ProbabilityValue) || {Elem, ProbabilityValue} <- List]),
ProbabilityList = lists:reverse(lists:sort([ProbabilityValue || {_Elem, ProbabilityValue} <- List])),
MaxProbabilityValue = lists:sum([lists:nth(N, ProbabilityList) || N <- lists:seq(1, Num)]),
Result2 = get_random_list(Result1, Num + MaxProbabilityValue - length(List)),
Result3 = lists:usort(Result2),
Result4 = get_random_list(Result3, Num),
Result4.
get_randomId_by_value([], _) ->
fail;
get_randomId_by_value(Data, Random) ->
case lists:nth(1, Data) of
{Id, Weight} ->
case Weight >= Random of
true ->
Id;
false ->
get_randomId_by_value(Data -- [{Id, Weight}], Random - Weight)
end;
_ ->
io:format("get_randomId_by_value no match")
end.
%%[{a,7},{b,8},{c,9},{d,10}],a为7权重,b为8权重
get_random_by_weight(List) ->
F = fun({Id, Weight}, Sum) ->
Sum + Weight
end,
Sum2 = lists:foldl(F, 0, List),
RandomVa = random:uniform(Sum2),
RandomId = get_randomId_by_value(List, RandomVa).
%%
ceil(N) ->
T = trunc(N),
case N == T of
true -> T;
false -> 1 + T
end.
%%
floor(X) ->
T = trunc(X),
case (X < T) of
true -> T - 1;
_ -> T
end.
sleep(T) ->
receive
after T -> ok
end.
sleep(T, F) ->
receive
after T -> F()
end.
get_list([], _) ->
[];
get_list(X, F) ->
F(X).
%% for循环
for(Max, Max, F) ->
F(Max);
for(I, Max, F) ->
F(I),
for(I + 1, Max, F).
%% for循环
%% @return {ok, State}
for(Max, Min, _F, State) when Min < Max ->
{ok, State};
for(Max, Max, F, State) -> F(Max, State);
for(I, Max, F, State) -> {ok, NewState} = F(I, State), for(I + 1, Max, F, NewState).
for_new(Min, Max, _F, State) when (Min > Max) ->
{ok, State};
for_new(Min, Max, F, State) ->
{ok, NewState} = F(Min, State),
for_new(Min + 1, Max, F, NewState).
%% term序列化term转换为string格式e.g., [{a},1] => "[{a},1]"

Loading…
Cancel
Save