Skip to content

Commit

Permalink
feat: 新增購買月卡功能
Browse files Browse the repository at this point in the history
新增了一個用女神石購買月卡的功能,讓使用者可以購買不同數量的月卡。購買成功後,會生成對應數量的月卡序號,並將序號發送給使用者。
  • Loading branch information
hanshino committed Jun 6, 2024
1 parent 67a5e64 commit 80a92aa
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
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
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

0 comments on commit 80a92aa

Please sign in to comment.