#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "emlen.h"

/* sucksville searching and sorting demo it seems.  maybe I shoulda taken a coding
class first */

void 
check_topten (CHAR_DATA * ch)
{
  int k;
  int slotnum;
  int tk;
  bool in_topten_already;
  if (IS_MOB (ch))
    return;
  slotnum = -1;
  for (k = 0; k < 10; k++)
    {
      if (ch->pcdata->warpoints > topten.topten_warpoints[k] && str_cmp (capitalize (NAME (ch)), topten.topten_name[k]))
	{
	  slotnum = k;
	  break;
	}
      if (!str_cmp (capitalize (NAME (ch)), topten.topten_name[k]))
	topten.topten_warpoints[k] = ch->pcdata->warpoints;
    }
  if (slotnum == -1)
    return;
  in_topten_already = FALSE;
/*Scan if already somewhere ELSE in topten */
  for (tk = 0; tk < 10; tk++)
    {
      if (!str_cmp (capitalize (NAME (ch)), topten.topten_name[tk]))
	{
	  /*move everyone after that slot down on the list */
	  for (k = tk; k < 9; k++)
	    {
	      if (k > 8)
		break;
	      strcpy (topten.topten_name[k], topten.topten_name[k + 1]);
	      topten.topten_warpoints[k] = topten.topten_warpoints[k + 1];
	      topten.topten_good[k] = topten.topten_good[k + 1];
	    }
	  break;
	}
    }
/*Move everyone after that slot down on the list */
  for (k = 9; k > slotnum; k--)
    {
      strcpy (topten.topten_name[k], topten.topten_name[k - 1]);
      topten.topten_warpoints[k] = topten.topten_warpoints[k - 1];
      topten.topten_good[k] = topten.topten_good[k - 1];
    }
  topten.topten_warpoints[slotnum] = ch->pcdata->warpoints;
  strcpy (topten.topten_name[slotnum], capitalize (NAME (ch)));
  if (IS_EVIL (ch))
    topten.topten_good[slotnum] = FALSE;
  else
    topten.topten_good[slotnum] = TRUE;
  save_topten ();
  return;
}

void 
check_rating (CHAR_DATA * ch)
{
  int k;
  int slotnum;
  int tk;
  bool in_topten_already;
  int ratng;
  if (IS_MOB (ch) || LEVEL (ch) > 99)
    return;
  ratng = rating (ch);
  slotnum = -1;
  for (k = 0; k < 10; k++)
    {
      if (ratng > ratingd.rating_rating[k] && str_cmp (capitalize (NAME (ch)), ratingd.rating_name[k]))
	{
	  slotnum = k;
	  break;
	}
      if (!str_cmp (capitalize (NAME (ch)), ratingd.rating_name[k]))
	ratingd.rating_rating[k] = ratng;
    }
  if (slotnum == -1)
    return;
  in_topten_already = FALSE;
/*Scan if already somewhere ELSE in topten */
  for (tk = 0; tk < 10; tk++)
    {
      if (!str_cmp (capitalize (NAME (ch)), ratingd.rating_name[tk]))
	{
	  /*move everyone after that slot down on the list */
	  for (k = tk; k < 9; k++)
	    {
	      if (k > 8)
		break;
	      strcpy (ratingd.rating_name[k], ratingd.rating_name[k + 1]);
	      ratingd.rating_rating[k] = ratingd.rating_rating[k + 1];
	      ratingd.rating_good[k] = ratingd.rating_good[k + 1];
	    }
	  break;
	}
    }
/*Move everyone after that slot down on the list */
  for (k = 9; k > slotnum; k--)
    {
      strcpy (ratingd.rating_name[k], ratingd.rating_name[k - 1]);
      ratingd.rating_rating[k] = ratingd.rating_rating[k - 1];
      ratingd.rating_good[k] = ratingd.rating_good[k - 1];
    }
  ratingd.rating_rating[slotnum] = ratng;
  strcpy (ratingd.rating_name[slotnum], capitalize (NAME (ch)));
  if (IS_EVIL (ch))
    ratingd.rating_good[slotnum] = FALSE;
  else
    ratingd.rating_good[slotnum] = TRUE;
  if (number_range (1, 5) == 3)
    save_rating ();
  return;
}

