koishi-plugin-monetary-bourse 2.0.3-Alpha.11 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +29 -25
- package/lib/templates/holding-card.html +556 -379
- package/lib/templates/stock-chart.html +478 -468
- package/lib/templates/trade-result.html +624 -559
- package/package.json +1 -1
- package/readme.md +2 -66
package/lib/index.js
CHANGED
|
@@ -138,7 +138,7 @@ function apply(ctx, config) {
|
|
|
138
138
|
}
|
|
139
139
|
__name(isMarketOpen, "isMarketOpen");
|
|
140
140
|
async function getCashBalance(uid, currency) {
|
|
141
|
-
if (
|
|
141
|
+
if (uid === void 0 || uid === null || typeof uid !== "number" || Number.isNaN(uid)) {
|
|
142
142
|
logger.warn(`getCashBalance: 无效的uid: ${uid}`);
|
|
143
143
|
return 0;
|
|
144
144
|
}
|
|
@@ -157,7 +157,7 @@ function apply(ctx, config) {
|
|
|
157
157
|
}
|
|
158
158
|
__name(getCashBalance, "getCashBalance");
|
|
159
159
|
async function changeCashBalance(uid, currency, delta) {
|
|
160
|
-
if (
|
|
160
|
+
if (uid === void 0 || uid === null || typeof uid !== "number" || Number.isNaN(uid)) {
|
|
161
161
|
logger.warn(`changeCashBalance: 无效的uid: ${uid}`);
|
|
162
162
|
return false;
|
|
163
163
|
}
|
|
@@ -190,7 +190,7 @@ function apply(ctx, config) {
|
|
|
190
190
|
}
|
|
191
191
|
__name(changeCashBalance, "changeCashBalance");
|
|
192
192
|
async function getBankDemandBalance(uid, currency) {
|
|
193
|
-
if (
|
|
193
|
+
if (uid === void 0 || uid === null || typeof uid !== "number" || Number.isNaN(uid)) return 0;
|
|
194
194
|
try {
|
|
195
195
|
const tables = ctx.database.tables;
|
|
196
196
|
if (!tables || !("monetary_bank_int" in tables)) {
|
|
@@ -211,7 +211,7 @@ function apply(ctx, config) {
|
|
|
211
211
|
}
|
|
212
212
|
__name(getBankDemandBalance, "getBankDemandBalance");
|
|
213
213
|
async function deductBankDemand(uid, currency, amount) {
|
|
214
|
-
if (
|
|
214
|
+
if (uid === void 0 || uid === null || typeof uid !== "number" || Number.isNaN(uid) || amount <= 0) return false;
|
|
215
215
|
try {
|
|
216
216
|
const tables = ctx.database.tables;
|
|
217
217
|
if (!tables || !("monetary_bank_int" in tables)) return false;
|
|
@@ -727,7 +727,7 @@ function apply(ctx, config) {
|
|
|
727
727
|
});
|
|
728
728
|
}
|
|
729
729
|
} else if (txn.type === "sell") {
|
|
730
|
-
if (txn.uid && typeof txn.uid === "number") {
|
|
730
|
+
if (txn.uid !== void 0 && txn.uid !== null && typeof txn.uid === "number" && !Number.isNaN(txn.uid)) {
|
|
731
731
|
const amount = Number(txn.cost.toFixed(2));
|
|
732
732
|
const success = await changeCashBalance(txn.uid, config.currency, amount);
|
|
733
733
|
if (!success) {
|
|
@@ -755,7 +755,7 @@ function apply(ctx, config) {
|
|
|
755
755
|
}));
|
|
756
756
|
}
|
|
757
757
|
__name(getPriceHistory, "getPriceHistory");
|
|
758
|
-
ctx.command("stock [interval:string]", "查看股市行情").action(async ({ session }, interval) => {
|
|
758
|
+
ctx.command("stock [interval:string]", "查看股市行情").userFields(["id"]).action(async ({ session }, interval) => {
|
|
759
759
|
if (["buy", "sell", "my"].includes(interval)) {
|
|
760
760
|
const parts = session.content.trim().split(/\s+/).slice(2);
|
|
761
761
|
const rest = parts.join(" ");
|
|
@@ -815,15 +815,15 @@ function apply(ctx, config) {
|
|
|
815
815
|
return "请输入有效的购买股数(整数)。";
|
|
816
816
|
}
|
|
817
817
|
if (!await isMarketOpen()) return "休市中,无法交易。";
|
|
818
|
-
let uid = session.user?.id;
|
|
819
818
|
const visibleUserId = session.userId;
|
|
820
|
-
if (!
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
819
|
+
if (!session.user || session.user.id === void 0 || session.user.id === null) {
|
|
820
|
+
logger.error(`stock.buy: session.user 不存在或 id 为空 user=${session.userId}`);
|
|
821
|
+
return "无法获取用户ID,请稍后重试。";
|
|
822
|
+
}
|
|
823
|
+
const uid = session.user.id;
|
|
824
|
+
if (typeof uid !== "number") {
|
|
825
|
+
logger.error(`stock.buy: 无法获取数字UID user=${session.userId}, rawId=${uid}`);
|
|
826
|
+
return "无法获取用户ID,请稍后重试。";
|
|
827
827
|
}
|
|
828
828
|
const cost = Number((currentPrice * amount).toFixed(2));
|
|
829
829
|
const payResult = await pay(uid, cost, config.currency);
|
|
@@ -838,7 +838,7 @@ function apply(ctx, config) {
|
|
|
838
838
|
if (freezeMinutes < config.minFreezeTime) freezeMinutes = config.minFreezeTime;
|
|
839
839
|
}
|
|
840
840
|
const freezeMs = freezeMinutes * 60 * 1e3;
|
|
841
|
-
const userPendingOrders = await ctx.database.get("bourse_pending", { userId: visibleUserId }, { sort: { endTime: "desc" }, limit: 1 });
|
|
841
|
+
const userPendingOrders = await ctx.database.get("bourse_pending", { userId: visibleUserId, type: "buy" }, { sort: { endTime: "desc" }, limit: 1 });
|
|
842
842
|
let startTime = /* @__PURE__ */ new Date();
|
|
843
843
|
if (userPendingOrders.length > 0) {
|
|
844
844
|
const lastOrderEndTime = userPendingOrders[0].endTime;
|
|
@@ -901,15 +901,15 @@ function apply(ctx, config) {
|
|
|
901
901
|
return "请输入有效的卖出股数。";
|
|
902
902
|
}
|
|
903
903
|
if (!await isMarketOpen()) return "休市中,无法交易。";
|
|
904
|
-
let uid = session.user?.id;
|
|
905
904
|
const visibleUserId = session.userId;
|
|
906
|
-
if (!
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
905
|
+
if (!session.user || session.user.id === void 0 || session.user.id === null) {
|
|
906
|
+
logger.error(`stock.sell: session.user 不存在或 id 为空 user=${session.userId}`);
|
|
907
|
+
return "无法获取用户ID,请稍后重试。";
|
|
908
|
+
}
|
|
909
|
+
const uid = session.user.id;
|
|
910
|
+
if (typeof uid !== "number") {
|
|
911
|
+
logger.error(`stock.buy: 无法获取数字UID user=${session.userId}, rawId=${uid}`);
|
|
912
|
+
return "无法获取用户ID,请稍后重试。";
|
|
913
913
|
}
|
|
914
914
|
const holding = await ctx.database.get("bourse_holding", { userId: visibleUserId, stockId });
|
|
915
915
|
if (holding.length === 0 || holding[0].amount < amount) {
|
|
@@ -944,7 +944,7 @@ function apply(ctx, config) {
|
|
|
944
944
|
if (freezeMinutes < config.minFreezeTime) freezeMinutes = config.minFreezeTime;
|
|
945
945
|
}
|
|
946
946
|
const freezeMs = freezeMinutes * 60 * 1e3;
|
|
947
|
-
const userPendingOrders = await ctx.database.get("bourse_pending", { userId: visibleUserId }, { sort: { endTime: "desc" }, limit: 1 });
|
|
947
|
+
const userPendingOrders = await ctx.database.get("bourse_pending", { userId: visibleUserId, type: "sell" }, { sort: { endTime: "desc" }, limit: 1 });
|
|
948
948
|
let startTime = /* @__PURE__ */ new Date();
|
|
949
949
|
if (userPendingOrders.length > 0) {
|
|
950
950
|
const lastOrderEndTime = userPendingOrders[0].endTime;
|
|
@@ -1010,7 +1010,11 @@ function apply(ctx, config) {
|
|
|
1010
1010
|
tradeMeta
|
|
1011
1011
|
);
|
|
1012
1012
|
});
|
|
1013
|
-
ctx.command("stock.my", "我的持仓").action(async ({ session }) => {
|
|
1013
|
+
ctx.command("stock.my", "我的持仓").userFields(["id"]).action(async ({ session }) => {
|
|
1014
|
+
if (!session.user || session.user.id === void 0 || session.user.id === null) {
|
|
1015
|
+
logger.error(`stock.my: session.user 不存在或 id 为空 user=${session.userId}`);
|
|
1016
|
+
return "无法获取用户ID,请稍后重试。";
|
|
1017
|
+
}
|
|
1014
1018
|
const userId = session.userId;
|
|
1015
1019
|
const holdings = await ctx.database.get("bourse_holding", { userId });
|
|
1016
1020
|
const pending = await ctx.database.get("bourse_pending", { userId });
|