forked from VANCOLD/fm_drive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FM_MAPVOTE_PREVIOUS.sma
151 lines (118 loc) · 3.39 KB
/
FM_MAPVOTE_PREVIOUS.sma
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
#include "feckinmad/fm_global"
#include "feckinmad/fm_time"
#include "feckinmad/fm_mapfile_api"
enum ePreviousMap_t
{
m_sMapName[MAX_MAP_LEN],
m_iLastPlayed,
m_iLengthPlayed
}
new const g_sPreviousFile[] = "fm_previousmaps.dat"
new const g_iPreviousTimeLimit = 7200 // 2 hours
new Array:g_PreviousMapList
new g_iPreviousMapCount
new g_sCurrentMap[MAX_MAP_LEN]
new g_iStartTime
public plugin_init()
{
fm_RegisterPlugin()
get_mapname(g_sCurrentMap, charsmax(g_sCurrentMap))
g_iStartTime = get_systime()
g_PreviousMapList = ArrayCreate(ePreviousMap_t)
ReadPreviousMapFile()
}
ReadPreviousMapFile()
{
fm_DebugPrintLevel(1, "ReadPreviousMapFile()")
new sFile[128]; get_localinfo("amxx_datadir", sFile, charsmax(sFile))
format(sFile, charsmax(sFile), "%s/%s", sFile, g_sPreviousFile)
new iFileHandle = fopen(sFile, "rb")
if (!iFileHandle)
{
return 0
}
new iCount; fread(iFileHandle, iCount, BLOCK_INT)
new Buffer[ePreviousMap_t]
for (new i = 0; i < iCount; i++)
{
fread_blocks(iFileHandle, Buffer, ePreviousMap_t, BLOCK_INT)
fm_DebugPrintLevel(2, "%s: %d - %d = %d", Buffer[m_sMapName], get_systime(), Buffer[m_iLastPlayed], get_systime() - Buffer[m_iLastPlayed])
if (get_systime() - Buffer[m_iLastPlayed] < g_iPreviousTimeLimit)
{
ArrayPushArray(g_PreviousMapList, Buffer)
g_iPreviousMapCount++
}
}
fclose(iFileHandle)
log_amx("Read %d previous maps from \"%s\"", g_iPreviousMapCount, sFile)
return 1
}
public plugin_end()
{
WritePreviousMapFile()
if (g_PreviousMapList != Invalid_Array)
{
ArrayDestroy(g_PreviousMapList)
}
}
WritePreviousMapFile()
{
fm_DebugPrintLevel(1, "WritePreviousMapFile()")
new sFile[128]; get_localinfo("amxx_datadir", sFile, charsmax(sFile))
format(sFile, charsmax(sFile), "%s/%s", sFile, g_sPreviousFile)
new iFileHandle = fopen(sFile, "wb")
if (!iFileHandle)
{
fm_WarningLog(FM_FOPEN_WARNING, sFile)
return 0
}
fwrite(iFileHandle, g_iPreviousMapCount + 1, BLOCK_INT)
new Buffer[ePreviousMap_t]
for (new i = 0; i < g_iPreviousMapCount; i++)
{
ArrayGetArray(g_PreviousMapList, i, Buffer)
fwrite_blocks(iFileHandle, Buffer, ePreviousMap_t, BLOCK_INT)
}
Buffer[m_iLastPlayed] = get_systime()
Buffer[m_iLengthPlayed] = Buffer[m_iLastPlayed] - g_iStartTime
copy(Buffer[m_sMapName], MAX_MAP_LEN - 1, g_sCurrentMap)
fwrite_blocks(iFileHandle, Buffer, ePreviousMap_t, BLOCK_INT)
fclose(iFileHandle)
log_amx("Wrote %d previous maps to \"%s\"", g_iPreviousMapCount + 1, sFile)
return 1
}
public plugin_natives()
{
register_native("fm_IsPreviousMap", "Native_IsPreviousMap")
register_library("fm_mapvote_previous")
}
public Native_IsPreviousMap()
{
fm_DebugPrintLevel(1, "Native_IsPreviousMap()")
new sMap[MAX_MAP_LEN]; get_string(1, sMap, charsmax(sMap))
new Buffer[ePreviousMap_t]
for (new i = 0; i < g_iPreviousMapCount; i++)
{
ArrayGetArray(g_PreviousMapList, i, Buffer)
if (equali(sMap, Buffer[m_sMapName]))
{
return 1
}
}
return 0
}
public fm_CanUserNominate(id, sMap[])
{
new Buffer[ePreviousMap_t]
for (new i = 0; i < g_iPreviousMapCount; i++)
{
ArrayGetArray(g_PreviousMapList, i, Buffer)
if (equali(sMap, Buffer[m_sMapName]))
{
new sTime[64]; fm_SecondsToText(get_systime() - Buffer[m_iLastPlayed], sTime, charsmax(sTime))
client_print(id, print_chat, "* You cannot nominate \"%s\" because it was played recently (%s ago)", sMap, sTime)
return PLUGIN_HANDLED
}
}
return PLUGIN_CONTINUE
}