Register
View RSS Feed

//no comment

Starting from Scratch

Rate this Entry
Some of the code in TradeSkillMaster is fairly new, some is years older than TSM itself. Most of the oldest code is found in the Auctioning, Crafting, and Mailing modules as it's code which was taken from TSM's predecessors: Quick Auctions 3 and Scroll Master.

While I love fixing things that aren't broken as much as anybody, there are often situations in which a "it works so don't touch it" mindset comes into play. Here is a function out of TSM_Auctioning which is arguably the most important (and definitely most called) piece of code in all of TSM_Auctioning.

Code:
function Config:GetConfigValue(itemString, key, isGroup)
	local groupValue
	local specialSettingKey = specialSettingKeys[key]
	if specialSettingKey then
		if TSM.db.profile.groups[itemString] or TSM.db.profile.categories[itemString] then
			if TSM.db.profile[specialSettingKey][itemString] then
				groupValue = itemString
			elseif TSM.db.profile[specialSettingKey][TSM.groupReverseLookup[itemString]] then
				groupValue = TSM.groupReverseLookup[itemString]
			else
				groupValue = "default"
			end
		else
			groupValue = "default"
		end
	elseif goldSettingKeys[key] then
		if TSM.itemReverseLookup[itemString] and TSM.db.profile[key][TSM.itemReverseLookup[itemString]] and TSM.db.profile[key][TSM.itemReverseLookup[itemString]] then
			groupValue = TSM.itemReverseLookup[itemString]
		elseif TSM.groupReverseLookup[TSM.itemReverseLookup[itemString] or ""] and TSM.db.profile[key][TSM.groupReverseLookup[TSM.itemReverseLookup[itemString] or ""]] then
			groupValue = TSM.groupReverseLookup[TSM.itemReverseLookup[itemString]]
		elseif TSM.db.profile.groups[itemString] or TSM.db.profile.categories[itemString] then
			-- we got passed a group not an itemString (used by GetGroupMoney method in the options)
			if TSM.db.profile[key][itemString] then
				groupValue = itemString
			elseif TSM.groupReverseLookup[itemString] and TSM.db.profile[key][TSM.groupReverseLookup[itemString]] then
				groupValue = TSM.groupReverseLookup[itemString]
			else
				groupValue = "default"
			end
		else
			groupValue = "default"
		end
		if TSM.db.profile[key][groupValue] ~= nil then
			TSM.db.profile[key.."PriceMethod"][groupValue] = TSM.db.profile[key.."PriceMethod"][groupValue] or "gold"
		end
		local method = TSM.db.profile[key.."PriceMethod"][groupValue]
		if method ~= "gold" then
			local group = TSM.itemReverseLookup[itemString] or (TSM.db.profile.groups[itemString] and itemString)
			if not group then return 0 end
			local percent = TSM.db.profile[key.."Percent"][groupValue]
			local value = TSM:GetMarketValue(group, percent, method)
			return value
		end
	else
		if not isGroup then
			if TSM.itemReverseLookup[itemString] and TSM.db.profile[key][TSM.itemReverseLookup[itemString]] then
				groupValue = TSM.itemReverseLookup[itemString]
			elseif TSM.groupReverseLookup[TSM.itemReverseLookup[itemString] or ""] and TSM.db.profile[key][TSM.groupReverseLookup[TSM.itemReverseLookup[itemString] or ""]] then
				groupValue = TSM.groupReverseLookup[TSM.itemReverseLookup[itemString]]
			else
				groupValue = "default"
			end
		else
			if TSM.db.profile[key][itemString] then
				groupValue = itemString
			elseif TSM.groupReverseLookup[itemString] and TSM.db.profile[key][TSM.groupReverseLookup[itemString]] then
				groupValue = TSM.groupReverseLookup[itemString]
			else
				groupValue = "default"
			end
		end
	end
	return TSM.db.profile[key][groupValue]
end
The purpose of the function is to look up a particular setting for a group. Without going into too much detail on how Auctioning stores group settings, there are many things which make this seemingly simple (and common) function somewhat complex. For example, the setting you're looking for may be at the group, category, or global level and if it's a threshold / fallback, other settings (ie price source) need to be looked up and then the original setting needs to be calculated.

The point is, this function was originally taken from Quick Auctions 3, and then when TSM added the concepts of categories and non-fixed prices (can you remember not having % based thresholds/fallbacks and/or categories? - how did we survive?) new functionality was hacked into the function. Many bug fixes later and everything worked well, but the function had become the complete mess that it is now, where even I would have to sit down for an hour to understand it.

In cases like this, if new functionality needs to be added, it's much easier just to start from scratch than try and tweak the function. Fortunately, on a related note to my last post, eventually a better system comes along and the old, messy code gets made obsolete by new, fresh code, which in turn over the following year or two turns into unreadable crap and the cycle repeats.

Updated November 30th, 2012 at 01:14 PM by Sapu94

Categories
World of Warcraft , Off-Topic , Development

Comments

  1. Stede's Avatar
    Yeah, building on legacy code - Frankensteining more blocks, loops, and macros until the monster becomes too unweildy to bear... I've had my share of those and they are no fun... at all.