Arcturus-Community/src/main/java/com/eu/habbo/habbohotel/catalog/CatalogLimitedConfiguration.java

129 lines
4.2 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.habbohotel.catalog;
import com.eu.habbo.Emulator;
import com.eu.habbo.habbohotel.users.Habbo;
import com.eu.habbo.habbohotel.users.HabboItem;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collections;
import java.util.LinkedList;
public class CatalogLimitedConfiguration implements Runnable
{
private final int itemId;
private int totalSet;
private final LinkedList<Integer> limitedNumbers;
public CatalogLimitedConfiguration(int itemId, LinkedList<Integer> availableNumbers, int totalSet)
{
this.itemId = itemId;
this.totalSet = totalSet;
this.limitedNumbers = availableNumbers;
2019-05-12 10:15:24 +02:00
if(Emulator.getConfig().getBoolean("catalog.ltd.random", true)) {
Collections.shuffle(this.limitedNumbers);
}
else {
Collections.reverse(this.limitedNumbers);
}
2018-07-06 15:30:00 +02:00
}
public int getNumber()
{
synchronized (this.limitedNumbers)
{
int num = this.limitedNumbers.pop();
2018-09-28 21:25:00 +02:00
if(this.limitedNumbers.isEmpty())
{
2019-03-18 02:22:00 +01:00
Emulator.getGameEnvironment().getCatalogManager().moveCatalogItem(Emulator.getGameEnvironment().getCatalogManager().getCatalogItem(this.itemId), Emulator.getConfig().getInt("catalog.ltd.page.soldout"));
2018-09-28 21:25:00 +02:00
}
2018-07-06 15:30:00 +02:00
return num;
}
}
public void limitedSold(int catalogItemId, Habbo habbo, HabboItem item)
{
synchronized (this.limitedNumbers)
{
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("UPDATE catalog_items_limited SET user_id = ?, timestamp = ?, item_id = ? WHERE catalog_item_id = ? AND number = ? AND user_id = 0 LIMIT 1"))
{
statement.setInt(1, habbo.getHabboInfo().getId());
statement.setInt(2, Emulator.getIntUnixTimestamp());
statement.setInt(3, item.getId());
statement.setInt(4, catalogItemId);
statement.setInt(5, item.getLimitedSells());
statement.execute();
}
catch (SQLException e)
{
Emulator.getLogging().logSQLException(e);
}
}
}
public void generateNumbers(int starting, int amount)
{
synchronized (this.limitedNumbers)
{
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("INSERT INTO catalog_items_limited (catalog_item_id, number) VALUES (?, ?)"))
{
statement.setInt(1, this.itemId);
for (int i = starting; i <= amount; i++)
{
statement.setInt(2, i);
statement.addBatch();
this.limitedNumbers.push(i);
}
statement.executeBatch();
}
catch (SQLException e)
{
Emulator.getLogging().logSQLException(e);
}
this.totalSet += amount;
2019-05-12 10:15:24 +02:00
if(Emulator.getConfig().getBoolean("catalog.ltd.random", true)) {
Collections.shuffle(this.limitedNumbers);
}
else {
Collections.reverse(this.limitedNumbers);
}
2018-07-06 15:30:00 +02:00
}
}
public int available()
{
return this.limitedNumbers.size();
}
public int getTotalSet()
{
return this.totalSet;
}
public void setTotalSet(int totalSet)
{
this.totalSet = totalSet;
}
@Override
public void run()
{
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("UPDATE catalog_items SET limited_stack = ?, limited_sells = ? WHERE id = ?"))
{
statement.setInt(1, this.totalSet);
statement.setInt(2, this.totalSet - this.available());
statement.setInt(3, this.itemId);
statement.execute();
}
catch (SQLException e)
{
Emulator.getLogging().logSQLException(e);
}
}
}