koishi-plugin-eqserver-connect 0.0.1 → 0.0.2

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.d.ts CHANGED
@@ -10,6 +10,7 @@ export interface Config {
10
10
  timezoneOffset: number;
11
11
  minReward: number;
12
12
  maxReward: number;
13
+ currencyField: string;
13
14
  }
14
15
  export declare const Config: Schema<Config>;
15
16
  export declare function apply(ctx: Context, config: Config): void;
package/lib/index.js CHANGED
@@ -34,7 +34,8 @@ const Config = import_koishi.Schema.object({
34
34
  groupIds: import_koishi.Schema.array(import_koishi.Schema.string()).role("table").default([]).description("\u5141\u8BB8\u7B7E\u5230\u7684\u7FA4\u804A ID \u5217\u8868\uFF0C\u53EF\u586B channelId \u6216 platform:channelId\u3002sandbox \u7FA4\u804A\u9ED8\u8BA4\u5141\u8BB8\u3002"),
35
35
  timezoneOffset: import_koishi.Schema.number().default(8).description("\u7B7E\u5230\u65F6\u533A\u504F\u79FB\uFF08\u5C0F\u65F6\uFF09\uFF0C\u9ED8\u8BA4 +8\u3002"),
36
36
  minReward: import_koishi.Schema.number().default(100).description("\u7B7E\u5230\u6700\u5C0F\u5956\u52B1\u3002"),
37
- maxReward: import_koishi.Schema.number().default(500).description("\u7B7E\u5230\u6700\u5927\u5956\u52B1\u3002")
37
+ maxReward: import_koishi.Schema.number().default(500).description("\u7B7E\u5230\u6700\u5927\u5956\u52B1\u3002"),
38
+ currencyField: import_koishi.Schema.string().default("credits").description("\u7B7E\u5230\u5956\u52B1\u5199\u5165\u7684 store_players \u8D27\u5E01\u5B57\u6BB5\u3002")
38
39
  });
39
40
  const HOUR_MS = 60 * 60 * 1e3;
40
41
  function getSignDay(now, timezoneOffset) {
@@ -71,6 +72,16 @@ function getRewardRange(minReward, maxReward) {
71
72
  function randomInt(min, max) {
72
73
  return Math.floor(Math.random() * (max - min + 1)) + min;
73
74
  }
75
+ function normalizeColumnName(columnName) {
76
+ const text = typeof columnName === "string" ? columnName.trim() : "";
77
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(text)) {
78
+ throw new Error("currencyField \u53EA\u80FD\u5305\u542B\u82F1\u6587\u5B57\u6BCD\u3001\u6570\u5B57\u3001\u4E0B\u5212\u7EBF\uFF0C\u4E14\u4E0D\u80FD\u4EE5\u6570\u5B57\u5F00\u5934\u3002");
79
+ }
80
+ return text;
81
+ }
82
+ function quoteIdentifier(identifier) {
83
+ return `"${identifier.replace(/"/g, '""')}"`;
84
+ }
74
85
  function isUniqueViolation(error) {
75
86
  if (!error || typeof error !== "object") return false;
76
87
  return error.code === "23505";
@@ -107,6 +118,8 @@ function apply(ctx, config) {
107
118
  });
108
119
  const allowedGroupIds = normalizeGroupIds(config.groupIds);
109
120
  const [minReward, maxReward] = getRewardRange(config.minReward, config.maxReward);
121
+ const currencyField = normalizeColumnName(config.currencyField || "credits");
122
+ const currencyFieldSql = quoteIdentifier(currencyField);
110
123
  let initialized = false;
111
124
  let initializeTask = null;
112
125
  async function initDatabase() {
@@ -115,7 +128,7 @@ function apply(ctx, config) {
115
128
  initializeTask = (async () => {
116
129
  const playerIdType = await getStorePlayersColumnType(pool, "id");
117
130
  await getStorePlayersColumnType(pool, "authid");
118
- await getStorePlayersColumnType(pool, "credits");
131
+ await getStorePlayersColumnType(pool, currencyField);
119
132
  await pool.query(`
120
133
  CREATE TABLE IF NOT EXISTS koishi_player_bind (
121
134
  id BIGSERIAL PRIMARY KEY,
@@ -283,9 +296,9 @@ function apply(ctx, config) {
283
296
  const creditResult = await client.query(
284
297
  `
285
298
  UPDATE store_players
286
- SET credits = COALESCE(credits, 0) + $1
299
+ SET ${currencyFieldSql} = COALESCE(${currencyFieldSql}, 0) + $1
287
300
  WHERE id = $2
288
- RETURNING credits
301
+ RETURNING ${currencyFieldSql} AS credits
289
302
  `,
290
303
  [reward, bind.player_id]
291
304
  );
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-eqserver-connect",
3
3
  "description": "EQ server PostgreSQL connector with register + daily signin",
4
- "version": "0.0.1",
4
+ "version": "0.0.2",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
package/readme.md CHANGED
@@ -5,7 +5,7 @@ PostgreSQL connector plugin for Koishi:
5
5
  - Register player mapping with `注册 <authid>` (alias: `register`).
6
6
  - Daily sign-in with `签到` (alias: `qd`).
7
7
  - One sign-in per day, reset at 12:00 (configurable timezone offset).
8
- - Rewards `100-500` by default, and directly adds to `store_players.credits`.
8
+ - Rewards `100-500` by default, and directly adds to the configured `store_players` currency field (`credits` by default).
9
9
  - Group whitelist in plugin config (`groupIds`), with sandbox groups allowed by default.
10
10
 
11
11
  ## Required database table
@@ -14,6 +14,8 @@ The plugin expects an existing table:
14
14
 
15
15
  - `store_players(id, authid, credits, ...)`
16
16
 
17
+ Set `currencyField` to use a different currency column, for example `shop_credits`.
18
+
17
19
  It auto-creates:
18
20
 
19
21
  - `koishi_player_bind` (Koishi user -> player binding)