Skip to content

Commit

Permalink
feat: Add capability to sort market data
Browse files Browse the repository at this point in the history
  • Loading branch information
blank038 committed Dec 14, 2023
1 parent 14ac72c commit 75618ba
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.blank038.servermarket.api.handler.sort;

import com.blank038.servermarket.api.handler.sort.impl.DefaultSortHandlerImpl;
import com.blank038.servermarket.api.handler.sort.impl.PriceHighSortHandlerImpl;
import com.blank038.servermarket.api.handler.sort.impl.PriceLowSortHandlerImpl;
import com.blank038.servermarket.internal.data.DataContainer;

/**
* @author Blank038
*/
public abstract class AbstractSortHandler implements SortHandler {
protected final String sortKey;

public AbstractSortHandler(String key) {
this.sortKey = key;
}

public void register() {
DataContainer.SORT_HANDLER_MAP.putIfAbsent(this.sortKey, this);
}

public static void registerDefaults() {
new DefaultSortHandlerImpl().register();
new PriceHighSortHandlerImpl().register();
new PriceLowSortHandlerImpl().register();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.blank038.servermarket.api.handler.sort;

import com.blank038.servermarket.internal.cache.sale.SaleCache;

import java.util.Comparator;
import java.util.Map;

/**
* @author Blank038
*/
public interface SortHandler extends Comparator<SaleCache> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.blank038.servermarket.api.handler.sort.impl;

import com.blank038.servermarket.api.handler.sort.AbstractSortHandler;
import com.blank038.servermarket.internal.cache.sale.SaleCache;

/**
* @author Blank038
*/
public class DefaultSortHandlerImpl extends AbstractSortHandler {

public DefaultSortHandlerImpl() {
super("default");
}

@Override
public int compare(SaleCache o1, SaleCache o2) {
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.blank038.servermarket.api.handler.sort.impl;

import com.blank038.servermarket.api.handler.sort.AbstractSortHandler;
import com.blank038.servermarket.internal.cache.sale.SaleCache;

/**
* @author Blank038
*/
public class PriceHighSortHandlerImpl extends AbstractSortHandler {

public PriceHighSortHandlerImpl() {
super("price_high");
}

@Override
public int compare(SaleCache o1, SaleCache o2) {
if (o1.getPrice() == o2.getPrice()) {
return 0;
}
return o1.getPrice() > o2.getPrice() ? -1 : 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.blank038.servermarket.api.handler.sort.impl;

import com.blank038.servermarket.api.handler.sort.AbstractSortHandler;
import com.blank038.servermarket.internal.cache.sale.SaleCache;

/**
* @author Blank038
*/
public class PriceLowSortHandlerImpl extends AbstractSortHandler {

public PriceLowSortHandlerImpl() {
super("price_low");
}

@Override
public int compare(SaleCache o1, SaleCache o2) {
if (o1.getPrice() == o2.getPrice()) {
return 0;
}
return o1.getPrice() < o2.getPrice() ? -1 : 1;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.blank038.servermarket.internal.data;

import com.blank038.servermarket.api.handler.sort.SortHandler;
import com.blank038.servermarket.internal.plugin.ServerMarket;
import com.blank038.servermarket.internal.command.virtual.VirtualMarketCommand;
import com.blank038.servermarket.api.entity.MarketData;
Expand Down Expand Up @@ -27,8 +28,10 @@
public class DataContainer {
public static final List<String> REGISTERED_COMMAND = new ArrayList<>();
public static final Map<String, List<String>> SALE_TYPES = new HashMap<>();
public static final Map<String, String> SALE_TYPE_DISPLAY_NAME = new HashMap<>();
public static final HashMap<String, MarketData> MARKET_DATA = new HashMap<>();
public static final Map<String, String> SALE_TYPE_DISPLAY_NAME = new HashMap<>(),
SORT_TYPE_DISPLAY_NAME = new HashMap<>();
public static final Map<String, MarketData> MARKET_DATA = new HashMap<>();
public static final Map<String, SortHandler> SORT_HANDLER_MAP = new HashMap<>();

public static void loadData() {
ServerMarket.getInstance().saveResource("types.yml", "types.yml", false, (file) -> {
Expand All @@ -45,6 +48,13 @@ public static void loadData() {
}
}
});
ServerMarket.getInstance().saveResource("sorts.yml", "sorts.yml", false, (file) -> {
DataContainer.SORT_TYPE_DISPLAY_NAME.clear();
FileConfiguration data = YamlConfiguration.loadConfiguration(file);
for (String key : data.getKeys(false)) {
SORT_TYPE_DISPLAY_NAME.put(key, data.getString(key));
}
});
// Save all data
if (!MARKET_DATA.isEmpty() && ServerMarket.getStorageHandler() != null) {
ServerMarket.getStorageHandler().saveAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,16 @@

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author Blank038
*/
public class MarketGui extends AbstractGui {
private static final ExecuteInterface CLICK_FUNC = (e) -> {

};
private final String sourceMarketKey;
private FilterHandler filter;
private int currentPage;
private String currentType = "all", currentSort = "none";
private String currentType = "all", currentSort = "default";

public MarketGui(String sourceMarketKey, int currentPage, FilterHandler filter) {
this.sourceMarketKey = sourceMarketKey;
Expand Down Expand Up @@ -79,32 +77,32 @@ public void openGui(Player player) {
model.setCloseRemove(true);
// 设置界面物品
this.initializeDisplayItem(model, data);
// 开始获取全球市场物品
// Get sale list
// TODO: Optimize logic, slice the data.
Integer[] slots = CommonUtil.formatSlots(data.getString("sale-item-slots"));
String[] keys = ServerMarket.getStorageHandler().getSaleItemsByMarket(this.sourceMarketKey)
.entrySet().stream()
.filter((entry) -> (filter == null || filter.check(entry.getValue())))
.map(Map.Entry::getKey).toArray(String[]::new);
List<SaleCache> saleList = ServerMarket.getStorageHandler().getSaleItemsByMarket(this.sourceMarketKey)
.values().stream()
.filter((entry) -> (filter == null || filter.check(entry)))
.sorted(DataContainer.SORT_HANDLER_MAP.get(this.currentSort))
.collect(Collectors.toList());
// 计算下标
int maxPage = keys.length / slots.length;
maxPage += (keys.length % slots.length) == 0 ? 0 : 1;
int maxPage = saleList.size() / slots.length;
maxPage += (saleList.size() % slots.length) == 0 ? 0 : 1;
// 判断页面是否超标, 如果是的话就设置为第一页
if (currentPage > maxPage) {
currentPage = 1;
}
// 获得额外增加的信息
int start = slots.length * (currentPage - 1), end = slots.length * currentPage;
for (int i = start, index = 0; i < end; i++, index++) {
if (index >= slots.length || i >= keys.length) {
if (index >= slots.length || i >= saleList.size()) {
break;
}
// 开始设置物品
Optional<SaleCache> saleItemOptional = ServerMarket.getStorageHandler().getSaleItem(sourceMarketKey, keys[i]);
if (!saleItemOptional.isPresent()) {
SaleCache saleItem = saleList.get(i);
if (saleItem == null) {
--index;
continue;
}
SaleCache saleItem = saleItemOptional.get();
if ((filter != null && !filter.check(saleItem))) {
--index;
continue;
Expand Down Expand Up @@ -149,19 +147,10 @@ public void openGui(Player player) {
}
break;
case "type":
List<String> types = marketData.getSaleTypes();
if (types.size() <= 1) {
return;
}
int index = types.indexOf(currentType);
if (index == -1 || index == types.size() - 1) {
this.currentType = types.get(0);
} else {
this.currentType = types.get(index + 1);
}
this.filter.setTypeFilter(new TypeFilterImpl(Lists.newArrayList(this.currentType)));
this.openGui(clicker);
clicker.sendMessage(I18n.getStrAndHeader("changeSaleType").replace("%type%", this.getCurrentTypeDisplayName()));
this.nextType(marketData, clicker);
break;
case "sort":
this.nextSort(clicker);
break;
case "store":
new StoreContainerGui(clicker, lastPage, this.sourceMarketKey, this.filter).open(1);
Expand Down Expand Up @@ -222,7 +211,30 @@ private String getCurrentTypeDisplayName() {
}

private String getCurrentSortDisplayName() {
return DataContainer.SALE_TYPE_DISPLAY_NAME.getOrDefault(this.currentSort, this.currentSort);
return DataContainer.SORT_TYPE_DISPLAY_NAME.getOrDefault(this.currentSort, this.currentSort);
}

private void nextType(MarketData marketData, Player clicker) {
List<String> types = marketData.getSaleTypes();
if (types.size() <= 1) {
return;
}
int index = types.indexOf(currentType);
this.currentType = (index == -1 || index == types.size() - 1) ? types.get(0) : types.get(index + 1);
this.filter.setTypeFilter(new TypeFilterImpl(Lists.newArrayList(this.currentType)));
this.openGui(clicker);
clicker.sendMessage(I18n.getStrAndHeader("changeSaleType").replace("%type%", this.getCurrentTypeDisplayName()));
}

private void nextSort(Player clicker) {
List<String> sorts = new ArrayList<>(DataContainer.SORT_HANDLER_MAP.keySet());
if (sorts.size() <= 1) {
return;
}
int index = sorts.indexOf(currentSort);
this.currentSort = (index == -1 || index == sorts.size() - 1) ? sorts.get(0) : sorts.get(index + 1);
this.openGui(clicker);
clicker.sendMessage(I18n.getStrAndHeader("changeSortType").replace("%type%", this.getCurrentSortDisplayName()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.blank038.servermarket.internal.plugin;

import com.aystudio.core.bukkit.plugin.AyPlugin;
import com.blank038.servermarket.api.ServerMarketApi;
import com.blank038.servermarket.api.handler.sort.AbstractSortHandler;
import com.blank038.servermarket.internal.data.convert.LegacyBackup;
import com.blank038.servermarket.dto.AbstractStorageHandler;
import com.blank038.servermarket.dto.IStorageHandler;
Expand Down Expand Up @@ -29,27 +29,26 @@ public class ServerMarket extends AyPlugin {
@Getter
private static ServerMarket instance;
@Getter
private static ServerMarketApi api;
@Getter
@Setter
private static IStorageHandler storageHandler;


@Override
public void onEnable() {
instance = this;
api = new ServerMarketApi();
// 开始载入
// begin loading
this.getConsoleLogger().setPrefix("&f[&eServerMarket&f] &8");
this.loadConfig(true);
// 注册命令、事件及线程
// register command executor
super.getCommand("servermarket").setExecutor(new MainCommand(this));
// 注册事件监听类
// register listeners
new CoreListener().register();
new PlayerCommonListener().register();
if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_13_R1)) {
new PlayerLatestListener().register();
}
// register sort handler
AbstractSortHandler.registerDefaults();
// start tasks
Bukkit.getScheduler().runTaskTimerAsynchronously(this, storageHandler::removeTimeOutItem, 200L, 200L);
Bukkit.getScheduler().runTaskTimerAsynchronously(this, storageHandler::saveAll, 1200L, 1200L);
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/language/en_US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ no-permission: "You do not have permission to do that."
lack-money: "You do not have enough %economy% to purchase this item."
shout-tax: "You do not have enough %economy% to pay the tax fee."
changeSaleType: "Switched category. Current category: &b%type%"
changeSortType: "Switched sort, Current sort: &b%type%"
cooldown: "You cannot do this, it is in cooldown."
maximum-sale: "You have reached the maximum number of listings."
show:
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/language/zh_CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ no-permission: "你没有权限这么做."
lack-money: "你没有足够的%economy%来购买这个商品."
shout-tax: "你没有足够的%economy%来上缴税费."
changeSaleType: "已切换分类, 当前分类: &b%type%"
changeSortType: "已切换排序方式, 当前排序: &b%type%"
cooldown: "你不能这么做, 正在冷却中."
maximum-sale: "你已经达到上架数量上限."
show:
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/sorts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
default: "默认"
price_high: "价格高到低"
price_low: "价格低到高"

0 comments on commit 75618ba

Please sign in to comment.