summaryrefslogtreecommitdiff
path: root/sourcemod/scripting/gem_damage_report.sp
blob: 97ab187dad9cd544ae458887dea705233f00dcab (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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/*
  Description:
  When you die or round ends and you are still alive
  A left side menu shows who and how much you did damage to and who damaged you.
  
  Developed: 
  2007-12-20
  
  By:
  [30+]Gemeni (gemeni@30plus.ownit.se)
  
  Version history:
  1.0.0 - First Version
  1.0.1 - Added number of hits
  1.0.2 - Added hitgroup information
  1.0.3 - Created a long and short version of the report. 
          If the Long version that includes hitgroups does not fit 
          in the panel, the short version is displayed
          Added coloring in the panel
  1.1.0 - Added option so you can turn damage report on and off, and also
          decide if it should be printed in menu or chat. Use /damage_report help to get info
          Big thanks to death_beam team since I used their code to learn about
          KeyValues!
  1.1.1 - Stripped chat output. 
          Resetting damage data on round start
  1.1.2 - Added total dmg taken/given and X indicator for kill/killed
  1.1.3 - Removed errors from translation stuff that was not completed
          Still looking at getting translation to work
  1.1.5 - Added support to show again the last report that was shown to a player
  1.1.6 - Fixed saving of settings on map end
  1.1.7 - Problem with bogus entries in damagereportsetting file
  1.1.8 - Added most kills info
  1.1.9 - Fixed memory leak
  1.1.10 - Checking if player still ingame before trying to display.
  1.1.11 - Sometimes it seems that the settings for the users isnt written to file. Forcing this more often now.
  1.1.12 - Making sure damage data is not shown to surviving players if they have switched it off.
  1.1.13 - Added extra option, /damage_report long|short - If hitboxdata should be shown or not
*/

#pragma semicolon 1
#include <sourcemod>
#include <sdktools>

// Definitions
#define MAXHITGROUPS 7
#define NROFOPTIONS 3
#define MAX_FILE_LEN 80

#define DrON  1
#define DrOFF 0
#define DrPop 1
#define DrChat 0
#define DrLong 1
#define DrShort 0

#define propOnOff 0
#define propPopChat 1
#define propShortLong 2

#define PLUGIN_VERSION "1.1.13"

public Plugin:myinfo =
{
  name = "Damage report",
  author = "[30+]Gemeni, modified by networkheaven.net",
  description = "Reports who damaged you and who you damaged",
  version = PLUGIN_VERSION,
  url = "http://30plus.ownit.se/, networkheaven.net"
};

// Global variables
new g_DamageDone[MAXPLAYERS+1][MAXPLAYERS+1];
new g_HitsDone[MAXPLAYERS+1][MAXPLAYERS+1];
new g_HitboxDone[MAXPLAYERS+1][MAXPLAYERS+1][MAXHITGROUPS+1];

new g_DamageTaken[MAXPLAYERS+1][MAXPLAYERS+1];
new g_HitsTaken[MAXPLAYERS+1][MAXPLAYERS+1];
new g_HitboxTaken[MAXPLAYERS+1][MAXPLAYERS+1][MAXHITGROUPS+1];

new String:g_PlayerName[MAXPLAYERS+1][32];

new g_KilledPlayer[MAXPLAYERS+1][MAXPLAYERS+1];

new g_PlayerDROption[MAXPLAYERS+1][NROFOPTIONS];
new String:g_filenameSettings[MAX_FILE_LEN];
new Handle:KVSettings = INVALID_HANDLE;

new g_defaultPropOnOff = DrON;
new g_defaultPropChatPop = DrChat;
new g_defaultPropShortLong = DrShort;

new bool:g_lateLoaded;

// History stats
new String:g_HistDamageDone[MAXPLAYERS+1][512];
new String:g_HistDamageDoneLong[MAXPLAYERS+1][512];
new g_HistTotalDamageDone[MAXPLAYERS+1];
new String:g_HistDamageTaken[MAXPLAYERS+1][512];
new String:g_HistDamageTakenLong[MAXPLAYERS+1][512];
new g_HistTotalDamageTaken[MAXPLAYERS+1];


new Handle:g_versionConVar;

new g_maxClients;

new String:g_HitboxName[MAXHITGROUPS+1][20];

// Variable for Temp fix
new g_MenuCleared[MAXPLAYERS+1];


public bool:AskPluginLoad(Handle:myself, bool:late, String:error[], err_max)
{
  g_lateLoaded = late;
  return true;
}

// Hook events on plugin start
public OnPluginStart(){
  g_versionConVar = CreateConVar("sm_damage_report_version", PLUGIN_VERSION, "Damage report version", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY);
  SetConVarString(g_versionConVar, PLUGIN_VERSION);
  HookEvent("player_death", Event_PlayerDeath);
  HookEvent("player_hurt", Event_PlayerHurt);
  HookEvent("player_spawn", Event_PlayerSpawn);
  HookEvent("round_end", Event_RoundEnd);
  HookEvent("round_start", EventRoundStart);
  
  HookEvent("player_disconnect", Event_PlayerDisconnect);
  HookEvent("player_connect", Event_PlayerConnect);
    
  RegConsoleCmd("damage_report", Command_DamageReport);
  RegConsoleCmd("last_damage_report", Command_LastDamageReport);
  RegConsoleCmd("ldr", Command_LastDamageReport);

  KVSettings=CreateKeyValues("DamageReportSetting");
  BuildPath(Path_SM, g_filenameSettings, MAX_FILE_LEN, "data/damagereportsetting.txt");
  if(!FileToKeyValues(KVSettings, g_filenameSettings))
  {
    KeyValuesToFile(KVSettings, g_filenameSettings);
  }       

  if(g_lateLoaded)
  {
    // Plugin was not loaded at beginning of round
    // Find settings for all players already connected
    for(new i = 1; i < g_maxClients; i++)
    {
      if(IsClientInGame(i) && !IsFakeClient(i))
      {
        FindSettingsForClient(i);
      }
    }
  }
  g_HitboxName[0] = "Body";
  g_HitboxName[1] = "Head";
  g_HitboxName[2] = "Chest";
  g_HitboxName[3] = "Stomach";
  g_HitboxName[4] = "Left arm";
  g_HitboxName[5] = "Right arm";
  g_HitboxName[6] = "Left leg";
  g_HitboxName[7] = "Right leg";
}

public Action:Command_LastDamageReport(client, args)
{
  // Dont do anything if command comes from server
  if (client == 0) {
    return Plugin_Handled;
  }
  
  if ((strcmp(g_HistDamageDone[client], "") != 0) || (strcmp(g_HistDamageTaken[client], "") != 0)) {
    DisplayDamageReport(client, g_HistDamageDone[client] ,g_HistDamageDoneLong[client] ,g_HistDamageTaken[client], g_HistDamageTakenLong[client], g_HistTotalDamageDone[client], g_HistTotalDamageTaken[client]);
  }
  else {
    PrintToChat(client, "\x04No history to display");
  }
    
  return Plugin_Handled;
}
public Action:Command_DamageReport(client, args)
{
  // Dont do anything if command comes from server
  if (client == 0) {
    return Plugin_Handled;
  }
  
  // If you send in to few or to many arguments. Tell them to ask for help 
  if (args != 1)
  {
    PrintToChat(client, "\x04Usage: /damage_report help");
    return Plugin_Handled;
  }
  
  new String:option[20];
  GetCmdArg(1, option, sizeof(option));
  
  if (strcmp(option, "on", false) == 0) {
    g_PlayerDROption[client][propOnOff] = DrON;
    PrintToChat(client, "\x04Damage report switched on");
  }
  else if (strcmp(option, "off", false) == 0) {
    g_PlayerDROption[client][propOnOff] = DrOFF;
    PrintToChat(client, "\x04Damage report switched off");
  }
  else if (strcmp(option, "popup", false) == 0) {
    g_PlayerDROption[client][propPopChat] = DrPop;
    PrintToChat(client, "\x04Damage report printed in popup menu");
  }
  else if (strcmp(option, "chat", false) == 0) {
    g_PlayerDROption[client][propPopChat] = DrChat;
    PrintToChat(client, "\x04Damage report printed in chat");
  }
  else if (strcmp(option, "short", false) == 0) {
    g_PlayerDROption[client][propShortLong] = DrShort;
    PrintToChat(client, "\x04Hitbox data will not be shown");
  }
  else if (strcmp(option, "long", false) == 0) {
    g_PlayerDROption[client][propShortLong] = DrLong;
    PrintToChat(client, "\x04Hitbox data will be shown");
  }
  else if (strcmp(option, "status", false) == 0) {
    if(g_PlayerDROption[client][propOnOff] == DrON) {
      PrintToChat(client, "\x04Damage report is switched on");
      if(g_PlayerDROption[client][propPopChat] == DrPop) {
            PrintToChat(client, "\x04Damage report is printed in popup menu");
      }
      else {
            PrintToChat(client, "\x04Damage report is printed in chat");
      }
      if(g_PlayerDROption[client][propShortLong] == DrShort) {
            PrintToChat(client, "\x04Hitbox data is not shown");
      }
      else {
            PrintToChat(client, "\x04Hitbox data is shown");
      }
    }
    else {
      PrintToChat(client, "\x04Damage report is switched off");
    }
  }
  else {
    new String:helpString1[200] = "/damage_report on (Turn it on) /damage_report off (Turn it off)\n";
    new String:helpString2[200] = "/damage_report popup (It will show as a left menu) /damage_report chat (It will show up in the chat)";
    new String:helpString3[200] = "/damage_report short (Will remove hitbox info)";
    new String:helpString4[200] = "/damage_report long (Will display hitbox info)";
    new String:helpString5[200] = "/damage_report status (See current damage report settings)";
    new String:helpString6[200] = "/last_damage_report or /ldr will display the last damage report that was shown to you";    
    PrintToChat(client, "\x04%s", helpString1);
    PrintToChat(client, "\x04%s", helpString2);
    PrintToChat(client, "\x04%s", helpString3);
    PrintToChat(client, "\x04%s", helpString4);
    PrintToChat(client, "\x04%s", helpString5);
    PrintToChat(client, "\x04%s", helpString6);
  }
  
  StoreSettingsForClient(client);
  
  return Plugin_Handled;
}


// Save all settings on map end
public OnMapEnd() {
  // Save user settings to a file
  KvRewind(KVSettings);
  KeyValuesToFile(KVSettings, g_filenameSettings);
  
  clearAllDamageData();
}

public OnMapStart() {
  g_maxClients = GetMaxClients();
  

  // Temp fix to for clearing menues on start
  for (new i=1; i<=g_maxClients; i++)
  {
    g_MenuCleared[i] = 0;
  }
}  

// Initializations to be done at the beginning of the round
public EventRoundStart(Handle:event, const String:name[], bool:dontBroadcast)
{
  clearAllDamageData();
}


// In this event we store how much damage attacker did to victim in one array
// and how much damage victim took from attacker in another array
public Event_PlayerHurt(Handle:event, const String:name[], bool:dontBroadcast)
{
  new healthDmg = GetEventInt(event,"dmg_health");
  new hitgroup = GetEventInt(event, "hitgroup");
  
  new victim_id = GetEventInt(event, "userid");
  new attacker_id = GetEventInt(event, "attacker");

  new victim = GetClientOfUserId(victim_id);
  new attacker = GetClientOfUserId(attacker_id);
  
  // Log damage taken to the vicitm and damage done to the attacker
  g_DamageDone[attacker][victim] += healthDmg;
  g_HitsDone[attacker][victim]++;
  g_HitboxDone[attacker][victim][hitgroup]++;

  g_DamageTaken[victim][attacker] += healthDmg;
  g_HitsTaken[victim][attacker]++;
  g_HitboxTaken[victim][attacker][hitgroup]++;
}


// Upon player dead, check who is the victim and attacker and 
// Call up on the buildDamageString function
public Event_PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast)
{
  new victim_id = GetEventInt(event, "userid");
  new attacker_id = GetEventInt(event, "attacker");

  new victim = GetClientOfUserId(victim_id);
  new attacker = GetClientOfUserId(attacker_id);
  
  g_KilledPlayer[attacker][victim]=1;

  if ((g_PlayerDROption[victim][propOnOff] == DrON) && (!IsFakeClient(victim)) && IsClientInGame(victim)) {
    BuildDamageString(victim, attacker);  
  }
}

