enumerator
#1

Hi, I'm working on faction ranks in my gamemode right now, they are loaded from MySQL. But I don't know if they are even loading correctly, one time it's OK other time it's some messy stuff. Anyways for example if I set the rank 1 to "test" and when I go in-game and print that it says "None ne" or "None " <- extra space in the end there. Also when I save the faction everything is OK, except rank 15 (the last one), it saves as "N ne". The empty space there is sometimes some kind of a weird character. This is what the enumerator looks like:
Код:
enum factionInfo
{
	FactionRanks,
	FactionRank1[24],
	FactionRank2[24],
	FactionRank3[24],
	FactionRank4[24],
	FactionRank5[24],
	FactionRank6[24],
	FactionRank7[24],
	FactionRank8[24],
	FactionRank9[24],
	FactionRank10[24],
	FactionRank11[24],
	FactionRank12[24],
	FactionRank13[24],
	FactionRank14[24],
	FactionRank15[24]
}
Here is the loading callback and the saving function:
Код:
forward OnFactionsLoaded();
public OnFactionsLoaded()
{
	new rows, field[10], factionid;
	
	cache_get_row_count(rows);
	
	for (new i; i < rows; i++) if (i < MAX_FACTIONS)
	{
		cache_get_value_name_int(i, "ID", factionid);
		cache_get_value_name_int(i, "Ranks", FactionInfo[factionid][FactionRanks]);
		
		for (new index = 24, number = 1; index <= 360; index += 24, number++)
		{
			format(field, sizeof(field), "Rank%d", number);
			cache_get_value_name(i, field, FactionInfo[factionid][factionInfo:index], 24);
		}
	}
}

SaveFaction(factionid)
{
	new query[500], field[10];
	
	mysql_format(g_MySQL, query, sizeof(query), "UPDATE `factions` SET `Ranks` = %d", FactionInfo[factionid][FactionRanks]);
	
	for (new index = 24, number = 1; index <= 360; index += 24, number++)
	{
		format(field, sizeof(field), "Rank%d", number);
		mysql_format(g_MySQL, query, sizeof(query), "%s, `%s` = '%e'", query, field, FactionInfo[factionid][factionInfo:index]);
	}
	mysql_format(g_MySQL, query, sizeof(query), "%s WHERE `ID` = %d", query, factionid);
	mysql_tquery(g_MySQL, query);
}
I tried debugging the ranks like this:
Код:
printf("%s", FactionInfo[0][FactionRank1]);
printf("%s", FactionInfo[0][FactionRank2]);
printf("%s", FactionInfo[0][FactionRank3]);
printf("%s", FactionInfo[0][FactionRank4]);
printf("%s", FactionInfo[0][FactionRank5]);
printf("%s", FactionInfo[0][FactionRank6]);
printf("%s", FactionInfo[0][FactionRank7]);
printf("%s", FactionInfo[0][FactionRank8]);
printf("%s", FactionInfo[0][FactionRank9]);
printf("%s", FactionInfo[0][FactionRank10]);
printf("%s", FactionInfo[0][FactionRank11]);
printf("%s", FactionInfo[0][FactionRank12]);
printf("%s", FactionInfo[0][FactionRank13]);
printf("%s", FactionInfo[0][FactionRank14]);
printf("%s", FactionInfo[0][FactionRank15]);
and this:
Код:
printf("%s", FactionInfo[0][factionInfo:24]);
printf("%s", FactionInfo[0][factionInfo:48]);
printf("%s", FactionInfo[0][factionInfo:72]);
printf("%s", FactionInfo[0][factionInfo:96]);
printf("%s", FactionInfo[0][factionInfo:120]);
printf("%s", FactionInfo[0][factionInfo:144]);
printf("%s", FactionInfo[0][factionInfo:168]);
printf("%s", FactionInfo[0][factionInfo:192]);
printf("%s", FactionInfo[0][factionInfo:216]);
printf("%s", FactionInfo[0][factionInfo:240]);
printf("%s", FactionInfo[0][factionInfo:264]);
printf("%s", FactionInfo[0][factionInfo:288]);
printf("%s", FactionInfo[0][factionInfo:312]);
printf("%s", FactionInfo[0][factionInfo:336]);
printf("%s", FactionInfo[0][factionInfo:360]);
the output of the first one was:
Код:
est
one
one
one
one
one
one
one
one
one
one
one
one
one
and the second one (with indexes):
Код:
test
None
None
None
None
None
None
None
None
None
None
None
None
None
N
ne
Also this is what the saving query looks like:
Код:
UPDATE `factions` SET `Ranks` = 11, `Rank1` = 'test', `Rank2` = 'None', `Rank3` = 'None', `Rank4` = 'None', `Rank5` = 'None', `Rank6` = 'None', `Rank7` = 'None', `Rank8` = 'None', `Rank9` = 'None', `Rank10` = 'None', `Rank11` = 'None', `Rank12` = 'None', `Rank13` = 'None', `Rank14` = 'None', `Rank15` = 'N\nne' WHERE `ID` = 0
What am I doing wrong?
Reply
#2

