--[[
	Connect to the datastore, set up connections and create resources used by
	the server

	Parameters:
	configValues: A table containing configuration values
]]

local function setup(configValues)
	local tokenDataStore = DataStoreService:GetDataStore(configValues.datastoreName)
	local tokenManager = TokenManager.new(tokenDataStore)
	local tokens = CollectionService:GetTagged(configValues.tokenTag)

	tokenManager:setTokens(tokens)

	disconnectOldConnections()

	connections[1] = CollectionService
		:GetInstanceAddedSignal(configValues.tokenTag)
		:Connect(onTokenAdded(remoteEvents, tokenManager))
	connections[2] = CollectionService
		:GetInstanceRemovedSignal(configValues.tokenTag)
		:Connect(onTokenRemoved(remoteEvents, tokenManager))
	connections[3] = Players.PlayerAdded:Connect(onPlayerAdded(remoteEvents, tokenDataStore, tokenManager))
	connections[4] = Players.PlayerRemoving:Connect(onPlayerRemoved(tokenDataStore, tokenManager))
	connections[5] = events.disable.Event:Connect(onHuntDisabled(tokenManager))
	connections[6] = events.enable.Event:Connect(onHuntEnabled(tokenManager))
end

config.changed:Connect(setup)

setup(config.getValues())
