[Euphoria Dev Team Release] Starter Gears

07/31/2013 23:00 Philipp_#1
Hello community,

I recently saw users asking for a way to let their characters start equipped, since this is not possible by using the existing BaseItemsDefs table. Today I am releasing a way of doing it, which can also replace the BaseItemsDefs table but having more possibilities to realize your ideas.
If you are interested in this you might thought about a way of doing this before. The only problem I can imagine you could have had is the ItemUID. Since we want to insert one, or even multiple rows into the CharItems table we need to insert a new ItemUID as well, but there is no function to get an ItemUID. So how do we get a new ItemUID? There are several ways to get an uniqueidentifier, you will see my way few lines below. Since there is no function to call I decided to create my own, it surely is the most efficient way. Just execute the following querys to create my function and you can easily get a new UID whenever you want.
Code:
USE PS_GameData
GO
CREATE VIEW NewID
AS
SELECT NewID() AS NewID
Code:
USE [PS_GameData]
GO

/****** Object:  UserDefinedFunction [dbo].[ItemUID]    Script Date: 31.07.2013 00:03:25 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		Euphoria Dev Team
-- =============================================
CREATE FUNCTION [dbo].[ItemUID]
()
RETURNS BIGINT
AS
BEGIN
	
	DECLARE @ItemUID BIGINT

	NEWUID:
	SET @ItemUID = ABS(CAST(HASHBYTES('SHA1',CAST((SELECT NewID FROM PS_GameData.dbo.NewID) AS NVARCHAR(MAX))) AS BIGINT))

	IF EXISTS(SELECT * FROM PS_GameData.dbo.CharItems WHERE ItemUID = @ItemUID) OR
	EXISTS(SELECT * FROM PS_GameData.dbo.GuildStoredItems WHERE ItemUID = @ItemUID) OR
	EXISTS(SELECT * FROM PS_GameData.dbo.MarketItems WHERE ItemUID = @ItemUID) OR
	EXISTS(SELECT * FROM PS_GameData.dbo.UserStoredItems WHERE ItemUID = @ItemUID)
	GOTO NEWUID

	RETURN @ItemUID

END
GO
Secondly, I created a table to store all the data in. You can specify the item, its links, craftname and even more. I named it BaseGearsDefs, because of the reason mentioned at the beginning. In fact, you can do even more with it than it was meant to do (Starting with equipped gears). The query can be found below.
Code:
USE [PS_GameDefs]
GO

/****** Object:  Table [dbo].[BaseGearsDefs]    Script Date: 31.07.2013 22:00:04 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[BaseGearsDefs](
	[Family] [tinyint] NOT NULL,
	[Job] [tinyint] NOT NULL,
	[Level] [int] NOT NULL,
	[Bag] [smallint] NOT NULL,
	[Slot] [smallint] NOT NULL,
	[ItemID] [int] NOT NULL,
	[Type] [tinyint] NOT NULL,
	[TypeID] [tinyint] NOT NULL,
	[Gem1] [tinyint] NOT NULL,
	[Gem2] [tinyint] NOT NULL,
	[Gem3] [tinyint] NOT NULL,
	[Gem4] [tinyint] NOT NULL,
	[Gem5] [tinyint] NOT NULL,
	[Gem6] [tinyint] NOT NULL,
	[Craftname] [varchar](20) NULL,
	[Quality] [int] NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO
If you want to add a new row to it you have to fill in content in each column except the craftname one, if you do not want to add rerolls or let your players start with enchanted items leave this clear. The Level column should be interesting for servers with an instant leveling system only - If you enter 60 in the column for example, only characters whose starter level is 60 will have it equipped or in their inventory, depending on your setup. For those without instant leveling, logically write a 1 in it. About preparations, we are done now. Next we will take use of this and implement it in our usp_Create_Char_R procedure (located in PS_GameData).
Code:
INSERT INTO CharItems
SELECT @CharID AS CharID,ItemID,dbo.ItemUID() AS ItemUID,Type,TypeID,Bag,Slot,Quality,Gem1,Gem2,Gem3,Gem4,Gem5,Gem6,Craftname,1 AS COUNT,GETDATE() AS Maketime,'S' AS Maketype,0 AS Del
FROM PS_GameDefs.dbo.BaseGearsDefs WHERE Family = @Family AND Job = @Job AND Level = @Level
Paste the upper code right after the following line at the nearly end of your proc:
Code:
SET @CharID = IDENT_CURRENT('Chars')
Best regards,
Philipp.
07/31/2013 23:18 adm_anos#2
Epic Release!
Gz and God Bless you too!
08/01/2013 07:01 anton1312#3
why you release it? for thanks? lol
Now all servers will have this system and its bad
08/01/2013 08:00 Philipp_#4
Quote:
Originally Posted by anton1312 View Post
why you release it? for thanks? lol
Now all servers will have this system and its bad
I released it, because of the communitys' interest in it, plus I somehow felt like releasing something.

What do you mean by its bad? Do you mean this system is bad, or that it is bad all servers will have this system now? Both is not bad in my point of view.
08/01/2013 08:19 Shànks♠#5
Thanks for the release :)
08/01/2013 10:26 castor4878#6
Nice release Philipp, thanks for sharing such a brilliant method.

Just 2 comments regarding the generation of the UID:
- the sha2_256 mechanism is not available in all SQL Server release (not sure if it was introduced in the 2010 or 2012 release), also it seems to be not supported by the Express versions (all releases).
meantime, the function must generate a 64-bits number, so the 'SHA1' mechanism (with a 128 bits output) can be used instead of the 'sha2_256' (which generates a 256-bits digest).

- the NewID view is not mandatory, the intrinsic NewID() function can be used directly

the beginning of the ItemUID function can so - if you experience error with the SHA2 mechanism - be rewritten to:

Code:
DECLARE @ItemUID BIGINT

NEWUID:
SET @ItemUID = ABS(CAST(HASHBYTES('SHA1',CAST(NewID() AS NVARCHAR(MAX))) AS BIGINT))
note that if the 'sha2_256' mechanism is not supported, the hashbytes function will NOT throw an error, il will only returns null.
08/01/2013 12:30 Philipp_#7
Thank you for the information, castor. I did not know the SHA2 mechanism was introduced lately.

About your second comment, I have to say you are wrong. The view is needed - at least it is a good workaround.
Most nondeterministic functions, those who return different results each time they are called, cannot be used inside user-defined functions.

Cheers
08/01/2013 12:31 anton1312#8
Quote:
Originally Posted by Philipp_ View Post
I released it, because of the communitys' interest in it, plus I somehow felt like releasing something.

What do you mean by its bad? Do you mean this system is bad, or that it is bad all servers will have this system now? Both is not bad in my point of view.
i mean all servers will have this system because all servers will use it

a little earlier, I created my own script starter gear, and i was sad when i saw your release here.
08/01/2013 15:31 Battle-of-Shaiya#9
is that all you can say if so say nothing that wil be the best

nice release bro
08/01/2013 15:58 castor4878#10
Quote:
Originally Posted by Philipp_ View Post
I did not know the SHA2 mechanism was introduced lately.
I've just rechecked, it appears that SQL Svr 2008 R2 does not support it but the configs I've tried (a Developer Edition and an Express edition) were both on Win XP (NT 5.1) SP3; I can not exclude that the SQL engine uses some cryptographic of the system, and may be the XP's CSPs are still not aware of SHA2 (and AES, ECC, stuff ...)

Quote:
Originally Posted by Philipp_ View Post
About your second comment, I have to say you are wrong. The view is needed - at least it is a good workaround.
Most nondeterministic functions, those who return different results each time they are called, cannot be used inside user-defined functions.
You are right, the deterministic state of functions is very important in the coding of a stored procedure and your coding is *in all cases* correct; still the intrinsic NewID function is nondeterministic (is defined and known as nondeterministic by the server) [Only registered and activated users can see links. Click Here To Register...]) and I assumed that direct call can be made, but again your code is defensive and valid.

edit 1:
missread your point actually (I read: fct will be use internally of the SP as deterministic and thus always return same value) ... I recheck that point

edit 2:
nice days, I was just double-wrong :)
due to not-constant result, the NewID function can indeed not be used in a (stored) procedure that manage access to table(s) (the engine can not track what not limited to the SP context is changed), and your coding is a) mandatory b) the cleanest way to do it.

(sorry for not relevant posts)
08/01/2013 21:17 [Dev]Optimum#11
Hey Castor with SQL Server 2005 Express does not work.

Error:
Fix
08/01/2013 21:38 [Dev]Matheus#12
Here when I run these script when creating the character of "name already exists" Help me please.
08/01/2013 21:42 [Dev]Optimum#13
In the game or when the script run?
08/01/2013 21:52 [Dev]Matheus#14
Yes this error happens when I run the script

Code:
INSERT INTO CharItems
SELECT @CharID AS CharID,ItemID,dbo.ItemUID() AS ItemUID,Type,TypeID,Bag,Slot,Quality,Gem1,Gem2,Gem3,Gem4,Gem5,Gem6,Craftname,1 AS COUNT,GETDATE() AS Maketime,'S' AS Maketype,0 AS Del
FROM PS_GameDefs.dbo.BaseGearsDefs WHERE Family = @Family AND Job = @Job AND Level = @Level
in usp_Create_Char_R.
08/01/2013 22:02 Philipp_#15
Without providing me error messages or your logs located in PSM_Client/Bin/Logs I will not be able to help you. Be sure you edited the procedure correctly, otherwise an error like the one you get can easily occur.