-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjarserve2.lic
2320 lines (2042 loc) · 77.4 KB
/
jarserve2.lic
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
=begin
jarserve2 - Jar inventory management and API.
THIS SCRIPT IS UNSUPPORTED, and is not polished for a release. It's uploaded only for the convenience of those
that I mention it to.
;jarserve2 to start it and scan your inventory, then:
;vars set jarsack=cloak
;vars set jarsack2=backpack
....
;js2 jar container <container> - jar all gems in the container
;js2 jar right/left/item <itemname> - jar individual gems
;js2 query - query all gem statistics
;js2 query unc dia - query statistics for gems matching the pattern
;js2 bundle - Combine jars of the same gem into less jars.
;js2 drop emerald - Drops all emeralds
;js2 drop 5 emerald - Drops 5 emeralds
;js2 give Luxelle 5 emerald - Gives Luxelle 5 emeralds. (Unless she declines or the offer times out)
;js2 give Luxelle emerald - Gives Luxelle emeralds until she declines or supplies are exhausted
;js2 dropjar/givejar - like the above, but entire jars worth of gems
version: 0.2.3 (2019-06-12)
author: LostRanger (thisgenericname@gmail.com)
game: GS
tags: utility
required: Lich >= 4.6.0.
=end
=begin
# Shake
You give your <a exist="62298027" noun="jar">jar</a> a hard shake, causing <a exist="62416884" noun="coral">some polished red coral</a> to fall into your left hand.
# Shake empty
You shake your <a exist="62298050" noun="jar">jar</a> a few times before you realize that it is empty.
# Shake now empty
That was the last <a exist="62422737" noun="amethyst">amethyst</a> in the <a exist="62297972" noun="jar">jar</a>.
# Start new jar. Not a full item name!
You put your <a exist="62425706" noun="amethyst">smoky amethyst</a> into your empty <a exist="62297972" noun="jar">jar</a>.
# Partial? empty. Requires rescan of both jars. Has full gem name.
You empty some star sapphires into your <a exist="62298030" noun="jar">glass jar</a>, filling it.
# Full empty. May require rescan
You empty all of the light pink morganite stones into your <a exist="62298038" noun="jar">glass jar</a>.
=end
# unless $SAFE == 0
# echo "This script must be trusted to run because it needs access to the 'concurrent-ruby' library."
# echo "To trust it, type #{$lich_char}trust #{@script.name}"
#
# end
#
#
# # We require Ruby's 'Concurrent' library, so try to install it
# begin
# require 'concurrent'
# rescue Exception => ex
# echo ex'
# echo ex.message
# echo "Ruby's 'concurrent' gem does not appear to be installed. Trying to install it."
# begin
# require 'rubygems'
# require 'rubygems/dependency_installer'
# rescue Exception => ex
# echo ex.message
# echo "Failed to load rubygems dependencies."
# exit
# end
#
# begin
# installer = Gem::DependencyInstaller.new({:user_install => true, :document => nil})
# echo "Downloaded #{installer.install('concurrent-ruby')}"
# rescue Exception => ex
# echo ex.message
# echo "Failed to download gem from default sources, trying http."
# begin
# Gem.sources = ['http://rubygems.org/']
# installer = Gem::DependencyInstaller.new({:user_install => true, :document => nil})
# echo installer.inspect
# echo "Downloaded #{installer.install('concurrent-ruby')}"
# rescue Exception => ex
# echo ex.message
# echo "Failed to download gem from http sources, giving up"
# exit
# end
# end
#
# Gem.clear_paths
# require 'concurrent'
# end
script.want_downstream = false
script.want_downstream_xml = true
module Jarserve2
DEBUG_EVENTS = 0
class Gem
CACHE = {}
PLURAL_RULES = {
# First matching substitution is applied.
'shadowglass' => 'shadowglass',
'ies' => 'y',
#'lapis lazuli' => 'lapis lazuli',
#'coral' => 'coral',
'onyxes' => 'onyx',
'zes' => 'z',
'teeth' => 'tooth',
#'bloom' => 'bloom',
's' => '',
'teeth' => 'tooth',
}
PLURAL_REGEX = /^(.*?)(#{PLURAL_RULES.keys.map{|x| Regexp::escape(x)}.join('|')})$/
attr_writer :name, :noun_first, :noun_first_plural
attr_accessor :plural
def initialize(name, plural=nil, noun_first=nil, noun_first_plural=nil)
@name = name
@plural = plural
@noun_first = noun_first
@noun_first_plural = noun_first_plural
end
def name(count=1)
return @name unless @plural and count != 1
return @plural
end
def n(count=1)
"#{count} #{self.name(count)}"
end
def noun_first(count=1)
return self.noun_first_plural if count != 1
@noun_first = @name.sub(/^(.+)\s+(.+)$/, "\\2, \\1") unless @noun_first
return @noun_first
end
def noun_first_plural
return @noun_first_plural if @noun_first_plural
return self.noun_first unless @plural
@noun_first_plural = @plural.sub(/^(.+)\s+((?<!lapis ).+)$/, "\\2, \\1")
return @noun_first_plural
end
def Gem.one(name)
return nil unless name
name = Gem.normalize(name)
raise 'Ended up with empty-length gem name' if name == ''
CACHE[name] = Gem.new(name) unless CACHE[name]
return CACHE[name]
end
def Gem.many(plural)
return nil unless plural
plural = Gem.normalize(plural)
name = Gem.singular(plural, true)
result = Gem.one(name)
result.plural = plural
return result
end
def Gem.singular(plural, skip_normalize=false)
plural = Gem.normalize(plural) unless skip_normalize
if plural =~ PLURAL_REGEX
return "#{$1}#{PLURAL_RULES[$2]}"
else
return plural
end
end
def Gem.normalize(s)
s = s.full_name if s.is_a?(GameObj)
s = s.downcase
return $1 if s =~ /\b(polished (?:ivory|ivories))$/ # I hate ivory
unless s =~ /^\s*(?:(?:some|a|an|the) )?(?:(?:(?:piece|shard|slice|ribbon|essence)s? of|polished) )?(?:(?:tiny|small|medium|large) )?(.*?)\s*$/
raise 'FIXME: normalize pattern did not match but should match anything'
end
return $1
end
def to_s
return @name
end
def <=>(other)
return self.noun_first_plural <=> other.noun_first_plural
end
end
class JarSet < Set
attr_reader :gem
FULL_PROC = proc {|item| item.full?}
def initialize(gem, snapshot_of=nil)
if snapshot_of
super(snapshot_of.map{|jar| jar.snapshot})
@num = self.num
@is_snapshot = true
else
super()
@is_snapshot = false
end
@gem = gem
end
def snapshot
return @is_snapshot ? self : JarSet.new(@gem, self)
end
def num
return @num if @is_snapshot
ct = 0
self.each{|jar|
return nil unless jar.count
ct += jar.count
}
return ct
end
def filled
self.count(&FULL_PROC)
end
def full
self.select(&FULL_PROC)
end
def nonfull
self.reject(&FULL_PROC)
end
def sort(descending: false, include_full: true, &block)
what = include_full ? self.to_a : self.nonfull.to_a
if descending
if block_given?
return what.sort{|a,b| -yield(a,b)}
else
return what.sort{|a,b| b<=>a}
end
elsif block_given?
return what.sort(&block)
else
return what.sort
end
end
def least_filled(include_full=true)
# Returns a list of jars sorted by least filled. Full jars, regardless of capacity, will be last
# (include_full ? self.to : self.nonfull).sort
return sort(include_full: include_full)
end
def most_filled(include_full=true)
# Returns a list of jars sorted by most filled. Full jars, regardless of capacity, will be first
# (include_full ? self.to : self.nonfull).sort{|a,b| b<=>a}
return sort(descending: true, include_full: include_full)
end
def filter(
descending: false,
full: nil, # 0/false: No full jars. >0: Take up to this many full jars. <0: Take all but this many full jars.
nonfull: nil, # 0/false: No nonfull jars. >0: Take up to this many nonfull jars. <0: Take all but this many nonfull jars.
skip: 0, # Omit this many jars from the beginning. Counts towards full/nonfull jars not taken.
take: nil, # Take no more than this many jars. nil=no limit
leave: 0 # Omit this many jars from the end. Counts towards full/nonfull jars not taken.
)
len = self.length
take = len unless take
full = len unless full
nonfull = len unless nonfull
result = JarSet.new(@gem)
return result if (take <= 0)
return result if len <= skip+leave
return result if (full == 0 or -full >= len) and (nonfull == 0 or -nonfull >= len)
jars = self.sort(descending: descending)
return self if skip == 0 and leave == 0 and take >= len and full >= len and nonfull >= len
#echo "Gem=#{self.gem.to_s}. skip=#{skip} take=#{take} leave=#{leave}. Jars=#{jars.map{|x| x.full? ? '!' : '.'}.join('')}"
# skip, leave = leave, skip if descending # So we can always assume full jars will be last.
# How many full/nonfull jars in the result?
numfull = jars.count(&FULL_PROC)
numnonfull = jars.length - numfull
calc = proc {|base, query, count|
next([base, [-query, count].min].max) if query < 0
[base, count - query].max
}
skip, leave = leave, skip if descending
skip = calc.call(skip, nonfull, numnonfull)
leave = calc.call(leave, full, numfull)
skip, leave = leave, skip if descending
leave = [leave, len - skip - take].max
return result if skip+leave >= len
#echo "Gem=#{@gem.to_s}. skip=#{skip} take=#{take} leave=#{leave}. Jars=#{jars.map{|x| x.full? ? '!' : '.'}.join('')}"
if skip > 0 or leave > 0
skip, leave = leave, skip if descending
leave = [leave, len - skip - take].max
jars = jars[skip .. -(leave+1)]
else
return self
end
jars.each{|jar| result << jar}
return result
end
def filter?(**filters)
return self unless filters.length > 0
return self.filter(**filters)
end
end
class ReadOnlyJar
attr_reader :gem, :id, :status, :count, :container
def initialize(id, gem, container, count=nil, status=nil)
@gem = gem
@id = id
@container = container
if @gem
@count = count
@status = status
else
@count = 0
@status = 'empty'
end
end
def snapshot
self
end
def retrieve
fput "get ##{@id}"
end
def store
fput "put ##{@id} in ##{@container}"
end
def fast_retrieve
put "get ##{@id}"
end
def fast_store
put "put ##{@id} in ##{@container}"
end
def held?
(GameObj.right_hand.id == @id or GameObj.left_hand.id == @id)
end
def ref
"##{@id}"
end
def refresh
fput "look in #{self.ref}"
end
def empty?
@count == 0
end
def full?
@status.nil? ? nil : (@status == 'full')
end
def <=>(other)
if other.gem != self.gem
return 1 unless other.gem
return -1 unless self.gem
return other.gem<=>self.gem
end
if (self.full? || false) != (other.full? || false)
return self.full? ? 1 : -1
end
return (self.count || -1) <=> (other.count || -1)
end
end
class Jar < ReadOnlyJar
attr_reader :parent, :container
def initialize(id, gem, container, count=nil, status=nil)
super(id, gem, container, count, status)
@parent = nil
@snapshot = nil
# @gem = gem
# @id = id
# @container = container
# if @gem
# @count = count
# @status = status
# else
# @count = 0
# @status = 'empty'
# end
end
def snapshot
unless @snapshot
@snapshot = ReadOnlyJar.new(@id, @gem, @container, @count, @status)
end
@snapshot
end
def container=(newcontainer)
@snapshot = nil
@container = newcontainer
end
def status=(newstatus)
return if @status == newstatus
if @newstatus == 'empty'
self.gem = nil
elsif @count == 0 or @gem == nil
raise "Cannot set the status of a jar to anything other than empty unless it has a gem and a nonzero count."
end
@snapshot = nil
@status = newstatus
end
def count=(newcount)
raise "Count cannot be negative!" if newcount < 0
return if newcount == @count
if newcount == 0
self.gem = nil
elsif @gem == nil
raise "Cannot set the count of a jar to anything other than zero when it has no gem."
else
@count = newcount
@status = nil
@snapshot = nil
end
end
def gem=(newgem)
return if newgem == @gem
@snapshot = nil
if newgem
@count = nil
@status = nil
else
@count = 0
@status = 'empty'
end
@parent.move(self, newgem) # noop if parent is nil
@gem = newgem
end
def added_to(what)
raise "Jar #{self} already has a parent!" if @parent
@parent = what
end
def removed_from(what)
unless @parent == what
raise "Jar #{self} does not belong to what it's being removed from!" if @parent
@parent = what
end
@parent = nil
end
def store
if @parent
@parent.stash(self)
else
fput "put ##{@id} in ##{@container}"
end
end
def fast_store
if @parent
@parent.stash(self)
else
put "put ##{@id} in ##{@container}"
end
end
def empty?
@count == 0
end
def full?
@status.nil? ? nil : (@status == 'full')
return @status == 'full'
end
def Jar.from_item(item, container)
item.after_name =~ PATTERNS[:jarcontaining]
gem = Gem.many($1)
return Jar.new(item.id, gem, container)
end
def <=>(other)
if other.gem != self.gem
return 1 unless other.gem
return -1 unless self.gem
return other.gem<=>self.gem
end
if (self.full? || false) != (other.full? || false)
return self.full? ? 1 : -1
end
return (self.count || -1) <=> (other.count || -1)
end
end
class FutureTimedOut < Exception
def initialize(future, msg)
super(msg)
@future = future
end
end
class FutureFinished < Exception
def initialize(future, msg = 'Future completed.')
super(msg)
@future = future
end
end
class Future
def initialize
@waiting = Set.new # Threads currently blocked waiting for a result.
@lock = Mutex.new # Concurrent modification prevention
@done = false
@error = nil
@value = nil
end
def value=(new_value)
@lock.synchronize {
raise "Cannot assign a value to a completed Future." if @done
@value = new_value
make_done
}
@when_done.call(self)
end
def error=(new_error)
@lock.synchronize {
echo new_error.inspect
echo new_error.backtrace.join("\n")
raise "Cannot assign an exception to a completed Future." if @done
@error = new_error
make_done
}
@when_done.call(self)
end
def wait_result
begin
Thread.handle_interrupt(FutureFinished => :immediate) {
@lock.synchronize {
return if @done
@waiting << Thread.current
}
sleep
raise FutureTimedOut.new(self, "Operation timed out")
}
rescue FutureFinished
return
end
# Shouldn't be reachable
raise 'Thread awoke unexpectedly -- reached unreachable code in fetch_result. FIXME.'
end
private(:wait_result)
def value
wait_result
raise @error if @error
return @value
end
def error
wait_result
return @error
end
def make_done()
# Must already be locked!
@done = true
@waiting.each {|thr| thr.raise(FutureFinished.new(self, 'Future completed.')) }
@waiting.clear
end
private(:make_done)
def then(&block)
@lock.synchronize {
@when_done = block
@when_done.call(self) if @done
}
@when_done = block
end
def execute(&block)
@lock.synchronize {
raise "Cannot execute a block on a completed Future." if @done
begin
@value = block.call
rescue => ex
@error = ex
end
make_done
}
@when_done.call(self)
end
end
attr_accessor :inventory
TYPEAHEAD = 3 # Only used for the cataloging phase
PATTERNS = {
# Matches the result of INV HANDS and GLANCE
:hands => /(?x)
^ # Anchor to start
(?:<.*\/>)? # Discard leading XML, if any
(?:
(?:You\ glance\ down\ to\ see|You\ have)\ # GLANCE or INV HANDS
(?: # Right hand
(?:nothing|
(?:(?<right_pre>[^<]+)\ )?
<a\ exist="(?<right_exist>-?\d+)"\ noun="(?<right_noun>[^"]+)">(?<right_name>[^<]+)<\/a>
(?:\ (?<right_post>.+?))?)
\ in\ your\ right\ hand
(?:\ and \ )?
)
(?: # Left hand
(?:nothing|
(?:(?<left_pre>[^<]+)\ )?
<a\ exist="(?<left_exist>-?\d+)"\ noun="(?<left_noun>[^"]+)">(?<left_name>[^<]+)<\/a>
(?:\ (?<left_post>.+?))?)
\ in\ your\ left\ hand
(?:\ and \ )?
)
|You\ glance\ down\ at\ your\ empty\ hands
|You\ have\ nothing\ in\ your\ hands
)
\.\s*
$
/,
# Matches a fully qualified item identifier.
:itemref => /(?x)
^
(?:<.*>)? # Discard leading XML, if any
(?:
You\ accept\ <a\ exist="(?<from_player_id>-?\d+")\ noun="(?<from_player>[^"]+)">.*<\/a>\ offer\ and\ are\ now\ holding
| <a\ exist="(?<to_player_id>-?\d+")\ noun="(?<to_player>[^"]+)">.*<\/a>\ has\ accepted\ your\ offer\ and\ is\ now\ holding
| You\ (?<verb>pick\ up|drop|place|carefully place|put|remove)
)
\ (?:(?<pre>[^<]+)\ )?
<a\ exist="(?<exist>-?\d+)"\ noun="(?<noun>[^"]+)">(?<name>[^<]+)<\/a>
(?:\ (?<post>(?!from)[^<]+?))?
(?: # Source container reference
(?:\ from)?
\ (?:(?<location>in|on|under|behind)\ )
(?:(?<container_pre>[^<]+)\ )?
<a\ exist="(?<container_exist>-?\d+)"\ noun="(?<container_noun>[^"]+)">(?<container_name>[^<]+)<\/a>
(?:\ (?!that\ is\ on\ the\ ground)(?<container_post>[^<]+?))?
(?<floor>(?:\ that\ is\ on\ the\ ground|,\ and\ it\ quickly\ disappears))?
)?
\.\s*$ # Terminator
/,
# <a exist="-11118378" noun="Jhalk">Jhalk</a> has accepted your offer and is now holding an opalescent <a exist="63748264" noun="jar">glass jar</a> containing star rubies.
# INV FULL-like results
:invfull_none => /^You are carrying nothing at this time\.\s*$/,
:invfull_start => /^You are (?:currently wearing and carrying:\s*$)/,
:invfull => /^(?<depth> +)(?:(?<pre>[^<]+) )?<a exist="(?<exist>-?\d+)" noun="(?<noun>[^"]*)">(?<name>[^<]*)<\/a>(?: (?<post>.+?))?(?: (?<attrs>\(.*\)))? *$/,
:invfull_end => /^\((?<count>\d+) items? displayed\.\)\s*$/,
# Jar status XML
:jarstatus => /(?x)
^(?:<.*>)? # Anchor, and discard leading XML if any
(?:The|Inside\ the)
\ <a\ exist="(?<exist>-?\d+)"\ noun="(?<noun>[^"]+)">(?<name>[^<]+)<\/a>
\ (?:you\ see\ (?<count>\d+)\ portions?\ of\ (?<gem>.*)\.\ \ It\ )? # Jar contents, if not empty
is\ (?<fill>.*)
\.\s*$ # Terminator
/,
:jarcontaining => /^containing (.+)/,
# Shake success
:shake_success => /^(?:<.*>)?You give your .*?<a exist="(?<id>-?\d+)".*?<\/a> a hard shake, causing .*?<a exist="(?<gem_id>-?\d+)".*?>(?<gem>.*?)<\/a> to fall into your (?<hand>left|right) hand\.\s*$/,
# Shake fail
:shake_was_empty => /^(?:<.*>)?You shake your .*?<a exist="(?<id>-?\d+)".*?<\/a> a few times before you realize that it is empty\./,
# Jar now empty
:shake_now_empty => /^(?:<.*>)?That was the last <a.*> in the <a exist="(?<id>-?\d+)".*?<\/a>\./,
# Generic shake
:shake => /(?x)
^(?:<.*>)?
You\ (?:give|shake)\ your\ .*?<a\ exist="(?<id>-?\d+)".*?<\/a>
\ a\ (?:
few\ times\ before\ you\ realize\ that\ it\ is\ empty
| (?:
hard\ shake,\ causing
\ (?:(?<pre>[^<]+)\ )?
<a\ exist="(?<exist>-?\d+)"\ noun="(?<noun>[^"]+)">(?<name>[^<]+)<\/a>
(?:\ (?<post>[^<]+?))?
\ to\ fall\ into\ your\ (?<hand>left|right)\ hand
)
)\.\s*$
/,
# Empty
:empty => /^(?:<.*>)?You empty (?<qty>some|all)(?: of the)? (?<gem>.*) into your .*<a exist="(?<id>-?\d+)".*<\/a>(?<full>, filling it)?\./,
# Empty, empty source
:empty_empty_source => /^(?:<.*>)?But your <a exist="(?<id>-?\d+)".*?<\/a> is empty!$/,
# Successful gem store
:store_success => /(?x)
^
You\ (?<action>add|put)(?:\ your)?
\ (?:(?<pre>[^<]+)\ )?
<a\ exist="(?<exist>-?\d+)"\ noun="(?<noun>[^"]+)">(?<name>[^<]+)<\/a>
(?:\ (?<post>[^<]+?))?
\ (?:into\ your\ empty|to\ the\ contents\ of\ the)
\ <a\ exist="(?<jar>-?\d+)\".*<\/a>
(?<filled>,\ filling\ it)?
\.\s*$
/,
# Failed gem store
:store_failed => /(?x)
^
The\ <a\ exist="(?<jar>-?\d+)".*?<\/a>
\ (?:
(?<full>is\ full!)
| (?:
(?<wrong>
already\ contains\ (?<current>[^,]*),
\ so\ you\ think\ better\ of\ mixing\ in\ your
)
| (?<unsuitable>does\ not\ appear\ to\ be\ a\ suitable\ container\ for)
)
\ (?:(?<pre>[^<]+)\ )?
<a\ exist="(?<exist>-?\d+)"\ noun="(?<noun>[^"]+)">(?<name>[^<]+)<\/a>
(?:\ (?<post>[^<]+?))?\.
)\s*$
/,
# Giving items to another player
:offer_started => /^You offer your .* to <a exist="(?<to_player_id>-?\d+)" noun="(?<to_player>[^"]+)">.*, who has 30 seconds to accept the offer/,
:offer_failed => /^(?:You may only have one outstanding offer at a time\.|To whom\?)\s*$/,
:offer_declined => /^<a exist="(?<to_player_id>-?\d+)" noun="(?<to_player>[^"]+)">.+<\/a> has declined the offer.$/,
:offer_resolved => /(?:^<a exist="(?<to_player_id>-?\d+)" noun="(?<to_player>[^"]+)">.+<\/a> has (?<status>accepted|declined) (?:the|your) offer(?:\.| and is now holding))|(?<expired>Your offer to <a.*<\/a> has expired\.$)/,
}
def self.anon_hook(prefix = '')
now = Time.now
"Jarserve2::#{prefix}-#{now.tv_sec}.#{now.tv_usec}-#{Random.rand(10000)}"
end
def self.cleanup
[DownstreamHook, UpstreamHook].each{|provider|
provider.list.find_all{|name| name.start_with?('Jarserve2::')}.each{|name| provider.remove(name)}
}
end
def self.waitre(pattern, timeout=nil)
if timeout
begin
Timeout::timeout(timeout) {
return self.waitre(pattern)
}
rescue Timeout::Error
return nil
end
end
xml = nil
xml = get until xml =~ pattern
return xml, $~
end
def self.quiet_command(command, start_pattern, end_pattern = /<prompt/, include_end = true, timeout=5)
result = []
name = self.anon_hook
filter = false
begin
Timeout::timeout(timeout, Interrupt) {
DownstreamHook.add(name, proc {|xml|
if filter
if xml =~ end_pattern
DownstreamHook.remove(name)
filter = false
# result << xml.rstrip if include_end
# thread.raise(Interrupt)
# next(include_end ? nil : xml)
else
# result << xml.rstrip
next(nil)
end
elsif xml =~ start_pattern
filter = true
# result << xml.rstrip
next(nil)
else
xml
end
})
fput command
until (xml = get) =~ start_pattern; end
result << xml.rstrip
until (xml = get) =~ end_pattern
result << xml.rstrip
end
if include_end
result << xml.rstrip
end
}
rescue Interrupt
nil
end
return result
end
# def self.quiet_command(command, start_pattern, end_pattern = /<prompt/, include_end = true, timeout=nil)
# hook = self.mask_output(start_pattern, end_pattern, include_end)
# fput command
# xml, * = waitre(start_pattern, timeout)
# unless xml
# DownstreamHook.remove(hook)
# return nil
# end
# result = [xml]
# return result if xml =~ end_pattern
# while true
# xml = get
# if xml =~ end_pattern
# result << xml if include_end
# return result
# end
# result << xml
# end
# end
def self.mask_output(start_pattern, end_pattern = /<prompt/, include_end = true)
filter = false
name = self.anon_hook
DownstreamHook.add(name, proc {|xml|
if filter
if xml =~ end_pattern
DownstreamHook.remove(name)
filter = false
next(include_end ? nil : xml)
else
next(nil)
end
elsif xml =~ start_pattern
filter = true
next(nil)
else
xml
end
})
return name
end
def self.gems
return @gems
end
def self.empty
return @gems[nil]
end
def self.[](k)
@gems[k] = JarSet.new(k) unless @gems[k]
return @gems[k]
end
def self.[]=(k,v)
@gems[k] = v
end
def self.add(jar)
raise "Jar #{jar} is already added." if @jars[jar.id]
@jars[jar.id] = jar
self[jar.gem].add(jar)
jar.added_to(self)
return jar
end
def self.remove(jar)
raise "Jar #{jar} is not part of our set of jars." unless @jars[jar.id]
@jars.delete(jar.id)
unless self[jar.gem].delete?(jar)
raise "Jar #{jar} is not a member of the gemset it should be part of."
end
jar.removed_from(self)
@gems.delete(jar.gem) if @gems[jar.gem].length == 0
return jar
end
def self.move(jar, new_gem)
raise "Jar #{jar} is not part of our set of jars." unless @jars[jar.id]
return if new_gem == jar.gem
unless self[jar.gem].delete?(jar)
raise "Jar #{jar} is not a member of the gemset it should have been part of."
end
unless self[new_gem].add?(jar)
raise "Jar #{jar} is already a member of the gemset it is being moved to."
end
end
def self.is_jar?(item)
return true if item.type =~ /(?:^|,)jar(?:$|,)/
return false
end
def self.is_jarable?(item)
return true if item.type =~ /(?:^|,)(?:gem|reagent)(?:$|,)/
return false
end
def self.refresh(jar)
return if @refresh_inflight.include?(jar.id) or @refresh_pending.include?(jar.id)
if @refresh_inflight.length > TYPEAHEAD
@refresh_pending << jar.id
else
@refresh_inflight << jar.id
silence_me if $SAFE == 0
put "look in #{jar.ref}"
silence_me if $SAFE == 0
end
end
def self.update_cache(matchdata, prefix='')
return nil unless matchdata[:"#{prefix}exist"]
item = item_from_match(matchdata, prefix)
@itemcache.delete(item.id)
@itemcache.each{|k,v|
break if @itemcache.length < 50
@itemcache.delete(k)
}
@itemcache[item.id] = item
end
def self.item_from_match(matchdata, prefix='')
return GameObj.new(
matchdata[:"#{prefix}exist"],
matchdata[:"#{prefix}noun"],
matchdata[:"#{prefix}name"],
matchdata[:"#{prefix}pre"],
matchdata[:"#{prefix}post"]
)
end
def self.resolve_gem(text)
# Returns candidate for a gem description. Returns matching gems that we have.
parts = text.strip.downcase.split(/\s+/).map{|word| Regexp::escape(word)}
pattern = Regexp::new("^#{parts.join('\\S*\\s+')}\\S*$")
matches = @gems.keys.find_all{|key| key.name =~ pattern or key.plural =~ pattern}
return matches if matches.length > 0
pattern = Regexp::new("\\b#{parts.join('.*\\b')}\\S*$")
return @gems.keys.find_all{|key| key.name =~ pattern or key.plural =~ pattern}
end
def self.ingest(xml)
if xml =~ PATTERNS[:hands]
items = [update_cache($~, 'right_'), update_cache($~, 'left_')]
event = {
:type => :glance,
:items => items,