[RELEASE]PHP Scripts

01/08/2011 07:50 zargon05#1
Here are php scripts which people might find useful.
1. PVP Rankings - It shows the top 25 players in the server in order of kills
2. UM Res - Allows users to rez dead UM toons

I'll add more scripts as I make them.If you have any feedback post here :)

UPDATE1 : Char-res improved (new approach)
UPDATE2: Char-res, now easier with more than 1 toon with same name
01/08/2011 09:11 abrasive#2
I only looked at the res script so far and I noticed you can cut out some duplicate code and cut down the queries by one by changing this:
Code:
	if($slot[0]>-1 && $slot[0]<5)
	{
		$res3 = mssql_query("SELECT * FROM [dbo].[UserMaxGrow] WHERE UserUID IN (SELECT UserUID FROM dbo.Chars WHERE UserID='$login')");
		$country = mssql_fetch_array($res3);
		if ( $country['Country'] == 0 ) {
			$res4 = mssql_query("SELECT family FROM [dbo].[Chars] WHERE charname='$toon'");
			$family =  mssql_fetch_array($res4);
			echo "Family = $family[0] <br />";
			if ( $family[0] == 0 || $family[0] == 1)
			{
				mssql_query("UPDATE dbo.Chars SET Del=0, Slot=$slot[0] , Map=42 , PosX=63 , PosZ=57 , DeleteDate=NULL WHERE CharName = '$toon'");
				echo "$toon was successfully resurrected</font>";
			}
			else 
			echo "Faction Error: Cannot ressurect UoF toon on AoL";
		}
		else if( $country['Country'] == 1) {
		
			$res4 = mssql_query("SELECT family FROM [dbo].[Chars] WHERE charname='$toon'");
			$family =  mssql_fetch_array($res4);
			if ( $family[0] == 2 || $family[0] == 3)
			{
				mssql_query("UPDATE dbo.Chars SET Del=0, Slot=$slot[0] , Map=42 , PosX=63 , PosZ=57 , DeleteDate=NULL WHERE CharName = '$toon'");
				echo "$toon was successfully resurrected</font>";
			}
			else 
			echo "Faction Error: Cannot ressurect AoL toon on UoF";
		}
		else
		echo "Faction error";
	}
	else
	echo "No slot avaliable";
To something like this:
Code:
	if($slot[0]>-1 && $slot[0]<5){
		$res3 = mssql_query("SELECT umg.Country, c.Family FROM [dbo].[UserMaxGrow] AS umg INNER JOIN [dbo].[Chars] AS c ON umg.UserUID = c.UserUID WHERE c.UserID = '$login' AND c.CharName = '$toon'");
		$result =  mssql_fetch_array($res3);
		if($result['Country'] == 0 || $result['Country'] == 1){
			if($result['Family'] >= 0 && $result['Family'] <= 3){
				mssql_query("UPDATE dbo.Chars SET Del=0, Slot=$slot[0], Map=42, PosX=63 , PosZ=57, DeleteDate=NULL WHERE CharName = '$toon'");
				echo "$toon was successfully resurrected</font>";
			}else{
				if($result['Family'] == 0 || $result['Family'] == 1){
					echo "Faction Error: Cannot ressurect UoF toon on AoL";
				}else if($result['Family'] == 2 || $result['Family'] == 3){
					echo "Faction Error: Cannot ressurect AoL toon on UoF";
				}
			}
		}else{
			echo "Faction error";
		}
	}else{
		echo "No slot available";
	}
Generally it is considered bad practice to use SELECT *, because you are returning more data than you are actually using, unless you are using ALL columns. It is better to specifically ask for the columns you want.

I would suggest using joins instead of sub queries when possible as well. Take a look at my $res3 query as an example.

Also I would use dictionary indexes instead of array indexes when asking for data from query results, otherwise you are asking for trouble if the order ever changes.

How come you chose to echo some of the html, but not all of it, or vice versa?

Overall the res script looks good. It also appears to be secure also which is the most important part. I didn't actually test anything though, just browsed through the script.
01/08/2011 17:17 Catafalque#3
gratz!

Great Job! Thank you : )
01/08/2011 22:56 Bаne#4
# added to my guide. good job to both of you

-Bane
01/09/2011 17:47 ProfNerwosol#5
@ Abrasive

I haven't read into the original script but your optimization will do the same what I had problem with. Meaning my first UM resurrection script resurrected an AoL character on UoF side. You need to check for Country separately with extra checks for Family. Players can change sides if they loose all of their characters.

Country=2 is only when all characters are dead. It means Faction Selection screen.

EDIT:
Here's my solution to this problem:

