summaryrefslogtreecommitdiff
path: root/SuperTimer/SuperTimer.lua
blob: d93dbbda18a667c1bdb7e10150d79a9eff85b7ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
-- check yor installed steam mods for reference here: \steamapps\workshop\content\289070
-- events: -- https://forums.civfanatics.com/threads/list-of-game-events.604914/#post-14561939 
-- put your mod in: C:\Users\Miguel\Documents\My Games\Sid Meier's Civilization VI\Mods\SuperTimer
-- lua log is here: /Documents and Settings/Miguel/My Documents/My Games/Sid Meier's Civilization VI/Logs
-- Icon codes: https://forums.civfanatics.com/threads/list-of-all-icon-codes.613516/
-- lua objects: https://forums.civfanatics.com/threads/lua-objects.601146/
-- persistance:
-- they're not saved, but see
-- https://forums.civfanatics.com/threads/lua-objects.601146/page-4#post-14634018
-- https://forums.civfanatics.com/threads/saving-loading-simple-tables-with-a-game.609397/
-- https://forums.civfanatics.com/threads/persisting-custom-data-with-saves.611878/
-- this lua file, will be auto-reloaded on change by civ6

verbose = false

function log( txt )
	if verbose then print ( '[' .. os.time() .. ']' .. " [SuperTimer] " .. txt ); end
end

function getState()
	state = GameConfiguration.GetValue ("SuperTimerState")
	if state == nil
		then 
			GameConfiguration.SetValue ("SuperTimerState","")
			return ""
		else 
			return state
	end
end

function split(s, delimiter)
    result = {};
    for match in (s..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match);
    end
    return result;
end

function toEvent(event, turn)
	if event == "B"	then return "Turn " .. turn .. " begins" end
	if event == "E"	then return "Turn ends" end
	if event == "S"	then return "Game Saved" end
	if event == "L"	then return "Game Loaded" end
	return "Unknown Event"
end

function addState( state, turn )

	newstate = getState() .. os.time() .. " " .. turn .. " " .. state .. "|";
	GameConfiguration.SetValue ("SuperTimerState",newstate);
	tooltip = "[ICON_Capital]Miguel's SuperTimer[ICON_Capital][NEWLINE]"

	lastturn=0
	lastevent=0;
	sumtime=0;
	
	for m in (newstate):gmatch("(.-)".."|") do
		spl = split(m," ")
		unixtime = tonumber(spl[1]);
		turn = tonumber(spl[2]);
		event = spl[3];
		if event == "B" then lastturn = turn - 1 end
		if event == "L"
			then
				lastevent = unixtime;
			else
				sumtime = sumtime + ( unixtime-lastevent );
				log (sumtime);
				lastevent = unixtime;
			end

    		t = os.date("*t", unixtime)
	        txt = toEvent(event,spl[2]);
		log (t.hour .. ":" .. t.min .. ":"  .. t.sec .. " " .. txt)
        end
	log("Total: " .. sumtime .. " seconds");
	log("Total: " .. os.date('!%T', sumtime));
	if lastturn == 0
	then 
		tooltip = "[ICON_Capital]Miguel's SuperTimer[ICON_Capital]" .. 
			  "[NEWLINE]Complete first turn to collect data"
	else
		tooltip = "[ICON_Capital]Miguel's SuperTimer[ICON_Capital]" .. 
			  "[NEWLINE]Turns Completed:" .. lastturn ..
			  "[NEWLINE]Total Time: " .. os.date('!%T', sumtime) ..
			  "[NEWLINE]Average Turn: " ..  os.date('!%T', sumtime/lastturn);
	end

	if Controls.SuperTimerButton ~= nil
		then
			Controls.SuperTimerButton:SetToolTipString (tooltip);
		end
end


function mouseOverTimer()
	log ( "mouseOverTimer" );
end


function Initialise()
	log ( "Game started/loaded" );
	addState( "L" , 0 );
	local topPanel = ContextPtr:LookUpControl("/InGame/TopPanel/RightContents");
        Controls.SuperTimerButton:ChangeParent(topPanel);
	topPanel:AddChildAtIndex(Controls.SuperTimerButton, 0);
	topPanel:CalculateSize();
        topPanel:ReprocessAnchoring();
    	Controls.SuperTimerButton:RegisterCallback( Mouse.eMouseEnter, mouseOverTimer);
end

function OnTurnBegin( turn )
	addState( "B" , turn )
end

function OnTurnEnd()
	addState( "E" , 0 );
end

function OnPlayerTurnEnd( player )
end

function SaveList()
    log( "Show Savegame List" );
    addState( "S" , 0 );
end

function SaveButton ( actionID )
    log( "InputAction" );
    if actionID == Input.GetActionId("QuickSave") then
    	addState( "S" , 0 );
    end
end

function SaveKey ( pInputStruct:table )
    log( "InputHandler" );
    local uiMsg = pInputStruct:GetMessageType();
    if uiMsg == KeyEvents.KeyDown then
        if pInputStruct:GetKey() == Keys.VK_F5 then -- but the binding can be changed...
    	    addState( "S" , 0 );
        end
    end
end

log ( "Mod Loaded" );

--- EVENTS
LuaEvents.FileListQueryComplete.Add( SaveList );
Events.InputActionTriggered.Add( SaveButton );
ContextPtr:SetInputHandler( SaveKey, true ); -- does not work ?
Events.LoadScreenClose.Add( Initialise );
Events.TurnBegin.Add( OnTurnBegin );
Events.LocalPlayerTurnEnd.Add( OnTurnEnd );
Events.RemotePlayerTurnEnd.Add( OnPlayerTurnEnd );