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

108 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;
2020-05-04 22:24:09 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2018-07-06 15:30:00 +02:00
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collections;
import java.util.LinkedList;
2019-05-26 20:14:53 +02:00
public class CatalogLimitedConfiguration implements Runnable {
2020-05-04 22:24:09 +02:00
private static final Logger LOGGER = LoggerFactory.getLogger(CatalogLimitedConfiguration.class);
2018-07-06 15:30:00 +02:00
private final int itemId;
private final LinkedList<Integer> limitedNumbers;
2019-05-26 20:14:53 +02:00
private int totalSet;
2018-07-06 15:30:00 +02:00
2019-05-26 20:14:53 +02:00
public CatalogLimitedConfiguration(int itemId, LinkedList<Integer> availableNumbers, int totalSet) {
2018-07-06 15:30:00 +02:00
this.itemId = itemId;
this.totalSet = totalSet;
this.limitedNumbers = availableNumbers;
2019-05-12 10:15:24 +02:00
2019-05-26 20:14:53 +02:00
if (Emulator.getConfig().getBoolean("catalog.ltd.random", true)) {
2019-05-12 10:15:24 +02:00
Collections.shuffle(this.limitedNumbers);
2019-05-26 20:14:53 +02:00
} else {
2019-05-12 10:15:24 +02:00
Collections.reverse(this.limitedNumbers);
}
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public int getNumber() {
synchronized (this.limitedNumbers) {
2018-07-06 15:30:00 +02:00
int num = this.limitedNumbers.pop();
2019-05-26 20:14:53 +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;
}
}
2019-05-26 20:14:53 +02:00
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")) {
2018-07-06 15:30:00 +02:00
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();
2019-05-26 20:14:53 +02:00
} catch (SQLException e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught SQL exception", e);
2018-07-06 15:30:00 +02:00
}
}
}
2019-05-26 20:14:53 +02:00
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 (?, ?)")) {
2018-07-06 15:30:00 +02:00
statement.setInt(1, this.itemId);
2019-05-26 20:14:53 +02:00
for (int i = starting; i <= amount; i++) {
2018-07-06 15:30:00 +02:00
statement.setInt(2, i);
statement.addBatch();
this.limitedNumbers.push(i);
}
statement.executeBatch();
2019-05-26 20:14:53 +02:00
} catch (SQLException e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught SQL exception", e);
2018-07-06 15:30:00 +02:00
}
this.totalSet += amount;
2019-05-12 10:15:24 +02:00
2019-05-26 20:14:53 +02:00
if (Emulator.getConfig().getBoolean("catalog.ltd.random", true)) {
2019-05-12 10:15:24 +02:00
Collections.shuffle(this.limitedNumbers);
2019-05-26 20:14:53 +02:00
} else {
2019-05-12 10:15:24 +02:00
Collections.reverse(this.limitedNumbers);
}
2018-07-06 15:30:00 +02:00
}
}
2019-05-26 20:14:53 +02:00
public int available() {
2018-07-06 15:30:00 +02:00
return this.limitedNumbers.size();
}
2019-05-26 20:14:53 +02:00
public int getTotalSet() {
2018-07-06 15:30:00 +02:00
return this.totalSet;
}
2019-05-26 20:14:53 +02:00
public void setTotalSet(int totalSet) {
2018-07-06 15:30:00 +02:00
this.totalSet = totalSet;
}
@Override
2019-05-26 20:14:53 +02:00
public void run() {
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("UPDATE catalog_items SET limited_stack = ?, limited_sells = ? WHERE id = ?")) {
2018-07-06 15:30:00 +02:00
statement.setInt(1, this.totalSet);
statement.setInt(2, this.totalSet - this.available());
statement.setInt(3, this.itemId);
statement.execute();
2019-05-26 20:14:53 +02:00
} catch (SQLException e) {
2020-05-04 22:24:09 +02:00
LOGGER.error("Caught SQL exception", e);
2018-07-06 15:30:00 +02:00
}
}
}