Module: Lich::Gemstone::Infomon::Parser

Defined in:
documented/gemstone/infomon/parser.rb

Overview

this module handles all of the logic for parsing game lines that infomon depends on

Defined Under Namespace

Modules: Pattern, State

Class Method Summary collapse

Class Method Details

.find_cat(category) ⇒ String

Finds the category based on the given string.

Parameters:

  • category (String)

    The category string to match.

Returns:

  • (String)

    The matched category.



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'documented/gemstone/infomon/parser.rb', line 292

def self.find_cat(category)
  case category
  when /Armor/
    'Armor'
  when /Ascension/
    'Ascension'
  when /Combat/
    'CMan'
  when /Feat/
    'Feat'
  when /Shield/
    'Shield'
  when /Weapon/
    'Weapon'
  end
end

.parse(line) ⇒ Symbol

Parses a line of input and updates the state accordingly.

Examples:

Parsing a line

result = Parser.parse("Your character has leveled up!")

Parameters:

  • line (String)

    The line to parse.

Returns:

  • (Symbol)

    The result of the parsing operation.

Raises:

  • (StandardError)

    if an error occurs during parsing.



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
# File 'documented/gemstone/infomon/parser.rb', line 315

def self.parse(line)
  # O(1) vs O(N)
  return :noop unless line =~ Pattern::All

  begin
    case line
    # blob saves
    when Pattern::CharRaceProf
      # name captured here, but do not rely on it - use XML instead
      @stat_hold = []
      Infomon.mutex_lock
      match = Regexp.last_match
      @stat_hold.push(['stat.race', match[:race].to_s],
                      ['stat.profession', match[:profession].to_s]) unless Effects::Spells.active?(1212)
      :ok
    when Pattern::CharGenderAgeExpLevel
      # level captured here, but do not rely on it - use XML instead
      match = Regexp.last_match
      @stat_hold.push(['stat.gender', match[:gender].to_s],
                      ['stat.age', match[:age].delete(',').to_i]) unless Effects::Spells.active?(1212)
      @stat_hold.push(['stat.experience', match[:experience].delete(',').to_i])
      :ok
    when Pattern::Stat
      match = Regexp.last_match
      @stat_hold.push(['stat.%s' % match[:stat], match[:value].to_i],
                      ['stat.%s_bonus' % match[:stat], match[:bonus].to_i],
                      ['stat.%s.enhanced' % match[:stat], match[:enhanced_value].to_i],
                      ['stat.%s.enhanced_bonus' % match[:stat], match[:enhanced_bonus].to_i])
      :ok
    when Pattern::StatEnd
      match = Regexp.last_match
      @stat_hold.push(['currency.silver', match[:silver].delete(',').to_i])
      Infomon.upsert_batch(@stat_hold)
      Infomon.mutex_unlock
      :ok
    when Pattern::Fame # serves as ExprStart
      @expr_hold = []
      Infomon.mutex_lock
      match = Regexp.last_match
      @expr_hold.push(['experience.fame', match[:fame].delete(',').to_i])
      :ok
    when Pattern::RealExp
      match = Regexp.last_match
      @expr_hold.push(['experience.field_experience_current', match[:fxp_current].delete(',').to_i],
                      ['experience.field_experience_max', match[:fxp_max].delete(',').to_i])
      :ok
    when Pattern::AscExp
      match = Regexp.last_match
      @expr_hold.push(['experience.ascension_experience', match[:ascension_experience].delete(',').to_i])
      :ok
    when Pattern::TotalExp
      match = Regexp.last_match
      @expr_hold.push(['experience.total_experience', match[:total_experience].delete(',').to_i],
                      ['experience.deaths_sting', match[:deaths_sting]])
      :ok
    when Pattern::LTE
      match = Regexp.last_match
      @expr_hold.push(['experience.long_term_experience', match[:long_term_experience].delete(',').to_i],
                      ['experience.deeds', match[:deeds].to_i])
      :ok
    when Pattern::ExprEnd
      Infomon.upsert_batch(@expr_hold)
      Infomon.mutex_unlock
      :ok
    when Pattern::SkillStart
      @skills_hold = []
      Infomon.mutex_lock
      :ok
    when Pattern::Skill
      if Infomon.mutex.owned?
        match = Regexp.last_match
        @skills_hold.push(['skill.%s' % match[:name].downcase, match[:ranks].to_i],
                          ['skill.%s_bonus' % match[:name], match[:bonus].to_i])
        :ok
      else
        :noop
      end
    when Pattern::SpellRanks
      if Infomon.mutex.owned?
        match = Regexp.last_match
        @skills_hold.push(['spell.%s' % match[:name].downcase, match[:rank].to_i])
        :ok
      else
        :noop
      end
    when Pattern::SkillEnd
      if Infomon.mutex.owned?
        Infomon.upsert_batch(@skills_hold)
        Infomon.mutex_unlock
        :ok
      else
        :noop
      end
    when Pattern::GoalsDetected
      State.set(State::Goals)
      :ok
    when Pattern::GoalsEnded
      if State.get.eql?(State::Goals)
        State.set(State::Ready)
        respond
        _respond Lich::Messaging.monsterbold('You just trained your character.  Lich will gather your updated skills.')
        respond
        # temporary inform for users about command
        # fixme: update ExecCommand to consistently perform local API actions from lib files
        respond "[infomon_sync]#{$SEND_CHARACTER}skills"
        Game._puts("#{$cmd_prefix}skills")
        :ok
      else
        :noop
      end
    when Pattern::PSMStart
      match = Regexp.last_match
      @psm_hold = []
      @psm_cat = find_cat(match[:cat])
      Infomon.mutex_lock
      :ok
    when Pattern::PSM
      match = Regexp.last_match
      @psm_hold.push(["#{@psm_cat.downcase}.%s" % match[:command], match[:ranks].to_i])
      :ok
    when Pattern::PSMEnd
      Infomon.upsert_batch(@psm_hold)
      Infomon.mutex_unlock
      :ok
    when Pattern::NoWarcries
      Infomon.upsert_batch([['warcry.bertrandts_bellow', 0],
                            ['warcry.yerties_yowlp', 0],
                            ['warcry.gerrelles_growl', 0],
                            ['warcry.seanettes_shout', 0],
                            ['warcry.carns_cry', 0],
                            ['warcry.horlands_holler', 0]])
      :ok
    # end of blob saves
    when Pattern::Warcries
      match = Regexp.last_match
      Infomon.set('warcry.%s' % match[:name].split(' ')[1], 1)
      :ok
    when Pattern::Levelup
      match = Regexp.last_match
      Infomon.upsert_batch([['stat.%s' % match[:stat], match[:value].to_i],
                            ['stat.%s_bonus' % match[:stat], match[:bonus].to_i]])
      :ok
    when Pattern::SpellsSolo
      match = Regexp.last_match
      Infomon.set('spell.%s' % match[:name].downcase, match[:rank].to_i)
      :ok
    when Pattern::Citizenship
      Infomon.set('citizenship', Regexp.last_match[:town].to_s)
      :ok
    when Pattern::NoCitizenship
      Infomon.set('citizenship', 'None')
      :ok
    when Pattern::Society
      match = Regexp.last_match
      Infomon.set('society.status', match[:society].to_s)
      Infomon.set('society.rank', match[:rank].to_i)
      case match[:standing] # if Master in society the rank match is nil
      when 'Master'
        if /Voln/.match?(match[:society])
          Infomon.set('society.rank', 26)
        elsif /Council of Light|Guardians of Sunfist/.match?(match[:society])
          Infomon.set('society.rank', 20)
        end
      end
      :ok
    when Pattern::NoSociety
      Infomon.set('society.status', 'None')
      Infomon.set('society.rank', 0)
      :ok
    when Pattern::SocietyJoin
      match = Regexp.last_match.to_s
      case match[/Order|Council|Guardians/]
      when 'Order'
        Infomon.set('society.status', 'Order of Voln')
        Infomon.set('society.rank', 1)
      when 'Guardians'
        Infomon.set('society.status', "Guardians of Sunfist")
        Infomon.set('society.rank', 0)
      when 'Lodge'
        Infomon.set('society.status', 'Council of Light')
        Infomon.set('society.rank', 1)
      end
      :ok
    when Pattern::SocietyStep
      Infomon.set('society.rank', Infomon.get('society.rank') + 1)
      :ok
    when Pattern::SocietyResign
      Infomon.set('society.status', 'None')
      Infomon.set('society.rank', 0)
      :ok
    when Pattern::LearnPSM, Pattern::LearnTechnique
      match = Regexp.last_match
      @psm_cat = find_cat(match[:cat])
      seek_name = PSMS.name_normal(match[:psm])
      db_name = PSMS.find_name(seek_name, @psm_cat)
      Infomon.set("#{@psm_cat.downcase}.#{db_name[:short_name]}", match[:rank].to_i)
      :ok
    when Pattern::UnlearnPSM, Pattern::UnlearnTechnique
      match = Regexp.last_match
      @psm_cat = find_cat(match[:cat])
      seek_name = PSMS.name_normal(match[:psm])
      no_decrement = (match.string =~ /have decreased to/)
      db_name = PSMS.find_name(seek_name, @psm_cat)
      Infomon.set("#{@psm_cat.downcase}.#{db_name[:short_name]}", (no_decrement ? match[:rank].to_i : match[:rank].to_i - 1))
      :ok
    when Pattern::LostTechnique
      match = Regexp.last_match
      @psm_cat = find_cat(match[:cat])
      seek_name = PSMS.name_normal(match[:psm])
      db_name = PSMS.find_name(seek_name, @psm_cat)
      Infomon.set("#{@psm_cat.downcase}.#{db_name[:short_name]}", 0)
      :ok
    when Pattern::Resource
      match = Regexp.last_match
      Infomon.set('resources.weekly', match[:weekly].delete(',').to_i)
      Infomon.set('resources.total', match[:total].delete(',').to_i)
      :ok
    when Pattern::Suffused
      match = Regexp.last_match
      Infomon.set('resources.type', match[:type].to_s)
      Infomon.set('resources.suffused', match[:suffused].delete(',').to_i)
      :ok
    when Pattern::VolnFavor
      match = Regexp.last_match
      Infomon.set('resources.voln_favor', match[:favor].delete(',').to_i)
      :ok
    when Pattern::CovertArtsCharges
      match = Regexp.last_match
      Infomon.set('resources.covert_arts_charges', match[:charges].delete(',').to_i)
      :ok
    when Pattern::GigasArtifactFragments
      match = Regexp.last_match
      Infomon.set('currency.gigas_artifact_fragments', match[:gigas_artifact_fragments].delete(',').to_i)
      :ok
    when Pattern::RedsteelMarks
      match = Regexp.last_match
      Infomon.set('currency.redsteel_marks', match[:redsteel_marks].delete(',').to_i)
      :ok
    when Pattern::GemstoneDust
      match = Regexp.last_match
      Infomon.set('currency.gemstone_dust', match[:gemstone_dust].delete(',').to_i)
      :ok
    when Pattern::TicketGeneral
      match = Regexp.last_match
      Infomon.set('currency.tickets', match[:tickets].delete(',').to_i)
      :ok
    when Pattern::TicketBlackscrip
      match = Regexp.last_match
      Infomon.set('currency.blackscrip', match[:blackscrip].delete(',').to_i)
      :ok
    when Pattern::TicketBloodscrip
      match = Regexp.last_match
      Infomon.set('currency.bloodscrip', match[:bloodscrip].delete(',').to_i)
      :ok
    when Pattern::TicketEtherealScrip
      match = Regexp.last_match
      Infomon.set('currency.ethereal_scrip', match[:ethereal_scrip].delete(',').to_i)
      :ok
    when Pattern::TicketSoulShards
      match = Regexp.last_match
      Infomon.set('currency.soul_shards', match[:soul_shards].delete(',').to_i)
      :ok
    when Pattern::TicketRaikhen
      match = Regexp.last_match
      Infomon.set('currency.raikhen', match[:raikhen].delete(',').to_i)
      :ok
    when Pattern::WealthSilver
      match = Regexp.last_match
      case match[:silver]
      when 'no'
        Infomon.set('currency.silver', 0)
      when 'but one'
        Infomon.set('currency.silver', 1)
      else
        Infomon.set('currency.silver', match[:silver].delete(',').to_i)
      end
      :ok
    when Pattern::WealthSilverContainer
      match = Regexp.last_match
      Infomon.set('currency.silver_container', match[:silver].delete(',').to_i)
      :ok
    when Pattern::AccountName
      if Account.name.nil?
        match = Regexp.last_match
        Account.name = match[:name].upcase
        :ok
      else
        :noop
      end
    when Pattern::AccountSubscription
      if Account.subscription
        match = Regexp.last_match
        Account.subscription = match[:subscription].gsub('Standard', 'Normal').gsub('F2P', 'Free').gsub('Platinum', 'Premium').upcase
        Infomon.set('account.type', match[:subscription].gsub('Standard', 'Normal').gsub('F2P', 'Free').upcase)
        :ok
      else
        :noop
      end
    when Pattern::ProfileStart
      State.set(State::Profile)
      :ok
    when Pattern::ProfileName
      match = Regexp.last_match
      if State.get.eql?(State::Profile) && !match[:name].split(' ').include?(Char.name)
        State.set(State::Ready)
        :ok
      else
        :noop
      end
    when Pattern::ProfileHouseCHE
      if State.get.eql?(State::Profile)
        match = Regexp.last_match
        Infomon.set('che', (match[:none] ? 'none' : Lich::Util.normalize_name(match[:house])))
        State.set(State::Ready)
        :ok
      else
        :noop
      end
    when Pattern::ResignCHE
      match = Regexp.last_match
      Infomon.set('che', (match[:none] ? 'none' : Lich::Util.normalize_name(match[:house])))
      :ok

    # TODO: refactor / streamline?
    when Pattern::ThornPoisonStart, Pattern::ThornPoisonProgression, Pattern::ThornPoisonDeprogression
      Infomon.set('status.thorned', true)
      :ok
    when Pattern::ThornPoisonEnd
      Infomon.set('status.thorned', false)
      :ok
    when Pattern::SleepActive
      Infomon.set('status.sleeping', true)
      :ok
    when Pattern::SleepNoActive
      Infomon.set('status.sleeping', false)
      :ok
    when Pattern::BindActive
      Infomon.set('status.bound', true)
      :ok
    when Pattern::BindNoActive
      Infomon.set('status.bound', false)
      :ok
    when Pattern::SilenceActive
      Infomon.set('status.silenced', true)
      :ok
    when Pattern::SilenceNoActive
      Infomon.set('status.silenced', false)
      :ok
    when Pattern::CalmActive
      Infomon.set('status.calmed', true)
      :ok
    when Pattern::CalmNoActive
      Infomon.set('status.calmed', false)
      :ok
    when Pattern::CutthroatActive
      Infomon.set('status.cutthroat', true)
      :ok
    when Pattern::CutthroatNoActive
      Infomon.set('status.cutthroat', false)
      :ok
    when Pattern::SpellUpMsgs
      spell = Spell.list.find do |s|
        line =~ /^#{s.msgup}$/
      end
      spell.putup unless spell.active?
      # add various cooldowns back without affecting parse speed
      Spells.require_cooldown(spell)
      :ok
    when Pattern::SpellDnMsgs
      spell = Spell.list.find do |s|
        line =~ /^#{s.msgdn}$/
      end
      spell.putdown if spell.active?
      :ok
    when Pattern::SpellsongRenewed
      Spellsong.renewed
      :ok
    else
      :noop
    end
  rescue StandardError
    respond "--- Lich: error: Infomon::Parser.parse: #{$!}"
    respond "--- Lich: error: line: #{line}"
    Lich.log "error: Infomon::Parser.parse: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
    Lich.log "error: line: #{line}\n\t"
  end
end