-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminos.lua
executable file
·333 lines (228 loc) · 7 KB
/
minos.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env lua
if arg[1] == "-h" or arg[1] == "--help" then
io.stderr:write( [[
Usage: minos.lua [ -h | --help ]
minos.lua [ search_key1 ... ]
Lists .mino files in the current directory, satisfying conditions,
described by $1 ... $7
$1 - game type, one of "g[ame]", "p[reset]", "e[rror]" or "x" (any)
$2 - string of up to 4 decimal digits, describing correspondingly:
FigureWeightMax, Apperture, Metric and FigureWeightMin
$3 - string of up to 5 decimal digits, describing correspondingly:
Goal, Gravity, SingleLayer, DiscardFullRows and FixedSequence options
$4 - GlassWidth
$5 - GlassHeight
$6 - FillLevel
$7 - FillRatio
.mino files are grouped in branches, having the same parent. Game records
have scores. Presets have no scores. For corrupted files the first line
of the error message is shown.
After descriptions will be shown, You will be asked to choose sequentional
number of the branch desired (optional) and sequential number of the game in
the branch (optional). Empty input means "list all". You can enter the new
filter keys and repeat the search.
Interactive output is written to stderr, resulting list is written to stdout.
]] )
os.exit()
end
-------------------------------------
-- Try to locate "omnimino" binary --
-------------------------------------
local OmniminoName = "omnimino "
if not (os.execute("which omnimino > /dev/null")) then
OmniminoName = "./omnimino "
if not (os.execute("test -x omnimino")) then
io.stderr:write("\nThis utility needs 'omnimino' binary in the current directory or in the $PATH .\n\n")
os.exit(1)
end
end
-- local OmniminoName = "./omnimino "
--[[
Apperture App
Metric Metr
WeightMax WMax
WeightMin WMin
Gravity Grav
SingleLayer SL
DiscardFullRows DFR
Goal Goal
GlassWidth GW
GlassHeight GH
FillLevel FL
FillRatio FR
FixedSequence FS
Parameters order in .mino file
App Metr WMax WMin Grav SL DFR Goal GW GH FL FR FS
Parameters order for display
WMax WMin App Metr Goal Grav SL DFR GW GH FL FR FS
Parameters order for keys
Type <WMax App Metr WMin> <Goal Grav SL DFR> GW GH FL FR
Parameters order for sort - internal data order
Type WMax Goal App Metr Grav SL DFR GW GH FL FR WMin
]]
--------------------
-- Reading .minos --
--------------------
local Branch={}
local pipe = io.popen("ls *.mino | " .. OmniminoName, "r")
local chunk = assert(pipe:read("a"))
assert(pipe:close())
local f = load("_G = nil _ENV = nil return {" .. chunk .. "}")
if not f then
print("Bad chunk") os.exit(1)
end
local IBranch = f()
for i,B in ipairs(IBranch) do
local P = B.Parameters
local Parent = P[#P]
if Branch[Parent] then
table.insert(Branch[Parent], B.Data)
else
Branch[Parent] = {Parms = P, B.Data}
end
end
-------------------------------------------------
-- Make filter keys of commannd-line arguments --
-------------------------------------------------
local MakeKey = function(arg)
Key = {}
local GameType = {["g"] = 1, ["p"] = 2, ["e"] = 3}
local SelectGameType = function(Arg, ArgPos)
Key[ArgPos] = GameType[string.sub(Arg,1,1)]
end
local Quad = function(Arg, ArgPos)
for i,Pos in ipairs(ArgPos) do
Key[Pos] = tonumber(string.sub(Arg,i,i))
end
end
local Single = function(Arg, ArgPos)
Key[ArgPos] = tonumber(Arg)
end
local Decoder = {{SelectGameType, 1}, {Quad, {2, 4, 5, 14}}, {Quad, {3, 6, 7, 8, 13}},
{Single, 9}, {Single, 10 }, {Single, 11}, {Single, 12}}
for i,D in ipairs(Decoder) do
if arg[i] then
D[1](arg[i], D[2])
end
end
return Key
end
local SelectBranchesMatching = function(Key)
local SBranch = {}
local ParmsMatchKey = function(Parameter)
for i, V in ipairs(Parameter) do
if Key[i] and Key[i] ~= V then return false end
end
return true
end
for i,j in pairs(Branch) do
if ParmsMatchKey(j.Parms) then
table.insert(SBranch,j)
end
end
return SBranch
end
local CompareBranches = function(B1, B2)
local Q = B2.Parms
for i, V in ipairs(B1.Parms) do
if V ~= Q[i] then
return V < Q[i]
end
end
return false
end
local ReadAndSplitInputLine = function(Subject)
io.stderr:write("\nEnter " .. Subject .. " or new filter keys : ")
local AnsL = io.stdin:read("l") or "" -- if input pipe is empty
local Ans = {}
for i in string.gmatch(AnsL,"%w+") do
table.insert(Ans,i)
end
return Ans
end
local ShowSortedBranches = function(SBranch)
local BrParms
local Exhibit = function(ListOfPairs)
for i, Pair in ipairs(ListOfPairs) do
io.stderr:write(string.format("%" .. tostring(Pair[2]) .. "s", Key[Pair[1]] and " " or tostring(BrParms[Pair[1]])))
end
end
local CompareData = function(D1, D2)
for i = #D1, 1, -1 do
if D1[i] ~= D2[i] then
return D1[i] < D2[i]
end
end
return false
end
io.stderr:write([[
Metric Gravity SingleLayer FixedSequence
\ \ / |
Aperture | \ | DiscardFullRows |
\ | \ | / |
Weight A M Goal G S D Glass Fill F Scores
]])
for i, Br in ipairs(SBranch) do
BrParms = Br.Parms
io.stderr:write(string.format("%2d", i))
if Br.Parms[1] ~= 3 then
Exhibit{{2,4}}
io.stderr:write(">=")
Exhibit{{14, 1}, {4, 3}, {5, 2}, {3, 4}, {6, 4}, {7, 2}, {8, 2}, {9, 4}}
io.stderr:write("*")
Exhibit{{10, -3}, {11, 4}}
io.stderr:write("*")
Exhibit{{12, -3}, {13,-3}}
end
table.sort(Br,CompareData)
for j, k in ipairs(Br) do
if k[4] then
io.stderr:write(" ", k[4])
end
end
io.stderr:write("\n")
end
end
local ShowSortedGames = function(B)
io.stderr:write("\n Score Date Player\n\n")
for i = 1, #B do
io.stderr:write(string.format("%2d",i))
io.stderr:write(string.format("%7s",B[i][4]))
io.stderr:write(string.format("%20s",os.date("%y-%m-%d %H:%M:%S",B[i][3])))
io.stderr:write(string.format(" %s",B[i][2]))
io.stderr:write("\n")
end
io.stderr:write("\n")
end
local SBranch
local Ans = arg
local BN
local GN
repeat ------------------------------------------------------------------------
repeat -----------------------------------------
SBranch = SelectBranchesMatching(MakeKey(Ans))
table.sort(SBranch, CompareBranches)
ShowSortedBranches(SBranch)
Ans = ReadAndSplitInputLine("branch [ game ]")
BN = tonumber(Ans[1])
GN = tonumber(Ans[2])
until #Ans == 0 or BN --------------------------
io.stderr:write("\n")
if #Ans == 0 or GN or (not SBranch[BN]) or #SBranch[BN] < 2 then break end
ShowSortedGames(SBranch[BN])
Ans = ReadAndSplitInputLine("game")
GN = tonumber(Ans[1])
until #Ans == 0 or GN ---------------------------------------------------------
io.stderr:write("\n")
--------------------------------------
-- Writing list of .mino file names --
--------------------------------------
for i, Br in ipairs(SBranch) do
if not BN or BN == i then
for j, k in ipairs(Br) do
if not GN or GN == j then
io.stdout:write(k[1],"\n")
end
end
end
end