The design of the table itself is poor to begin with. If you need to add a number to a column name then you're doing databases wrong. You should have one table "Faction" and another "Rank". The "Rank" table should have 3 columns: factionId, rankNr and rankName. Where factionId and rankNr are the primary key: this is needed to identify the row for update or deletion (update ... where factionid = 1 and rankNr = 5).

factionId rankNr name
1 1 Beginner
1 2 Intermediate
1 3 Expert
Reply
#3

Rank15` = 'N\nne'

^^ shouldn't this be none?
Reply
#4

Quote:
Originally Posted by Vince
Посмотреть сообщение
The design of the table itself is poor to begin with. If you need to add a number to a column name then you're doing databases wrong. You should have one table "Faction" and another "Rank". The "Rank" table should have 3 columns: factionId, rankNr and rankName. Where factionId and rankNr are the primary key: this is needed to identify the row for update or deletion (update ... where factionid = 1 and rankNr = 5).

factionId rankNr name
1 1 Beginner
1 2 Intermediate
1 3 Expert
What's wrong with mine? I mean it's better to have one table and one row for each faction instead of two tables and 15 rows for each faction. I have families done like this as well and they work perfectly so I like how it is right now.

Quote:
Originally Posted by Sunehildeep
Посмотреть сообщение
Rank15` = 'N\nne'

^^ shouldn't this be none?
That's what I'm talking about.
Reply
#5

Diving to more tables is better because if a faction doesn't have many ranks, all the rest of columns would go waste.

About the loop, it's incorrect too. It starts from index 1.

pawn Код:
for (new index = 1, number = 1, until_rank = (FactionInfo[factionid][FactionRanks] - 1) * 24 + 1; index <= until_rank; index += 24, number++)
{
    format(field, sizeof(field), "Rank%d", number);
    cache_get_value_name(i, field, FactionInfo[factionid][factionInfo:index], 24);
}
Reply
#6

Why don't you do it like this? (Not the fix)

Код:
enum factionInfo
{
	FactionRanks[16][24],
}
Reply
#7

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Diving to more tables is better because if a faction doesn't have many ranks, all the rest of columns would go waste.

About the loop, it's incorrect too. It starts from index 1.

pawn Код:
for (new index = 1, number = 1, until_rank = (FactionInfo[factionid][FactionRanks] - 1) * 24 + 1; index <= until_rank; index += 24, number++)
{
    format(field, sizeof(field), "Rank%d", number);
    cache_get_value_name(i, field, FactionInfo[factionid][factionInfo:index], 24);
}
Oh, thank you. I will divide them. But let's say a faction has 15 ranks and I want to set them to 10, this means I need to delete 5 rows. What would be the best way to do it? Doing
Код:
DELETE FROM factionranks WHERE ID = 0 AND Rank = 11;
DELETE FROM factionranks WHERE ID = 0 AND Rank = 12;
DELETE FROM factionranks WHERE ID = 0 AND Rank = 13;
DELETE FROM factionranks WHERE ID = 0 AND Rank = 14;
DELETE FROM factionranks WHERE ID = 0 AND Rank = 15;
is not the best option I assume?
Quote:
Originally Posted by K0P
Посмотреть сообщение
Why don't you do it like this? (Not the fix)

Код:
enum factionInfo
{
	FactionRanks[16][24],
}
Because that's not possible.
Reply
#8

Quote:
Originally Posted by GoldenLion
Посмотреть сообщение
Oh, thank you. I will divide them. But let's say a faction has 15 ranks and I want to set them to 10, this means I need to delete 5 rows. What would be the best way to do it? Doing
Код:
DELETE FROM factionranks WHERE ID = 0 AND Rank = 11;
DELETE FROM factionranks WHERE ID = 0 AND Rank = 12;
DELETE FROM factionranks WHERE ID = 0 AND Rank = 13;
DELETE FROM factionranks WHERE ID = 0 AND Rank = 14;
DELETE FROM factionranks WHERE ID = 0 AND Rank = 15;
is not the best option I assume?
Operators work in SQL as well.

pawn Код:
DELETE FROM factionranks WHERE ID = 0 AND Rank >= 11;
or
pawn Код:
DELETE FROM factionranks WHERE ID = 0 AND (Rank >= 11 AND Rank <= 15);
Quote:
Originally Posted by GoldenLion
Посмотреть сообщение
Because that's not possible.
I was about to edit my post if you hadn't reply and say about this. It is simple - don't use an enumerator for the ranks and avoid all the complicated stuff about finding each index.
Reply
#9

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Operators work in SQL as well.

pawn Код:
DELETE FROM factionranks WHERE ID = 0 AND Rank >= 11;
or
pawn Код:
DELETE FROM factionranks WHERE ID = 0 AND (Rank >= 11 AND Rank <= 15);


I was about to edit my post if you hadn't reply and say about this. It is simple - don't use an enumerator for the ranks and avoid all the complicated stuff about finding each index.
Thanks, but if I shouldn't use an enumerator for ranks, then what should I use?
Reply
#10

Quote:
Originally Posted by GoldenLion
Посмотреть сообщение
Thanks, but if I shouldn't use an enumerator for ranks, then what should I use?
pawn Код:
new gFaction_Rank[MAX_FACTIONS][15][24];
If you move the ranks to another table, you don't need a column to store how many ranks there are as well. In case you want to know, you can use COUNT aggregate function.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)