diff Controller.lua @ 18:a3328fffef5c

Split of main file into components
author wobin
date Thu, 07 May 2009 02:52:23 +1000
parents
children 431f2fce08f2
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Controller.lua	Thu May 07 02:52:23 2009 +1000
@@ -0,0 +1,127 @@
+local Controller = Squawk.Controller
+local Model = Squawk.Model
+local View = Squawk.View
+local Settings = Model.UserSettings
+local Squawks = Model.Squawks
+
+--Controller--
+
+function Controller:TheyWantToFollowMe(Name)
+	if Settings:IsPrivate() then
+		Settings:AddPending(Name)
+		self:PutForwardFollowRequest(Name)
+		self:SendMessageToTarget(Name, "#Pending|"..UnitName("player"))
+	else
+		Settings:AddFollower(Name)
+		View:NotifyOfNewFollower(Name)
+		self:SendMessageToTarget(Name, "#Follow|"..UnitName("player"))
+	end
+end
+
+function Controller:TheyWantToUnfollowMe(Name)
+	Settings:RemoveFollower(Name)
+end
+
+function Controller:IWantToFollowThem(Name)
+	self:SendMessageToTarget(Name, "#Request|"..UnitName("player"))
+	Settings:AddRequested(Name)
+end
+
+function Controller:IWantToUnfollowThem(Name)
+	Settings:RemoveFollowing(Name)
+	self:SendMessageToTarget(Name, "#Unfollow|"..UnitName("player"))
+	View:NotifyOfUnfollowing(Name)
+end
+
+function Controller:IAmNowFollowingThem(Name)
+	Settings:AddFollowing(Name)
+	View:NotifyOfNewFollowing(Name)
+end
+
+function Controller:AddANewSquawk(Name, Message, Source)
+	if not Settings.Blocked[Name] then
+	
+		if Source == "WHISPER" then
+			if Settings.Requested[Name] then -- We've been approved offline!
+				Settings:AddFollowing(Name)
+			end
+
+			if not Settings.Following[Name] then -- If we're no longer following this person
+				self:SendMessageToTarget(Name, "#Unfollow|"..UnitName("player"))
+				return
+			end
+		end	
+		
+		if Source == "GUILD" and Name == UnitName("player") then
+			return
+		end
+
+		table.insert(Model.Squawks, Squawks:new(Message, Name))
+		View:UpdateSquawkList()	
+	end
+end
+
+local trigger
+local function RepressFailure(frame, event, ...)
+	if arg1:match(string.gsub(ERR_CHAT_PLAYER_NOT_FOUND_S, "%%s", "(.*)")) then
+		if trigger then Controller:CancelTimer(trigger, true) end
+		trigger = Controller:ScheduleTimer(
+				function() 
+					ChatFrame_RemoveMessageEventFilter("CHAT_MSG_SYSTEM", RepressFailure) 
+				end, 3) -- Give it three seconds and then remove the filter.
+		return true
+	else
+		return false, unpack(...)
+	end
+end
+
+function Controller:SendNewSquawk(Message)
+	if not Settings:IsPrivate() then
+		self:SendMessageToGuild("#Squawk|"..UnitName("player").."|"..Message)
+	end
+
+	self:AddANewSquawk(UnitName("player"), Message)
+	for name, _ in pairs(Settings.Following) do
+		self:SendMessageToTarget(name, "#Squawk|"..UnitName("player").."|"..Message)
+	end
+end
+
+function Controller:ImPending(Name)
+	View:NotifyOfPending(Name)
+end
+
+function Controller:PutForwardFollowRequest(Name)
+	View:NotifyOfPendingRequest(Name)
+end
+
+function Controller:ApprovePendingRequest(Name)
+	Settings:AddFollower(Name)
+	View:NotifyOfNewFollower(Name)
+	self:SendMessageToTarget(Name, "#Follow|"..UnitName("player"))
+end
+
+
+
+function Controller:SendMessageToTarget(Name, Message)
+	ChatFrame_AddMessageEventFilter("CHAT_MSG_SYSTEM", RepressFailure)
+	self:SendCommMessage("Squawk", Message, "WHISPER", Name)
+end
+
+function Controller:SendMessageToGuild(Message)
+	self:SendCommMessage("Squawk", Message, "GUILD")
+end
+
+local Parse = { 
+		["#Pending"] = Controller.ImPending,
+		["#Follow"] = Controller.IAmNowFollowingThem,
+		["#Unfollow"] = Controller.TheyWantToUnfollowMe,
+		["#Squawk"] = Controller.AddANewSquawk,
+		["#Request"] = Controller.TheyWantToFollowMe,
+	}
+
+function Controller:ReceiveMessage(Message, Distribution, Sender)
+	local command, name, info = strsplit("|",Message)
+	View:Print(Distribution..":"..Message)
+	Parse[command](Controller, name, info, Distribution)
+end
+