module WebSocket #--------------------------------------------------------------------------- # pending time (чтобы применять в main update, а не в thread WebSocket) #--------------------------------------------------------------------------- @pending_overmap = nil @pending_date_units = nil @last_applied_date_units = nil @last_applied_dn = nil @server_day_night_mode = nil #-------------------------------------------------------------------------- # Time helpers (fixed) #-------------------------------------------------------------------------- def self.get_current_overmap_count return 0.0 unless $story_stats && $story_stats.respond_to?(:[]) v = $story_stats["OverMapEvent_DateCount"] return 0.0 if v.nil? v.to_f end # ВАЖНО: возвращаем внутренние units (dateAmt / @date), а не кодировку Y*1000+M*100+D def self.get_current_game_date_units return 1 unless $game_date if $game_date.respond_to?(:dateAmt) return $game_date.dateAmt.to_i end # fallback: берём внутреннее @date если есть v = ($game_date.instance_variable_get(:@date) rescue nil) return v.to_i if v d = $game_date.date rescue 1 return d.to_i end def self.day_night_mode_from_date_units(date_units) return nil if date_units.nil? date_units = date_units.to_i return (date_units.even? ? :night : :day) rescue nil end # Теперь НЕ пишем сразу, а кладём в pending def self.set_overmap_count_mod100(v) @pending_overmap = (v.to_f % 100.0) end # Теперь НЕ трогаем карту/свет в сокет-колбэке — только pending def self.set_game_date_units(date_units) @pending_date_units = date_units.to_i @server_day_night_mode = day_night_mode_from_date_units(@pending_date_units) end def self.server_day_night_mode return nil unless connected? return nil if @pending_date_units.nil? && !@time_initialized && @last_server_date.nil? @server_day_night_mode || day_night_mode_from_date_units(@pending_date_units || @last_server_date) rescue nil end def self.server_authoritative_day_night? !server_day_night_mode.nil? rescue false end def self.refresh_day_night_light_sources begin if $game_map && $game_map.respond_to?(:screen) && $game_map.screen $game_map.screen.brightness = 255 if $game_map.screen.respond_to?(:brightness=) end rescue end begin if $game_map && $game_map.respond_to?(:map_background_changed=) $game_map.map_background_changed = true end rescue end begin if $game_map && $game_map.respond_to?(:events) $game_map.events.each_value do |ev| next if ev.nil? next if ev.instance_variable_defined?(:@net_player_id) next if ev.respond_to?(:deleted?) && ev.deleted? ev.refresh if ev.respond_to?(:refresh) end end rescue end begin refresh_remote_player_lights if respond_to?(:refresh_remote_player_lights) rescue end begin if $game_map && $game_map.respond_to?(:lights) && !$game_map.lights.nil? $game_map.lights.each do |source| next if source.nil? source.restore_light if source.respond_to?(:restore_light) end end rescue end end # Применяется в главном цикле (Scene update) def self.flush_time_pending changed = false # overmap count if !@pending_overmap.nil? && $story_stats && $story_stats.respond_to?(:[]=) cur = $story_stats["OverMapEvent_DateCount"].to_f rescue nil if cur != @pending_overmap $story_stats["OverMapEvent_DateCount"] = @pending_overmap changed = true end @pending_overmap = nil end # game date units date_units_changed = false if !@pending_date_units.nil? && $game_date && $game_date.respond_to?(:calculateDate) new_units = @pending_date_units @pending_date_units = nil cur_units = ($game_date.instance_variable_get(:@date) rescue nil) cur_units = cur_units.to_i if cur_units # если реально поменялось — применяем if cur_units != new_units $game_date.instance_variable_set(:@date, new_units) $game_date.calculateDate @server_day_night_mode = day_night_mode_from_date_units(new_units) changed = true date_units_changed = true puts "[TIME] date units updated to: #{new_units}" begin if defined?(MPDailyServerEffects) && MPDailyServerEffects.respond_to?(:on_date_units_changed) MPDailyServerEffects.on_date_units_changed(cur_units, new_units) end rescue => e puts "[TIME] daily effects failed: #{e.class}: #{e.message}" end end end return unless changed # Light/map refresh only when day↔night actually flips after the first # armed sync. First date apply only stores the mode (avoids login blink). return unless date_units_changed begin if $game_date && $game_date.respond_to?(:night?) dn = $game_date.night? if @last_applied_dn.nil? @last_applied_dn = dn elsif @last_applied_dn != dn @last_applied_dn = dn $game_player.light_check if $game_player && $game_player.respond_to?(:light_check) $game_map.set_light if $game_map && $game_map.respond_to?(:set_light) refresh_day_night_light_sources $game_map.need_refresh = true if $game_map end end rescue end end def self.apply_time_snapshot(server_total, server_date, steps_per_cycle=100) @last_server_total = server_total.to_f @last_server_date = server_date.to_i @steps_per_cycle = steps_per_cycle.to_i # ВНИМАНИЕ: тут теперь только pending set_overmap_count_mod100(@last_server_total) set_game_date_units(@last_server_date) end def self.server_authoritative_time? connected? rescue false end end class Scene_Base alias _ws_time_flush_update update def update WebSocket.flush_time_pending rescue nil _ws_time_flush_update end end class Game_Player unless method_defined?(:mp_server_time__orig_handle_on_move_overmap) alias mp_server_time__orig_handle_on_move_overmap handle_on_move_overmap end unless method_defined?(:mp_server_time__orig_process_movement_inputs_overmap_only) alias mp_server_time__orig_process_movement_inputs_overmap_only process_movement_inputs_overmap_only end def handle_on_move_overmap(*args, &block) return mp_server_time__orig_handle_on_move_overmap(*args, &block) unless mp_server_time_authoritative? before = mp_server_time_current_overmap_count result = mp_server_time__orig_handle_on_move_overmap(*args, &block) mp_server_time_restore_overmap_count(before) result end def process_movement_inputs_overmap_only(*args, &block) return mp_server_time__orig_process_movement_inputs_overmap_only(*args, &block) unless mp_server_time_authoritative? before = mp_server_time_current_overmap_count result = mp_server_time__orig_process_movement_inputs_overmap_only(*args, &block) mp_server_time_restore_overmap_count(before) result end def mp_server_time_authoritative? defined?(WebSocket) && WebSocket.respond_to?(:server_authoritative_time?) && WebSocket.server_authoritative_time? rescue false end def mp_server_time_current_overmap_count return nil unless $story_stats && $story_stats.respond_to?(:[]) $story_stats["OverMapEvent_DateCount"] rescue nil end def mp_server_time_restore_overmap_count(value) return if value.nil? return unless $story_stats && $story_stats.respond_to?(:[]=) $story_stats["OverMapEvent_DateCount"] = value rescue end end