Lucky Box

02/15/2019 23:41 ILowe#1
Hello

i want to make a lucky box
but as this% chance

sample: Lucky Box

I want 50% successful 50% fail when pressed

will not give anything when it fails
02/16/2019 00:45 modyuasty3#2
Channel discord
02/16/2019 02:00 #HB#3
Ain't hard to implement as an SQL side only.

Code:
declare $LinesCC int = (select count(*) from $MainTable where ScrollObjID = $ItemRefID)

if ($LinesCC > 0)
begin
	declare $idx int = 1
	declare $LuckTable table (ItemCode varchar(128), ItemCount int, ItemOptLevel int)

	while ($idx <= $LinesCC)
	begin
	declare $Ratio int = 0
	select $Ratio = Ratio from $MainTable where ScrollObjID = $ItemRefID and ID = $idx
	
	if ($Ratio > 0)
	begin
		INSERT INTO $LuckTable
		select MT.ItemCode, MT.ItemCount, MT.ItemOptLevel from $MainTable MT cross join
		(select top 100 ROW_NUMBER() over(order by (select 1)) from sys.columns ) D(n) --Max ratio: 100
		where ScrollObjID = $ItemRefID and ID = $idx and D.n <= MT.Ratio;
	end

	set $idx = $idx + 1
	end

	if ((select count(*) from $LuckTable) > 0)
	begin
		declare $RandomItemCode varchar(128), $RandomItemCount int, $RandomItemOptLevel int
		select TOP 1 $RandomItemCode = LT.ItemCode, $RandomItemCount = LT.ItemCount, $RandomItemOptLevel = LT.ItemOptLevel from $LuckTable LT order by NEWID();

		-- reward
	end
end
While:
Code:
$MainTable table (ID int IDENTITY(1,1) NOT NULL, ScrollObjID int, ItemCode varchar(128), ItemCount int, ItemOptLevel int, Ratio int)
Hope Microsoft won't come to complain about my code, I am not an SQL coder, I am just giving this guy a hint to implement that thing.
02/16/2019 07:52 ILowe#4
Thank you

I tried but still 100%
I want to fail or successful
will give nothing if it fails

Sample :

Box open > Items in premium > successful
10% chance premium give after using

Box open > Items in premium > fail
Box disappears and nothing gets lost

02/16/2019 10:41 #HB#5
Just think about it, put an item at your main table with a code like a fail code, item code example: "Failed", so this part will be like:
Code:
if ((select count(*) from $LuckTable) > 0)
	begin
		declare $RandomItemCode varchar(128), $RandomItemCount int, $RandomItemOptLevel int
		select TOP 1 $RandomItemCode = LT.ItemCode, $RandomItemCount = LT.ItemCount, $RandomItemOptLevel = LT.ItemOptLevel from $LuckTable LT where LT.ItemCode not in ('Failed') order by NEWID();

		-- reward
	end
Also, you can make a condition to check if the random item is an actual item in your database.
02/17/2019 03:57 ILowe#6
Thank you all for <3