Array does not work properly " array index out of bounds "
#2

Here is how I would set things up. Didn't test it but I think I got the basics down here take a look and learn. You are doing it all wrong man!

Код:
#include <a_samp>
#include <sscanf2>
// These kind of systems almost never change it's always like this when
// creating any dynamic system check here for another example it is older
// and there is a couple of things I would do differently today and also doesn't
// use y_iterate doesn't matter though it's the same concept either way
// https://sampforum.blast.hk/showthread.ph...25216281// Going to use foreach for looping
#include <YSI\y_iterate>
#include <YSI\y_commands>
#include <YSI\y_hooks>

// You can have as many repots as you want
#define             MAX_REPORTS                 1000

// Fine tune your length
#define             MAX_REPORT_REASON           128

// Make sure there is actually some kind of reason
#define             MIN_REASON_LENGTH           4

// Creation error reasons
#define             ERROR_REPORT_NOTCON         -1
#define             ERROR_REPORT_SHORT          -2
#define             ERROR_REPORT_NOFINDEX       -3

// Create our iterator
static Iterator:ReportIter<MAX_REPORTS>;

// Data structure of storing reports
enum g_REPORTINFO
{
	g_ReportReason[MAX_REPORT_REASON],
	g_ReportID,
	g_ReportedID,
	g_ReportTime,
}

// Create our variable to store data using the g_REPORTINFO enum data structure
static g_ReportData[MAX_REPORT_REASON][g_REPORTINFO];


//--Callbacks-------------------------------------------------------------------

// If a player goes off line remove their reports!
hook OnPlayerDisconnect(playerid, reason)
{
	// Loop through and check if this player had reports
	foreach(new i : ReportIter)
	{
		// Player had a report but the report might still be good to check
		// Just set the report id to an invalid player
		if(g_ReportData[i][g_ReportID] == playerid)
			g_ReportData[i][g_ReportID] = INVALID_PLAYER_ID;
	}
	return 1;
}

//--Create Delete Functions-----------------------------------------------------

// Create a report
CreateReport(playerid, reportid, reason[])
{
	// Set up variables
	new line[128], rname[MAX_PLAYER_NAME+1], name[MAX_PLAYER_NAME+1];
	
	// We want to make sure there is actually a reason
	if(strlen(reason) < MIN_REASON_LENGTH)
	{
		GetPlayerName(playerid, name, MAX_PLAYER_NAME+1);
		printf("ERROR::CreateReport::Reason Too Short::ID: %i Name: %s", playerid, name);
		return ERROR_REPORT_SHORT;
	}
	// Usually this would be checked before calling CreateReport still a good sanity check
	if(IsPlayerConnected(reportid))
	{
		// Get a free index to use for the report
	    new index = Iter_Free(ReportIter);

		// Is there any free indexes?
		if(index > -1)
		{
			// Add the index this to the iterator
		    Iter_Add(ReportIter, index);

			// Store all the data
            g_ReportData[index][g_ReportID] = playerid;
            g_ReportData[index][g_ReportedID] = reportid;
            g_ReportData[index][g_ReportTime] = gettime();
			format(g_ReportData[index][g_ReportReason], MAX_REPORT_REASON, "%s", reason);

			// Finally the report will always have to be annonced!
			GetPlayerName(playerid, rname, MAX_PLAYER_NAME+1);
			format(line, sizeof(line), "%s(%i) has reported %s{%i) for %s", rname, playerid, name, reportid);

			// Loop through players
			foreach(new i : Player)
			{
				// Is player an admin? (Change this to meet your system's needs)
				if(IsPlayerAdmin(i))
			 		SendClientMessage(i, 0xFF0000FF, line);
			}

			// Always return the index of the report
	    	return index;
		}
		
		// No indexes? This is a serious problem and shouldn't happen under
		// normal conditions show the error
		else
		{
		    // No free indexes return the error
			printf("ERROR::CreateReport::No Free Report Indexes");
			return ERROR_REPORT_NOFINDEX;
		}
	}

	// Reported id is not connected show the error
	printf("ERROR::CreateReport::Reporter Not Connected");
	return ERROR_REPORT_NOTCON;
}

// Remove a report
RemoveReport(index)
{
	// Is there an index with a report?
	if(Iter_Contains(ReportIter, index))
	{
		Iter_Remove(ReportIter, index);
		return 1;
	}

	// Failed the index does not exist!
	return 0;
}
//------------------------------------------------------------------------------


//--Support Functions-----------------------------------------------------------
IsPlayersReport(playerid, index)
{
	// Does this index exist?
	if(Iter_Contains(ReportIter, index))
	{
		// Yes this player did report this player!
		if(g_ReportData[index][g_ReportID] == playerid)
			return 1;
	}
	return 0;
}
//------------------------------------------------------------------------------

// There our Create() Remove/Destory() functions are created now we can use
// these to do useful stuff! This is the core support of the entire system
// If you write this part of your system well the rest is so damn easy all
// your support is there and everything falls into place like dominos!

CMD:report(playerid, params[])
{
	new reportid, reason[MAX_REPORT_REASON], index;

	// Get reported id and reason
	sscanf(params, "us[128]", reportid, reason);

	// Check if reported id is connected
	if(IsPlayerConnected(reportid))
	{
		// Make sure the reason is a valid length yes we check in the function too
		// but you might call that function in another way skipping this check
		// when you plan of doing nothing if there is a failure (unlikely) but
		// we put that support in naturally.
	    if(strlen(reason) >= MIN_REASON_LENGTH)
	    {
	        // Can't report yourself
			if(playerid != reportid)
			{
				// We have a valid report issue the report!
				index = CreateReport(playerid, reportid, reason);

				// You can update this to show more information
				// Lets reuse this variable since we're done with it
				// Just going to re-purpose it's use.
				format(reason, sizeof(reason), "Report Accepted! Type /cancelreport %i to cancel this report!", index);
				SendClientMessage(playerid, 0x00FF00FF, reason);
			}
			// Player is stupid and reported himself!
			else SendClientMessage(playerid, 0xFF0000FF, "You can't report yourself idiot!");
		}
		// No reason provided
		else SendClientMessage(playerid, 0xFF0000FF, "You must provide a reason!");
	}
	// Reported player is not connected
	else SendClientMessage(playerid, 0xFF0000FF, "Player is connected!");

	// Return 1 on commands
	return 1;
}

// Simple function to cancel a report
CMD:cancelreport(playerid, params[])
{
	new index = strval(params);
	if(index >= 0 && index < MAX_REPORTS)
	{
		// Make sure this is this players report!
		if(IsPlayersReport(playerid, index))
		{
		    RemoveReport(index);
		    SendClientMessage(playerid, 0x00FF00FF, "Report Has Been Removed");
		}
	}
	// No reports to cancel
	else SendClientMessage(playerid, 0xFF0000FF, "There is no report to cancel!");
	return 1;
}
Reply


Messages In This Thread
Array does not work properly " array index out of bounds " - by XpoZzA - 11.02.2019, 21:25
Re: Array does not work properly " array index out of bounds " - by Pottus - 11.02.2019, 22:23

Forum Jump:


Users browsing this thread: 1 Guest(s)