Damien
Don't need glasses if you can C#
- Feb 26, 2012
- 434
- 647
Someone asked me to code this into plus for them, thought I'd share it for everyone. @Meap
And you're done.
Firstly add this method:
Don't forget the reference:
Code:
public static void Shuffle<T>(this List<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = r.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
Don't forget the reference:
Code:
using System.Collections.Generic;
Replace the constructor with this:
Making sure to add the variable
Code:
public CatalogItem(int id, int itemId, ItemData data, string catalogName, int pageId, int costCredits, int costPixels,
int costDiamonds, int amount, int limitedEditionSells, int limitedEditionStack, bool hasOffer, string extraData, string badge, int offerId)
int costDiamonds, int amount, List<int> limitedNumbers, int limitedEditionStack, bool hasOffer, string extraData, string badge, int offerId)
{
Id = id;
Name = catalogName;
ItemId = itemId;
Data = data;
PageID = pageId;
CostCredits = costCredits;
CostPixels = costPixels;
CostDiamonds = costDiamonds;
Amount = amount;
LimitedEditionSells = limitedEditionSells;
LimitedNumbers = limitedNumbers;
LimitedEditionSells = limitedEditionStack - limitedNumbers.Count;
LimitedEditionStack = limitedEditionStack;
IsLimited = (limitedEditionStack > 0);
IsLimited = limitedEditionStack > 0;
HaveOffer = hasOffer;
ExtraData = extraData;
Badge = badge;
OfferId = offerId;
}
Making sure to add the variable
Code:
public List<int> LimitedNumbers { get; private set; }
Add this method:
Then change this script:
To this:
Making sure to add the appropriate references:
Code:
private List<int> GetLimitedNumbers(int itemId, int size)
{
List<int> availableNumbers = new List<int>();
List<int> takenNumbers = new List<int>();
using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
{
dbClient.SetQuery("SELECT `number` FROM `catalog_items_limited` WHERE `item_id` = @itemId");
dbClient.AddParameter("itemId", itemId);
DataTable limitedNumbers = dbClient.GetTable();
if (limitedNumbers != null)
{
foreach (DataRow row in limitedNumbers.Rows)
{
int numberId = Convert.ToInt32(row["number"]);
if (!takenNumbers.Contains(numberId))
takenNumbers.Add(numberId);
}
}
}
for (int i = 1; i <= size; i++)
{
if (!takenNumbers.Contains(i))
availableNumbers.Add(i);
}
return availableNumbers;
}
Then change this script:
Code:
_items[PageId].Add(Convert.ToInt32(Row["id"]), new CatalogItem(...));
To this:
Code:
List<int> limitedNumbers = GetLimitedNumbers(
Convert.ToInt32(Row["id"]),
Convert.ToInt32(Row["limited_stack"]));
limitedNumbers.Shuffle();
_items[PageId].Add(Convert.ToInt32(Row["id"]), new CatalogItem(Convert.ToInt32(Row["id"]), Convert.ToInt32(Row["item_id"]),
Data, Convert.ToString(Row["catalog_name"]), Convert.ToInt32(Row["page_id"]), Convert.ToInt32(Row["cost_credits"]), Convert.ToInt32(Row["cost_pixels"]), Convert.ToInt32(Row["cost_diamonds"]),
Convert.ToInt32(Row["amount"]), Convert.ToInt32(Row["limited_sells"]), Convert.ToInt32(Row["limited_stack"]), PlusEnvironment.EnumToBool(Row["offer_active"].ToString()),
Convert.ToInt32(Row["amount"]), limitedNumbers, Convert.ToInt32(Row["limited_stack"]), PlusEnvironment.EnumToBool(Row["offer_active"].ToString()),
Convert.ToString(Row["extradata"]), Convert.ToString(Row["badge"]), Convert.ToInt32(Row["offer_id"])));
Making sure to add the appropriate references:
Code:
using Plus.Utilities;
Change this script:
To this:
Code:
ItemDataList.Add(new CatalogItem(...));
To this:
Code:
ItemDataList.Add(new CatalogItem(0, itemId, data, string.Empty, 0, 0, 0, 0, Amount, new List<int>(), 0, false, "", "", 0));
Find this if statement:
And change it to this:
Then change every reference for:
To this:
Code:
if (item.IsLimited)
{
....
}
And change it to this:
Code:
if (item.IsLimited)
{
if (item.LimitedNumbers.Count <= 0)
{
session.SendNotification("This item has sold out!\n\n" + "Please note, you have not recieved another item (You have also not been charged for it!)");
session.SendPacket(new CatalogUpdatedComposer());
session.SendPacket(new PurchaseOKComposer());
return;
}
limitedEditionNumber = item.LimitedNumbers[0];
limitedEditionStack = item.LimitedEditionStack;
item.LimitedEditionSells++;
using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
{
dbClient.SetQuery("INSERT INTO `catalog_items_limited` (`number`, `item_id`) VALUES (@number, @itemId)");
dbClient.AddParameter("number", limitedEditionNumber);
dbClient.AddParameter("itemId", item.Id);
dbClient.RunQuery();
item.LimitedNumbers.Remove(limitedEditionNumber);
}
}
Then change every reference for:
Code:
limitedEditionSells
To this:
Code:
limitedEditionNumber
Code:
CREATE TABLE `catalog_items_limited` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`number` int(11) NOT NULL DEFAULT 0,
`item_id` int(11) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `number, item_id`(`number`, `item_id`) USING BTREE,
INDEX `item_id`(`item_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;
And you're done.
Last edited: