[Scripts] Can Experts simplify this trash for me :/ ?

4 replies [Last post]
UltiMaxKom
UltiMaxKom's picture
User offline. Last seen 4 years 10 weeks ago. Offline
Joined: 2016-06-26
Posts: 381

Well, I've makes more than 80 library out of nowhere
Which I've make in really random times where my boredom and mood is suitable for writing
Only a fraction of times since I play Minecraft too (etc)
BUT!
One non-important problem is up

I'm improved every script created (but isn't that much, cuz the big time gap)
My first script is absolutely trash compared to my latest one
And the problem is, I don't have time to update nor look for it these days (well, I had a life too xD)
Annnddd
I still realize that my simplifying skill isn't good at all! (well, idk what to compare with mine, so I just assume mine is really bad (it is! *iiner self))

That's why I'm here with this post and its help request
May you help me to simplify this garbage below?
Because my knowledge upon Lua isn't good at all (my perspective)
But please: Don't laugh at me xD !

Here it is:

Log = {}
LogKey = {}

function ToLog(val, mode, k1, k2, k3, k4, k5, k6, k7, modedetail)
	
	-- Optimization
	local LocLog = Log
	local LocLogKey = LogKey
	
	if k2 ~= nil then
		if LocLog[k1] == nil then
			LocLog[k1] = {}
		end
		if LocLogKey[k1] == nil then
			LocLogKey[k1] = {}
		end
	end
	if k3 ~= nil then
		if LocLog[k1][k2] == nil then
			LocLog[k1][k2] = {}
		end
		if LocLogKey[k1][k2] == nil then
			LocLogKey[k1][k2] = {}
		end
	end
	if k4 ~= nil then
		if LocLog[k1][k2][k3] == nil then
			LocLog[k1][k2][k3] = {}
		end
		if LocLogKey[k1][k2][k3] == nil then
			LocLogKey[k1][k2][k3] = {}
		end
	end
	if k5 ~= nil then
		if LocLog[k1][k2][k3][k4] == nil then
			LocLog[k1][k2][k3][k4] = {}
		end
		if LocLogKey[k1][k2][k3][k4] == nil then
			LocLogKey[k1][k2][k3][k4] = {}
		end
	end
	if k6 ~= nil then
		if LocLog[k1][k2][k3][k4][k5] == nil then
			LocLog[k1][k2][k3][k4][k5] = {}
		end
		if LocLogKey[k1][k2][k3][k4][k5] == nil then
			LocLogKey[k1][k2][k3][k4][k5] = {}
		end
	end
	if k7 ~= nil then
		if LocLog[k1][k2][k3][k4][k5][k6] == nil then
			LocLog[k1][k2][k3][k4][k5][k6] = {}
		end
		if LocLogKey[k1][k2][k3][k4][k5][k6] == nil then
			LocLogKey[k1][k2][k3][k4][k5][k6] = {}
		end
	end
	if mode == 1 then
		
		-- Optimization
		local tableinsert
		
		if k7 ~= nil then
			if LocLog[k1][k2][k3][k4][k5][k6][k7] == nil then
				LocLog[k1][k2][k3][k4][k5][k6][k7] = {}
				LocLogKey[k1][k2][k3][k4][k5][k6][k7] = {}
			end
			tableinsert = #LocLog[k1][k2][k3][k4][k5][k6][k7] + 1
			LocLog[k1][k2][k3][k4][k5][k6][k7][tableinsert] = val
			LocLogKey[k1][k2][k3][k4][k5][k6][k7][tableinsert] = val
		elseif k6 ~= nil then
			if LocLog[k1][k2][k3][k4][k5][k6] == nil then
				LocLog[k1][k2][k3][k4][k5][k6] = {}
				LocLogKey[k1][k2][k3][k4][k5][k6] = {}
			end
			tableinsert = #LocLog[k1][k2][k3][k4][k5][k6] + 1
			LocLog[k1][k2][k3][k4][k5][k6][tableinsert] = val
			LocLogKey[k1][k2][k3][k4][k5][k6][tableinsert] = val
		elseif k5 ~= nil then
			if LocLog[k1][k2][k3][k4][k5] == nil then
				LocLog[k1][k2][k3][k4][k5] = {}
				LocLogKey[k1][k2][k3][k4][k5] = {}
			end
			tableinsert = #LocLog[k1][k2][k3][k4][k5] + 1
			LocLog[k1][k2][k3][k4][k5][tableinsert] = val
			LocLogKey[k1][k2][k3][k4][k5][tableinsert] = val
		elseif k4 ~= nil then
			if LocLog[k1][k2][k3][k4] == nil then
				LocLog[k1][k2][k3][k4] = {}
				LocLogKey[k1][k2][k3][k4] = {}
			end
			tableinsert = #LocLog[k1][k2][k3][k4] + 1
			LocLog[k1][k2][k3][k4][tableinsert] = val
			LocLogKey[k1][k2][k3][k4][tableinsert] = val
		elseif k3 ~= nil then
			if LocLog[k1][k2][k3] == nil then
				LocLog[k1][k2][k3] = {}
				LocLogKey[k1][k2][k3] = {}
			end
			tableinsert = #LocLog[k1][k2][k3] + 1
			LocLog[k1][k2][k3][tableinsert] = val
			LocLogKey[k1][k2][k3][tableinsert] = val
		elseif k2 ~= nil then
			if LocLog[k1][k2] == nil then
				LocLog[k1][k2] = {}
				LocLogKey[k1][k2] = {}
			end
			tableinsert = #LocLog[k1][k2] + 1
			LocLog[k1][k2][tableinsert] = val
			LocLogKey[k1][k2][tableinsert] = val
		elseif k1 ~= nil then
			if LocLog[k1] == nil then
				LocLog[k1] = {}
				LocLogKey[k1] = {}
			end
			tableinsert = #LocLog[k1] + 1
			LocLog[k1][tableinsert] = val
			LocLogKey[k1][tableinsert] = val
		end
	end
	if mode == 2 then
		if k7 ~= nil then
			LocLog[k1][k2][k3][k4][k5][k6][k7] = val
			LocLogKey[k1][k2][k3][k4][k5][k6][k7] = GameTime
		elseif k6 ~= nil then
			LocLog[k1][k2][k3][k4][k5][k6] = val
			LocLogKey[k1][k2][k3][k4][k5][k6] = GameTime
		elseif k5 ~= nil then
			LocLog[k1][k2][k3][k4][k5] = val
			LocLogKey[k1][k2][k3][k4][k5] = GameTime
		elseif k4 ~= nil then
			LocLog[k1][k2][k3][k4] = val
			LocLogKey[k1][k2][k3][k4] = GameTime
		elseif k3 ~= nil then
			LocLog[k1][k2][k3] = val
			LocLogKey[k1][k2][k3] = GameTime
		elseif k2 ~= nil then
			LocLog[k1][k2] = val
			LocLogKey[k1][k2] = GameTime
		elseif k1 ~= nil then
			LocLog[k1] = val
			LocLogKey[k1] = GameTime
		end
	end
	if mode > 2 then
		local modekey
		if mode == 3 then
			modekey = GameTime
			if modedetail ~= nil then
				modekey = GameTime + modedetail
			end
		elseif mode == 4 then
			modekey = TotalRounds
			if modedetail ~= nil then
				modekey = TotalRounds + modedetail
			end
		elseif mode == 5 then
			modekey = TurnTimeLeft
			if modedetail ~= nil then
				modekey = TurnTimeLeft + modedetail
			end
		elseif mode == 6 then
			modekey = WaterLine
			if modedetail ~= nil then
				modekey = WaterLine + modedetail
			end
		end
		if k7 ~= nil then
			if LocLog[k1][k2][k3][k4][k5][k6][k7] == nil then
				LocLog[k1][k2][k3][k4][k5][k6][k7] = {}
			end
			if LocLogKey[k1][k2][k3][k4][k5][k6][k7] == nil then
				LocLogKey[k1][k2][k3][k4][k5][k6][k7] = {}
			end
			LocLog[k1][k2][k3][k4][k5][k6][k7][modekey] = val
			LocLogKey[k1][k2][k3][k4][k5][k6][k7][modekey] = GameTime
		elseif k6 ~= nil then
			if LocLog[k1][k2][k3][k4][k5][k6] == nil then
				LocLog[k1][k2][k3][k4][k5][k6] = {}
			end
			if LocLogKey[k1][k2][k3][k4][k5][k6] == nil then
				LocLogKey[k1][k2][k3][k4][k5][k6] = {}
			end
			LocLog[k1][k2][k3][k4][k5][k6][modekey] = val
			LocLogKey[k1][k2][k3][k4][k5][k6][modekey] = GameTime
		elseif k5 ~= nil then
			if LocLog[k1][k2][k3][k4][k5] == nil then
				LocLog[k1][k2][k3][k4][k5] = {}
			end
			if LocLogKey[k1][k2][k3][k4][k5] == nil then
				LocLogKey[k1][k2][k3][k4][k5] = {}
			end
			LocLog[k1][k2][k3][k4][k5][modekey] = val
			LocLogKey[k1][k2][k3][k4][k5][modekey] = GameTime
		elseif k4 ~= nil then
			if LocLog[k1][k2][k3][k4] == nil then
				LocLog[k1][k2][k3][k4] = {}
			end
			if LocLogKey[k1][k2][k3][k4] == nil then
				LocLogKey[k1][k2][k3][k4] = {}
			end
			LocLog[k1][k2][k3][k4][modekey] = val
			LocLogKey[k1][k2][k3][k4][modekey] = GameTime
		elseif k3 ~= nil then
			if LocLog[k1][k2][k3] == nil then
				LocLog[k1][k2][k3] = {}
			end
			if LocLogKey[k1][k2][k3] == nil then
				LocLogKey[k1][k2][k3] = {}
			end
			LocLog[k1][k2][k3][modekey] = val
			LocLogKey[k1][k2][k3][modekey] = GameTime
		elseif k2 ~= nil then
			if LocLog[k1][k2] == nil then
				LocLog[k1][k2] = {}
			end
			if LocLogKey[k1][k2] == nil then
				LocLogKey[k1][k2] = {}
			end
			LocLog[k1][k2][modekey] = val
			LocLogKey[k1][k2][modekey] = GameTime
		elseif k1 ~= nil then
			if LocLog[k1] == nil then
				LocLog[k1] = {}
			end
			if LocLogKey[k1] == nil then
				LocLogKey[k1] = {}
			end
			LocLog[k1][modekey] = val
			LocLogKey[k1][modekey] = GameTime
		elseif val ~= nil then
			LocLog[modekey] = val
			LocLogKey[modekey] = GameTime
		end
	end
	
	Log = LocLog
	LogKey = LocLogKey
	
