Half-Life Icon HL Script

[AMXX][ALL] Rank 3.0

SEX! Ok, we got your attention. We are one of the oldest mod sites on the Interwebs. Sign up and maximize your browsing experience.

  • Subscribe: Get notified of new submissions like this.
  • Say Thanks: Show your appreciation by sending Rj. points.
  • Post: Give Rj. your thoughts on this Script.
  • Vote: Help Rj. win the Monthly Awards.
  • Rate: Give this Script a rating out of 10.
  • Flag: Alert moderators and warn members of a problem with this Script.
  • Watch: Get notified when this Script is updated.
Mascot

Stop Lurking!

Sign up

Already a Bananite? Login

289 lines | 6 kb | Raw Code
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <amxmodx>
#include <amxmisc>

//#define HTML_IN_MOTD
#define SAVE_RANKS_AFTER_SORT
#define SORT_INTERVAL 15.0

enum RankData
{
        Data_SteamID[32],
        Data_Name[32],
        Data_Kills,
        Data_Deaths
};

new g_File[64];
new Array:g_SteamID;
new Trie:g_Name, Trie:g_Kills, Trie:g_Deaths;
new g_Top15[2048];
new bool:g_Sort = true;
new g_Data[33][RankData], bool:g_Authorized[33];

public plugin_init()
{
        register_plugin("Rank", "3.0", "hleV");

        get_datadir(g_File, 63);
        add(g_File, 63, "/ranks.ini");

        g_SteamID = ArrayCreate(32, 1);
        g_Name = TrieCreate();
        g_Kills = TrieCreate();
        g_Deaths = TrieCreate();

        LoadRanks();
        ArraySort(g_SteamID, "SortRanks");
        WriteTop15();

        set_task(SORT_INTERVAL, "SortTask", _, _, _, "b");

        register_clcmd("say /rank", "SayRank");
        register_clcmd("say /top15", "SayTop15");

        register_event("DeathMsg", "DeathMsg", "a");
}

public plugin_end()
{
        ArraySort(g_SteamID, "SortRanks");
        SaveRanks();

        ArrayDestroy(g_SteamID);
}

public client_putinserver(Client)
{
        get_user_authid(Client, g_Data[Client][Data_SteamID], 31);

        if (!str_to_num(g_Data[Client][Data_SteamID][10]))
                return;

        get_user_name(Client, g_Data[Client][Data_Name], 31);

        if (!TrieKeyExists(g_Name, g_Data[Client][Data_SteamID]))
                AddRank(Client);
        else
                LoadData(Client);

        UpdateRank(Client, true);

        g_Authorized[Client] = true;
}

public client_disconnect(Client)
        g_Authorized[Client] = false;

public client_infochanged(Client)
{
        if (!g_Authorized[Client])
                return;

        static Name[32];
        get_user_info(Client, "name", Name, 31);

        if (equal(Name, g_Data[Client][Data_Name]))
                return;

        copy(g_Data[Client][Data_Name], 31, Name);
        UpdateRank(Client, true);
}

public SayRank(Client)
{
        new Position = GetPosition(Client);

        if (!g_Authorized[Client] || !Position)
        {
                client_print(Client, print_chat, "* You are not ranked.");

                return;
        }

        client_print
        (
                Client,
                print_chat,
                "* Your rank is %d of %d with %d kills and %d deaths.",
                Position,
                ArraySize(g_SteamID),
                g_Data[Client][Data_Kills],
                g_Data[Client][Data_Deaths]
        );
}

public SayTop15(Client)
        show_motd(Client, g_Top15, "Top 15");

public DeathMsg()
{
        new Killer = read_data(1);
        new Victim = read_data(2);

        if (g_Authorized[Victim])
        {
                g_Data[Victim][Data_Deaths]++;
                g_Sort = true;

                UpdateRank(Victim, false);
        }

        if (g_Authorized[Killer] && Killer != Victim)
        {
                g_Data[Killer][Data_Kills]++;
                g_Sort = true;

                UpdateRank(Killer, false);
        }
}

public SortTask()
{
        if (!g_Sort)
                return;

        ArraySort(g_SteamID, "SortRanks");
        WriteTop15();

#if defined SAVE_RANKS_AFTER_SORT
        SaveRanks();
#endif
}

public SortRanks(Array:SteamID, Position1, Position2)
{
        static SteamID1[32];
        ArrayGetString(SteamID, Position1, SteamID1, 31);

        static SteamID2[32];
        ArrayGetString(SteamID, Position2, SteamID2, 31);

        static Kills1;
        TrieGetCell(g_Kills, SteamID1, Kills1);

        static Kills2;
        TrieGetCell(g_Kills, SteamID2, Kills2);

        static Deaths1;
        TrieGetCell(g_Deaths, SteamID1, Deaths1);

        static Deaths2;
        TrieGetCell(g_Deaths, SteamID2, Deaths2);

        if (Kills1 - Deaths1 < Kills2 - Deaths2)
                return 1;
        else if (Kills1 - Deaths1 > Kills2 - Deaths2)
                return -1;

        return 0;
}

LoadRanks()
{
        new File = fopen(g_File, "r");

        if (!File)
                return;

        new Data[96], SteamID[32], Name[32], Kills[16], Deaths[16];

        while (!feof(File))
        {
                fgets(File, Data, 96);

                if (!strlen(Data))
                        continue;

                parse(Data, SteamID, 31, Name, 31, Kills, 15, Deaths, 15);

                ArrayPushString(g_SteamID, SteamID);
                TrieSetString(g_Name, SteamID, Name);
                TrieSetCell(g_Kills, SteamID, str_to_num(Kills));
                TrieSetCell(g_Deaths, SteamID, str_to_num(Deaths));
        }

        fclose(File);
}

