2026-10-29 21:59:06 +04:00
|
|
|
# encoding: UTF-8
|
|
|
|
|
# LonaCraft online camp persistence fix 1.5.3 - metadata-safe camps.
|
|
|
|
|
# Uses the online client's EXISTING deistvia relay; does not modify the server.
|
|
|
|
|
# Only players running this same mod, in the same map instance, will sync.
|
|
|
|
|
# Inventory, chest contents, raid AI, and account/cloud data remain independent.
|
|
|
|
|
module LonaCraft
|
|
|
|
|
module OnlineSync
|
|
|
|
|
ACTION = 'lonacraft_world_v1'
|
|
|
|
|
MAX_PACKET = 1150
|
|
|
|
|
MAX_OBJECTS = 300
|
|
|
|
|
REQUEST_INTERVAL = 45.0
|
|
|
|
|
RESPONSE_INTERVAL = 6.0
|
|
|
|
|
SCAN_INTERVAL = 0.6
|
|
|
|
|
MAX_QUEUE = 1000
|
|
|
|
|
SEND_PER_TICK = 2
|
|
|
|
|
VALID_PIECES = ['W', 'B', 'F', 'D', 'FD', 'DS', 'FDS'].freeze
|
|
|
|
|
@map_token = nil
|
|
|
|
|
@observed = {}
|
|
|
|
|
@outbox = []
|
|
|
|
|
@last_scan = 0.0
|
|
|
|
|
@last_request = 0.0
|
|
|
|
|
@responses = {}
|
|
|
|
|
@online_last = false
|
|
|
|
|
@failure_last = 0.0
|
|
|
|
|
@newly_founded_token = nil
|
|
|
|
|
@last_discovery = 0.0
|
|
|
|
|
@last_camp_request = 0.0
|
|
|
|
|
@camp_sync_token = nil
|
|
|
|
|
@camp_sync_started = 0.0
|
|
|
|
|
@camp_sync_done = false
|
|
|
|
|
@last_camp_retry = 0.0
|
|
|
|
|
@saved_camps = {}
|
|
|
|
|
@pending_new_camps = {}
|
|
|
|
|
@pending_camp_meta = {}
|
|
|
|
|
|
|
|
|
|
# Temporary camp diagnostics are disabled in this release.
|
|
|
|
|
def self.log(message)
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.data
|
|
|
|
|
return nil if !$game_system || !$game_system.respond_to?(:lonacraft_data)
|
|
|
|
|
$game_system.lonacraft_data
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
STORY_ID_KEY = 'LonaCraftOnlineId'
|
|
|
|
|
STORY_ALIASES_KEY = 'LonaCraftOnlineIds'
|
|
|
|
|
|
|
|
|
|
def self.valid_player_id?(id)
|
|
|
|
|
id.is_a?(String) && id =~ /\A[a-z0-9]{12}\z/
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.story_get(key)
|
|
|
|
|
return nil unless defined?($story_stats) && $story_stats
|
|
|
|
|
$story_stats[key]
|
|
|
|
|
rescue
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.story_set(key, value)
|
|
|
|
|
return false unless defined?($story_stats) && $story_stats && $story_stats.respond_to?(:[]=)
|
|
|
|
|
$story_stats[key] = value
|
|
|
|
|
true
|
|
|
|
|
rescue
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Durable identity: lonacraft_data is wiped on cloud restore, story_stats is not.
|
|
|
|
|
def self.player_id
|
|
|
|
|
d = data
|
|
|
|
|
return 'unknown' unless d
|
|
|
|
|
|
|
|
|
|
story_id = story_get(STORY_ID_KEY).to_s
|
|
|
|
|
if valid_player_id?(story_id)
|
|
|
|
|
d['lc_online_identity'] = story_id
|
|
|
|
|
remember_identity(story_id)
|
|
|
|
|
return story_id
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
id = d['lc_online_identity']
|
|
|
|
|
unless valid_player_id?(id)
|
|
|
|
|
id = adopt_identity_from_owned_furniture
|
|
|
|
|
id = ('%08x%04x' % [Time.now.to_i & 0xffffffff, rand(65536)]) unless valid_player_id?(id)
|
|
|
|
|
end
|
|
|
|
|
d['lc_online_identity'] = id
|
|
|
|
|
story_set(STORY_ID_KEY, id)
|
|
|
|
|
remember_identity(id)
|
|
|
|
|
id
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.remember_identity(id)
|
|
|
|
|
return false unless valid_player_id?(id)
|
|
|
|
|
raw = story_get(STORY_ALIASES_KEY).to_s
|
|
|
|
|
list = raw.split(',').map { |s| s.strip }.select { |s| valid_player_id?(s) }
|
|
|
|
|
unless list.include?(id)
|
|
|
|
|
list << id
|
|
|
|
|
list = list.last(8)
|
|
|
|
|
story_set(STORY_ALIASES_KEY, list.join(','))
|
|
|
|
|
end
|
|
|
|
|
true
|
|
|
|
|
rescue
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.known_identity?(id)
|
|
|
|
|
return false unless valid_player_id?(id)
|
|
|
|
|
story_id = story_get(STORY_ID_KEY).to_s
|
|
|
|
|
return true if id == story_id
|
|
|
|
|
begin
|
|
|
|
|
cur = data && data['lc_online_identity']
|
|
|
|
|
return true if id == cur.to_s
|
|
|
|
|
rescue
|
|
|
|
|
end
|
|
|
|
|
raw = story_get(STORY_ALIASES_KEY).to_s
|
|
|
|
|
raw.split(',').map { |s| s.strip }.include?(id)
|
|
|
|
|
rescue
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# After a cloud wipe the random identity is new, but placed furniture still
|
|
|
|
|
# carries the old lc_net_id prefix. Reclaim that prefix when our durable
|
|
|
|
|
# chest snapshots still reference it.
|
|
|
|
|
def self.adopt_identity_from_owned_furniture
|
|
|
|
|
counts = Hash.new(0)
|
|
|
|
|
begin
|
|
|
|
|
store = nil
|
|
|
|
|
store = LonaCraft::ChestCloud.durable_store if defined?(LonaCraft::ChestCloud)
|
|
|
|
|
if store.is_a?(Hash)
|
|
|
|
|
store.each_key do |net_id|
|
|
|
|
|
prefix = net_id.to_s.split('.', 2)[0]
|
|
|
|
|
counts[prefix] += 1 if valid_player_id?(prefix)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
rescue
|
|
|
|
|
end
|
|
|
|
|
begin
|
|
|
|
|
if defined?(LonaCraft::ChestCloud) && LonaCraft::ChestCloud.respond_to?(:cache)
|
|
|
|
|
LonaCraft::ChestCloud.cache.each_key do |net_id|
|
|
|
|
|
prefix = net_id.to_s.split('.', 2)[0]
|
|
|
|
|
counts[prefix] += 1 if valid_player_id?(prefix)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
rescue
|
|
|
|
|
end
|
|
|
|
|
begin
|
|
|
|
|
records = LonaCraft::Furniture.records if defined?(LonaCraft::Furniture)
|
|
|
|
|
Array(records).each do |rec|
|
|
|
|
|
next unless rec.is_a?(Hash)
|
|
|
|
|
prefix = rec['lc_net_id'].to_s.split('.', 2)[0]
|
|
|
|
|
counts[prefix] += 1 if valid_player_id?(prefix)
|
|
|
|
|
end
|
|
|
|
|
rescue
|
|
|
|
|
end
|
|
|
|
|
return nil if counts.empty?
|
|
|
|
|
best = counts.max_by { |_, n| n }
|
|
|
|
|
best && valid_player_id?(best[0]) ? best[0] : nil
|
|
|
|
|
rescue
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.reclaim_identity!(id)
|
|
|
|
|
return false unless valid_player_id?(id)
|
|
|
|
|
d = data
|
|
|
|
|
d['lc_online_identity'] = id if d.is_a?(Hash)
|
|
|
|
|
story_set(STORY_ID_KEY, id)
|
|
|
|
|
remember_identity(id)
|
|
|
|
|
true
|
|
|
|
|
rescue
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.stamps
|
|
|
|
|
d = data
|
|
|
|
|
return {} unless d
|
|
|
|
|
d['lc_online_stamps'] = {} unless d['lc_online_stamps'].is_a?(Hash)
|
|
|
|
|
d['lc_online_stamps']
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.map_revisions(token)
|
|
|
|
|
store = stamps
|
|
|
|
|
store[token] = {} unless store[token].is_a?(Hash)
|
|
|
|
|
store[token]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.map_token
|
|
|
|
|
map = $game_map
|
|
|
|
|
return nil unless map && map.respond_to?(:map_id_rec) && map.respond_to?(:map_id)
|
|
|
|
|
# Include BOTH the map key and numeric ID. Never apply packets to a
|
|
|
|
|
# different randomly generated field map / private base instance.
|
|
|
|
|
key = map.map_id_rec.to_s
|
|
|
|
|
return nil if key.empty? || key.size > 120
|
|
|
|
|
"#{map.map_id.to_i}|#{key}"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.ready?
|
|
|
|
|
return false unless defined?(::WebSocket)
|
|
|
|
|
return false unless ::WebSocket.respond_to?(:connected?) && ::WebSocket.connected?
|
|
|
|
|
return false unless ::WebSocket.respond_to?(:worldsync?) && ::WebSocket.worldsync?
|
|
|
|
|
return false unless ::WebSocket.respond_to?(:send_deistvia)
|
|
|
|
|
return false if ::WebSocket.respond_to?(:gameplay_scene_active?) && !::WebSocket.gameplay_scene_active?
|
|
|
|
|
!data.nil? && !map_token.nil?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.now
|
|
|
|
|
Time.now.to_f
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# RPG Maker bundles game_guy JSON 1.2 (encode/decode, not generate/parse).
|
|
|
|
|
# Prefer the native encoder when present, but support the bundled one.
|
|
|
|
|
def self.wire(packet)
|
|
|
|
|
str = JSON.respond_to?(:encode) ? JSON.encode(packet) : JSON.generate(packet)
|
|
|
|
|
return nil unless str.is_a?(String) && str.bytesize <= MAX_PACKET
|
|
|
|
|
[str].pack('m0')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.queue(packet)
|
|
|
|
|
line = wire(packet)
|
|
|
|
|
return false if line.nil? || @outbox.size >= MAX_QUEUE
|
|
|
|
|
@outbox << line
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.send_now(packet)
|
|
|
|
|
line = wire(packet)
|
|
|
|
|
return false if line.nil? || !ready?
|
|
|
|
|
::WebSocket.send_deistvia(ACTION, 'msg', line)
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.decode(payload)
|
|
|
|
|
return nil if !payload.is_a?(String) || payload.bytesize > 1700 || payload !~ /\A[A-Za-z0-9+\/=]+\z/
|
|
|
|
|
raw = payload.unpack1('m')
|
|
|
|
|
return nil if raw.bytesize > MAX_PACKET
|
|
|
|
|
obj = JSON.respond_to?(:decode) ? JSON.decode(raw) : JSON.parse(raw)
|
|
|
|
|
obj.is_a?(Hash) && obj['v'] == 1 ? obj : nil
|
|
|
|
|
rescue StandardError
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.stamp_cmp(a, b)
|
|
|
|
|
a = a.to_s.split('@', 2)
|
|
|
|
|
b = b.to_s.split('@', 2)
|
|
|
|
|
n = a[0].to_i <=> b[0].to_i
|
|
|
|
|
return n unless n == 0
|
|
|
|
|
a[1].to_s <=> b[1].to_s
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.next_stamp
|
|
|
|
|
d = data
|
|
|
|
|
value = [d['lc_online_clock'].to_i + 1, 1].max
|
|
|
|
|
d['lc_online_clock'] = value
|
|
|
|
|
"#{value}@#{player_id}"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.touch_clock(stamp)
|
|
|
|
|
d = data
|
|
|
|
|
return if d.nil?
|
|
|
|
|
counter = stamp.to_s.split('@', 2)[0].to_i
|
|
|
|
|
d['lc_online_clock'] = counter if counter > d['lc_online_clock'].to_i
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.normal_stamp(revisions, id)
|
|
|
|
|
r = revisions[id]
|
|
|
|
|
r.is_a?(Hash) && r['stamp'].is_a?(String) ? r['stamp'] : '0@0'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.furniture_id(rec)
|
|
|
|
|
net_id = rec['lc_net_id']
|
|
|
|
|
unless net_id.is_a?(String) && net_id =~ /\A[a-z0-9]{12}\.[0-9]{1,12}\z/
|
|
|
|
|
net_id = "#{player_id}.#{rec['uid'].to_i}"
|
|
|
|
|
rec['lc_net_id'] = net_id
|
|
|
|
|
end
|
|
|
|
|
'f:' + net_id
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.state
|
|
|
|
|
map = $game_map
|
|
|
|
|
token = map_token
|
|
|
|
|
return {} if map.nil? || token.nil?
|
|
|
|
|
result = {}
|
|
|
|
|
furn = LonaCraft::Furniture
|
|
|
|
|
count = 0
|
|
|
|
|
furn.records.each do |rec|
|
|
|
|
|
next unless rec.is_a?(Hash) && rec['map_id'].to_i == map.map_id.to_i
|
|
|
|
|
next unless furn.kind_for(rec['item_name'])
|
|
|
|
|
x, y = rec['x'].to_i, rec['y'].to_i
|
|
|
|
|
next unless map.valid?(x, y)
|
|
|
|
|
id = furniture_id(rec)
|
|
|
|
|
result[id] = {'t'=>'f', 'x'=>x, 'y'=>y, 'name'=>rec['item_name'].to_s}
|
|
|
|
|
count += 1
|
|
|
|
|
break if count >= MAX_OBJECTS
|
|
|
|
|
end
|
|
|
|
|
base = LonaCraft::Bases.record_for_map(map)
|
|
|
|
|
if base && base['pieces'].is_a?(Hash)
|
|
|
|
|
base['pieces'].each do |key, value|
|
|
|
|
|
break if count >= MAX_OBJECTS
|
|
|
|
|
next unless key.to_s =~ /\A\d{1,4},\d{1,4}\z/
|
|
|
|
|
x, y = key.split(',').map(&:to_i)
|
|
|
|
|
next unless map.valid?(x, y) && VALID_PIECES.include?(value.to_s)
|
|
|
|
|
hp = base['hp'].is_a?(Hash) ? base['hp'][key] : nil
|
|
|
|
|
info = {'t'=>'b', 'x'=>x, 'y'=>y, 'code'=>value.to_s}
|
|
|
|
|
info['hp'] = hp.to_i if hp.is_a?(Integer) && hp >= 0
|
|
|
|
|
result['b:' + key] = info
|
|
|
|
|
count += 1
|
|
|
|
|
break if count >= MAX_OBJECTS
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
result
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.changed?(a, b)
|
|
|
|
|
a != b
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.publish_changes(current)
|
|
|
|
|
revisions = map_revisions(@map_token)
|
|
|
|
|
ids = (@observed.keys + current.keys).uniq
|
|
|
|
|
ids.each do |id|
|
|
|
|
|
old_value = @observed[id]
|
|
|
|
|
value = current[id]
|
|
|
|
|
next unless changed?(old_value, value)
|
|
|
|
|
stamp = next_stamp
|
|
|
|
|
revisions[id] = {'stamp'=>stamp, 'deleted'=>value.nil?}
|
|
|
|
|
queue({'v'=>1, 'm'=>@map_token, 'id'=>id, 's'=>stamp, 'o'=>value.nil? ? 'del' : 'put', 'd'=>value})
|
|
|
|
|
end
|
|
|
|
|
@observed = current
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.snapshot
|
|
|
|
|
return unless ready?
|
|
|
|
|
token = map_token
|
|
|
|
|
current = state
|
|
|
|
|
revisions = map_revisions(token)
|
|
|
|
|
current.each do |id, value|
|
|
|
|
|
queue({'v'=>1, 'm'=>token, 'id'=>id, 's'=>(revisions.key?(id) ? normal_stamp(revisions, id) : ('0@' + player_id)), 'o'=>'put', 'd'=>value})
|
|
|
|
|
end
|
|
|
|
|
if $game_map.respond_to?(:isOverMap) && $game_map.isOverMap
|
|
|
|
|
LonaCraft::Bases.records_on_overmap($game_map.map_id).first(80).each do |base|
|
|
|
|
|
packet = foundation_packet(base, token)
|
|
|
|
|
queue(packet) if packet
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
revisions.each do |id, rev|
|
|
|
|
|
next unless rev.is_a?(Hash) && rev['deleted'] && !current.key?(id)
|
|
|
|
|
queue({'v'=>1, 'm'=>token, 'id'=>id, 's'=>rev['stamp'].to_s, 'o'=>'del'})
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Base creation must be announced while the sender still occupies the
|
|
|
|
|
# original field map on the server. The receiver registers the same map ID,
|
|
|
|
|
# allowing both clients to move to the same online map instance. Other
|
|
|
|
|
# existing bases with clashing IDs are never overwritten.
|
|
|
|
|
# Owner names stay compact for the automatic "Лагерь <ник>" label. A
|
|
|
|
|
# manually renamed camp is validated separately by Bases.clean_name and
|
|
|
|
|
# may contain up to 48 characters.
|
|
|
|
|
def self.owner_name(raw)
|
|
|
|
|
raw.to_s.gsub(/[\r\n\t,\[\]:\\<>]/, '').strip[0, 24]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.camp_name(owner)
|
|
|
|
|
"Лагерь #{owner_name(owner)}"[0, 32]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.owns_camp?(rec)
|
|
|
|
|
return false unless rec.is_a?(Hash)
|
|
|
|
|
owner = owner_name(rec['owner'])
|
|
|
|
|
local = owner_name((::WebSocket.local_nickname rescue nil))
|
|
|
|
|
!owner.empty? && !local.empty? && owner.casecmp(local) == 0
|
|
|
|
|
rescue StandardError
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Wilderness maps can have numeric keys (e.g. 26), not only names.
|
|
|
|
|
# Preserve the original map-key type so Bases.register locates MapInfo.
|
|
|
|
|
def self.normal_field_key(raw)
|
|
|
|
|
if raw.is_a?(Integer)
|
|
|
|
|
return raw if raw > 0 && raw < 2_000_000_000
|
|
|
|
|
elsif raw.is_a?(String) && raw =~ /\A[a-zA-Z0-9_]{1,90}\z/
|
|
|
|
|
if raw =~ /\A[0-9]+\z/ && $data_mapinfos && $data_mapinfos[raw.to_i]
|
|
|
|
|
return raw.to_i
|
|
|
|
|
end
|
|
|
|
|
return raw
|
|
|
|
|
end
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.foundation_packet(rec, token)
|
|
|
|
|
return nil unless rec.is_a?(Hash) && rec['om'].is_a?(Array) && rec['om'].size == 3
|
|
|
|
|
field = normal_field_key(rec['field'])
|
|
|
|
|
return nil if field.nil?
|
|
|
|
|
# Migrate the owner's pre-fix camp name on the next snapshot. A camp
|
|
|
|
|
# imported from the server is tagged lc_shared and must keep its owner.
|
|
|
|
|
unless rec['lc_shared']
|
|
|
|
|
nick = (::WebSocket.local_nickname.to_s rescue '').strip
|
|
|
|
|
if !nick.empty?
|
|
|
|
|
nick = owner_name(nick)
|
|
|
|
|
rec['owner'] = nick
|
|
|
|
|
rec['name'] = camp_name(nick) if LonaCraft::Bases.clean_name(rec['name']).empty?
|
|
|
|
|
rec['lc_shared'] = true
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
id = rec['id'].to_i
|
|
|
|
|
return nil if id < 1 || id > 90000
|
|
|
|
|
{'v'=>1, 'm'=>token, 'o'=>'base', 'd'=>{
|
|
|
|
|
'id'=>id, 'om'=>rec['om'].map(&:to_i), 'key'=>rec['key'].to_s,
|
|
|
|
|
'field'=>field, 'region'=>rec['region'].to_i, 'no'=>rec['map_no'].to_i,
|
|
|
|
|
'owner'=>rec['owner'].to_s, 'name'=>LonaCraft::Bases.name_of(rec).to_s,
|
|
|
|
|
'danger'=>LonaCraft::Bases.danger_of(rec)
|
|
|
|
|
}}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Discovery uses OVERMAP coordinates, so camps persist across field-map
|
|
|
|
|
# randomization and two players who entered at different moments converge.
|
|
|
|
|
def self.discover_camp
|
|
|
|
|
return false unless ready?
|
|
|
|
|
map = $game_map
|
|
|
|
|
return false unless map && !map.isOverMap
|
|
|
|
|
e = LonaCraft::Bases.entry
|
|
|
|
|
return false unless e.is_a?(Hash) && e['om'].is_a?(Array) && e['om'].size == 3
|
|
|
|
|
return false if LonaCraft::Bases.record_for_tile(*e['om'])
|
|
|
|
|
send_now({'v'=>1, 'm'=>map_token, 'o'=>'discover', 'om'=>e['om'].map(&:to_i)})
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# The map can already be loaded when server replay arrives. Always ask
|
|
|
|
|
# MySQL-backed server for the complete camp list for this OverMap.
|
|
|
|
|
# Request again after login/map transition to survive bootstrap packet loss.
|
|
|
|
|
def self.request_overmap_camps
|
|
|
|
|
return false unless ready? && $game_map && $game_map.isOverMap
|
|
|
|
|
time = now
|
|
|
|
|
token = map_token
|
|
|
|
|
if @camp_sync_token != token
|
|
|
|
|
@camp_sync_token = token
|
|
|
|
|
@camp_sync_started = time
|
|
|
|
|
@camp_sync_done = false
|
|
|
|
|
@last_camp_request = 0.0
|
|
|
|
|
end
|
|
|
|
|
# At first login the map packet and camps_req can cross during server
|
|
|
|
|
# bootstrap. Retry quickly until the server confirms the complete list;
|
|
|
|
|
# after that retain the slow poll for camps created by other players.
|
|
|
|
|
interval = if @camp_sync_done
|
|
|
|
|
8.0
|
|
|
|
|
elsif time - @camp_sync_started < 6.0
|
|
|
|
|
0.75
|
|
|
|
|
elsif time - @camp_sync_started < 20.0
|
|
|
|
|
2.0
|
|
|
|
|
else
|
|
|
|
|
8.0
|
|
|
|
|
end
|
|
|
|
|
return false if time - @last_camp_request < interval
|
|
|
|
|
@last_camp_request = time
|
|
|
|
|
send_now({'v'=>1, 'm'=>map_token, 'o'=>'camps_req', 'om_id'=>$game_map.map_id.to_i})
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.receive_camps_done(packet, sender)
|
|
|
|
|
return false unless sender.to_i == 0
|
|
|
|
|
info = packet['d']
|
|
|
|
|
return false unless info.is_a?(Hash) && info['om_id'].is_a?(Integer)
|
|
|
|
|
return false unless $game_map && $game_map.respond_to?(:isOverMap) && $game_map.isOverMap
|
|
|
|
|
return false unless info['om_id'] == $game_map.map_id.to_i
|
|
|
|
|
@camp_sync_token = map_token
|
|
|
|
|
@camp_sync_done = true
|
|
|
|
|
LonaCraft::Bases.records_on_overmap($game_map.map_id).each{|rec| refresh_marker(rec) }
|
|
|
|
|
update_tab_camp
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Send existing LOCAL foundations until the server confirms they were
|
|
|
|
|
# committed to MySQL. A local OverMap marker is not an acknowledgement.
|
|
|
|
|
# Retry also covers bootstrap timing, map transitions and packet loss.
|
|
|
|
|
def self.retry_unsaved_camps
|
|
|
|
|
return false unless ready?
|
|
|
|
|
time = now
|
|
|
|
|
return false if time - @last_camp_retry < 6.0
|
|
|
|
|
@last_camp_retry = time
|
|
|
|
|
map = $game_map
|
|
|
|
|
return false unless map
|
|
|
|
|
records = if map.respond_to?(:isOverMap) && map.isOverMap
|
|
|
|
|
LonaCraft::Bases.records_on_overmap(map.map_id)
|
|
|
|
|
else
|
|
|
|
|
e = LonaCraft::Bases.entry
|
|
|
|
|
e.is_a?(Hash) && e['om'].is_a?(Array) ? [LonaCraft::Bases.record_for_tile(*e['om'])].compact : []
|
|
|
|
|
end
|
|
|
|
|
# Keep retrying a creation/rename/danger update even after the owner has
|
|
|
|
|
# left that map. The server authenticates ownership before accepting it.
|
|
|
|
|
pending = @pending_new_camps.keys + @pending_camp_meta.keys
|
|
|
|
|
records += LonaCraft::Bases.list.select do |rec|
|
|
|
|
|
rec.is_a?(Hash) && rec['om'].is_a?(Array) && pending.include?(camp_ack_key(rec['om'], rec['id']))
|
|
|
|
|
end
|
|
|
|
|
records = records.uniq
|
|
|
|
|
nick = owner_name((::WebSocket.local_nickname rescue nil))
|
|
|
|
|
return false if nick.empty?
|
|
|
|
|
records.first(100).each do |rec|
|
|
|
|
|
next unless rec.is_a?(Hash) && rec['om'].is_a?(Array)
|
|
|
|
|
# Old locally-created bases may have been founded before the socket
|
|
|
|
|
# became ready. They still need their first MySQL synchronization.
|
|
|
|
|
unless rec['lc_shared']
|
|
|
|
|
rec['owner'] = nick
|
|
|
|
|
rec['name'] = camp_name(nick) if LonaCraft::Bases.clean_name(rec['name']).empty?
|
|
|
|
|
rec['lc_shared'] = true
|
|
|
|
|
end
|
|
|
|
|
next unless owner_name(rec['owner']).casecmp(nick) == 0
|
|
|
|
|
key = camp_ack_key(rec['om'], rec['id'])
|
|
|
|
|
next if @saved_camps[key]
|
|
|
|
|
packet = foundation_packet(rec, map_token)
|
|
|
|
|
if packet
|
|
|
|
|
if @pending_new_camps[key]
|
|
|
|
|
packet['o'] = 'base_new'
|
|
|
|
|
elsif @pending_camp_meta[key]
|
|
|
|
|
packet['o'] = 'base_meta'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
if packet && send_now(packet)
|
|
|
|
|
log "camp awaiting SQL confirmation #{key}"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.camp_ack_key(om, id)
|
|
|
|
|
om.map(&:to_i).join(':') + ':' + id.to_i.to_s
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.receive_camp_ack(packet, sender)
|
|
|
|
|
return false unless sender.to_i == 0
|
|
|
|
|
d = packet['d']
|
|
|
|
|
return false unless d.is_a?(Hash) && d['om'].is_a?(Array) && d['om'].size == 3
|
|
|
|
|
return false unless d['om'].all? { |n| n.is_a?(Integer) && n >= 0 && n < 5000 }
|
|
|
|
|
return false unless d['id'].is_a?(Integer) && d['id'] > 0 && d['id'] < 90000
|
|
|
|
|
key = camp_ack_key(d['om'], d['id'])
|
|
|
|
|
desired = @pending_camp_meta[key]
|
|
|
|
|
if desired
|
|
|
|
|
ack_name = LonaCraft::Bases.clean_name(d['name'])
|
|
|
|
|
ack_danger = d['danger'].is_a?(Integer) ? d['danger'] : nil
|
|
|
|
|
return true unless ack_name == desired['name'] && ack_danger == desired['danger']
|
|
|
|
|
@pending_camp_meta.delete(key)
|
|
|
|
|
end
|
|
|
|
|
@saved_camps[key] = true
|
|
|
|
|
@pending_new_camps.delete(key)
|
|
|
|
|
log "camp stored in MySQL #{key}"
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.update_tab_camp
|
|
|
|
|
if defined?(MPTabLocationStatus) && MPTabLocationStatus.respond_to?(:send_current)
|
|
|
|
|
MPTabLocationStatus.send_current(true)
|
|
|
|
|
end
|
|
|
|
|
rescue StandardError => e
|
|
|
|
|
log "TAB location error: #{e.class}: #{e.message}"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.newly_founded!(token)
|
|
|
|
|
@newly_founded_token = token
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.announce_foundation(rec, previous_token)
|
|
|
|
|
if rec.is_a?(Hash) && rec['om'].is_a?(Array)
|
|
|
|
|
key = camp_ack_key(rec['om'], rec['id'])
|
|
|
|
|
@saved_camps.delete(key)
|
|
|
|
|
@pending_new_camps[key] = true
|
|
|
|
|
end
|
|
|
|
|
return unless ready?
|
|
|
|
|
packet = foundation_packet(rec, previous_token)
|
|
|
|
|
if packet
|
|
|
|
|
# Only a foundation built NOW can reactivate a previously deleted camp.
|
|
|
|
|
# Ordinary snapshots use 'base' and never resurrect tombstones.
|
|
|
|
|
packet['o'] = 'base_new'
|
|
|
|
|
send_now(packet)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Reliable owner-only update for the two mutable camp properties. It uses
|
|
|
|
|
# the existing foundation identity, but the server copies only name/danger
|
|
|
|
|
# into its canonical row and ACKs after MySQL commit.
|
|
|
|
|
def self.announce_metadata(rec)
|
|
|
|
|
return false unless rec.is_a?(Hash) && rec['om'].is_a?(Array)
|
|
|
|
|
return false unless owns_camp?(rec)
|
|
|
|
|
key = camp_ack_key(rec['om'], rec['id'])
|
|
|
|
|
@saved_camps.delete(key)
|
|
|
|
|
@pending_camp_meta[key] = {
|
|
|
|
|
'name'=>LonaCraft::Bases.clean_name(LonaCraft::Bases.name_of(rec)),
|
|
|
|
|
'danger'=>LonaCraft::Bases.danger_of(rec)
|
|
|
|
|
}
|
|
|
|
|
return false unless ready?
|
|
|
|
|
packet = foundation_packet(rec, map_token)
|
|
|
|
|
return false unless packet
|
|
|
|
|
packet['o'] = 'base_meta'
|
|
|
|
|
send_now(packet)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.announce_removed(rec)
|
|
|
|
|
return unless ready? && rec.is_a?(Hash) && rec['om'].is_a?(Array)
|
|
|
|
|
packet = {'v'=>1, 'm'=>map_token, 'o'=>'base_del',
|
|
|
|
|
'd'=>{'om'=>rec['om'].map(&:to_i), 'id'=>rec['id'].to_i,
|
|
|
|
|
'no'=>rec['map_no'].to_i}}
|
|
|
|
|
@saved_camps.delete(camp_ack_key(rec['om'], rec['id']))
|
|
|
|
|
@pending_new_camps.delete(camp_ack_key(rec['om'], rec['id']))
|
|
|
|
|
@pending_camp_meta.delete(camp_ack_key(rec['om'], rec['id']))
|
|
|
|
|
send_now(packet)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.receive_removal(packet, sender)
|
|
|
|
|
# Never accept camp deletion straight from a peer; only the server has
|
|
|
|
|
# authenticated the camp owner's profile before broadcasting this.
|
|
|
|
|
return false unless sender.to_i == 0
|
|
|
|
|
info = packet['d']
|
|
|
|
|
return false unless info.is_a?(Hash) && info['om'].is_a?(Array) && info['om'].size == 3
|
|
|
|
|
coords = info['om']
|
|
|
|
|
return false unless coords.all?{|v| v.is_a?(Integer) && v >= 0 && v < 5000}
|
|
|
|
|
bases = LonaCraft::Bases
|
|
|
|
|
rec = bases.record_for_tile(*coords)
|
|
|
|
|
return true unless rec
|
|
|
|
|
return false unless rec['id'].to_i == info['id'].to_i
|
|
|
|
|
# Do not re-transmit a removal; the server already broadcast it.
|
|
|
|
|
bases.list.delete(rec)
|
|
|
|
|
key = camp_ack_key(coords, info['id'])
|
|
|
|
|
@saved_camps.delete(key)
|
|
|
|
|
@pending_new_camps.delete(key)
|
|
|
|
|
@pending_camp_meta.delete(key)
|
|
|
|
|
begin
|
|
|
|
|
bases.remove_live_markers($game_map, [rec]) if $game_map
|
|
|
|
|
rescue StandardError => e
|
|
|
|
|
log "remove marker error: #{e.class}: #{e.message}"
|
|
|
|
|
end
|
|
|
|
|
update_tab_camp
|
|
|
|
|
log "camp permanently removed: #{coords.inspect}"
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.receive_foundation(packet)
|
|
|
|
|
info = packet['d']
|
|
|
|
|
return false unless info.is_a?(Hash)
|
|
|
|
|
id = info['id']
|
|
|
|
|
om = info['om']
|
|
|
|
|
key = info['key']
|
|
|
|
|
no = info['no']
|
|
|
|
|
field = normal_field_key(info['field'])
|
|
|
|
|
region = info['region']
|
|
|
|
|
return false unless id.is_a?(Integer) && id > 0 && id < 90000
|
|
|
|
|
return false unless om.is_a?(Array) && om.size == 3 && om.all?{|v| v.is_a?(Integer) && v >= 0 && v < 5000}
|
|
|
|
|
return false unless key == "LonaCraftBase_#{id}" && no == LonaCraft::Bases::MAP_ID_BASE + id
|
|
|
|
|
return false unless region.is_a?(Integer) && region >= 0 && region <= 200
|
|
|
|
|
return false if field.nil?
|
|
|
|
|
owner = owner_name(info['owner'])
|
|
|
|
|
label = LonaCraft::Bases.clean_name(info['name'])
|
|
|
|
|
label = camp_name(owner) if label.empty? && !owner.empty?
|
2026-11-02 00:15:52 +04:00
|
|
|
access = Array(info['access']).select do |entry|
|
|
|
|
|
entry.is_a?(Hash) && entry['profile'].is_a?(Integer) && entry['profile'] > 0 &&
|
|
|
|
|
entry['name'].is_a?(String) && !owner_name(entry['name']).empty?
|
|
|
|
|
end.first(12).map do |entry|
|
|
|
|
|
{'profile'=>entry['profile'], 'name'=>owner_name(entry['name'])}
|
|
|
|
|
end
|
2026-10-29 21:59:06 +04:00
|
|
|
danger = info['danger'].is_a?(Integer) && info['danger'] >= -1 && info['danger'] <= 1 ? info['danger'] : 0
|
|
|
|
|
bases = LonaCraft::Bases
|
|
|
|
|
root = bases.root
|
|
|
|
|
return false unless root && $data_mapinfos && $data_mapinfos[field]
|
|
|
|
|
# Either we are on the same original field tile, or on the overmap.
|
|
|
|
|
world = $game_map.respond_to?(:isOverMap) && $game_map.isOverMap
|
|
|
|
|
unless world
|
|
|
|
|
ent = bases.entry
|
|
|
|
|
return false unless ent.is_a?(Hash) && ent['om'].is_a?(Array)
|
|
|
|
|
return false unless ent['om'].map(&:to_i) == om && ent['to'].to_s == field
|
|
|
|
|
end
|
|
|
|
|
if world
|
|
|
|
|
return false unless $game_map.map_id.to_i == om[0]
|
|
|
|
|
end
|
|
|
|
|
existing = bases.record_for_tile(*om)
|
|
|
|
|
if existing
|
|
|
|
|
# An old local/offline camp may already occupy the same tile. Never
|
|
|
|
|
# replace its structures; fill in its shared display metadata.
|
|
|
|
|
if existing['key'].to_s == key && existing['map_no'].to_i == no
|
|
|
|
|
existing['owner'] = owner unless owner.empty?
|
2026-11-02 00:15:52 +04:00
|
|
|
existing['lc_access'] = access
|
2026-10-29 21:59:06 +04:00
|
|
|
pending_meta = @pending_camp_meta[camp_ack_key(om, id)]
|
|
|
|
|
unless pending_meta
|
|
|
|
|
existing['name'] = label unless label.empty?
|
|
|
|
|
existing['danger'] = danger
|
|
|
|
|
end
|
|
|
|
|
existing['lc_shared'] = true
|
|
|
|
|
refresh_marker(existing) if world
|
|
|
|
|
update_tab_camp
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
if bases.record_by_id(id) || root['maps'].key?(key)
|
|
|
|
|
log "shared camp ID collision #{id} at #{om.inspect}; keeping local record"
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
registered = bases.register(key, no, field)
|
|
|
|
|
return false if registered.to_i != no
|
|
|
|
|
rec = {'id'=>id, 'om'=>om, 'region'=>region, 'field'=>field,
|
|
|
|
|
'key'=>key, 'map_no'=>no, 'pieces'=>{}, 'made'=>bases.date_now,
|
2026-11-02 00:15:52 +04:00
|
|
|
'owner'=>owner, 'name'=>label, 'danger'=>danger, 'lc_access'=>access, 'lc_shared'=>true}
|
2026-10-29 21:59:06 +04:00
|
|
|
bases.list << rec
|
|
|
|
|
root['next_id'] = [root['next_id'].to_i, id + 1].max
|
|
|
|
|
refresh_marker(rec) if world
|
|
|
|
|
log "camp discovered: #{label} on #{om.inspect}, overmap=#{world}"
|
|
|
|
|
unless world
|
|
|
|
|
ent = bases.entry
|
|
|
|
|
ent['base'] = id
|
|
|
|
|
bases.rekey($game_map, rec)
|
|
|
|
|
end
|
|
|
|
|
log "registered shared base #{id} on #{om.inspect}"
|
|
|
|
|
update_tab_camp
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# A foundation can arrive AFTER Game_Map#setup_event_hack. Make its icon
|
|
|
|
|
# visible immediately, instead of only after exiting/re-entering the map.
|
|
|
|
|
def self.refresh_marker(rec)
|
|
|
|
|
map = $game_map
|
|
|
|
|
return unless map && map.respond_to?(:isOverMap) && map.isOverMap
|
|
|
|
|
return unless rec['om'].is_a?(Array) && rec['om'][0].to_i == map.map_id.to_i
|
|
|
|
|
map.lonacraft_inject_base_markers
|
|
|
|
|
raw_map = map.instance_variable_get(:@map)
|
|
|
|
|
return unless raw_map && map.events.is_a?(Hash)
|
|
|
|
|
raw_map.events.each do |event_id, raw_event|
|
|
|
|
|
next unless raw_event && raw_event.name.to_s == LonaCraft::Bases.marker_name(rec)
|
|
|
|
|
next if map.events.key?(event_id)
|
|
|
|
|
map.events[event_id] = Game_Event.new(map.map_id, raw_event)
|
|
|
|
|
if map.respond_to?(:added_ev_ids) && map.added_ev_ids
|
|
|
|
|
map.added_ev_ids << event_id unless map.added_ev_ids.include?(event_id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
rescue StandardError => e
|
|
|
|
|
log "marker refresh error: #{e.class}: #{e.message}"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.accepts_foundation?(packet)
|
|
|
|
|
info = packet['d']
|
|
|
|
|
return false unless info.is_a?(Hash) && info['om'].is_a?(Array) && info['om'].size == 3
|
|
|
|
|
return true if packet['m'] == map_token
|
|
|
|
|
return false unless $game_map
|
|
|
|
|
if $game_map.respond_to?(:isOverMap) && $game_map.isOverMap
|
|
|
|
|
return $game_map.map_id.to_i == info['om'][0].to_i
|
|
|
|
|
end
|
|
|
|
|
# A reply to the geographic discovery request carries the founder's
|
|
|
|
|
# original map token, which may no longer equal the visitor's token.
|
|
|
|
|
e = LonaCraft::Bases.entry
|
|
|
|
|
e.is_a?(Hash) && e['om'].is_a?(Array) &&
|
|
|
|
|
e['om'].map(&:to_i) == info['om'].map(&:to_i) &&
|
|
|
|
|
e['to'].to_s == info['field'].to_s
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.tick
|
|
|
|
|
online = ready?
|
|
|
|
|
unless online
|
|
|
|
|
@online_last = false
|
|
|
|
|
@map_token = nil
|
|
|
|
|
@outbox.clear
|
|
|
|
|
@observed.clear
|
|
|
|
|
@newly_founded_token = nil
|
|
|
|
|
@saved_camps.clear
|
|
|
|
|
@last_camp_retry = 0.0
|
|
|
|
|
@camp_sync_token = nil
|
|
|
|
|
@camp_sync_started = 0.0
|
|
|
|
|
@camp_sync_done = false
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
token = map_token
|
|
|
|
|
time = now
|
|
|
|
|
if !@online_last || token != @map_token
|
|
|
|
|
@online_last = true
|
|
|
|
|
@map_token = token
|
|
|
|
|
founding = (@newly_founded_token == token)
|
|
|
|
|
@newly_founded_token = nil
|
|
|
|
|
@observed = founding ? {} : state
|
|
|
|
|
@outbox.clear
|
|
|
|
|
@last_scan = time
|
|
|
|
|
@last_request = time
|
|
|
|
|
send_now({'v'=>1, 'm'=>token, 'o'=>'req'})
|
|
|
|
|
@last_discovery = time
|
|
|
|
|
discover_camp
|
|
|
|
|
@camp_sync_token = nil
|
|
|
|
|
@last_camp_request = 0.0
|
|
|
|
|
request_overmap_camps
|
|
|
|
|
update_tab_camp
|
|
|
|
|
if founding
|
|
|
|
|
# The very FIRST floor is already laid by the time the map changes.
|
|
|
|
|
# Publish it with a nonzero revision, so a stale snapshot from the
|
|
|
|
|
# other player cannot erase it when both clients enter the camp.
|
|
|
|
|
publish_changes(state)
|
|
|
|
|
else
|
|
|
|
|
snapshot
|
|
|
|
|
end
|
|
|
|
|
log "entered #{token}, requested object state"
|
|
|
|
|
elsif time - @last_scan >= SCAN_INTERVAL
|
|
|
|
|
@last_scan = time
|
|
|
|
|
publish_changes(state)
|
|
|
|
|
end
|
|
|
|
|
if time - @last_discovery >= 7.0
|
|
|
|
|
@last_discovery = time
|
|
|
|
|
discover_camp
|
|
|
|
|
end
|
|
|
|
|
request_overmap_camps
|
|
|
|
|
retry_unsaved_camps
|
|
|
|
|
request_delay = ($game_map.respond_to?(:isOverMap) && $game_map.isOverMap) ? 7.0 : REQUEST_INTERVAL
|
|
|
|
|
if time - @last_request > request_delay
|
|
|
|
|
@last_request = time
|
|
|
|
|
send_now({'v'=>1, 'm'=>token, 'o'=>'req'})
|
|
|
|
|
end
|
|
|
|
|
SEND_PER_TICK.times do
|
|
|
|
|
line = @outbox.shift
|
|
|
|
|
break if line.nil?
|
|
|
|
|
::WebSocket.send_deistvia(ACTION, 'msg', line)
|
|
|
|
|
end
|
|
|
|
|
rescue StandardError => e
|
|
|
|
|
time = now
|
|
|
|
|
if time - @failure_last > 10.0
|
|
|
|
|
log "tick error: #{e.class}: #{e.message}"
|
|
|
|
|
@failure_last = time
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.receive(sender, phase, payload)
|
|
|
|
|
return false unless phase == 'msg' && ready?
|
|
|
|
|
return false if sender.to_i == ::WebSocket.my_id.to_i
|
|
|
|
|
packet = decode(payload)
|
|
|
|
|
return false unless packet
|
|
|
|
|
if packet['o'] == 'base_ack'
|
|
|
|
|
return receive_camp_ack(packet, sender)
|
|
|
|
|
end
|
|
|
|
|
if packet['o'] == 'camps_done'
|
|
|
|
|
return receive_camps_done(packet, sender)
|
|
|
|
|
end
|
|
|
|
|
if packet['o'] == 'base' || packet['o'] == 'base_new'
|
|
|
|
|
return false unless accepts_foundation?(packet)
|
|
|
|
|
return receive_foundation(packet)
|
|
|
|
|
end
|
|
|
|
|
if packet['o'] == 'base_del'
|
|
|
|
|
return receive_removal(packet, sender)
|
|
|
|
|
end
|
|
|
|
|
return false unless packet['m'] == map_token
|
|
|
|
|
if packet['o'] == 'req'
|
|
|
|
|
last = @responses[sender.to_i].to_f
|
|
|
|
|
if now - last >= RESPONSE_INTERVAL
|
|
|
|
|
@responses[sender.to_i] = now
|
|
|
|
|
snapshot
|
|
|
|
|
end
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
id = packet['id'].to_s
|
|
|
|
|
stamp = packet['s'].to_s
|
|
|
|
|
return false unless id =~ /\A(?:f:[a-z0-9]{12}\.[0-9]{1,12}|b:\d{1,4},\d{1,4})\z/
|
|
|
|
|
return false unless stamp =~ /\A\d{1,16}@[a-z0-9]{1,16}\z/
|
|
|
|
|
op = packet['o']
|
|
|
|
|
return false unless op == 'put' || op == 'del'
|
|
|
|
|
revisions = map_revisions(packet['m'])
|
|
|
|
|
return true if stamp_cmp(stamp, normal_stamp(revisions, id)) <= 0
|
|
|
|
|
value = packet['d']
|
|
|
|
|
return false if op == 'put' && !valid_payload?(id, value)
|
|
|
|
|
touch_clock(stamp)
|
|
|
|
|
if apply(id, op, value)
|
|
|
|
|
revisions[id] = {'stamp'=>stamp, 'deleted'=>op == 'del'}
|
|
|
|
|
@observed = state
|
|
|
|
|
true
|
|
|
|
|
else
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
rescue StandardError => e
|
|
|
|
|
log "incoming packet rejected: #{e.class}: #{e.message}"
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.valid_payload?(id, value)
|
|
|
|
|
return false unless value.is_a?(Hash) && value['x'].is_a?(Integer) && value['y'].is_a?(Integer)
|
|
|
|
|
x, y = value['x'], value['y']
|
|
|
|
|
return false unless x >= 0 && y >= 0 && x < 1000 && y < 1000 && $game_map.valid?(x,y)
|
|
|
|
|
if id.start_with?('f:')
|
|
|
|
|
return false unless value['t'] == 'f' && value['name'].is_a?(String) && value['name'].size < 110
|
|
|
|
|
!LonaCraft::Furniture.kind_for(value['name']).nil?
|
|
|
|
|
else
|
|
|
|
|
return false unless value['t'] == 'b' && id == "b:#{x},#{y}" && VALID_PIECES.include?(value['code'])
|
|
|
|
|
value['hp'].nil? || (value['hp'].is_a?(Integer) && value['hp'] >= 0 && value['hp'] < 100000)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.apply(id, op, value)
|
|
|
|
|
return apply_furniture(id, op, value) if id.start_with?('f:')
|
|
|
|
|
apply_piece(id, op, value)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.apply_furniture(id, op, value)
|
|
|
|
|
furn = LonaCraft::Furniture
|
|
|
|
|
map = $game_map
|
|
|
|
|
rec = furn.records.find{|r| r.is_a?(Hash) && r['lc_net_id'] == id[2..-1]}
|
|
|
|
|
if op == 'del'
|
|
|
|
|
if rec && rec['map_id'].to_i == map.map_id.to_i
|
|
|
|
|
furn.remove(map, rec['uid'])
|
|
|
|
|
if defined?(LonaCraft::Bases) && LonaCraft::Bases.record_for_map(map)
|
|
|
|
|
LonaCraft::Bases.after_furniture_removed(map)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
kind = furn.kind_for(value['name'])
|
|
|
|
|
return false unless kind
|
|
|
|
|
return true if rec && rec['x'].to_i == value['x'] && rec['y'].to_i == value['y'] && rec['item_name'] == value['name']
|
|
|
|
|
# Never create a base or transfer a player's inventory on behalf of a peer.
|
|
|
|
|
# Only sync furniture on an already allowed map instance.
|
|
|
|
|
return false unless rec || furn.map_allowed?(map)
|
|
|
|
|
keep_box = 0
|
|
|
|
|
begin
|
|
|
|
|
if kind[:storage] && defined?(LonaCraft::ChestCloud)
|
|
|
|
|
LonaCraft::ChestCloud.snapshot_record(rec) if rec
|
|
|
|
|
end
|
|
|
|
|
keep_box = rec['box'].to_i if rec && kind[:storage]
|
|
|
|
|
rescue
|
|
|
|
|
keep_box = 0
|
|
|
|
|
end
|
|
|
|
|
if rec
|
|
|
|
|
furn.remove(map, rec['uid'])
|
|
|
|
|
end
|
|
|
|
|
fresh = {'uid'=>furn.next_uid, 'map_id'=>map.map_id, 'x'=>value['x'], 'y'=>value['y'],
|
|
|
|
|
'item_name'=>value['name'], 'lc_net_id'=>id[2..-1]}
|
|
|
|
|
if kind[:storage]
|
|
|
|
|
# Reuse the previous box so a sync echo cannot orphan filled storage.
|
|
|
|
|
fresh['box'] = (keep_box >= LonaCraft::Furniture::BOX_BASE) ? keep_box : furn.next_box_id
|
|
|
|
|
end
|
|
|
|
|
furn.records << fresh
|
|
|
|
|
furn.spawn(map, fresh, kind)
|
|
|
|
|
begin
|
|
|
|
|
if kind[:storage] && defined?(LonaCraft::ChestCloud)
|
|
|
|
|
LonaCraft::ChestCloud.apply_to_record(fresh)
|
|
|
|
|
end
|
|
|
|
|
rescue
|
|
|
|
|
end
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.apply_piece(id, op, value)
|
|
|
|
|
map = $game_map
|
|
|
|
|
bases = LonaCraft::Bases
|
|
|
|
|
x, y = id[2..-1].split(',').map(&:to_i)
|
|
|
|
|
return false unless map.valid?(x, y)
|
|
|
|
|
rec = bases.record_for_map(map)
|
|
|
|
|
return true if op == 'del' && rec.nil?
|
|
|
|
|
# Do not create/re-key an unsynchronised field map when a remote player
|
|
|
|
|
# builds. This avoids splitting the existing online map instance.
|
|
|
|
|
return false if rec.nil?
|
|
|
|
|
pieces = bases.pieces_of(rec)
|
|
|
|
|
key = bases.piece_key(x, y)
|
|
|
|
|
old = pieces[key]
|
|
|
|
|
new_value = op == 'del' ? nil : value['code']
|
|
|
|
|
if old && bases.door?(old) && (!new_value || !bases.door?(new_value))
|
|
|
|
|
bases.remove_door_event(map, x, y)
|
|
|
|
|
end
|
|
|
|
|
if new_value
|
|
|
|
|
pieces[key] = new_value
|
|
|
|
|
else
|
|
|
|
|
pieces.delete(key)
|
|
|
|
|
end
|
|
|
|
|
bases.clear_hp(rec, x, y)
|
|
|
|
|
bases.set_hp(rec, x, y, value['hp']) if new_value && value['hp']
|
|
|
|
|
bases.paint(map, pieces, bases.around(x, y))
|
|
|
|
|
bases.repaint(map)
|
|
|
|
|
bases.sync_doors(map, rec)
|
|
|
|
|
bases.orient_doors_near(map, x, y)
|
|
|
|
|
bases.update_block(map, pieces)
|
|
|
|
|
bases.refresh_spawn_lists(map)
|
|
|
|
|
bases.revert_if_empty(map) if pieces.empty? && bases.respond_to?(:revert_if_empty)
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Cloud login starts on NoerSewer (0, 0) and then transfers to the saved
|
|
|
|
|
# numeric map. A camp map (9200 + id) does not exist until it is registered,
|
|
|
|
|
# so that transfer is dropped and the player stays outside the sewer.
|
|
|
|
|
def self.camp_resume
|
|
|
|
|
return nil unless defined?(LonaCraft::Bases) && $game_map
|
|
|
|
|
rec = LonaCraft::Bases.record_for_map($game_map)
|
|
|
|
|
return nil unless rec.is_a?(Hash)
|
|
|
|
|
field = normal_field_key(rec['field'])
|
|
|
|
|
return nil if field.nil?
|
|
|
|
|
om = rec['om'].is_a?(Array) && rec['om'].size == 3 ? rec['om'].map(&:to_i) : nil
|
|
|
|
|
{
|
|
|
|
|
'id' => rec['id'].to_i,
|
|
|
|
|
'key' => rec['key'].to_s,
|
|
|
|
|
'field' => field.to_s,
|
|
|
|
|
'no' => rec['map_no'].to_i,
|
|
|
|
|
'om' => om
|
|
|
|
|
}
|
|
|
|
|
rescue StandardError
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.stamp_camp_resume(meta)
|
|
|
|
|
return meta unless meta.is_a?(Hash)
|
|
|
|
|
resume = camp_resume
|
|
|
|
|
if resume
|
|
|
|
|
meta['lc_camp_id'] = resume['id']
|
|
|
|
|
meta['lc_camp_key'] = resume['key']
|
|
|
|
|
meta['lc_camp_field'] = resume['field']
|
|
|
|
|
meta['lc_camp_no'] = resume['no']
|
|
|
|
|
meta['lc_camp_om'] = resume['om'] if resume['om']
|
|
|
|
|
else
|
|
|
|
|
%w[lc_camp_id lc_camp_key lc_camp_field lc_camp_no lc_camp_om].each { |key| meta.delete(key) }
|
|
|
|
|
end
|
|
|
|
|
meta
|
|
|
|
|
rescue StandardError
|
|
|
|
|
meta
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.camp_map_known?(map_id)
|
|
|
|
|
numeric = map_id.to_i
|
|
|
|
|
return false if numeric <= 0 || $data_mapinfos.nil?
|
|
|
|
|
return true if $data_mapinfos[numeric] || $data_mapinfos[map_id]
|
|
|
|
|
$data_mapinfos.any? { |_key, info| info && info.respond_to?(:id) && info.id.to_i == numeric }
|
|
|
|
|
rescue StandardError
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.resume_from_player(player, map_id)
|
|
|
|
|
info = player.is_a?(Hash) ? player : {}
|
|
|
|
|
id = info['lc_camp_id'].to_i
|
|
|
|
|
no = info['lc_camp_no'].to_i
|
|
|
|
|
no = map_id.to_i if no <= 0
|
|
|
|
|
id = no - LonaCraft::Bases::MAP_ID_BASE if id <= 0 && no >= LonaCraft::Bases::MAP_ID_BASE
|
|
|
|
|
key = info['lc_camp_key'].to_s
|
|
|
|
|
key = "#{LonaCraft::Bases::KEY_PREFIX}#{id}" if key.empty? && id > 0
|
|
|
|
|
field = normal_field_key(info['lc_camp_field'])
|
|
|
|
|
om = info['lc_camp_om']
|
|
|
|
|
om = om.map(&:to_i) if om.is_a?(Array) && om.size == 3
|
|
|
|
|
om = nil unless om.is_a?(Array) && om.size == 3 && om[0] > 0
|
|
|
|
|
return nil if id <= 0 || key.empty?
|
|
|
|
|
{'id' => id, 'key' => key, 'field' => field, 'no' => no, 'om' => om}
|
|
|
|
|
rescue StandardError
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.ensure_login_record(resume)
|
|
|
|
|
bases = LonaCraft::Bases
|
|
|
|
|
root = bases.root
|
|
|
|
|
return false if root.nil? || resume.nil?
|
|
|
|
|
existing = bases.record_by_id(resume['id'])
|
|
|
|
|
if existing.is_a?(Hash)
|
|
|
|
|
bases.ensure_registered(existing) if bases.respond_to?(:ensure_registered)
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
field = resume['field']
|
|
|
|
|
return false if field.nil?
|
|
|
|
|
registered = bases.register(resume['key'], resume['no'], field)
|
|
|
|
|
return false if registered.nil?
|
|
|
|
|
resume['no'] = registered.to_i
|
|
|
|
|
om = resume['om']
|
|
|
|
|
rec = {
|
|
|
|
|
'id' => resume['id'], 'om' => om, 'region' => 0, 'field' => field,
|
|
|
|
|
'key' => resume['key'], 'map_no' => resume['no'], 'pieces' => {},
|
|
|
|
|
'made' => (bases.respond_to?(:date_now) ? bases.date_now : ''),
|
|
|
|
|
'lc_shared' => true
|
|
|
|
|
}
|
|
|
|
|
bases.list << rec
|
|
|
|
|
root['next_id'] = [root['next_id'].to_i, resume['id'] + 1].max
|
|
|
|
|
if om.is_a?(Array)
|
|
|
|
|
root['entry'] = {
|
|
|
|
|
'om' => om, 'region' => 0, 'event' => 0, 'to' => field,
|
|
|
|
|
'at' => resume['key'], 'base' => resume['id'], 'allowed' => true
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
true
|
|
|
|
|
rescue StandardError => e
|
|
|
|
|
log "camp login register error: #{e.class}: #{e.message}" if respond_to?(:log)
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.overmap_fallback(state)
|
|
|
|
|
story = defined?($story_stats) ? $story_stats : nil
|
|
|
|
|
return false unless story && story.respond_to?(:[])
|
|
|
|
|
mid = story['OverMapID'].to_i
|
|
|
|
|
x = story['LastOverMapX'].to_i
|
|
|
|
|
y = story['LastOverMapY'].to_i
|
|
|
|
|
return false if mid <= 0 || x < 0 || y < 0
|
|
|
|
|
return false if defined?(WebSocket) && WebSocket.respond_to?(:overmap_map_id?) && !WebSocket.overmap_map_id?(mid)
|
|
|
|
|
state['map_id'] = mid
|
|
|
|
|
state['x'] = x
|
|
|
|
|
state['y'] = y
|
|
|
|
|
state['direction'] = 2
|
|
|
|
|
true
|
|
|
|
|
rescue StandardError
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Register the saved camp before the login transfer. If the field map is
|
|
|
|
|
# not in this snapshot yet, stand on the camp's overmap tile instead of
|
|
|
|
|
# the blank NoerSewer cell.
|
|
|
|
|
def self.prepare_camp_login(state)
|
|
|
|
|
return unless state.is_a?(Hash)
|
|
|
|
|
map_id = state['map_id'].to_i
|
|
|
|
|
return if map_id < LonaCraft::Bases::MAP_ID_BASE
|
|
|
|
|
return if camp_map_known?(map_id)
|
|
|
|
|
player = state['player_json']
|
|
|
|
|
resume = resume_from_player(player, map_id)
|
|
|
|
|
if resume && resume['field'] && ensure_login_record(resume)
|
|
|
|
|
om = resume['om']
|
|
|
|
|
if om && defined?(WebSocket) && WebSocket.respond_to?(:capture_overmap_location)
|
|
|
|
|
WebSocket.capture_overmap_location(om[0], om[1], om[2], 'camp_login')
|
|
|
|
|
end
|
|
|
|
|
state['map_id'] = resume['no'] if resume['no'].to_i > 0
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
if resume && resume['om']
|
|
|
|
|
om = resume['om']
|
|
|
|
|
if !defined?(WebSocket) || !WebSocket.respond_to?(:overmap_map_id?) || WebSocket.overmap_map_id?(om[0])
|
|
|
|
|
state['map_id'] = om[0]
|
|
|
|
|
state['x'] = om[1]
|
|
|
|
|
state['y'] = om[2]
|
|
|
|
|
state['direction'] = 2
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
overmap_fallback(state)
|
|
|
|
|
rescue StandardError => e
|
|
|
|
|
log "camp login prepare error: #{e.class}: #{e.message}" if respond_to?(:log)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if defined?(WebSocket)
|
|
|
|
|
module WebSocket
|
|
|
|
|
class << self
|
|
|
|
|
if method_defined?(:_live_sync_collect_player_meta) && !method_defined?(:lc_camp_collect_player_meta)
|
|
|
|
|
alias lc_camp_collect_player_meta _live_sync_collect_player_meta
|
|
|
|
|
def _live_sync_collect_player_meta(actor)
|
|
|
|
|
out = lc_camp_collect_player_meta(actor)
|
|
|
|
|
LonaCraft::OnlineSync.stamp_camp_resume(out) if defined?(LonaCraft::OnlineSync)
|
|
|
|
|
out
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if method_defined?(:_bootstrap_apply_position) && !method_defined?(:lc_camp_bootstrap_position)
|
|
|
|
|
alias lc_camp_bootstrap_position _bootstrap_apply_position
|
|
|
|
|
def _bootstrap_apply_position(state)
|
|
|
|
|
LonaCraft::OnlineSync.prepare_camp_login(state) if defined?(LonaCraft::OnlineSync)
|
|
|
|
|
lc_camp_bootstrap_position(state)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Socket/multiplayer scripts are loaded by the online client, before LonaCraft
|
|
|
|
|
# in the 2026-10-09 build. No edits to ModScripts/multiplayer are necessary.
|
|
|
|
|
if defined?(::WebSocket) && ::WebSocket.respond_to?(:update)
|
|
|
|
|
module WebSocket
|
|
|
|
|
class << self
|
|
|
|
|
alias_method :lc_online_previous_update, :update
|
|
|
|
|
def update(*args, **kwargs, &block)
|
|
|
|
|
result = lc_online_previous_update(*args, **kwargs, &block)
|
|
|
|
|
LonaCraft::OnlineSync.tick
|
|
|
|
|
result
|
|
|
|
|
end
|
|
|
|
|
if method_defined?(:handle_remote_deistvia)
|
|
|
|
|
alias_method :lc_online_previous_deistvia, :handle_remote_deistvia
|
|
|
|
|
def handle_remote_deistvia(player_id, action_id, phase, payload)
|
|
|
|
|
if action_id.to_s == LonaCraft::OnlineSync::ACTION
|
|
|
|
|
LonaCraft::OnlineSync.receive(player_id, phase.to_s, payload.to_s)
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
lc_online_previous_deistvia(player_id, action_id, phase, payload)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
puts '[LonaCraft Online] WebSocket unavailable when mod loaded; online relay disabled.'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Send the foundation BEFORE WebSocket.update reports the re-keyed map ID.
|
|
|
|
|
# The packet travels through the old shared map's peer relay.
|
|
|
|
|
module LonaCraft
|
|
|
|
|
module Bases
|
|
|
|
|
class << self
|
|
|
|
|
# Clearing the final floor must NOT silently delete a persistent camp.
|
|
|
|
|
# The dedicated "Destroy all bases" action remains an explicit deletion.
|
|
|
|
|
alias_method :lc_online_previous_on_piece_removed, :on_piece_removed
|
|
|
|
|
def on_piece_removed(map, x, y)
|
|
|
|
|
rec = record_for_map(map)
|
|
|
|
|
if LonaCraft::OnlineSync.ready? && rec && rec['om'].is_a?(Array)
|
|
|
|
|
pieces = rec['pieces'].is_a?(Hash) ? rec['pieces'] : {}
|
|
|
|
|
key = piece_key(x, y)
|
|
|
|
|
return nil unless pieces.key?(key)
|
|
|
|
|
pieces.delete(key)
|
|
|
|
|
return :removed
|
|
|
|
|
end
|
|
|
|
|
lc_online_previous_on_piece_removed(map, x, y)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
alias_method :lc_online_previous_destroy_all, :destroy_all!
|
|
|
|
|
def destroy_all!
|
|
|
|
|
shared = list.select{|rec| rec.is_a?(Hash) && rec['lc_shared'] && rec['om'].is_a?(Array) }
|
|
|
|
|
return lc_online_previous_destroy_all if shared.empty?
|
|
|
|
|
removable = list.select do |rec|
|
|
|
|
|
next false unless rec.is_a?(Hash)
|
|
|
|
|
!rec['lc_shared'] || (LonaCraft::OnlineSync.ready? && LonaCraft::OnlineSync.owns_camp?(rec))
|
|
|
|
|
end
|
|
|
|
|
removable.each do |rec|
|
|
|
|
|
LonaCraft::OnlineSync.announce_removed(rec) if rec['lc_shared'] && rec['om'].is_a?(Array)
|
|
|
|
|
end
|
|
|
|
|
destroy_records!(removable)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
alias_method :lc_online_previous_after_furniture_removed, :after_furniture_removed
|
|
|
|
|
def after_furniture_removed(map)
|
|
|
|
|
rec = record_for_map(map)
|
|
|
|
|
return false if LonaCraft::OnlineSync.ready? && rec && rec['om'].is_a?(Array)
|
|
|
|
|
lc_online_previous_after_furniture_removed(map)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
alias_method :lc_online_previous_revert_if_empty, :revert_if_empty
|
|
|
|
|
def revert_if_empty(map)
|
|
|
|
|
rec = record_for_map(map)
|
|
|
|
|
return false if LonaCraft::OnlineSync.ready? && rec && rec['om'].is_a?(Array)
|
|
|
|
|
lc_online_previous_revert_if_empty(map)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
alias_method :lc_online_previous_remove_record, :remove_record
|
|
|
|
|
def remove_record(rec)
|
|
|
|
|
if rec.is_a?(Hash) && rec['om'].is_a?(Array)
|
|
|
|
|
LonaCraft::OnlineSync.announce_removed(rec)
|
|
|
|
|
end
|
|
|
|
|
lc_online_previous_remove_record(rec)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
alias_method :lc_online_previous_create_record, :create_record
|
|
|
|
|
def create_record(map)
|
|
|
|
|
previous = LonaCraft::OnlineSync.map_token
|
|
|
|
|
root_data = root
|
|
|
|
|
original_next = root_data && root_data['next_id']
|
|
|
|
|
online_field = LonaCraft::OnlineSync.ready? && !named_map?(map) &&
|
|
|
|
|
entry.is_a?(Hash) && entry['om'].is_a?(Array)
|
|
|
|
|
# Every player gets the same ID for the same world tile, regardless of
|
|
|
|
|
# their private save's next_id. Avoid the old all-players-use-ID-1 race.
|
|
|
|
|
if online_field
|
|
|
|
|
coords = entry['om'].map(&:to_i).join(':')
|
|
|
|
|
hash = coords.each_byte.inject(2166136261) { |h, b| ((h ^ b) * 16777619) & 0xffffffff }
|
|
|
|
|
root_data['next_id'] = 1000 + hash % 85000
|
|
|
|
|
end
|
|
|
|
|
rec = lc_online_previous_create_record(map)
|
|
|
|
|
if rec && online_field
|
|
|
|
|
nick = (::WebSocket.local_nickname.to_s rescue '').strip
|
|
|
|
|
nick = 'Игрок' if nick.empty?
|
|
|
|
|
nick = LonaCraft::OnlineSync.owner_name(nick)
|
|
|
|
|
rec['owner'] = nick
|
|
|
|
|
rec['name'] = LonaCraft::OnlineSync.camp_name(nick)
|
|
|
|
|
rec['lc_shared'] = true
|
|
|
|
|
end
|
|
|
|
|
if root_data && original_next
|
|
|
|
|
root_data['next_id'] = [original_next.to_i, root_data['next_id'].to_i].max
|
|
|
|
|
end
|
|
|
|
|
if rec && previous
|
|
|
|
|
LonaCraft::OnlineSync.announce_foundation(rec, previous)
|
|
|
|
|
LonaCraft::OnlineSync.update_tab_camp
|
|
|
|
|
LonaCraft::OnlineSync.newly_founded!(LonaCraft::OnlineSync.map_token) if online_field
|
|
|
|
|
end
|
|
|
|
|
rec
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# The original LonaCraft randomly selects a wilderness field every visit. In
|
|
|
|
|
# co-op that sends two players at the SAME world tile to different map rooms.
|
|
|
|
|
# Select the same field deterministically while connected. Offline choice stays
|
|
|
|
|
# completely unchanged. Registered/pinned existing bases still take priority.
|
|
|
|
|
module LonaCraft
|
|
|
|
|
module Bases
|
|
|
|
|
class << self
|
|
|
|
|
alias_method :lc_online_previous_pick_for_entry, :pick_for_entry
|
|
|
|
|
def pick_for_entry(region)
|
|
|
|
|
return lc_online_previous_pick_for_entry(region) unless LonaCraft::OnlineSync.ready?
|
|
|
|
|
return lc_online_previous_pick_for_entry(region) unless $game_map && $game_map.isOverMap
|
|
|
|
|
maps = ($data_region_maps && $data_region_maps[region])
|
|
|
|
|
return lc_online_previous_pick_for_entry(region) unless maps.is_a?(Array) && maps.size > 1
|
|
|
|
|
if record_for_tile($game_map.map_id, $game_player.x, $game_player.y)
|
|
|
|
|
return lc_online_previous_pick_for_entry(region)
|
|
|
|
|
end
|
|
|
|
|
event = $story_stats && $story_stats['OverMapEvent_name']
|
|
|
|
|
return lc_online_previous_pick_for_entry(region) if event && event.to_s != '0'
|
|
|
|
|
selected = maps.sort_by(&:to_s)
|
|
|
|
|
seed = "#{$game_map.map_id}|#{$game_player.x}|#{$game_player.y}|#{region}"
|
|
|
|
|
number = seed.each_byte.inject(5381){|h, b| ((h * 33) ^ b) & 0xffffffff}
|
|
|
|
|
$data_region_maps[region] = [selected[number % selected.size]]
|
|
|
|
|
begin
|
|
|
|
|
lc_online_previous_pick_for_entry(region)
|
|
|
|
|
ensure
|
|
|
|
|
$data_region_maps[region] = maps
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Every visible overmap camp marker carries the shared owner's label. This is
|
|
|
|
|
# deliberately tied to the event sprite: scrolling, loading a save and the
|
|
|
|
|
# runtime addition of an online marker all follow the same path.
|
|
|
|
|
if defined?(Sprite_Character)
|
|
|
|
|
class Sprite_Character
|
|
|
|
|
unless method_defined?(:lc_camp_previous_update)
|
|
|
|
|
alias_method :lc_camp_previous_update, :update
|
|
|
|
|
def update(*args, **kwargs, &block)
|
|
|
|
|
result = lc_camp_previous_update(*args, **kwargs, &block)
|
|
|
|
|
lc_camp_update_label
|
|
|
|
|
result
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def lc_camp_update_label
|
|
|
|
|
game_event = @character
|
|
|
|
|
prefix = LonaCraft::Bases::MARKER_PREFIX
|
|
|
|
|
name = game_event && game_event.respond_to?(:name) ? game_event.name.to_s : ''
|
|
|
|
|
unless name.start_with?(prefix)
|
|
|
|
|
@lc_camp_label.visible = false if @lc_camp_label && !@lc_camp_label.disposed?
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
id = name[prefix.length..-1].to_i
|
|
|
|
|
rec = LonaCraft::Bases.record_by_id(id)
|
|
|
|
|
label = rec ? LonaCraft::Bases.name_of(rec).to_s : ''
|
|
|
|
|
if label.empty?
|
|
|
|
|
@lc_camp_label.visible = false if @lc_camp_label && !@lc_camp_label.disposed?
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
if @lc_camp_label.nil? || @lc_camp_label.disposed?
|
|
|
|
|
@lc_camp_label = Sprite.new(viewport)
|
|
|
|
|
@lc_camp_label.ox = 240
|
|
|
|
|
@lc_camp_label.oy = 32
|
|
|
|
|
end
|
|
|
|
|
if @lc_camp_label_text != label
|
|
|
|
|
old_bitmap = @lc_camp_label.bitmap
|
|
|
|
|
old_bitmap.dispose if old_bitmap && !old_bitmap.disposed?
|
|
|
|
|
bitmap = Bitmap.new(480, 32)
|
|
|
|
|
LonaCraft.apply_ui_font(bitmap)
|
|
|
|
|
bitmap.font.size = 18
|
|
|
|
|
bitmap.draw_text(0, 0, 480, 30, label, 1)
|
|
|
|
|
@lc_camp_label.bitmap = bitmap
|
|
|
|
|
@lc_camp_label_text = label
|
|
|
|
|
end
|
|
|
|
|
@lc_camp_label.x = x
|
|
|
|
|
@lc_camp_label.y = y - 25
|
|
|
|
|
@lc_camp_label.z = z + 10
|
|
|
|
|
@lc_camp_label.visible = visible && opacity > 0
|
|
|
|
|
rescue StandardError
|
|
|
|
|
# Map rendering must never crash because of a cosmetic label.
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
alias_method :lc_camp_previous_dispose, :dispose
|
|
|
|
|
def dispose(*args, **kwargs, &block)
|
|
|
|
|
if @lc_camp_label && !@lc_camp_label.disposed?
|
|
|
|
|
bmp = @lc_camp_label.bitmap
|
|
|
|
|
@lc_camp_label.dispose
|
|
|
|
|
bmp.dispose if bmp && !bmp.disposed?
|
|
|
|
|
end
|
|
|
|
|
lc_camp_previous_dispose(*args, **kwargs, &block)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|