This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOnlineTimer.lxl.js
223 lines (180 loc) · 5.04 KB
/
OnlineTimer.lxl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//LiteXLoader Dev Helper
/// <reference path="c:\Users\yqs11\.vscode\extensions\moxicat.lxldevhelper-0.1.4/Library/JS/Api.js" />
// 文件名:OnlineTimer.lxl.js
// 文件功能:LXL平台下玩家在线时间统计工具
// 作者:yqs112358
// 首发平台:MineBBS
var _VER = '1.2.3'
ProcessOldDataJson();
var records=data.openConfig('.\\plugins\\OnlineTimer\\data.json' ,"json","{}");
var conf = data.openConfig('.\\plugins\\OnlineTimer\\config.ini' ,"ini");
//Utils
function GetTimeGap(timestr)
{
return ((new Date().getTime() - new Date(timestr).getTime()) / 1000).toFixed(0);
}
function ReplaceStr(str,from,to)
{
while(str.indexOf(from) != -1)
str = str.replace(from,String(to));
return str;
}
function SolveStr(name,str,isWelcome)
{
let dat = records.get(name);
if(!dat)
return str;
let lastLogin = dat.LastLogin;
let minutes = dat.TotalTime;
let days = 0, hours = 0;
while(minutes > 60)
{
minutes -= 60;
++hours;
}
while(hours > 24)
{
hours -= 24;
++days;
}
str = ReplaceStr(str,"%DAY%", days);
str = ReplaceStr(str,"%HOUR%", hours);
str = ReplaceStr(str,"%MINUTE%", minutes);
str = ReplaceStr(str,"%LASTLOGIN%", lastLogin);
if(isWelcome)
{
let timeMinus = GetTimeGap(lastLogin);
let nodays = (timeMinus / 86400).toFixed(0);
str = ReplaceStr(str,"%NODAYS%", nodays);
}
return str;
}
//Enter & Leave
function EnterPlayer(name)
{
let dat = records.get(name);
if(!dat)
records.set(name, { "LastLogin":system.getTimeStr(), "TotalTime":0 } );
else
{
dat.LastLogin = system.getTimeStr();
records.set(name,dat);
}
}
function GetWelcome(name)
{
let dat = records.get(name);
if(!dat)
return SolveStr(name, conf.getStr("Welcome","FirstTime"), true);
let timeMinus = GetTimeGap(dat.LastLogin);
let days = (timeMinus / 86400).toFixed(0);
if(days < 1)
return SolveStr(name, conf.getStr("Welcome","Back"), true);
else
return SolveStr(name, conf.getStr("Welcome","BackLong"), true);
}
function GetOnlineTime(name)
{
let dat = records.get(name);
if(!dat)
return "Data no found";
else
return SolveStr(name, conf.getStr("OnlineTime","Query"), false);
}
//Time Record
function AddTime(name)
{
let dat = records.get(name);
if(!dat)
records.set(name, { "LastLogin":system.getTimeStr(), "TotalTime":0 } );
else
{
++dat.TotalTime;
records.set(name,dat);
}
}
function QueryTime(pl)
{
mc.runcmdEx(`titleraw ${pl.realName} actionbar {"rawtext":[{"text":"${GetOnlineTime(pl.realName)}"}]}`);
}
function QueryRank(pl)
{
let arr = [];
let dat = JSON.parse(records.read());
let names = Object.keys(dat);
for(i in names)
{
try
{
arr.push( {name:names[i], time:dat[names[i]]['TotalTime'] } );
}
catch(_)
{}
}
arr.sort(function(a,b){
return b.time-a.time;
});
let content = "";
for(i in arr)
{
content += `${Number(i)+1}. ${arr[i].name} - ${arr[i].time} 分钟\n`;
}
let fm = mc.newSimpleForm();
fm.setTitle("Online Time Ranking");
fm.setContent(content);
fm.addButton("关闭");
pl.sendForm(fm,function(pl,id){;});
}
//Main
setInterval(function(){
let pls = mc.getOnlinePlayers();
for(let i=0; i<pls.length; ++i)
{
AddTime(pls[i].realName);
}
},60000);
mc.listen("onJoin",function(pl){
let name = pl.realName;
if(conf.getBool("Welcome","Enable"))
{
let welcomeStr = GetWelcome(name);
setTimeout(function(){
mc.runcmdEx(`titleraw ${name} actionbar {"rawtext":[{"text":"${welcomeStr}"}]}`);
},1000);
}
EnterPlayer(name);
});
mc.regPlayerCmd("onlinetime","查询你的在线时长",function(pl,args){
QueryTime(pl);
});
mc.regPlayerCmd("onlinetime rank","查询在线时长排名",function(pl,args){
QueryRank(pl);
});
log('[OnlineTimer] OnlineTimer在线时间统计插件-已装载 当前版本:' + _VER);
log('[OnlineTimer] 作者:yqs112358 首发平台:MineBBS');
log('[OnlineTimer] 欲联系作者可前往MineBBS论坛');
//For Compatibility
function ProcessOldDataJson()
{
try{
let f = file.open('.\\plugins\\OnlineTimer\\data.json',file.ReadMode);
let c = f.readSync(1);
f.close();
if(c == '[')
{
log("[OnlineTimer] 检测到旧版数据文件。正在自动更新...");
let oldData = JSON.parse(file.readFrom('.\\plugins\\OnlineTimer\\data.json'));
let newData = {};
for(i in oldData)
{
newData[oldData[i].name] = {};
newData[oldData[i].name].LastLogin = oldData[i].lastLogin;
newData[oldData[i].name].TotalTime = oldData[i].totalTime;
}
file.writeTo('.\\plugins\\OnlineTimer\\data.json',JSON.stringify(newData,null,4));
log("[OnlineTimer] 数据文件自动升级完毕");
}
}
catch(_)
{ }
}