// Local Function where we loop through all attackers and victims for a client.
// if any damage is taken or done a timer is called that will display a Panel with the info.
BuildDamageString (in_victim, in_attacker) {
  new String:damageReport[512];
  new String:damageReportLong[600];
  new String:damageDone[512];
  new String:damageTaken[512];
  new String:damageDoneLong[512];
  new String:damageTakenLong[512];
  new String:killer[10];
  new String:xkiller[10];
  new String:killed[10];
  new String:xkilled[10];
  new totalDmgTaken, totalDmgDone;
  
  // Loop through all damage where you inflicted damage
  for (new i=1; i<=g_maxClients; i++)
  {
    if(g_DamageDone[in_victim][i] >0)
    {
      if (g_KilledPlayer[in_victim][i] == 1) {
        killed=" (Killed)";
        xkilled=" X";
      }
      else {
        killed="";
        xkilled="";
      }
      
      Format(damageDone, sizeof(damageDone), "%s%s [%d dmg, %d hits]%s\n", damageDone, g_PlayerName[i], g_DamageDone[in_victim][i], g_HitsDone[in_victim][i], xkilled);
      Format(damageDoneLong, sizeof(damageDoneLong), "%s%s%s [%d dmg, %d hits]%s\n", damageDoneLong, xkilled, g_PlayerName[i], g_DamageDone[in_victim][i], g_HitsDone[in_victim][i], killed);
      Format(damageDoneLong, sizeof(damageDoneLong), "%s  ", damageDoneLong);
      totalDmgDone += g_DamageDone[in_victim][i];
      for(new j=0; j<=MAXHITGROUPS; j++) {
        if (g_HitboxDone[in_victim][i][j] > 0) {
          Format(damageDoneLong, sizeof(damageDoneLong), "%s%s:%d ", damageDoneLong, g_HitboxName[j], g_HitboxDone[in_victim][i][j]);
        }
      }
      Format(damageDoneLong, sizeof(damageDoneLong), "%s\n", damageDoneLong);
    }
  }

  // If you did any damage, add it to the report
  if (strcmp(damageDone, "", false) != 0) {
    Format(damageReport, sizeof(damageReport), "%s\n%s", "Damage done:", damageDone);
    Format(damageReportLong, sizeof(damageReportLong), "%s\n%s", "Damage done:", damageDoneLong);
  }

  // Loop through all damage where you took damage
  for (new i=1; i<=g_maxClients; i++)
  {
    if(g_DamageTaken[in_victim][i] >0)
    {
      if (i == in_attacker) {
        killer=" (Killer)";
        xkiller=" X";
      }
      else {
        killer="";
        xkiller="";
      }
      
      Format(damageTaken, sizeof(damageTaken), "%s%s [%d dmg, %d hits]%s\n", damageTaken, g_PlayerName[i], g_DamageTaken[in_victim][i], g_HitsTaken[in_victim][i], xkiller);

      Format(damageTakenLong, sizeof(damageTakenLong), "%s%s%s [%d dmg, %d hits]%s\n", damageTakenLong, xkiller, g_PlayerName[i], g_DamageTaken[in_victim][i], g_HitsTaken[in_victim][i], killer);
      Format(damageTakenLong, sizeof(damageTakenLong), "%s  ", damageTakenLong);
      totalDmgTaken += g_DamageTaken[in_victim][i];
      for(new j=0; j<=MAXHITGROUPS; j++) {
        if (g_HitboxTaken[in_victim][i][j] > 0) {
          Format(damageTakenLong, sizeof(damageTakenLong), "%s%s:%d ", damageTakenLong, g_HitboxName[j], g_HitboxTaken[in_victim][i][j]);
        }
      }
      Format(damageTakenLong, sizeof(damageTakenLong), "%s\n", damageTakenLong);
    }
  }

  // If you took any damage, add it to the report
  if (strcmp(damageTaken, "") != 0) {
    Format(damageReport, sizeof(damageReport), "%s%s\n%s", damageReport, "Damage taken:", damageTaken);
    Format(damageReportLong, sizeof(damageReportLong), "%s%s\n%s", damageReportLong, "Damage taken:", damageTakenLong);
  }

  // If damageReport is not empy
  if (strcmp(damageReport, "") != 0) {
    // store values if players what to view the last stats shown
    g_HistDamageDone[in_victim] = damageDone;
    g_HistDamageDoneLong[in_victim] = damageDoneLong;
    g_HistTotalDamageDone[in_victim] = totalDmgDone;
  
    g_HistDamageTaken[in_victim] = damageTaken;
    g_HistDamageTakenLong[in_victim] = damageTakenLong;
    g_HistTotalDamageTaken[in_victim] = totalDmgTaken;

    DisplayDamageReport(in_victim, damageDone ,damageDoneLong ,damageTaken, damageTakenLong, totalDmgDone, totalDmgTaken);
  }
}