end

If you may ask for more details
Then:
I need more shorten method to do such things
and without consuming large resources beforehand (local var?)
Also! The simplified stuff is doing the same thing as mine!
I know there is a way!

I've read some help, but haven't found the real solution on those
But I know, that my savior must be
You...

╟───NW──────┼──────N╢

╓──────────────────╖ ╓──────────────────╖
⠀HP: ██████████ 1E9/1E91E9/1E9 ██████████ :MP
╙──────────────────╜ ╙──────────────────╜

KoBeWi
KoBeWi's picture
User offline. Last seen 3 days 15 hours ago. Offline
Joined: 2010-12-25
Posts: 564

I see lots of repeated code. You can simplify it a lot if you just add some methods with proper arguments.

UltiMaxKom
UltiMaxKom's picture
User offline. Last seen 4 years 10 weeks ago. Offline
Joined: 2016-06-26
Posts: 381

KoBeWi allegedly wrote:

I see lots of repeated code. You can simplify it a lot if you just add some methods with proper arguments.

Well, that is
Lots of repeated code is main the problem
Still didn't know the proper method to do such things ATM
Can you write down how to do it =/ ? Thanks
*Well, I need some milk*

╟───NW──────┼──────N╢

╓──────────────────╖ ╓──────────────────╖
⠀HP: ██████████ 1E9/1E91E9/1E9 ██████████ :MP
╙──────────────────╜ ╙──────────────────╜

