Module: MaimaiNet::Client::ConnectionSupportUserOption

Included in:
Connection
Defined in:
lib/maimai_net/client.rb

Instance Method Summary collapse

Instance Method Details

#get_gameplay_settingsHash<Symbol, UserOption::Option>

obtain current user’s gameplay settings



883
884
885
886
887
888
# File 'lib/maimai_net/client.rb', line 883

def get_gameplay_settings
  send_request(
    'get', '/home/userOption/updateUserOption', nil,
    response_page: Page::UserOption,
  )
end

#set_gameplay_settings(changes) ⇒ void



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
# File 'lib/maimai_net/client.rb', line 895

def set_gameplay_settings(changes)
  fail TypeError, "expected Hash given #{changes.class}" unless Hash === changes
  fail ArgumentError, "provided empty argument" if changes.empty?
  changes.each_key do |k|
    fail TypeError, "expected Symbol keys, given #{k.class}" unless Symbol === k
  end
  changes.values.tap do |options|
    all_options = options.all? do |option| UserOption::Option === option end
    all_raw     = options.all? do |option|
      [UserOption::Choice, Symbol, Integer].any? do |cls| cls === option end
    end
    option_classes = options.map(&:class).uniq
    fail TypeError, "expected either all #{UserOption::Option} or any of #{UserOption::Choice}, Symbol, or Integer. given #{option_classes.join(', ')}" unless all_options || all_raw
  end

  fetch_and_submit_form(
    '/home/userOption/updateUserOption', nil,
    response_page: Page::UserOption,
  ) do |data, current|
    # do not send update query if there's no change
    return if changes == current

    update = {}
    update.update(current, changes) do |key, option_old, option_new|
      case option_new
      when UserOption::Option then option_new
      when Integer, Symbol then
        option_old.dup.tap do |option|
          option.select(option_new)
        end
      when UserOption::Choice then
        option_old.dup.tap do |option|
          fail ArgumentError, "provided choice #{option_new.inspect} is not part of '#{option_old.name}' option." unless option_old.choices.include?(option_new)
          option.selected = option_new
        end
      end
    end

    update.transform_values do |option|
      option.selected_id
    end.tap &data.method(:update)

    true
  end
end