295 lines
10 KiB
Ruby
295 lines
10 KiB
Ruby
# encoding: UTF-8
|
|
# BankGuard client 1.0.1. Compatible with BankGuard server 1.0.0.
|
|
# New game resets documents/debt normally. Only the existing bank survives.
|
|
module MPBankGuard
|
|
REFUSAL = "У вы не можете пополнять счёт пока не закроете долг и не будете иметь оригинальные документы Лоны!"
|
|
KEYS = {
|
|
"principal" => "BackStreetArrearsPrincipal",
|
|
"interest" => "BackStreetArrearsInterest",
|
|
"workDebt" => "BackStreetArrearsWhorePrincipal",
|
|
"loanSeen" => "RecordBackStreetArrears"
|
|
}
|
|
class << self
|
|
def connected?
|
|
defined?(WebSocket) && WebSocket.connected?
|
|
end
|
|
def available?
|
|
connected? && WebSocket.bootstrap_sync_unlocked? &&
|
|
defined?($game_party) && $game_party && defined?($story_stats) && $story_stats &&
|
|
defined?($game_date) && $game_date &&
|
|
(!WebSocket.respond_to?(:gameplay_scene_active?) || WebSocket.gameplay_scene_active?)
|
|
end
|
|
def snapshot
|
|
state = {"documents" => $game_party.item_number("ItemPassportLona").to_i > 0}
|
|
KEYS.each { |key, story| state[key] = [$story_stats[story].to_i, 0].max }
|
|
state["graceDays"] = [$story_stats["BackStreetArrearsPrepayDateAMT"].to_i - $game_date.dateAmt.to_i, 0].max
|
|
state
|
|
end
|
|
def encode(value)
|
|
blob = WebSocket._live_sync_blob_from_object(value, nil, "bank_guard")
|
|
raise "Cannot encode BankGuard state" if blob.to_s.empty?
|
|
blob
|
|
end
|
|
def decode(value)
|
|
raise "Oversized BankGuard state" if value.to_s.size > 16384
|
|
result = WebSocket.bootstrap_decode_json_blob(value)
|
|
raise "Invalid BankGuard state" unless result.is_a?(Hash)
|
|
result
|
|
end
|
|
def reset_connection
|
|
@ready = false
|
|
@token = nil
|
|
@request = nil
|
|
@last_report = nil
|
|
@hello_frame = nil
|
|
@party_id = nil
|
|
end
|
|
def before_new_game
|
|
report if @ready && connected?
|
|
# setup_new_game is also used to construct objects for an ordinary cloud load.
|
|
# Only an already running game explicitly starts a new run here.
|
|
@force_new = true if @ready || (connected? && WebSocket.bootstrap_sync_unlocked?)
|
|
reset_connection
|
|
end
|
|
def ready?
|
|
!!@ready && connected? && @party_id == $game_party.object_id
|
|
end
|
|
def tick
|
|
unless connected?
|
|
reset_connection
|
|
return
|
|
end
|
|
return unless available?
|
|
if @party_id && @party_id != $game_party.object_id
|
|
@force_new = true
|
|
reset_connection
|
|
end
|
|
unless @request
|
|
@party_id = $game_party.object_id
|
|
@request = "#{Time.now.to_i}_#{rand(1000000000)}"
|
|
mode = WebSocket.instance_variable_get(:@bootstrap_mode).to_s
|
|
@mode = (@force_new || mode == "new") ? "new" : "resume"
|
|
@hello_blob = encode(snapshot)
|
|
end
|
|
unless @token
|
|
frame = Graphics.frame_count
|
|
if !@hello_frame || frame - @hello_frame >= 180
|
|
@hello_frame = frame
|
|
WebSocket.send_line("bankguard_hello:#{@mode}:#{@request}:#{@hello_blob}")
|
|
end
|
|
return
|
|
end
|
|
report if ready?
|
|
rescue => e
|
|
@ready = false
|
|
puts "[BankGuard] #{e.class}: #{e.message}"
|
|
end
|
|
def apply(state)
|
|
KEYS.each { |key, story| $story_stats[story] = [state.fetch(key).to_i, 0].max }
|
|
days = [state.fetch("graceDays").to_i, 0].max
|
|
$story_stats["BackStreetArrearsPrepayDateAMT"] = days > 0 ? $game_date.dateAmt.to_i + days : 0
|
|
count = $game_party.item_number("ItemPassportLona").to_i
|
|
if state.fetch("documents")
|
|
$game_party.gain_item("ItemPassportLona", 1) if count <= 0
|
|
elsif count > 0
|
|
$game_party.lose_item("ItemPassportLona", count)
|
|
end
|
|
end
|
|
def receive(message)
|
|
if message.start_with?("bankguard_state:")
|
|
parts = message.split(":", 4)
|
|
return true unless parts[1] == @request && @party_id == $game_party.object_id
|
|
@token = parts[2]
|
|
state = decode(parts[3])
|
|
# Server replies with new-run facts for 'new', old-run facts only for 'resume'.
|
|
apply(state)
|
|
@force_new = false
|
|
@last_report = encode(state)
|
|
WebSocket.send_line("bankguard_report:#{@token}:#{@last_report}")
|
|
return true
|
|
elsif message.start_with?("bankguard_ok:")
|
|
@ready = true if message.split(":", 2)[1] == @token
|
|
return true
|
|
elsif message.start_with?("bankguard_denied:")
|
|
@denied = true
|
|
@ready = false if message.end_with?(":state_error")
|
|
return true
|
|
elsif message == "bankguard_wait"
|
|
@hello_frame = nil
|
|
return true
|
|
end
|
|
false
|
|
rescue => e
|
|
@ready = false
|
|
puts "[BankGuard] invalid response: #{e.message}"
|
|
true
|
|
end
|
|
def report
|
|
return unless @token && connected? && @party_id == $game_party.object_id
|
|
blob = encode(snapshot)
|
|
return if blob == @last_report
|
|
WebSocket.send_line("bankguard_report:#{@token}:#{blob}")
|
|
@last_report = blob
|
|
end
|
|
def metadata
|
|
return nil unless ready?
|
|
snapshot.merge("token" => @token)
|
|
end
|
|
def may_deposit?
|
|
return false unless ready?
|
|
!restricted_locally?
|
|
end
|
|
def restricted_locally?
|
|
state = snapshot
|
|
!state["documents"] || %w[principal interest workDebt].any? { |key| state[key] > 0 }
|
|
end
|
|
def close_bank_choices(interpreter)
|
|
interpreter.wait_for_message
|
|
current_scene = SceneManager.scene
|
|
window = current_scene.instance_variable_get(:@message_window) if current_scene
|
|
if window
|
|
choice = window.instance_variable_get(:@choice_window)
|
|
if choice && !choice.disposed?
|
|
choice.deactivate
|
|
choice.close
|
|
choice.openness = 0
|
|
end
|
|
window.close
|
|
# Let the previous message finish its close animation/fiber before adding
|
|
# the new reply. Otherwise it can share the old choice page.
|
|
Fiber.yield while window.instance_variable_get(:@fiber)
|
|
end
|
|
if defined?($game_message) && $game_message
|
|
$game_message.choices.clear
|
|
$game_message.choice_proc = nil
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
module WebSocket
|
|
class << self
|
|
alias bankguard_parse parse
|
|
def parse(message)
|
|
return if MPBankGuard.receive(message)
|
|
result = bankguard_parse(message)
|
|
MPBankGuard.reset_connection if message.start_with?("bootstrap_mode:")
|
|
result
|
|
end
|
|
alias bankguard_update update
|
|
def update(*args)
|
|
result = bankguard_update(*args)
|
|
MPBankGuard.tick
|
|
result
|
|
end
|
|
alias bankguard_send_line send_line
|
|
def send_line(line)
|
|
packet = line.to_s
|
|
# Integrity v2 is a PRE-LOGIN authentication packet, not gameplay state.
|
|
# BankGuard becomes ready only after bootstrap, so gating this packet
|
|
# causes a circular dependency and a server-side integrity_missing timeout.
|
|
if packet.start_with?("sync_state:integrity_attest:v2:")
|
|
integrity_debug('bankguard_integrity_allow', 'pre_login_attestation') if respond_to?(:integrity_debug)
|
|
elsif packet.start_with?("sync_state:")
|
|
return -1 unless MPBankGuard.ready?
|
|
MPBankGuard.report
|
|
end
|
|
bankguard_send_line(line)
|
|
end
|
|
alias bankguard_collect_stats _live_sync_collect_stats
|
|
def _live_sync_collect_stats(actor)
|
|
result = bankguard_collect_stats(actor)
|
|
meta = MPBankGuard.metadata
|
|
result["bank_guard"] = meta if meta
|
|
result
|
|
end
|
|
alias bankguard_build_packet _live_sync_build_packet
|
|
def _live_sync_build_packet
|
|
result = bankguard_build_packet
|
|
return result unless result.is_a?(Array) && result[0].is_a?(String)
|
|
parts = result[0].split(":", -1)
|
|
# Encode an empty bank explicitly; the base server ignores an empty string.
|
|
parts[13] = MPBankGuard.encode(_live_sync_collect_bank_storage)
|
|
packet = parts.join(":")
|
|
[packet, packet]
|
|
end
|
|
end
|
|
end
|
|
|
|
module DataManager
|
|
class << self
|
|
alias bankguard_setup_new_game setup_new_game
|
|
def setup_new_game(*args)
|
|
MPBankGuard.before_new_game
|
|
bankguard_setup_new_game(*args)
|
|
end
|
|
end
|
|
end
|
|
|
|
module SceneManager
|
|
class << self
|
|
alias bankguard_goto goto
|
|
def goto(scene_class)
|
|
warned = false
|
|
if scene_class == Scene_BankStorage && MPBankGuard.restricted_locally?
|
|
interpreter = $game_map.interpreter if defined?($game_map) && $game_map
|
|
# Banker events run in this fiber. Show her reply BEFORE switching scenes,
|
|
# while the normal map dialogue window and portrait are still alive.
|
|
if interpreter && interpreter.instance_variable_get(:@fiber).equal?(Fiber.current)
|
|
MPBankGuard.close_bank_choices(interpreter)
|
|
interpreter.call_msg("\\SETpl[MaaniNormal]\\c[4]Банкирша:\\c[0] " + MPBankGuard::REFUSAL)
|
|
warned = true
|
|
end
|
|
end
|
|
result = bankguard_goto(scene_class)
|
|
scene.instance_variable_set(:@bankguard_warned_on_open, warned) if scene_class == Scene_BankStorage
|
|
result
|
|
end
|
|
end
|
|
end
|
|
|
|
class Scene_BankStorage
|
|
alias bankguard_start start
|
|
def start(*args)
|
|
result = bankguard_start(*args)
|
|
if !@mp_bank_storage_blocked && !@bankguard_warned_on_open && MPBankGuard.restricted_locally?
|
|
bankguard_show_refusal
|
|
end
|
|
result
|
|
end
|
|
alias bankguard_move_item move_item
|
|
def move_item(value = nil)
|
|
if @bag_window && @bag_window.active && @bag_window.item &&
|
|
(!MPBankGuard.may_deposit? || @bag_window.item.item_name == "ItemPassportLona")
|
|
SndLib.sys_buzzer
|
|
bankguard_show_refusal
|
|
return
|
|
end
|
|
bankguard_move_item(value)
|
|
end
|
|
def bankguard_show_refusal
|
|
return if @bankguard_dialog
|
|
@bankguard_dialog = Window_Base.new(8, (Graphics.height - 184) / 2, Graphics.width - 16, 184)
|
|
@bankguard_dialog.z = 9999
|
|
lines = ["Банкирша:", "У вы не можете пополнять счёт пока не", "закроете долг и не будете иметь", "оригинальные документы Лоны!", "", "Enter / Esc — закрыть"]
|
|
lines.each_with_index { |line, i| @bankguard_dialog.draw_text(0, i * 24, Graphics.width - 48, 24, line) }
|
|
end
|
|
alias bankguard_scene_update update
|
|
def update
|
|
if @bankguard_dialog
|
|
if Input.trigger?(:C) || Input.trigger?(:B)
|
|
@bankguard_dialog.dispose
|
|
@bankguard_dialog = nil
|
|
end
|
|
return
|
|
end
|
|
bankguard_scene_update
|
|
end
|
|
alias bankguard_terminate terminate
|
|
def terminate
|
|
@bankguard_dialog.dispose if @bankguard_dialog && !@bankguard_dialog.disposed?
|
|
@bankguard_dialog = nil
|
|
bankguard_terminate
|
|
end
|
|
end
|