-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathderemod.rb
540 lines (458 loc) · 13.2 KB
/
deremod.rb
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
=begin
DereMod.rb
Starlight Stage Note Composition Breakdown
=end
require_relative 'raw_json'
require_relative 'rhythm'
require_relative 'kernel_snippet'
require_relative 'typed_array'
# INCLUSION LINE STARTS HERE
module Deresute
class ImportedSong < RawJSON
end
class ImportedChart < RawJSON
def build(**options)
ChartBuilder.new(Chart,@raw) do |raw|
options.each do |key,value|
set_property key,value
end
raw[:chartData].tap do |chart_data|
# First iteration - define notes
chart_data.each do |note_data|
kw = { id:note_data[:id], at:note_data[:sec], pos1:note_data[:startPos], pos2:note_data[:finishPos], param:note_data[:status], type: note_data[:type] }
if [4,5,6,7].include? note_data[:type] then
kw[:chuunithm] = true
define_note **kw
elsif [1,2,3].include? note_data[:type] then
define_note **kw
else
define_raw **kw
end
end
m = {h:{},s:{},p:[]}
# Second iteration - define holds
chart_data.select { |note_data| note_data[:type].between?(1,2) }
.tap do |hold_list|
hs = [0,0,0,0,0,0]
hold_list.each do |hold_data|
i,j,k = *hold_data.values_at(:id,:type,:finishPos)
if hs[k].nonzero? then
m[:h][ hs[k] ] = define_hold(hs[k],i)
hs[k] = 0
elsif j == 2 then
hs[k] = i
end
# ::Kernel.p hs if get_property(:difficulty) > 2
end
if hs.any?(&:nonzero?) then
::STDERR.puts "Stalling hold note detected"
end
end
# Third iteration - define slides
chart_data.select { |note_data| note_data[:groupId].nonzero? }
.group_by { |note_data| note_data[:groupId] }
.tap { |slide_set| next; ::Kernel.p slide_set.map{|k,v|[k,v.size]} }
.each do |group_id,note_list|
begin
m[:s][group_id] = define_group(*note_list.map{|note|note[:id]})
rescue TypeError, RangeError
::STDERR.puts "Bad slide chain detected: #{note_list.map(&:inspect) * ', '} (#{$!.class}: #{$!.message})"
end
end
# Fourth iteration - define pairs
chart_data.select { |note_data| note_data[:sync].nonzero? }
.group_by { |note_data| note_data[:sec] }
.each do |pair_time,pair_note|
if pair_note.size != 2 then
::STDERR.puts "Bad pair setup detected (#{pair_note.size})"
else
m[:p] << define_pair(*pair_note.map{|note|note[:id]})
end
end
@holds, @slides, @pairs = m.values_at(:h,:s,:p)
end
end
end
end
class Song
end
class BasicChart
# constructor
def initialize(build_data)
fail TypeError, "Expected builder class!" unless ObjectSpace.each_object(ChartBuilder).to_a.include?(build_data)
build_data.instance_exec(method(:get),method(:set)) do |get,set|
set.call :diff , get_property(:difficulty)
set.call :checksum, get_property(:hash)
set.call :notes , get_notes
set.call :holds , get_holds
set.call :slides , get_slides
set.call :pairs , get_pairs
set.call :raws , get_raws
end
end
# accessors
# private methods
private
def get(var)
instance_variable_get(:"@#{var}")
end
def set(var,val)
instance_variable_set(:"@#{var}",val)
end
# protected methods
protected
# public methods
public
def notes;@notes;end
def holds;@holds;end
def slides;@slides;end
def pairs;@pairs;end
def raws;@raws;end
def inspect
"<%s diff:%d note:%d hold:%d slide:%d pair:%d>" % [
self.class,
@diff.to_i,
@notes.size,
@holds.size,
@slides.size,
@pairs.size,
]
end
def method_missing(m,*a,&b)
if instance_variable_defined?("@#{m}") then
self.class.class_exec { define_method("#{m}") { instance_variable_get("@#{m}") } }
send m
else
super(m,*a,&b)
end
end
# class methods
class << self
# private static methods
private
# protected static methods
protected
# public static methods
public
end
end
class Chart < BasicChart
end
class ChartBuilder < ::BasicObject
'Builder based class are formed through DSL'
'Structure priority'
'- Pair'
'- Hold'
'- Slide'
'- Tap'
# constants
# constructor
def initialize
@options = {}
@notes = {}
@holds = {}
@slides = {}
@pairs = []
@rawcall = {}
end
# accessors
# private methods
private
def get_property(key)
@options[key]
end
def get_notes
@notes
end
def get_holds
@holds
end
def get_slides
@slides
end
def get_pairs
@pairs
end
def get_raws
@rawcall
end
def set_property(key,val)
@options.store(key,val)
end
def define_note(id:,at:,pos1:,pos2:,type:,param:false,chuunithm:false)
wi = 1
if chuunithm then
case type
when 4; type, param, wi = 1, false, param
when 5; type, param, wi = 3, false, param
when 6; type, param, wi = 1, 1, param
when 7; type, param, wi = 1, 2, param
end
end
noteitem = if param.is_a?(::Integer) && param.nonzero? then
case param
when 1,2
FlickNote.new(param,at,pos2,pos1)
when 100,101,102
TapColorNote.new(param - 99,at,pos2,pos1)
else
$stderr.puts "WARNING! Note #{id} have status of #{param}"
nil
end
elsif type == 3
SlideNote.new(at,pos2,pos1)
else
TapNote.new(at,pos2,pos1)
end
return if noteitem.nil?
noteitem.width = wi
@notes.store(id,noteitem)
end
def define_raw(id:,**opts)
@rawcall.store(id,opts)
end
->(){
note_checker = ->(ary){
ary.each_index do |i|
o = ary[i]
if @notes.has_key? o then
ary[i] = @notes[o]
elsif !(o.is_a?(BaseNote) || o.is_a?(MixNotes)) then
::Kernel.fail ::TypeError, "Expected BaseNote or MixNotes class, given #{o.class}"
end
end
}
['Hold','Pair','Group'].each do |type|
define_method(:"define_#{type.downcase}") do |*notes|
self.instance_exec(notes,¬e_checker)
ChartBuilder.const_get(:"#{type}Note").new(*notes)
end
end
}.call
def add_pattern(note)
::Kernel.fail ::TypeError, "Expected BaseNote or MixNotes class, given #{note.class}" unless note.is_a?(BaseNote) || note.is_a?(MixNotes)
@pattern << note
end
# protected methods
protected
# public methods
public
# class methods
class << self
# private static methods
private
alias :_df_new :new
# protected static methods
protected
# public static methods
public
# new(klass,*args,&block)
# klass [Class] - a supplied class for the builder, expected to be inheritance of BasicChart
def new(klass,*args,&block)
::Kernel.fail TypeError, "Expected `klass' is class, given #{klass.class}" unless klass.is_a? Class
::Kernel.fail TypeError, "Expected BasicChart class, given #{klass}" unless klass.ancestors.include? BasicChart
if block.is_a? Proc then
builder = _df_new
builder.instance_exec(*args,&block)
klass.new(builder)
end
end
end
end
class BaseNote
"Represents a standard definition of a note
"
TimingModes = [:exact,:rhythmic].freeze
@@timing_mode = nil
# constructor
def initialize(time,pos,source=nil)
fail TypeError,sprintf("Expecting Time-object or Floats, given %s",
time.class) unless [Numeric,Time].any? { |cls| cls === time }
fail TypeError,sprintf("Expecting Low-ranged Integer (1..99), given %s %s",
pos,pos.class) unless pos.is_a?(Integer) && pos.between?(1,99)
self.time = time
self.pos = pos
self.cpos = source
self.width = 1
end
# accessors
public
def time
@time
end
def pos
return @pos
end
def cpos
return @cpos.nil? ? @pos : @cpos
end
def width
@width
end
def time=(time)
case @@timing_mode
when :exact
@time = Float(time)
when :rhythmic
@time = Timing(time)
end
end
def time!
self.time = @time
end
def pos=(pos)
@pos = ((pos.to_i if pos.to_i.between?(1,99)) rescue @pos)
end
def cpos=(cpos)
@cpos = ((cpos.to_i if cpos.nil? || cpos.to_i.between?(-99,99)) rescue @cpos)
end
def width=(width)
@width = width
end
# private methods
private
# protected methods
protected
# public methods
public
# class methods
class << self
# private static methods
private
# protected static methods
protected
# public static methods
public
def timing_mode
TimingModes.include?(@@timing_mode) ? @@timing_mode : TimingModes.first
end
def timing_mode=(value)
@@timing_mode = TimingModes.include?(value) ? value : self.timing_mode
ObjectSpace.each_object(self,&:time!)
end
end
self.timing_mode = nil
end
class TapNote < BaseNote
def color
4
end
def param
nil
end
end
class TapColorNote < TapNote
def initialize(color,time,position,source=nil)
fail TypeError,sprintf("Expecting Integer, given %s for Note Color",
color.class) unless [Integer].any? { |cls| cls === color }
self.color = color
super(time,position,source)
end
def color
@color
end
def color=(new_color)
return color unless new_color.is_a? Integer
@color = new_color
end
end
class SlideNote < BaseNote
end
class FlickNote < BaseNote
"Class Description"
# constructor
def initialize(face,time,position,source=nil)
fail TypeError,sprintf("Expecting Integer, given %s for Note Facing",
face.class) unless [Integer].any? { |cls| cls === face }
self.param = face
super(time,position,source)
end
# accessors
public
def param
@param
end
def param=(facing)
return param unless facing.is_a? Integer
@param = facing
end
# private methods
private
# protected methods
protected
# public methods
public
# class methods
class << self
# private static methods
private
# protected static methods
protected
# public static methods
public
end
end
class MixNotes < TypedArray
end
class PairNotes < MixNotes
"Representing a pair of notes
Ensures given pair of notes are requires
interaction at the same time.
"
# constructor
build base_class: [BaseNote], index_strict: true, size: 2
end
class HoldNote < MixNotes
"Representing a pair of notes
Ensures given pair of notes is only interactable
by sequence from first one to second one.
"
# constructor
build base_class: [TapNote, BaseNote], index_strict: true, size: 2
end
class SlidePath < MixNotes
"Representing a chain of flick notes
Constructs a path along given set of flicks,
to make a chain of sliding notes.
"
# constructor
build base_class: [FlickNote,SlideNote], index_strict: false, size: 2..Float::INFINITY
# removes any trailing flicks unless end of SuperHold chains.
def have_slide?
@data.first.is_a? SlideNote
end
alias :have_shold? :have_slide?
def truncate
return nil if !have_slide?
s = self.dup
s.truncate!
end
def truncate!
return nil if !have_slide?
slide_set = @data.select { |x| x.is_a? SlideNote }
end_note = @data[slide_set.size]
pop_size = self.length - (slide_set.size + (end_note.nil? ? 0 : 1))
self.pop(pop_size) if pop_size > 0
self
end
end
[
[:BaseNote ,BaseNote],
[:TapNote ,TapNote],
[:FlickNote,FlickNote],
[:SlideNote,SlideNote],
[:MixNotes ,MixNotes],
[:PairNote ,PairNotes],
[:HoldNote ,HoldNote],
[:GroupNote,SlidePath]
].each do |(const_name,const_data)|
ChartBuilder.const_set const_name, const_data
end
GlobalConstDeclare(self)
end
# INCLUSION LINE ENDS HERE
if __FILE__ == $0 then
puts("Loaded main module.")
else
#puts("Included #{__FILE__} module")
end