sheepluva
sheepluva's picture
User offline. Last seen 12 hours 11 min ago. Offline
Joined: 2009-07-18
Posts: 562

Not sure if this does exactly what you need, but hey - maybe it helps anyway:

Log = {}
LogKey = {}

-- Puts a value into table t based on an arbitrary amount of index arguments
-- if insert is true, table.insert will be used instead of assignment
-- If lastIdx is not nil, it will be used as the very last index, after all others
-- Returns t with subtables created and value inserted or set at last index
--
-- dev note: This function is recursive, it will call itself for each level it goes deeper into the subtables
function DeepValuePut(t, value, insert, lastIdx, ...)

    -- check if there are any arguments after table (ignoring as many as the end as specified by nskip)
    if (#arg > 0) and (arg[1] ~= nil) then
        local idx = arg[1]
        if t[idx] == nil then
            t[idx] = {}
        end
        -- modify argument list for next recursion level
        -- throw away first idx in params
        table.remove(arg, 1)
        -- recurse
        t[idx] = DeepValuePut(t[idx], value, insert, lastIdx, unpack(arg))
        return t
    elseif lastIdx ~= nil then
        return DeepValuePut(t, value, insert, nil, lastIdx)
    else
        if insert then
            t[#t + 1] = value
            return t
        else
            return value
        end
    end
end

function ToLog(val, mode, k1, k2, k3, k4, k5, k6, k7, modedetail)
	
	-- Optimization
	local LocLog = Log
	local LocLogKey = LogKey

	if mode == 1 then
        LocLog = DeepValuePut(LocLog, val, true, nil, k1, k2, k3, k4, k5, k6, k7)
        LocLogKey = DeepValuePut(LocLogKey, true, nil, val, k1, k2, k3, k4, k5, k6, k7)
	end
	if mode == 2 then
        LocLog = DeepValuePut(LocLog, val, false, nil, k1, k2, k3, k4, k5, k6, k7)
        LocLogKey = DeepValuePut(LocLogKey, GameTime, false, nil, k1, k2, k3, k4, k5, k6, k7)
	end
	if mode > 2 then
		local modekey
		if mode == 3 then
			modekey = GameTime
			if modedetail ~= nil then
				modekey = GameTime + modedetail
			end
		elseif mode == 4 then
			modekey = TotalRounds
			if modedetail ~= nil then
				modekey = TotalRounds + modedetail
			end
		elseif mode == 5 then
			modekey = TurnTimeLeft
			if modedetail ~= nil then
				modekey = TurnTimeLeft + modedetail
			end
		elseif mode == 6 then
			modekey = WaterLine
			if modedetail ~= nil then
				modekey = WaterLine + modedetail
			end
		end
		if val ~= nil then
            LocLog = DeepValuePut(LocLog, val, false, modekey, k1, k2, k3, k4, k5, k6, k7)
            LocLogKey = DeepValuePut(LocLogKey, GameTime, false, modekey, k1, k2, k3, k4, k5, k6, k7)
        end
	end
	
	Log = LocLog
	LogKey = LocLogKey
	
end

  sheepluva <- me  my code stats -> 
a Hedgewars Developer


   <- where I'm from  what I speak -> 

UltiMaxKom
UltiMaxKom's picture
User offline. Last seen 4 years 10 weeks ago. Offline
Joined: 2016-06-26
Posts: 381

Thank you very much sheepluva =DDDD!!!
I bet this was what I've search for (Sure it is, and will absolutely help me =))

I'll try to learn it in any way!
While recursive word is unfamiliar with me
Anyway, I've use this method like the so called recursive stuff in some scripts
Without knowing its name x)
But I think mine is quite "Ancient" and "Messy"
Hope will improve after read these =]

Well, your code seems really short, real short
My IQ isn't enough I guess x)
I think there are much to learn, my level still too low isn't it?
I just write my work rarely, maybe I need some milk here and do more with this narrow time
And get an internet connection... xD

After all
Information is expesive these days, lucky to get this knowledge =D
And it was an Awesome help for my problem! Thanks again
No wonder, its sheepluva !^^!

PS: Haven't test it yet, so I maybe update this comment later...

╟───NW──────┼──────N╢

╓──────────────────╖ ╓──────────────────╖
⠀HP: ██████████ 1E9/1E91E9/1E9 ██████████ :MP
╙──────────────────╜ ╙──────────────────╜

Copyright © 2004-2023 Hedgewars Project. All rights reserved. [ contact ]