void 
do_trophy (CHAR_DATA * ch, char *argy)
{
  bool foundy;
  char buffy[500];
  int used[15];
  int itt;
  int i;
  int curlevel;
  int ittt;
  int tempslot;
  int iHash;
  DEFINE_COMMAND ("trophy", do_trophy, POSITION_SLEEPING, 0, LOG_NORMAL, "Shows your mob or pkill trophy.")

    if (IS_MOB (ch))
    return;
#ifdef NEW_WORLD
  if (argy[0] != 'm' && argy[0] != 'M' && argy[0] != 'p' && argy[0] != 'P')
    {
      send_to_char ("Please specify trophy [M]obs or trophy [P]layers.\n\r", ch);
      return;
    }
  if (argy[0] == 'm' || argy[0] == 'M')
    {
      MOB_PROTOTYPE *mid;
      hugebuf_o[0] = '\0';
      for (iHash = 0; iHash < HASH_MAX; iHash++)
	{
	  for (mid = mob_index_hash[iHash]; mid != NULL; mid = mid->next)
	    {
	      if (mid->vnum >= MAX_MOB_TRO)
		continue;
	      if (ch->pcdata->killed_mobs[mid->vnum] == 0)
		continue;
	      i = mid->vnum;
	      if (ch->pcdata->killed_mobs[i] > 999)
		ch->pcdata->killed_mobs[i] = 999;
	      sprintf (hugebuf_o + strlen (hugebuf_o), "[%3d] %s\n\r", ch->pcdata->killed_mobs[i], mid->short_descr);
	      if (strlen (hugebuf_o) > 29800)
		{
		  hugebuf_o[29800] = '\0';
		  goto eld;
		}
	    }
	}
    eld:
      page_to_char (hugebuf_o, ch);
      return;
    }
#endif
  for (i = 0; i < 15; i++)
    used[i] = 0;
  foundy = FALSE;
  send_to_char (" \x1B[34;1m[\x1B[30mTrophy of Kills\x1B[34m]\x1B[37;1m\n\r\n\r", ch);
  sprintf (buffy, "%5s %6s %5s\n\r",
	   "\x1B[30;1m[\x1B[32mLvl\x1B[30m]",
	   "\x1B[31;1m[\x1B[37mName\x1B[31m]",
	   "\x1B[36;1m[\x1B[35mTimes\x1B[36m]");
  send_to_char (buffy, ch);
/*Sort Code */
  curlevel = 1000;
  for (ittt = 0; ittt < 15; ittt++)
    {
      tempslot = -1;
      for (i = 0; i < 15; i++)
	{
	  bool gotone;
	  gotone = FALSE;
	  /*Find highest level trophy entry */
	  if (ch->pcdata->trophy_level[i] < 1 || ch->pcdata->trophy_name[i][0] == '\0')
	    continue;
	  if (ch->pcdata->trophy_level[i] <= curlevel)
	    if (used[i] == 0)
	      {
		bool flagr;
		flagr = FALSE;
		for (itt = 0; itt < 15; itt++)
		  {
		    if (used[itt] == 1)
		      continue;
		    if (ch->pcdata->trophy_level[itt] > ch->pcdata->trophy_level[i])
		      flagr = TRUE;
		  }
		if (!flagr)
		  {
		    curlevel = ch->pcdata->trophy_level[i];
		    tempslot = i;
		    used[i] = 1;
		    gotone = TRUE;
		  }
	      }
	  if (gotone)
	    break;
	}
      if (tempslot != -1)
	{
	  foundy = TRUE;
	  sprintf (buffy,
	    "\x1B[37;0m[\x1B[34;1m%3d\x1B[37;0m] %-23s \x1B[37;1m*%-4d\n\r",
		   ch->pcdata->trophy_level[tempslot],
		   ch->pcdata->trophy_name[tempslot], ch->pcdata->trophy_times[tempslot]);
	  send_to_char (buffy, ch);
	  tempslot = -1;
	}
    }
  if (!foundy)
    {
      send_to_char ("\n\rNone.\n\r", ch);
    }
  send_to_char ("\x1B[37;0m", ch);
  return;
}

