Skip to content

Commit

Permalink
Merge pull request #493 from hanshino/feat/subscribe
Browse files Browse the repository at this point in the history
Update dependencies and add buy month card feature
  • Loading branch information
hanshino authored Jun 6, 2024
2 parents c9b0956 + 80a92aa commit 7d1aecb
Show file tree
Hide file tree
Showing 5 changed files with 783 additions and 152 deletions.
5 changes: 4 additions & 1 deletion app/locales/zh_tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@
"effects": {
"gacha_times": "每日單抽次數",
"daily_ration": "每日寶石"
}
},
"not_enough_money": "女神石不足,無法購買月卡",
"buy_month_card_success": "成功購買月卡,以下是您的月卡序號,請複製再發送此訊息",
"give_serial_nubmer": "#訂閱兌換 {{ serial_number }}"
},
"gacha": {
"not_enough_stone": "女神石不足,無法花錢進行特別抽取",
Expand Down
21 changes: 11 additions & 10 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
"migrate": "knex migrate:latest"
},
"dependencies": {
"@sentry/node": "^7.114.0",
"ajv": "^8.13.0",
"@sentry/node": "^8.7.0",
"ajv": "^8.16.0",
"ajv-formats": "^3.0.1",
"axios": "1.6.8",
"axios": "1.7.2",
"bottender": "1.5.5",
"brotli": "^1.3.3",
"cheerio": "^1.0.0-rc.12",
"config": "^3.3.11",
"cron": "^3.1.7",
"date-format": "^4.0.14",
"express": "^4.19.2",
"express-rate-limit": "^7.2.0",
"express-rate-limit": "^7.3.0",
"human-number": "^2.0.4",
"i18n": "^0.15.1",
"imgur": "^2.4.2",
Expand All @@ -34,8 +34,8 @@
"md5": "^2.3.0",
"minimist": "^1.2.8",
"moment": "^2.30.1",
"mysql2": "^3.9.7",
"redis": "^4.6.13",
"mysql2": "^3.10.0",
"redis": "^4.6.14",
"socket.io": "^4.7.5",
"sqlite3": "^5.1.7",
"table": "^6.8.2",
Expand All @@ -45,11 +45,12 @@
"@types/express": "^4.17.21",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"eslint": "^9.2.0",
"eslint": "^9.4.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"jest": "^29.7.0",
"nodemon": "^3.1.0",
"prettier": "^3.2.5"
}
"nodemon": "^3.1.3",
"prettier": "^3.3.1"
},
"packageManager": "[email protected]+sha1.4ba7fc5c6e704fce2066ecbfb0b0d8976fe62447"
}
1 change: 1 addition & 0 deletions app/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ async function OrderBased(context, { next }) {
...ScratchCardController.router,
...NumberController.router,
...(type === "user" ? JobController.router : []),
...(type === "user" ? SubscribeController.privateRouter : []),
text(/^[/#.](使用說明|help)$/, welcome),
text(/^[/#.]抽(\*(?<times>\d+))?(\s*(?<tag>[\s\S]+))?$/, gacha.play),
text(/^[/#.]消耗抽(\*(?<times>\d+))?(\s*(?<tag>[\s\S]+))?$/, (context, props) =>
Expand Down
75 changes: 75 additions & 0 deletions app/src/controller/application/SubscribeController.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const { text } = require("bottender/router");
const { get } = require("lodash");
const uuid = require("uuid-random");
const SubscribeCard = require("../../model/application/SubscribeCard");
const SubscribeCardCoupon = require("../../model/application/SubscribeCardCoupon");
const SubscribeUser = require("../../model/application/SubscribeUser");
const { inventory: inventoryModel } = require("../../model/application/Inventory");
const DailyRation = require("../../../bin/DailyRation");
const mement = require("moment");
const i18n = require("../../util/i18n");
Expand All @@ -18,6 +20,79 @@ exports.router = [
text(/^[.#/](訂閱兌換|sub-coupon)\s(?<serial_number>[\w-]{36})$/, subscribeCouponExchange),
];

exports.privateRouter = [text(/^[.#/](我要買月卡)\s(?<number>[135]{1})$/, buyMonthCard)];

/**
* 用女神石購買月卡
* @param {import("bottender").LineContext} context
*/
async function buyMonthCard(context, props) {
const { userId } = context.event.source;
const number = parseInt(get(props, "match.groups.number", "1"), 10);
const { amount: ownMoney = 0 } = await inventoryModel.getUserMoney(userId);
let cost = 0;

switch (number) {
case 1:
cost = 50 * 10000;
break;
case 3:
cost = 135 * 10000;
break;
case 5:
cost = 220 * 10000;
break;
default:
throw new Error("Invalid number");
}

if (parseInt(ownMoney) < cost) {
await context.replyText(i18n.__("message.subscribe.not_enough_money"));
return;
}

const trx = await inventoryModel.transaction();
SubscribeCardCoupon.setTransaction(trx);

try {
const coupons = Array.from({ length: number }).map(() => ({
serial_number: uuid(),
subscribe_card_key: "month",
status: SubscribeCardCoupon.status.unused,
issued_by: "system",
}));
await SubscribeCardCoupon.insert(coupons);

await inventoryModel.decreaseGodStone({
userId,
amount: cost,
note: "buy_month_card",
});

context.replyText(i18n.__("message.subscribe.buy_month_card_success"));

const serialMessages = coupons.map(coupon =>
i18n.__("message.subscribe.give_serial_nubmer", {
serial_number: get(coupon, "serial_number"),
})
);

await context.replyText(serialMessages.join("\n"));

trx.commit();
} catch (e) {
trx.rollback();
console.error(e);
await context.replyText(
i18n.__("message.error_contact_admin", {
user_id: userId,
error_key: "buy_month_card",
})
);
return;
}
}

/**
* 訂閱卡片資訊
* @param {import("bottender").LineContext} context
Expand Down
Loading

0 comments on commit 7d1aecb

Please sign in to comment.