finish buy logic

This commit is contained in:
zhangyuheng 2024-01-05 00:50:57 +08:00
parent d608243025
commit 48f7277e36
3 changed files with 75 additions and 11 deletions

View File

@ -1,13 +1,36 @@
package cn.lunadeer.newbtitle;
import cn.lunadeer.newbtitle.utils.Database;
import org.bukkit.entity.Player;
import java.util.UUID;
public class PlayerTitle extends Title {
private Long _expire_at = -1L;
private final UUID _player_uuid;
public PlayerTitle(Integer id, Long expire_at) {
super(id);
public PlayerTitle(Integer title_id, UUID player_uuid, Long expire_at) {
super(title_id);
this._player_uuid = player_uuid;
this._expire_at = expire_at;
}
public static PlayerTitle create(Integer title_id, UUID player_uuid) {
String sql = "";
sql += "INSERT INTO nt_player_title (title_id, player_uuid, expire_at) ";
sql += "VALUES (" + title_id + ", '" + player_uuid.toString() + "', -1) ";
sql += "RETURNING id;";
java.sql.ResultSet rs = Database.query(sql);
try {
if (rs != null && rs.next()) {
return new PlayerTitle(title_id, player_uuid, -1L);
}
} catch (Exception e) {
cn.lunadeer.newbtitle.utils.XLogger.err("PlayerTitle create failed: " + e.getMessage());
}
return null;
}
public String getExpireAt() {
if (this._expire_at == -1L) {
return "永久";
@ -18,6 +41,10 @@ public class PlayerTitle extends Title {
}
}
public Long getExpireAtTimestamp() {
return this._expire_at;
}
public Boolean isExpired() {
if (this._expire_at == -1L) {
return false;
@ -25,4 +52,19 @@ public class PlayerTitle extends Title {
return this._expire_at < System.currentTimeMillis();
}
}
public void setExpireAtTimestamp(Long expire_at) {
this._expire_at = expire_at;
this.save();
}
private void save() {
String sql = "";
sql += "UPDATE nt_player_title ";
sql += "SET expire_at = " + this._expire_at + ", ";
sql += "updated_at = CURRENT_TIMESTAMP ";
sql += "WHERE player_uuid = '" + _player_uuid.toString() + "', ";
sql += "AND title_id = " + this._id + ";";
Database.query(sql);
}
}

View File

@ -7,8 +7,7 @@ import cn.lunadeer.newbtitle.utils.XLogger;
import java.sql.ResultSet;
public class SaleTitle extends Title {
private final Integer _id;
private final Integer _title_id;
private Integer _sale_id;
private Integer _price;
private Integer _days;
private Integer _amount;
@ -16,8 +15,8 @@ public class SaleTitle extends Title {
public SaleTitle(Integer id, Integer title_id, Integer price, Integer days, Integer amount, Long sale_end_at) {
super(title_id);
this._id = id;
this._title_id = title_id;
this._sale_id = id;
this._id = title_id;
this._price = price;
this._days = days;
this._amount = amount;
@ -94,13 +93,13 @@ public class SaleTitle extends Title {
private void save() {
String sql = "";
sql += "UPDATE nt_title_shop ";
sql += "SET title_id = " + this._title_id + ", ";
sql += "SET title_id = " + this._id + ", ";
sql += "price = " + this._price + ", ";
sql += "days = " + this._days + ", ";
sql += "amount = " + this._amount + ", ";
sql += "sale_end_at = " + this._sale_end_at + ", ";
sql += "updated_at = CURRENT_TIMESTAMP ";
sql += "WHERE id = " + this._id + ";";
sql += "WHERE id = " + this._sale_id + ";";
Database.query(sql);
}

View File

@ -81,7 +81,7 @@ public class XPlayer {
while (rs != null && rs.next()) {
Integer title_id = rs.getInt("title_id");
Long expire_at = rs.getLong("expire_at");
PlayerTitle title = new PlayerTitle(title_id, expire_at);
PlayerTitle title = new PlayerTitle(title_id, uuid, expire_at);
titles.put(title_id, title);
}
} catch (Exception e) {
@ -151,8 +151,31 @@ public class XPlayer {
Notification.error(_player, "你的余额不足");
return;
}
// todo 校验是否已有此称号 以及是否已过期
// todo 如果已则续费 如果未则购买
if (!_titles.containsKey(title.getId())) {
_titles.put(title.getId(), PlayerTitle.create(title.getId(), _player.getUniqueId()));
}
PlayerTitle title_bought = _titles.get(title.getId());
if (title_bought.getExpireAtTimestamp() == -1) {
Notification.warn(_player, "你已经拥有此称号");
return;
}
set_coin(_coin - title.getPrice());
if (title.getDays() == -1) {
title_bought.setExpireAtTimestamp(-1L);
return;
}
if (title_bought.isExpired()) {
title_bought.setExpireAtTimestamp(System.currentTimeMillis() + title.getDays() * 24 * 60 * 60 * 1000L);
} else {
title_bought.setExpireAtTimestamp(title_bought.getExpireAtTimestamp() + title.getDays() * 24 * 60 * 60 * 1000L);
}
}
public void setTitle(Integer title_id, Long expire_at) {
if (!_titles.containsKey(title_id)) {
_titles.put(title_id, PlayerTitle.create(title_id, _player.getUniqueId()));
}
PlayerTitle title = _titles.get(title_id);
title.setExpireAtTimestamp(expire_at);
}
}