summaryrefslogtreecommitdiff
path: root/sourcemod/scripting/gokz-localranks/db/print_average.sp
blob: 7f7c4e42bb7b00713102472ee41a708dd47691ef (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
/*
	Gets the average personal best time of a course.
*/



void DB_PrintAverage(int client, int mapID, int course, int mode)
{
	char query[1024];
	
	DataPack data = new DataPack();
	data.WriteCell(GetClientUserId(client));
	data.WriteCell(course);
	data.WriteCell(mode);
	
	Transaction txn = SQL_CreateTransaction();
	
	// Retrieve Map Name of MapID
	FormatEx(query, sizeof(query), sql_maps_getname, mapID);
	txn.AddQuery(query);
	// Check for existence of map course with that MapID and Course
	FormatEx(query, sizeof(query), sql_mapcourses_findid, mapID, course);
	txn.AddQuery(query);
	// Get Average PB Time
	FormatEx(query, sizeof(query), sql_getaverage, mapID, course, mode);
	txn.AddQuery(query);
	// Get Average PRO PB Time
	FormatEx(query, sizeof(query), sql_getaverage_pro, mapID, course, mode);
	txn.AddQuery(query);
	
	SQL_ExecuteTransaction(gH_DB, txn, DB_TxnSuccess_PrintAverage, DB_TxnFailure_Generic_DataPack, data, DBPrio_Low);
}

public void DB_TxnSuccess_PrintAverage(Handle db, DataPack data, int numQueries, Handle[] results, any[] queryData)
{
	data.Reset();
	int client = GetClientOfUserId(data.ReadCell());
	int course = data.ReadCell();
	int mode = data.ReadCell();
	delete data;
	
	if (!IsValidClient(client))
	{
		return;
	}
	
	char mapName[33];
	int mapCompletions, mapCompletionsPro;
	float averageTime, averageTimePro;
	
	// Get Map Name from results
	if (SQL_FetchRow(results[0]))
	{
		SQL_FetchString(results[0], 0, mapName, sizeof(mapName));
	}
	if (SQL_GetRowCount(results[1]) == 0)
	{
		if (course == 0)
		{
			GOKZ_PrintToChat(client, true, "%t", "Main Course Not Found", mapName);
		}
		else
		{
			GOKZ_PrintToChat(client, true, "%t", "Bonus Not Found", mapName, course);
		}
		return;
	}
	
	// Get number of completions and average time
	if (SQL_FetchRow(results[2]))
	{
		mapCompletions = SQL_FetchInt(results[2], 1);
		if (mapCompletions > 0)
		{
			averageTime = GOKZ_DB_TimeIntToFloat(SQL_FetchInt(results[2], 0));
		}
	}
	
	// Get number of completions and average time (PRO)
	if (SQL_FetchRow(results[3]))
	{
		mapCompletionsPro = SQL_FetchInt(results[3], 1);
		if (mapCompletions > 0)
		{
			averageTimePro = GOKZ_DB_TimeIntToFloat(SQL_FetchInt(results[3], 0));
		}
	}
	
	// Print average time header to chat
	if (course == 0)
	{
		GOKZ_PrintToChat(client, true, "%t", "Average Time Header", mapName, gC_ModeNamesShort[mode]);
	}
	else
	{
		GOKZ_PrintToChat(client, true, "%t", "Average Time Header (Bonus)", mapName, course, gC_ModeNamesShort[mode]);
	}
	
	if (mapCompletions == 0)
	{
		CPrintToChat(client, "%t", "No Times Found");
	}
	else if (mapCompletionsPro == 0)
	{
		CPrintToChat(client, "%t, %t", 
			"Average Time - NUB", GOKZ_FormatTime(averageTime), mapCompletions, 
			"Average Time - No PRO Time");
	}
	else
	{
		CPrintToChat(client, "%t, %t", 
			"Average Time - NUB", GOKZ_FormatTime(averageTime), mapCompletions, 
			"Average Time - PRO", GOKZ_FormatTime(averageTimePro), mapCompletionsPro);
	}
}

void DB_PrintAverage_FindMap(int client, const char[] mapSearch, int course, int mode)
{
	DataPack data = new DataPack();
	data.WriteCell(GetClientUserId(client));
	data.WriteString(mapSearch);
	data.WriteCell(course);
	data.WriteCell(mode);
	
	DB_FindMap(mapSearch, DB_TxnSuccess_PrintAverage_FindMap, data, DBPrio_Low);
}

public void DB_TxnSuccess_PrintAverage_FindMap(Handle db, DataPack data, int numQueries, Handle[] results, any[] queryData)
{
	data.Reset();
	int client = GetClientOfUserId(data.ReadCell());
	char mapSearch[33];
	data.ReadString(mapSearch, sizeof(mapSearch));
	int course = data.ReadCell();
	int mode = data.ReadCell();
	delete data;
	
	if (!IsValidClient(client))
	{
		return;
	}
	
	if (SQL_GetRowCount(results[0]) == 0)
	{
		GOKZ_PrintToChat(client, true, "%t", "Map Not Found", mapSearch);
		return;
	}
	else if (SQL_FetchRow(results[0]))
	{  // Result is the MapID
		DB_PrintAverage(client, SQL_FetchInt(results[0], 0), course, mode);
	}
}