void 
do_topten (CHAR_DATA * ch, char *argy)
{
  bool foundy;
  char buffy[500];
  int used[10];
  int itt;
  int position;
  int i;
  int curlevel;
  int ittt;
  int tempslot;
  char ttt[200];
  DEFINE_COMMAND ("topten", do_topten, POSITION_SLEEPING, 0, LOG_NORMAL, "Shows the top 10 PKillers.")

    if (IS_MOB (ch))
    return;
  position = ch->position;
  ch->position = POSITION_STANDING;
  for (i = 0; i < 10; i++)
    used[i] = 0;
  foundy = FALSE;
  sprintf (ttt, "$B$0[$4Top Ten List of PKillers (%s/%s and %s/%s)$0]", good, evil, evil, good);
  act (ttt, ch, NULL, ch, TO_CHAR);
  send_to_char ("\n\r", ch);
/*Sort Code */
  curlevel = 999000;
  for (ittt = 0; ittt < 10; ittt++)
    {
      tempslot = -1;
      for (i = 0; i < 10; i++)
	{
	  bool gotone;
	  gotone = FALSE;
	  /*Find highest level trophy entry */
	  if (topten.topten_warpoints[i] <= curlevel)
	    if (used[i] == 0)
	      {
		bool flagr;
		flagr = FALSE;
		for (itt = 0; itt < 10; itt++)
		  {
		    if (used[itt] == 1)
		      continue;
		    if (topten.topten_warpoints[itt] > topten.topten_warpoints[i])
		      flagr = TRUE;
		  }
		if (!flagr)
		  {
		    curlevel = topten.topten_warpoints[i];
		    tempslot = i;
		    used[i] = 1;
		    gotone = TRUE;
		  }
	      }
	  if (gotone)
	    break;
	}
      if (tempslot != -1)
	{
	  foundy = TRUE;
	  sprintf (buffy, "%s $B$7#%-2d $1%-35s $3-->$2 %-6d$3 warpoints",
	     (topten.topten_good[tempslot] ? "\x1B[34;1m*" : "\x1B[31;1m*"),
		   ittt + 1, topten.topten_name[tempslot], topten.topten_warpoints[tempslot]);
	  act (buffy, ch, NULL, ch, TO_CHAR);
	  tempslot = -1;
	}
    }
  if (!foundy)
    {
      send_to_char ("\n\rNone.\n\r", ch);
    }
  send_to_char ("\x1B[37;0m", ch);
  ch->position = position;
  return;
}