Code:
IF (SELECT Country FROM PS_GameData.dbo.UserMaxGrow WHERE UserUID=@UserUID)<>2
begin
	PRINT 'Player chose faction after loosing all characters. Checking whether new characters exist.'
		-- let's get that new faction and see if it's the same as the last time
	SELECT @Country=Country FROM PS_GameData.dbo.UserMaxGrow WHERE UserUID=@UserUID
	
	IF EXISTS (SELECT * FROM PS_GameData.dbo.Chars WHERE UserUID=@UserUID AND Del=0)
	begin
		PRINT 'Player has new characters. Checking faction.'
		PRINT '----------'
		
		IF (@Family=0 OR @Family=1) AND @Country<>0
		begin
			-- Faction is not the same. Was Alliance of Light is Union of Fury. Character not resurrected.										
			
			INSERT INTO @ResultTable 
				(CharID, CharName, Resurrected, NoFreeSlots, FactionChange) 
			VALUES
				(@CharID, @CharName, 'no', 'no', 'yes');
			FETCH NEXT FROM dead_um INTO
				@UserUID,@CharID,@Slot,@CharName,@Family																										
			
			CONTINUE;
		end
		ELSE IF (@Family=2 OR @Family=3) AND @Country<>1
		begin
			-- Faction is not the same. Was Union of Fury is Alliance of Light. Character not resurrected.
			
			INSERT INTO @ResultTable
				(CharID, CharName, Resurrected, NoFreeSlots, FactionChange) 
			VALUES
				(@CharID, @CharName, 'no', 'no', 'yes');
			FETCH NEXT FROM dead_um INTO
				@UserUID,@CharID,@Slot,@CharName,@Family																										
			
			CONTINUE;										
		end																
	end
	ELSE
	begin
		PRINT 'No new characters detected. Proceeding with standard routine.'
		PRINT '----------'
	end						
end
01/09/2011 19:03 abrasive#6
Quote:
Originally Posted by ProfNerwosol View Post
@ Abrasive

I haven't read into the original script but your optimization will do the same what I had problem with. Meaning my first UM resurrection script resurrected an AoL character on UoF side. You need to check for Country separately with extra checks for Family. Players can change sides if they loose all of their characters.

Country=2 is only when all characters are dead. It means Faction Selection screen.
Thanks, I didn't catch that logic when I was optimizing. Zargon pm'ed me about it too.

Since both are running identical code, you could make the error message more generic and do it kind of like this:
Code:
if($slot[0]>-1 && $slot[0]<5){
	$res3 = mssql_query("SELECT umg.Country, c.Family FROM [dbo].[UserMaxGrow] AS umg INNER JOIN [dbo].[Chars] AS c ON umg.UserUID = c.UserUID WHERE c.UserID = '$login' AND c.CharName = '$toon'");
	$result =  mssql_fetch_array($res3);
	if(
		($result['Country'] != 0 && ($result['Family'] == 0 || $result['Family'] == 1)) ||
		($result['Country'] != 1 && ($result['Family'] == 2 || $result['Family'] == 3)) 
	){
		echo "Faction Error: Cannot ressurect toon into the opposite faction of which they were born into.";
	}else{
		mssql_query("UPDATE dbo.Chars SET Del=0, Slot=$slot[0], Map=42, PosX=63 , PosZ=57, DeleteDate=NULL WHERE CharName = '$toon'");
		echo "$toon was successfully resurrected</font>";
	}
}else{
	echo "No slot available";
}
Apparently Zargon is unable to use the dictionary indexes. I know that when I rewrote this [Only registered and activated users can see links. Click Here To Register...] script the dictionary indexes worked for me, so I'm unsure why he would have problems with them.
01/10/2011 08:57 zargon05#7
I made a new approch since abrasive's post, now toons that are in opposing faction are not dispalyed/avaliable to ressurection. I also forgot about more than 1 toon with same name so I am going to make a work-around. The dictionary index work when I use SELECT * but when I use JOINS it displays undefined index. I am going to try and fix this.
01/23/2011 03:43 Catafalque#8
Char-res not working. Toon not ressing.
01/23/2011 04:36 zargon05#9
I just downloaded and tested it and works perfectly in my DB. Do you get any error or it just does not res?
01/24/2011 13:59 Catafalque#10
Quote:
Originally Posted by zargon05 View Post
I just downloaded and tested it and works perfectly in my DB. Do you get any error or it just does not res?
İ dont get any error. This script perfectly. But toon's not ressing. :P
01/25/2011 05:51 -Infamy-#11
Quote:
Originally Posted by Catafalque View Post
İ dont get any error. This script perfectly. But toon's not ressing. :P
I had to add RemainTime=0 in the query, not sure if anyone else has had to do this.
01/25/2011 11:32 ProfNerwosol#12
Did you add it in character searching query? I was about to suggest it to avoid selecting characters which were deleted by user. RemainTime<>0 is delete requested by user.

Btw, the script works. I tested it on my database.
01/25/2011 11:53 -Infamy-#13
I had to add it the the updating query at the end, it was reviving the characters only partially, although that was before the latest post, so I am not sure if he has added that or not.
01/25/2011 19:55 ProfNerwosol#14
You shouldn't add RemainTime=0 in UPDATE statement. You will resurrect characters deleted by player this way. RemainTime=0 should be in WHERE clause in SELECT statement searching for player's dead characters.
01/31/2011 23:04 -Infamy-#15
The character resurrection script allowed for someone to resurrect a light toon on the fury side. :x