# encoding: UTF-8 #============================================================================== # AuthoritativePlayerDrops 1.3.0 # Manual/menu drops are registered immediately with the authoritative server. # Equal drops made in the same frame are coalesced into one server packet. # No local/PWO copy is created for supported RPG items, so pickup is one-shot. #============================================================================== module MPAuthoritativePlayerDrops VERSION = "1.3.0" @pending = {} @pending_raw = [] @pending_frame = nil class << self def online_ready? return false unless defined?(WebSocket) connected = WebSocket.instance_variable_get(:@connected) rescue false worldsync = WebSocket.instance_variable_get(:@worldsync) rescue false !!connected && !!worldsync && WebSocket.respond_to?(:net_drop_item) rescue false end def overmap? return false unless defined?($game_map) && $game_map !!($game_map.isOverMap rescue false) rescue false end def object_for(raw) return raw unless raw.is_a?(String) || raw.is_a?(Symbol) token = raw.to_s return nil if token.empty? begin obj = $data_ItemName[token] if defined?($data_ItemName) && $data_ItemName return obj unless obj.nil? rescue end nil rescue nil end def network_identity(raw) obj = object_for(raw) return nil if obj.nil? kind = nil begin kind = "item" if defined?(RPG::Item) && obj.is_a?(RPG::Item) kind = "weapon" if defined?(RPG::Weapon) && obj.is_a?(RPG::Weapon) kind = "armor" if defined?(RPG::Armor) && obj.is_a?(RPG::Armor) rescue end if kind.nil? klass = (obj.class.name.to_s rescue "") kind = "item" if klass == "RPG::Item" kind = "weapon" if klass == "RPG::Weapon" kind = "armor" if klass == "RPG::Armor" end iid = (obj.id.to_i rescue 0) return nil if kind.nil? || iid <= 0 [kind, iid] rescue nil end def character_xy(character) ch = character ch = $game_player if ch.nil? && defined?($game_player) return nil unless ch [ch.x.to_i, ch.y.to_i] rescue nil end def raw_token(raw) return raw.item_name.to_s if raw.respond_to?(:item_name) raw.to_s rescue raw.to_s end # Called instead of vanilla record_dropped_items for normal online items. # Returns true only when every supplied token is handled by the server path. def register_online(items, character=nil) return false unless online_ready? return false if overmap? pos = character_xy(character) return false if pos.nil? x, y = pos list = Array(items) return false if list.empty? resolved = [] list.each do |raw| ident = network_identity(raw) return false if ident.nil? # preserve vanilla semantics for custom tokens resolved << [ident[0], ident[1], raw_token(raw)] end @pending ||= {} resolved.each do |kind, iid, token| key = [kind, iid.to_i, x.to_i, y.to_i] rec = @pending[key] if rec rec[:qty] += 1 else @pending[key] = { :kind => kind, :id => iid.to_i, :x => x.to_i, :y => y.to_i, :qty => 1, :token => token } end end @pending_frame = (Graphics.frame_count rescue 0) true rescue => e puts "[DROP-AUTH] register error #{e.class}: #{e}" rescue nil false end # If connection vanished between menu click and next update, restore vanilla # local drop instead of silently eating the already removed item. def spawn_local_fallback(token, x, y, qty) return false unless defined?(EvLib) qty.to_i.times do EvLib.sum(token.to_s, x.to_i, y.to_i, {:user => $game_player}) end true rescue => e puts "[DROP-AUTH] fallback error #{e.class}: #{e}" rescue nil false end def flush_pending(force=false) rows = @pending return 0 if rows.nil? || rows.empty? unless force now = (Graphics.frame_count rescue 0) # Coalesce all SHIFT/drop repetitions generated during the same frame. return 0 if !@pending_frame.nil? && now <= @pending_frame.to_i end @pending = {} @pending_frame = nil sent = 0 rows.each_value do |rec| if online_ready? WebSocket.net_drop_item(rec[:kind], rec[:id], rec[:qty], rec[:x], rec[:y]) puts "[DROP-AUTH] sent #{rec[:kind]}##{rec[:id]} x#{rec[:qty]} @#{rec[:x]},#{rec[:y]}" rescue nil sent += 1 else spawn_local_fallback(rec[:token], rec[:x], rec[:y], rec[:qty]) puts "[DROP-AUTH] offline fallback #{rec[:token]} x#{rec[:qty]} @#{rec[:x]},#{rec[:y]}" rescue nil end end sent rescue => e puts "[DROP-AUTH] flush error #{e.class}: #{e}" rescue nil 0 end end end # Hook the actual registration point. This happens immediately when the item is # removed in the item menu, before the player closes the menu. if defined?(Game_Temp) class Game_Temp unless method_defined?(:mp_authdrop_register_item_drop_without_fix) alias mp_authdrop_register_item_drop_without_fix register_item_drop end def register_item_drop(items, character=$game_player) begin if MPAuthoritativePlayerDrops.register_online(items, character) return true end rescue => e puts "[DROP-AUTH] hook error #{e.class}: #{e}" rescue nil end mp_authdrop_register_item_drop_without_fix(items, character) end end end # Flush on the next UI/game frame. Scene_Base also updates while Scene_Menu is # open, so other players receive the canonical server drop immediately. if defined?(Scene_Base) class Scene_Base unless method_defined?(:mp_authdrop_update_without_fix) alias mp_authdrop_update_without_fix update end def update(*args, &block) result = mp_authdrop_update_without_fix(*args, &block) MPAuthoritativePlayerDrops.flush_pending(false) rescue nil result end end end # Safety flush before leaving the menu. Normally the next-frame flush above has # already sent everything; this prevents a pending row surviving a fast close. if defined?(Scene_Menu) class Scene_Menu if instance_methods.include?(:process_exit_menu) && !method_defined?(:mp_authdrop_process_exit_menu_without_fix) alias mp_authdrop_process_exit_menu_without_fix process_exit_menu def process_exit_menu(*args, &block) MPAuthoritativePlayerDrops.flush_pending(true) rescue nil mp_authdrop_process_exit_menu_without_fix(*args, &block) end end end end puts "[DROP-AUTH] loaded v#{MPAuthoritativePlayerDrops::VERSION}; live server-authoritative menu drops enabled" rescue nil #------------------------------------------------------------------------------ # Pickup input compatibility 1.3.0 # LonaRPG does NOT use multiplayer's hard-coded :E for normal item pickup. # It uses Game_Player#inputToTriggerEvent? (the remappable action/confirm key, # internally :C, plus the stock mouse path). Authoritative network drops must # obey that same input and only the exact tile under the player. #------------------------------------------------------------------------------ if defined?(WebSocket) module WebSocket class << self if method_defined?(:check_pickup_or_interaction) && !method_defined?(:mp_authdrop_check_pickup_without_action_fix) alias_method :mp_authdrop_check_pickup_without_action_fix, :check_pickup_or_interaction end end def self.mp_authdrop_action_trigger? return false unless defined?($game_player) && $game_player begin return false if defined?($game_map) && $game_map && $game_map.interpreter && $game_map.interpreter.running? rescue end begin return false if $game_player.respond_to?(:movable?) && !$game_player.movable? rescue end begin actor = $game_player.actor if $game_player.respond_to?(:actor) return false if actor && actor.respond_to?(:lonaDeath?) && actor.lonaDeath? rescue end begin return false if Input.respond_to?(:skillKeyPressed?) && Input.skillKeyPressed? rescue end begin return !!$game_player.inputToTriggerEvent? if $game_player.respond_to?(:inputToTriggerEvent?) rescue end # Fallback for unusual builds: :C is LonaRPG's remappable action symbol. begin return !!Input.trigger?(:C) rescue false end rescue false end def self.check_pickup_or_interaction begin if @worldsync && mp_authdrop_action_trigger? did = find_drop_on_tile($game_player.x, $game_player.y) if did # debounce / in-flight — без optimistic remove if request_map_drop_pickup(did) puts "[DROP-AUTH] pickup request #{did} tile=#{$game_player.x},#{$game_player.y}" rescue nil end return end end rescue => e puts "[DROP-AUTH] pickup input error #{e.class}: #{e}" rescue nil end # Preserve the old multiplayer interaction path (including its :E key) # for non-drop interactions; only authoritative drop pickup is changed. mp_authdrop_check_pickup_without_action_fix if respond_to?(:mp_authdrop_check_pickup_without_action_fix) end end end