void 
do_rating (CHAR_DATA * ch, char *argy)
{
  bool foundy;
  char buffy[500];
  int used[10];
  int itt;
  int position;
  int i;
  int curlevel;
  int ittt;
  int tempslot;
  DEFINE_COMMAND ("rating", do_rating, POSITION_SLEEPING, 0, LOG_NORMAL, "Shows you your current rating.")

    if (IS_MOB (ch))
    return;
  if (!pow.old_creation_method && LEVEL (ch) < 8)
    {
      send_to_char ("You can't see your rating until level 8.\n\r", ch);
      return;
    }
  if (LEVEL (ch) < 100)
    check_rating (ch);
  position = ch->position;
  ch->position = POSITION_STANDING;
  for (i = 0; i < 10; i++)
    used[i] = 0;
  foundy = FALSE;
  act ("$B$0[$4Top Ten Rated Players$0]", ch, NULL, ch, TO_CHAR);
  send_to_char ("\n\r", ch);
/*Sort Code */
  curlevel = 999000;
  for (ittt = 0; ittt < 10; ittt++)
    {
      tempslot = -1;
      for (i = 0; i < 10; i++)
	{
	  bool gotone;
	  gotone = FALSE;
	  /*Find highest level trophy entry */
	  if (ratingd.rating_rating[i] <= curlevel)
	    if (used[i] == 0)
	      {
		bool flagr;
		flagr = FALSE;
		for (itt = 0; itt < 10; itt++)
		  {
		    if (used[itt] == 1)
		      continue;
		    if (ratingd.rating_rating[itt] > ratingd.rating_rating[i])
		      flagr = TRUE;
		  }
		if (!flagr)
		  {
		    curlevel = ratingd.rating_rating[i];
		    tempslot = i;
		    used[i] = 1;
		    gotone = TRUE;
		  }
	      }
	  if (gotone)
	    break;
	}
      if (tempslot != -1)
	{
	  foundy = TRUE;
	  sprintf (buffy, "%s $B$7#%-2d $1%-35s $3-->$2 %-6d$3 **Rating**",
	    (ratingd.rating_good[tempslot] ? "\x1B[34;1m*" : "\x1B[31;1m*"),
		   ittt + 1, ratingd.rating_name[tempslot], ratingd.rating_rating[tempslot]);
	  act (buffy, ch, NULL, ch, TO_CHAR);
	  tempslot = -1;
	}
    }
  if (!foundy)
    {
      send_to_char ("\n\rNone.\n\r", ch);
    }
  sprintf (buffy, "\n\r\x1B[37;0mYour current rating: \x1B[37;1m%d\x1B[37;0m\n\r", rating (ch));
  send_to_char (buffy, ch);
  ch->position = position;
  return;
}

int 
free_trophy_slot (CHAR_DATA * ch, CHAR_DATA * victim)
{
  int i;
  int templev;
  int tempnum;
  if (IS_MOB (ch))
    return -1;
  for (i = 0; i < 15; i++)
    {
      if (ch->pcdata->trophy_name[i][0] == '\0')
	return i;
    }
/*Okay, no free slots, replace lowest level */
  templev = 100;
  tempnum = -1;
  for (i = 0; i < 15; i++)
    {
      if (ch->pcdata->trophy_level[i] < templev)
	{
	  tempnum = i;
	  templev = ch->pcdata->trophy_level[i];
	}
    }
  if (ch->pcdata->trophy_level[tempnum] <= LEVEL (victim))
    return tempnum;
  else
    return -1;
}

void 
check_add_trophy (CHAR_DATA * ch, CHAR_DATA * victim)
{
  int i;
  int nummy;
  if (IS_MOB (ch) || IS_MOB (victim))
    return;
/*Handle warpoints */
  if (LEVEL (victim) > 7)
    ch->pcdata->warpoints += ((1 + (LEVEL (victim) / 3)) / rchars_in_group (ch));
  if (LEVEL (victim) > 20)
    ch->pcdata->warpoints += 1;
  if (LEVEL (victim) > 35)
    ch->pcdata->warpoints += 2;
  if (LEVEL (victim) > 45)
    ch->pcdata->warpoints += 2;
  check_topten (ch);
/*End Handle warpoints */
  for (i = 0; i < 15; i++)
    {
      if (ch->pcdata->trophy_name[i][0] == '\0')
	continue;
      if (!str_cmp (RNAME (victim), ch->pcdata->trophy_name[i]))
	{
	  ch->pcdata->trophy_times[i]++;
	  ch->pcdata->trophy_level[i] = LEVEL (victim);
	  return;
	}
    }
  nummy = free_trophy_slot (ch, victim);
  if (nummy == -1)
    return;
  strcpy (ch->pcdata->trophy_name[nummy], RNAME (victim));
  ch->pcdata->trophy_times[nummy] = 1;
  ch->pcdata->trophy_level[nummy] = LEVEL (victim);
  return;
}