summaryrefslogtreecommitdiff
path: root/sourcemod/scripting/gokz-localdb/db/timer_setup.sp
blob: b123eeb17d4caf6aaec4b2c3f2c302be5f5379c6 (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
155
156
157
158
159
160
161
162
163
164
165
166
167

// ===== [ SAVE TIMER SETUP ] =====

void DB_SaveTimerSetup(int client)
{
	bool txnHasQuery = false;
	int course;
	float position[3], angles[3];
	
	if (!IsValidClient(client))
	{
		return;
	}
	
	int steamid = GetSteamAccountID(client);
	DataPack data = new DataPack();
	
	data.WriteCell(client);
	data.WriteCell(steamid);
	
	char query[1024];
	Transaction txn = SQL_CreateTransaction();
	
	if (GOKZ_GetStartPosition(client, position, angles) == StartPositionType_Custom)
	{
		FormatEx(query, sizeof(query), sql_startpos_upsert, steamid, gI_DBCurrentMapID, position[0], position[1], position[2], angles[0], angles[1]);
		txn.AddQuery(query);
		txnHasQuery = true;
	}
	
	course = GOKZ_GetVirtualButtonPosition(client, position, true);
	if (course != -1)
	{
		FormatEx(query, sizeof(query), sql_vbpos_upsert, steamid, gI_DBCurrentMapID, position[0], position[1], position[2], course, 1);
		txn.AddQuery(query);
		txnHasQuery = true;
	}
	
	course = GOKZ_GetVirtualButtonPosition(client, position, false);
	if (course != -1)
	{
		FormatEx(query, sizeof(query), sql_vbpos_upsert, steamid, gI_DBCurrentMapID, position[0], position[1], position[2], course, 0);
		txn.AddQuery(query);
		txnHasQuery = true;
	}
	
	if (txnHasQuery)
	{
		SQL_ExecuteTransaction(gH_DB, txn, DB_TxnSuccess_SaveTimerSetup, DB_TxnFailure_Generic_DataPack, data, DBPrio_Low);
	}
	else
	{
		delete data;
		delete txn;
	}
}

public void DB_TxnSuccess_SaveTimerSetup(Handle db, DataPack data, int numQueries, Handle[] results, any[] queryData)
{
	data.Reset();
	int client = data.ReadCell();
	int steamid = data.ReadCell();
	delete data;
	
	if (!IsValidClient(client) || steamid != GetSteamAccountID(client))
	{
		return;
	}
	
	GOKZ_PrintToChat(client, true, "%t", "Timer Setup Saved");
}



// ===== [ LOAD TIMER SETUP ] =====

void DB_LoadTimerSetup(int client, bool doChatMessage = false)
{
	if (!IsValidClient(client))
	{
		return;
	}
	
	int steamid = GetSteamAccountID(client);
	
	DataPack data = new DataPack();
	data.WriteCell(client);
	data.WriteCell(steamid);
	data.WriteCell(doChatMessage);
	
	char query[1024];
	Transaction txn = SQL_CreateTransaction();
	
	// Virtual Buttons
	FormatEx(query, sizeof(query), sql_vbpos_get, steamid, gI_DBCurrentMapID);
	txn.AddQuery(query);
	
	// Start Position
	FormatEx(query, sizeof(query), sql_startpos_get, steamid, gI_DBCurrentMapID);
	txn.AddQuery(query);
	
	SQL_ExecuteTransaction(gH_DB, txn, DB_TxnSuccess_LoadTimerSetup, DB_TxnFailure_Generic_DataPack, data, DBPrio_Normal);
}

public void DB_TxnSuccess_LoadTimerSetup(Handle db, DataPack data, int numQueries, DBResultSet[] results, any[] queryData)
{
	data.Reset();
	int client = data.ReadCell();
	int steamid = data.ReadCell();
	bool doChatMessage = data.ReadCell();
	delete data;
	
	if (!IsValidClient(client) || steamid != GetSteamAccountID(client))
	{
		return;
	}
	
	int course;
	bool isStart, vbSetup = false;
	float position[3], angles[3];
	
	if (results[0].RowCount > 0 && results[0].FetchRow())
	{
		position[0] = results[0].FetchFloat(TimerSetupDB_GetVBPos_PositionX);
		position[1] = results[0].FetchFloat(TimerSetupDB_GetVBPos_PositionY);
		position[2] = results[0].FetchFloat(TimerSetupDB_GetVBPos_PositionZ);
		course = results[0].FetchInt(TimerSetupDB_GetVBPos_Course);
		isStart = results[0].FetchInt(TimerSetupDB_GetVBPos_IsStart) == 1;
		
		GOKZ_SetVirtualButtonPosition(client, position, course, isStart);
		vbSetup = true;
	}
	
	if (results[0].RowCount > 1 && results[0].FetchRow())
	{
		position[0] = results[0].FetchFloat(TimerSetupDB_GetVBPos_PositionX);
		position[1] = results[0].FetchFloat(TimerSetupDB_GetVBPos_PositionY);
		position[2] = results[0].FetchFloat(TimerSetupDB_GetVBPos_PositionZ);
		course = results[0].FetchInt(TimerSetupDB_GetVBPos_Course);
		isStart = results[0].FetchInt(TimerSetupDB_GetVBPos_IsStart) == 1;
		
		GOKZ_SetVirtualButtonPosition(client, position, course, isStart);
		vbSetup = true;
	}
	
	if (results[1].RowCount > 0 && results[1].FetchRow())
	{
		position[0] = results[1].FetchFloat(TimerSetupDB_GetStartPos_PositionX);
		position[1] = results[1].FetchFloat(TimerSetupDB_GetStartPos_PositionY);
		position[2] = results[1].FetchFloat(TimerSetupDB_GetStartPos_PositionZ);
		angles[0] = results[1].FetchFloat(TimerSetupDB_GetStartPos_Angle0);
		angles[1] = results[1].FetchFloat(TimerSetupDB_GetStartPos_Angle1);
		angles[2] = 0.0;
		
		GOKZ_SetStartPosition(client, StartPositionType_Custom, position, angles);
	}
	
	if (vbSetup)
	{
		GOKZ_LockVirtualButtons(client);
	}
	
	if (doChatMessage)
	{
		GOKZ_PrintToChat(client, true, "%t", "Timer Setup Loaded");
	}
}