-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.lua
141 lines (134 loc) · 3.33 KB
/
lib.lua
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
VERSION = '0.7.0'
typeof = type;
-- tables stuff
function table.find(t,match)
local omatch = match;
if typeof(match) ~= 'function' then
match = function(v) return omatch == v end;
end
for k,v in pairs(t) do
if match(v,k) then return v end;
end
end
function table.indexof(t,match)
local omatch = match;
if typeof(match) ~= 'function' then
match = function(v) return omatch == v end;
end
for k,v in pairs(t) do
if match(v,k) then return k end;
end
end
function table.includes(t,match)
return table.indexof(t,match) ~= nil;
end
function table.filter(t,match)
local omatch = match;
if typeof(match) ~= 'function' then
match = function(v) return omatch ~= v end;
end
local nt = {};
for k,v in pairs(t) do
if match(v,k) then nt[k] = v end;
end
return nt;
end
function table.ifilter(t,match)
local omatch = match;
if typeof(match) ~= 'function' then
match = function(v) return omatch ~= v end;
end
local nt = {};
for k,v in pairs(t) do
if match(v,k) then table.insert(nt, v) end;
end
return nt;
end
function table.ishift(t)
local m = t[1] or 1;
for k,_ in pairs(t) do
m = math.min(m,k);
end
local nt = {};
for k,v in pairs(t) do
nt[k-m+1] = v;
end
return nt;
end
function table.map(t,fn)
local nt = {};
for k,v in pairs(t) do
nt[k] = fn(v,k,t);
end
return nt;
end
function table.imap(t,fn)
local nt = {};
for i,v in ipairs(t) do
table.insert(nt, fn(v,i,t));
end
return nt;
end
function table.keys(t)
local keys = {};
for k in pairs(t) do
table.insert(keys,k);
end
return keys;
end
-- deep-cloning function
function dclone(t)
return textutils.unserialise(textutils.serialise(t));
end
-- random things
function parseVersion(v)
if typeof(v) == 'string' then
local major, minor, patch = v:match('(%w+)%.(%w+)%.(%w+)');
return major * 1000000 + minor * 1000 + patch;
else
return v;
end
end
function pak(t,msg) -- Press Any Key
if msg == nil then msg = 'press any key to continue...' end;
if t == nil then t = 1 end;
local cx,cy = term.getCursorPos();
term.setTextColor(colors.gray);
print(msg);
sleep(t);
term.setCursorPos(1,cy);
term.setTextColor(colors.lightGray);
term.write(msg);
sleep(0.05);
term.setCursorPos(1,cy);
term.setTextColor(colors.white);
term.write(msg);
os.pullEvent('key');
end
-- Score Server
function loadss(ss,dat)
if ss then
local conn = http.get(ss,{action="get"});
local code, msg;
if conn then code, msg = conn.getResponseCode() end;
if conn ~= nil and code == 200 then
for _,s in pairs(textutils.unserialiseJSON(conn.readAll())) do
--print("loadss.scores.length="..tostring(#dat))
s.org = 'ss';
table.insert(dat,s);
end
else
return msg;
end
end
end
function savess(ss,scoret)
if ss then
local conn = http.get(ss,{action="save",payload=textutils.serialiseJSON(scoret)});
local code, msg;
if conn then code, msg = conn.getResponseCode() end;
if not( conn ~= nil and code == 200 ) then
return msg;
end
end
end