SaveRanks()
{
        new File = fopen(g_File, "w+");

        if (!File)
                return;

        for (new Position, Size = ArraySize(g_SteamID), SteamID[32], Name[32], Kills, Deaths; Position < Size; Position++)
        {
                ArrayGetString(g_SteamID, Position, SteamID, 31);
                TrieGetString(g_Name, SteamID, Name, 31);
                TrieGetCell(g_Kills, SteamID, Kills);
                TrieGetCell(g_Deaths, SteamID, Deaths);

                fprintf(File, "%s ^"%s^" %d %d^n", SteamID, Name, Kills, Deaths);
        }

        fclose(File);
}

AddRank(Client)
{
        g_Data[Client][Data_Kills] = 0;
        g_Data[Client][Data_Deaths] = 0;

        ArrayPushString(g_SteamID, g_Data[Client][Data_SteamID]);
        TrieSetString(g_Name, g_Data[Client][Data_SteamID], g_Data[Client][Data_Name]);
}

LoadData(Client)
{
        TrieGetCell(g_Kills, g_Data[Client][Data_SteamID], g_Data[Client][Data_Kills]);
        TrieGetCell(g_Deaths, g_Data[Client][Data_SteamID], g_Data[Client][Data_Deaths]);
}

UpdateRank(Client, bool:Name)
{
        if (Name)
                TrieSetString(g_Name, g_Data[Client][Data_SteamID], g_Data[Client][Data_Name]);

        TrieSetCell(g_Kills, g_Data[Client][Data_SteamID], g_Data[Client][Data_Kills]);
        TrieSetCell(g_Deaths, g_Data[Client][Data_SteamID], g_Data[Client][Data_Deaths]);
}

GetPosition(Client)
{
        static Position, Size, SteamID[32];

        for (Position = 0, Size = ArraySize(g_SteamID); Position < Size; Position++)
        {
                ArrayGetString(g_SteamID, Position, SteamID, 31);

                if (equal(SteamID, g_Data[Client][Data_SteamID]))
                        return Position + 1;
        }

        return 0;
}      

WriteTop15()
{
#if defined HTML_IN_MOTD
        static const Header[] = "<body bgcolor=#000000><font color=#FFB000><pre>%5s  %22s  %5s  %5s^n^n";
        static const Buffer[] = "%4d   %22s  %5d  %6d^n";
#else
        static const Header[] = "%5s  %22s  %5s  %5s^n^n";
        static const Buffer[] = "%5d   %22s  %5d  %5d^n";
#endif

        static Length, Position, Size, SteamID[32], Name[32], Kills, Deaths;
        Length = formatex(g_Top15[Length], 2047 - Length, Header, "Rank", "Name", "Kills", "Deaths");

        for (Position = 0, Size = min(ArraySize(g_SteamID), 15); Position < Size; Position++)
        {
                ArrayGetString(g_SteamID, Position, SteamID, 31);
                TrieGetString(g_Name, SteamID, Name, 31);
                TrieGetCell(g_Kills, SteamID, Kills);
                TrieGetCell(g_Deaths, SteamID, Deaths);

                Length += formatex(g_Top15[Length], 2047 - Length, Buffer, Position + 1, Name, Kills, Deaths);
        }
}
Well, this is rank for all Half-Life-based games that share DeathMsg event.

Commands:
  • say /rank - displays your current rank, rank count, kills and deaths;
  • say /top15 - displays top 15 ranked players in a MOTD.
Notes:
  • rank is based on "kills - deaths" calculation;
  • ranks are saved in data/ranks.ini file. Line numbers mean the actual position;
  • sadly MOTD fails to align kills and deaths well, sorry;
  • non-Steam players are not ranked;
  • team kills are not checked.
Code and Top15 MOTD optimization suggestions are welcome. 

Changelog:
Quote:
3.0
-completely rewriten code;
-changed rank data storing and sorting method to Arrays;
-removed rank data size limit.

2.0
-completely rewriten code;
-changed/upgraded sorting method;
-changed the way names are stored.

1.4
-optimized sorting;
-sorting on say /rank and say /top15 changed with sorting each 15 seconds (see #define SORT_INTERVAL 15.0).

1.3
-optimized parts of the code.

1.2
-added top 15 support for games that have HTML in MOTD. Check #define TOP15 HTML; in .sma;
-optimized rank calculation variables' creation.

1.1
-fixed kill not being added when victim is not authorized.

Posts

None found

Share

Shareable Image:
Share banner
HTML embed code:
BB embed code:
Markdown embed code:

More embeddable images

Bookmark and Share

Credits

Original Authors
HLEV
HLEV Offline
Member Joined 9y
AMXX Script/Codes

Submitter

Rj. avatar
Rj. avatar Dreamin'
Member Joined 10mo
655 points Ranked 6621st
35 medals 4 legendary 9 rare
  • Submitted 15 Tools Medal icon
  • Submitted 50 Gamefiles Medal icon
  • 15+ Entries! GameBanana’s Christmas Giveaway 2016 Medal icon
  • Thanked 500 submitters Medal icon
  • 2nd Place - Game Poetry Contest Medal icon
  • Submitted 5 Tools Medal icon

Rj. avatar
Rj.

Redistributor
Are you the owner? Request Ownership
Send a Private Message to the submitter

Category

Attributes

Language
Pawn (AMXMODX)

Stats

Posts
0
Views
44
Date Added
4d