DisplayDamageReport(in_victim, String:damageDone[512] ,String:damageDoneLong[512] ,String:damageTaken[512], String:damageTakenLong[512], totalDmgDone, totalDmgTaken) {
  if (g_PlayerDROption[in_victim][propPopChat] == DrPop) {
    // Display damage report to the dead vicitm
    new Handle:pack;
    CreateDataTimer(1.0,DisplayDamageReportMenu, pack, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
    WritePackCell(pack, in_victim);
    //LogToGame("%s", damageReport);
    if (strlen(damageTakenLong)+strlen(damageDoneLong)<512-30 && (g_PlayerDROption[in_victim][propShortLong] == DrLong)) {
      WritePackString(pack, damageDoneLong);
      WritePackString(pack, damageTakenLong);
      WritePackCell(pack, totalDmgDone);
      WritePackCell(pack, totalDmgTaken);
    }
    else {
      WritePackString(pack, damageDone);
      WritePackString(pack, damageTaken);
      WritePackCell(pack, totalDmgDone);
      WritePackCell(pack, totalDmgTaken);
    }
  }
  else {
    if (strcmp(damageDone, "", false) != 0) {
      PrintToChat(in_victim, "\n\x04==Victims (Total dmg:%d)]==\n", totalDmgDone);
      PrintToChat(in_victim, "\x04%s", damageDone);
    }
    if (strcmp(damageTaken, "", false) != 0) {
      PrintToChat(in_victim, "\x0E==[Attackers (Total dmg:%d)]==\n", totalDmgTaken);
      PrintToChat(in_victim, "\x0E%s", damageTaken);
    }
  }
}

// This is called by the timer.
// It checks if a menu/panel already is displayed ... if so
// let the timer try again after the delay, hoping the menu is closed
public Action:DisplayDamageReportMenu(Handle:timer, Handle:pack) {
  new String:p_damageDone[512];
  new String:p_damageTaken[512];
  new String:victimItem[100];
  new String:attackerItem[100];
  new p_victim;
  new p_totalDmgDone;
  new p_totalDmgTaken;
  
  ResetPack(pack);
  p_victim = ReadPackCell(pack);
  ReadPackString(pack, p_damageDone, sizeof(p_damageDone));
  ReadPackString(pack, p_damageTaken, sizeof(p_damageTaken));
  p_totalDmgDone = ReadPackCell(pack);
  p_totalDmgTaken = ReadPackCell(pack);

  if (!IsClientInGame(p_victim)) {
    return Plugin_Stop;
  }  


  if (GetClientMenu(p_victim)!=MenuSource_None) {
    return Plugin_Continue;
  }
  
  new Handle:damageReportPanel = CreatePanel();

  if (strcmp(p_damageDone, "") != 0) {
    //LogToGame("%s", p_damageReport);
    
    Format(victimItem, sizeof(victimItem), "Victims (Total dmg:%d)", p_totalDmgDone);
    DrawPanelItem(damageReportPanel, victimItem);
    DrawPanelText(damageReportPanel, p_damageDone);
  }
  if (strcmp(p_damageTaken, "") != 0) {
    //LogToGame("%s", p_damageReport);
    Format(attackerItem, sizeof(attackerItem), "Attackers (Total dmg:%d)", p_totalDmgTaken);
    DrawPanelItem(damageReportPanel, attackerItem);
    DrawPanelText(damageReportPanel, p_damageTaken);
  }
  DrawPanelItem(damageReportPanel, "Exit");
  DrawPanelText(damageReportPanel, "Type \"/damage_report help\" in chat for settings");
  
  SendPanelToClient(damageReportPanel, p_victim, Handler_MyPanel, 8);
  CloseHandle(damageReportPanel);
  return Plugin_Stop;
}


// Display time timer will close my panel ... no need to handle anything
// CloseHandle seems to be called when the timer for the panel runs out
public Handler_MyPanel(Handle:menu, MenuAction:action, param1, param2) {
}


// Store the name of the player at time of spawn. If player disconnects before
// round end, the name can still be displayed in the damage reports.
public Event_PlayerSpawn(Handle:event, const String:name[], bool:dontBroadcast){
  new userid = GetEventInt(event,"userid");
  new client = GetClientOfUserId(userid);
  // Temp fix menu
  if (g_MenuCleared[client] == 0) {
    new Handle:TempPanel = CreatePanel();
    DrawPanelText(TempPanel, "welcome to networkheaven");
    SendPanelToClient(TempPanel, client, Handler_MyPanel, 1);
    g_MenuCleared[client] = 1;
    CloseHandle(TempPanel);
  }
  // Store Player names if they disconnect before round has ended
  new String:clientName[32];
  GetClientName(client, clientName, sizeof(clientName));
  strcopy(g_PlayerName[client], sizeof(g_PlayerName[]), clientName);
  
  // This shows that there is something strange when you spawn for the first time.
}

// Temp Fix
public Event_PlayerDisconnect(Handle:event, const String:name[], bool:dontBroadcast){
  new userid = GetEventInt(event,"userid");
  new client = GetClientOfUserId(userid);
  g_MenuCleared[client] = 0;
  
  g_HistDamageDone[client] = "";
  g_HistDamageDoneLong[client] = "";
  g_HistTotalDamageDone[client] = 0;

  g_HistDamageTaken[client] = "";
  g_HistDamageTakenLong[client] = "";
  g_HistTotalDamageTaken[client] = 0;
}

public Event_PlayerConnect(Handle:event, const String:name[], bool:dontBroadcast){
  new userid = GetEventInt(event,"userid");
  new client = GetClientOfUserId(userid);
  g_MenuCleared[client] = 0;
  
  g_HistDamageDone[client] = "";
  g_HistDamageDoneLong[client] = "";
  g_HistTotalDamageDone[client] = 0;

  g_HistDamageTaken[client] = "";
  g_HistDamageTakenLong[client] = "";
  g_HistTotalDamageTaken[client] = 0;
}

public OnClientPostAdminCheck(client) {
  FindSettingsForClient(client);
}

FindSettingsForClient(client) {
  new String:steamId[20];
  GetClientAuthString(client, steamId, 20);
  
  KvRewind(KVSettings);
  if(KvJumpToKey(KVSettings, steamId))
  {
    g_PlayerDROption[client][propOnOff]  = KvGetNum(KVSettings, "OnOff", g_defaultPropOnOff);
    g_PlayerDROption[client][propPopChat]  = KvGetNum(KVSettings, "PopChat", g_defaultPropChatPop);
    g_PlayerDROption[client][propShortLong]  = KvGetNum(KVSettings, "ShortLong", g_defaultPropChatPop);
  } else {
    g_PlayerDROption[client][propOnOff]  = g_defaultPropOnOff;
    g_PlayerDROption[client][propPopChat]  = g_defaultPropChatPop;
    g_PlayerDROption[client][propShortLong]  = g_defaultPropShortLong;
  }
  KvRewind(KVSettings);
}

StoreSettingsForClient(client) {
  new String:steamId[40];
  GetClientAuthString(client, steamId, 20);
 
  if(StrContains(steamId, "steam", false) != -1) {
      
    KvRewind(KVSettings);
    if ((g_PlayerDROption[client][propOnOff] == g_defaultPropOnOff) && (g_PlayerDROption[client][propPopChat] == g_defaultPropChatPop) && (g_PlayerDROption[client][propShortLong] == g_defaultPropShortLong)) {
      if(KvJumpToKey(KVSettings, steamId))
      { 
        KvDeleteThis(KVSettings);
      }
    }
    else {
      KvJumpToKey(KVSettings, steamId, true);
      KvSetNum(KVSettings, "OnOff", g_PlayerDROption[client][propOnOff]);
      KvSetNum(KVSettings, "PopChat", g_PlayerDROption[client][propPopChat]);
      KvSetNum(KVSettings, "ShortLong", g_PlayerDROption[client][propShortLong]);
    }
    KvRewind(KVSettings);
    KeyValuesToFile(KVSettings, g_filenameSettings);
  }
}

// Check if there are any living players. If so, trigger a timer for them so they will see their damage report
// Calculate who did max damage and display it as a hint.
// clear global damage done/taken arrays
public Event_RoundEnd (Handle:event, const String:name[], bool:dontBroadcast)
{
  // Make sure that we have a normal round end
  // reason 16 is game commencing
  // other reasons are real round ends
  new reason = GetEventInt(event, "reason");
  if(reason == 16) {
    return;
  }

  new damage, hits, mostDamage, mostDamagePlayer, mostHits, kills, mostKills, mostKillsPlayer;

  // Display damage report to living players
  // -1 in the damage string will make sure noone will be shown as the (Killer)
  for (new i=1; i<=g_maxClients; i++)
  { 
    if(IsClientInGame (i)) {
      if ((g_PlayerDROption[i][propOnOff] == DrON) && IsClientConnected (i) && !IsFakeClient (i) && IsPlayerAlive (i)) {
        BuildDamageString(i, -1);        
      }
    }
  }
  
  // Finding out who did the most damage
  for (new i=1; i<=g_maxClients; i++)
  {
    damage = 0;
    hits = 0;
    for (new j=1; j<=g_maxClients; j++)
    { 
      damage = damage + g_DamageDone[i][j];
      hits = hits + g_HitsDone[i][j];
    }
    if (damage > mostDamage) {
      mostDamage = damage;
      mostDamagePlayer = i;
      mostHits = hits;
    }
  }
 
  mostKills = 0;
  for (new i=1; i<=g_maxClients; i++)
  {
    kills = 0;
    for (new j=1; j<=g_maxClients; j++)
    {
      kills = kills + g_KilledPlayer[i][j];
    }
    if (kills > mostKills) {
      mostKills = kills;
      mostKillsPlayer = i;
    }
  }

  // Display to all the player that did the most damage this round
  if(mostDamage > 0) {
    //PrintToChatAll("\x01%s\x04 inflicted most damage, \x01%d\x04 dmg in \x01%d\x04 hits!", g_PlayerName[mostDamagePlayer], mostDamage, mostHits);
  }
  if(mostKills > 0) {
    //PrintToChatAll("\x04Most kills: \x01%s\x04 with \x01%d\x04 kills" ,g_PlayerName[mostKillsPlayer], mostKills);
  }
}

clearAllDamageData() 
{
  // Clear all logged damage
  for (new i=1; i<=g_maxClients; i++)
  {
    for (new j=1; j<=g_maxClients; j++)
    { 
      g_DamageDone[i][j]=0;
      g_DamageTaken[i][j]=0;
      g_HitsDone[i][j]=0;
      g_HitsTaken[i][j]=0;
      g_KilledPlayer[i][j]=0;
      for (new k=0; k<=MAXHITGROUPS; k++) 
      {
        g_HitboxDone[i][j][k]=0;
        g_HitboxTaken[i][j][k]=0;
      }
    }
  }
}