Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confirmation behaviors #774

Merged
merged 12 commits into from
Aug 20, 2023
Prev Previous commit
Next Next commit
Implement the confirmation behaviors
  • Loading branch information
boatbomber committed Aug 15, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 3fd7c4d40951fcb723bf04f40d1689e0823cfea3
28 changes: 28 additions & 0 deletions plugin/src/App/init.lua
Original file line number Diff line number Diff line change
@@ -57,6 +57,7 @@ function App:init()

self.confirmationBindable = Instance.new("BindableEvent")
self.confirmationEvent = self.confirmationBindable.Event
self.knownProjects = {}
self.notifId = 0

self.waypointConnection = ChangeHistoryService.OnUndo:Connect(function(action: string)
@@ -416,6 +417,8 @@ function App:startSession()
})
self:addNotification("Connecting to session...")
elseif status == ServeSession.Status.Connected then
self.knownProjects[details] = true

local address = ("%s:%s"):format(host, port)
self:setState({
appStatus = AppStatus.Connected,
@@ -462,6 +465,31 @@ function App:startSession()
return "Accept"
end

local confirmationBehavior = Settings:get("confirmationBehavior")
if confirmationBehavior == "Initial" then
-- Only confirm if we haven't synced this project yet this session
if self.knownProjects[serverInfo.projectName] then
Log.trace("Accepting patch without confirmation because project has already been connected and behavior is set to Initial")
return "Accept"
end
elseif confirmationBehavior == "Large Changes" then
-- Only confirm if the patch impacts many instances
local instanceThreshold = 5
if PatchSet.countInstances(patch) <= instanceThreshold then
Log.trace("Accepting patch without confirmation because patch is small and behavior is set to Large Changes")
return "Accept"
end
elseif confirmationBehavior == "Unlisted PlaceId" then
-- Only confirm if the current placeId is not in the servePlaceIds allowlist
if serverInfo.expectedPlaceIds then
local isListed = table.find(serverInfo.expectedPlaceIds, game.PlaceId) ~= nil
if isListed then
Log.trace("Accepting patch without confirmation because placeId is listed and behavior is set to Unlisted PlaceId")
return "Accept"
end
end
end

-- The datamodel name gets overwritten by Studio, making confirmation of it intrusive
-- and unnecessary. This special case allows it to be accepted without confirmation.
if
24 changes: 23 additions & 1 deletion plugin/src/PatchSet.lua
Original file line number Diff line number Diff line change
@@ -116,7 +116,7 @@ function PatchSet.containsId(patchSet, instanceMap, id)
end

--[[
Tells whether the given PatchSet contains changes to the given instance.
Tells whether the given PatchSet contains changes to the given instance.
If the given InstanceMap does not contain the instance, this function always returns false.
]]
function PatchSet.containsInstance(patchSet, instanceMap, instance)
@@ -235,6 +235,28 @@ function PatchSet.countChanges(patch)
return count
end

--[[
Count the number of instances affected by the given PatchSet.
]]
function PatchSet.countInstances(patch)
local count = 0

-- Added instances
for _ in patch.added do
count += 1
end
-- Removed instances
for _ in patch.removed do
count += 1
end
-- Updated instances
for _ in patch.updated do
count += 1
end

return count
end

--[[
Merge multiple PatchSet objects into the given PatchSet.
]]