@lzpenguin/economy 1.0.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.
Files changed (4) hide show
  1. package/README.md +78 -0
  2. package/index.d.ts +82 -0
  3. package/index.js +158 -0
  4. package/package.json +37 -0
package/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # @lzpenguin/economy
2
+
3
+ Riffle 经济系统客户端 SDK,用于在游戏中管理用户的金币和钻石。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @lzpenguin/economy
9
+ ```
10
+
11
+ ## 快速开始
12
+
13
+ ```javascript
14
+ import { RiffleEconomy } from '@lzpenguin/economy';
15
+
16
+ // 创建实例(token 会自动从 URL 参数 ?token=xxx 中读取)
17
+ const economy = new RiffleEconomy({
18
+ baseURL: 'https://api.riffle.app'
19
+ });
20
+
21
+ // 查询账户余额
22
+ const account = await economy.query();
23
+ console.log('金币:', account.coin, '钻石:', account.diamond);
24
+
25
+ // 使用货币(带错误处理)
26
+ try {
27
+ const result = await economy.consume('coin', 10);
28
+ console.log('✅ 使用成功,剩余金币:', result.coin);
29
+ } catch (error) {
30
+ console.error('❌ 失败:', error.message);
31
+ }
32
+
33
+ // 充值货币
34
+ const recharged = await economy.recharge('diamond', 50);
35
+ console.log('充值后钻石:', recharged.diamond);
36
+ ```
37
+
38
+ ## API
39
+
40
+ ### `new RiffleEconomy(options)`
41
+ 创建实例
42
+ - `options.baseURL` - API 服务器 URL
43
+
44
+ ### `query()`
45
+ 查询账户余额
46
+ - **返回:** `Promise<{coin: number, diamond: number}>`
47
+
48
+ ### `consume(type, num)`
49
+ 使用货币
50
+ - `type` - 货币类型:`'coin'` 或 `'diamond'`
51
+ - `num` - 数量(必须 > 0)
52
+ - **返回:** `Promise<{coin: number, diamond: number}>`
53
+
54
+ ### `recharge(type, num)`
55
+ 充值货币
56
+ - `type` - 货币类型:`'coin'` 或 `'diamond'`
57
+ - `num` - 数量(必须 > 0)
58
+ - **返回:** `Promise<{coin: number, diamond: number}>`
59
+
60
+ ## TypeScript
61
+
62
+ ```typescript
63
+ import { RiffleEconomy, CurrencyType, AccountInfo } from '@lzpenguin/economy';
64
+
65
+ const economy = new RiffleEconomy({ baseURL: 'https://api.riffle.app' });
66
+ const account: AccountInfo = await economy.query();
67
+ ```
68
+
69
+ ## 常见错误
70
+
71
+ - `token not found in browser URL parameters` - URL 缺少 token 参数
72
+ - `type must be "coin" or "diamond"` - 货币类型错误
73
+ - `num must be greater than 0` - 数量必须大于 0
74
+ - `金币余额不足` / `钻石余额不足` - 余额不足
75
+
76
+ ## 许可证
77
+
78
+ ISC
package/index.d.ts ADDED
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Riffle 经济系统客户端 SDK
3
+ */
4
+
5
+ /**
6
+ * RiffleEconomy 构造函数选项
7
+ */
8
+ export interface RiffleEconomyOptions {
9
+ /** API 服务器 URL(例如:'https://api.riffle.app') */
10
+ baseURL: string;
11
+ }
12
+
13
+ /**
14
+ * 货币类型
15
+ */
16
+ export type CurrencyType = 'coin' | 'diamond';
17
+
18
+ /**
19
+ * 账户信息
20
+ */
21
+ export interface AccountInfo {
22
+ /** 金币数量 */
23
+ coin: number;
24
+ /** 钻石数量 */
25
+ diamond: number;
26
+ }
27
+
28
+ /**
29
+ * RiffleEconomy - 经济系统客户端
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * import { RiffleEconomy } from '@lzpenguin/economy';
34
+ *
35
+ * const economy = new RiffleEconomy({
36
+ * baseURL: 'https://api.riffle.app'
37
+ * // token 会自动从浏览器 URL 参数中读取
38
+ * // 例如:https://example.com/game.html?token=xxx
39
+ * });
40
+ *
41
+ * // 查询账户余额
42
+ * const account = await economy.query();
43
+ * console.log('Coin:', account.coin);
44
+ * console.log('Diamond:', account.diamond);
45
+ *
46
+ * // 使用货币
47
+ * const result = await economy.consume('coin', 10);
48
+ *
49
+ * // 充值货币
50
+ * const recharged = await economy.recharge('diamond', 50);
51
+ * ```
52
+ */
53
+ export class RiffleEconomy {
54
+ /**
55
+ * 创建 RiffleEconomy 实例
56
+ * @param options 配置选项
57
+ */
58
+ constructor(options: RiffleEconomyOptions);
59
+
60
+ /**
61
+ * 查询用户货币账户
62
+ * @returns 账户信息,包含 coin 和 diamond
63
+ */
64
+ query(): Promise<AccountInfo>;
65
+
66
+ /**
67
+ * 使用货币
68
+ * @param type 货币类型 ('coin' 或 'diamond')
69
+ * @param num 使用数量(必须大于 0)
70
+ * @returns 更新后的账户信息
71
+ */
72
+ consume(type: CurrencyType, num: number): Promise<AccountInfo>;
73
+
74
+ /**
75
+ * 充值货币
76
+ * @param type 货币类型 ('coin' 或 'diamond')
77
+ * @param num 充值数量(必须大于 0)
78
+ * @returns 更新后的账户信息
79
+ */
80
+ recharge(type: CurrencyType, num: number): Promise<AccountInfo>;
81
+ }
82
+
package/index.js ADDED
@@ -0,0 +1,158 @@
1
+ // 检测运行环境
2
+ let isBrowser = typeof window !== 'undefined';
3
+
4
+ /**
5
+ * RiffleEconomy - 经济系统客户端
6
+ *
7
+ * @example
8
+ * ```js
9
+ * import { RiffleEconomy } from '@lzpenguin/economy';
10
+ *
11
+ * const economy = new RiffleEconomy({
12
+ * baseURL: 'https://api.riffle.app'
13
+ * // token 会自动从浏览器 URL 参数中读取
14
+ * // 例如:https://example.com/game.html?token=xxx
15
+ * });
16
+ *
17
+ * // 查询账户余额
18
+ * const account = await economy.query();
19
+ * console.log('Coin:', account.coin);
20
+ * console.log('Diamond:', account.diamond);
21
+ *
22
+ * // 使用货币
23
+ * const result = await economy.consume('coin', 10);
24
+ * console.log('Remaining coin:', result.coin);
25
+ *
26
+ * // 充值货币
27
+ * const recharged = await economy.recharge('diamond', 50);
28
+ * console.log('New diamond:', recharged.diamond);
29
+ * ```
30
+ */
31
+ export class RiffleEconomy {
32
+ /**
33
+ * @param {Object} options - 配置选项
34
+ * @param {string} options.baseURL - API 服务器 URL(例如:'https://api.riffle.app')
35
+ */
36
+ constructor(options) {
37
+ const { baseURL } = options;
38
+
39
+ if (!baseURL) {
40
+ throw new Error('baseURL is required');
41
+ }
42
+
43
+ this.baseURL = baseURL.replace(/\/$/, ''); // 移除末尾的斜杠
44
+
45
+ // 从浏览器 URL 参数中读取 token
46
+ let token;
47
+
48
+ if (isBrowser && typeof window !== 'undefined' && window.location) {
49
+ const urlParams = new URLSearchParams(window.location.search);
50
+ if (urlParams.has('token')) {
51
+ token = urlParams.get('token');
52
+ }
53
+ }
54
+
55
+ if (!token) {
56
+ throw new Error('token not found in browser URL parameters. Please ensure URL contains ?token=xxx');
57
+ }
58
+
59
+ this.token = token;
60
+ }
61
+
62
+ /**
63
+ * 发送 HTTP 请求
64
+ * @private
65
+ * @param {string} path - API 路径
66
+ * @param {Object} [body] - 请求体(可选)
67
+ * @returns {Promise<Object>} 响应数据
68
+ */
69
+ async _request(path, body = null) {
70
+ const url = `${this.baseURL}${path}`;
71
+
72
+ const options = {
73
+ method: 'POST',
74
+ headers: {
75
+ 'Content-Type': 'application/json',
76
+ 'Authorization': `Bearer ${this.token}`
77
+ }
78
+ };
79
+
80
+ if (body) {
81
+ options.body = JSON.stringify(body);
82
+ }
83
+
84
+ try {
85
+ const response = await fetch(url, options);
86
+
87
+ if (!response.ok) {
88
+ const errorData = await response.json().catch(() => ({}));
89
+ throw new Error(errorData.message || `HTTP ${response.status}: ${response.statusText}`);
90
+ }
91
+
92
+ const data = await response.json();
93
+
94
+ // 检查响应中是否有错误
95
+ if (data.code !== 0) {
96
+ throw new Error(data.message || 'API request failed');
97
+ }
98
+
99
+ return data.data;
100
+ } catch (error) {
101
+ console.error('[RiffleEconomy] Request failed:', error);
102
+ throw error;
103
+ }
104
+ }
105
+
106
+ /**
107
+ * 查询用户货币账户
108
+ * @returns {Promise<{coin: number, diamond: number}>} 账户信息
109
+ * @example
110
+ * const account = await economy.query();
111
+ * console.log('Coin:', account.coin);
112
+ * console.log('Diamond:', account.diamond);
113
+ */
114
+ async query() {
115
+ return await this._request('/api/v1/economy/query');
116
+ }
117
+
118
+ /**
119
+ * 使用货币
120
+ * @param {string} type - 货币类型 ('coin' 或 'diamond')
121
+ * @param {number} num - 使用数量
122
+ * @returns {Promise<{coin: number, diamond: number}>} 更新后的账户信息
123
+ * @example
124
+ * const result = await economy.consume('coin', 10);
125
+ * console.log('Remaining coin:', result.coin);
126
+ */
127
+ async consume(type, num) {
128
+ if (!type || (type !== 'coin' && type !== 'diamond')) {
129
+ throw new Error('type must be "coin" or "diamond"');
130
+ }
131
+ if (!num || num <= 0) {
132
+ throw new Error('num must be greater than 0');
133
+ }
134
+
135
+ return await this._request('/api/v1/economy/consume', { type, num });
136
+ }
137
+
138
+ /**
139
+ * 充值货币
140
+ * @param {string} type - 货币类型 ('coin' 或 'diamond')
141
+ * @param {number} num - 充值数量
142
+ * @returns {Promise<{coin: number, diamond: number}>} 更新后的账户信息
143
+ * @example
144
+ * const result = await economy.recharge('diamond', 50);
145
+ * console.log('New diamond:', result.diamond);
146
+ */
147
+ async recharge(type, num) {
148
+ if (!type || (type !== 'coin' && type !== 'diamond')) {
149
+ throw new Error('type must be "coin" or "diamond"');
150
+ }
151
+ if (!num || num <= 0) {
152
+ throw new Error('num must be greater than 0');
153
+ }
154
+
155
+ return await this._request('/api/v1/economy/recharge', { type, num });
156
+ }
157
+ }
158
+
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@lzpenguin/economy",
3
+ "version": "1.0.0",
4
+ "description": "Riffle 经济系统客户端 SDK",
5
+ "license": "ISC",
6
+ "author": "lzpenguin",
7
+ "type": "module",
8
+ "main": "./index.js",
9
+ "types": "./index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "import": "./index.js",
13
+ "types": "./index.d.ts"
14
+ }
15
+ },
16
+ "files": [
17
+ "index.js",
18
+ "index.d.ts",
19
+ "README.md"
20
+ ],
21
+ "keywords": [
22
+ "riffle",
23
+ "economy",
24
+ "currency"
25
+ ],
26
+ "dependencies": {},
27
+ "devDependencies": {
28
+ "@types/node": "^20.10.0"
29
+ },
30
+ "scripts": {
31
+ "test": "echo \"Error: no test specified\" && exit 1"
32
+ },
33
+ "publishConfig": {
34
+ "registry": "https://registry.npmjs.org/"
35
+ }
36
+ }
37
+