Files
LonaRPG-Online-Games/ModScripts/multiplayer/08_vars_switches.rb
T

105 lines
2.9 KiB
Ruby
Raw Normal View History

2026-10-29 21:59:06 +04:00
module WebSocket
# WORLD: Vars/Switches
#--------------------------------------------------------------------------
# Intentionally world-shared story keys. Unique NPC death flags are map-wide
# on purpose; personal quest progression (Guild*/QuProg*/etc.) must stay local.
def self.world_shared_story_key?(name)
text = name.to_s
return false if text == ""
return true if text.start_with?("UniqueChar")
false
rescue
false
end
def self.personal_quest_story_key?(name)
return false if world_shared_story_key?(name)
return true if defined?(MPQuestCloudState) && MPQuestCloudState.respond_to?(:quest_key?) && MPQuestCloudState.quest_key?(name)
false
rescue
false
end
def self.apply_sync_var(name, vtype, val)
return unless $story_stats && $story_stats.respond_to?(:[]=)
return if personal_quest_story_key?(name)
parsed =
case vtype
when "bool"
(val == "1" || val == "true" || val == "True")
when "int"
val.to_i
when "string"
val.to_s
when "array"
val.to_s.split(",")
else
val
end
begin
cur = $story_stats[name]
return if cur == parsed
rescue
end
$story_stats[name] = parsed
end
def self.apply_sync_switch(sid, val)
return unless $game_switches
newv = (val.to_i == 1)
begin
return if $game_switches[sid] == newv
rescue
end
$game_switches[sid] = newv
$game_map.need_refresh = true rescue nil
end
def self.net_set_story(name, value, vtype=nil)
return unless @connected && @worldsync
return if name.nil? || name.empty?
return if personal_quest_story_key?(name)
if vtype.nil?
vtype =
case value
when TrueClass, FalseClass then "bool"
when Integer then "int"
when Array then "array"
else "string"
end
end
vstr = value.is_a?(Array) ? value.join(",") : value.to_s
@sync_var_last_sent = {} if !instance_variable_defined?(:@sync_var_last_sent) || @sync_var_last_sent.nil?
sig = vtype.to_s + ":" + vstr.to_s
return if @sync_var_last_sent[name] == sig
if respond_to?(:send_world_line)
send_world_line("sync_var:#{name}:#{vtype}:#{vstr}")
else
send_line("sync_var:#{name}:#{vtype}:#{vstr}")
end
@sync_var_last_sent[name] = sig
end
def self.net_set_switch(id, value)
return unless @connected && @worldsync
@sync_switch_last_sent = {} if !instance_variable_defined?(:@sync_switch_last_sent) || @sync_switch_last_sent.nil?
sid = id.to_i
sval = (value ? 1 : 0)
return if @sync_switch_last_sent[sid] == sval
if respond_to?(:send_world_line)
send_world_line("sync_switch:#{sid}:#{sval}")
else
send_line("sync_switch:#{sid}:#{sval}")
end
@sync_switch_last_sent[sid] = sval
end
#--------------------------------------------------------------------------
end