Skip to content

Commit

Permalink
Merge pull request #531 from hanshino:fix/gacha
Browse files Browse the repository at this point in the history
Add GachaPoolModel to handle transactions in Market
  • Loading branch information
hanshino authored Oct 5, 2024
2 parents 920746e + 087c0a9 commit f380ff6
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 135 deletions.
10 changes: 5 additions & 5 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"migrate": "knex migrate:latest"
},
"dependencies": {
"@sentry/node": "^8.30.0",
"@sentry/node": "^8.33.1",
"ajv": "^8.17.1",
"ajv-formats": "^3.0.1",
"axios": "1.7.7",
Expand All @@ -23,7 +23,7 @@
"cron": "^3.1.7",
"date-format": "^4.0.14",
"express": "^4.21.0",
"express-rate-limit": "^7.4.0",
"express-rate-limit": "^7.4.1",
"human-number": "^2.0.4",
"i18n": "^0.15.1",
"imgur": "^2.4.2",
Expand All @@ -36,7 +36,7 @@
"moment": "^2.30.1",
"mysql2": "^3.11.3",
"redis": "^4.7.0",
"socket.io": "^4.7.5",
"socket.io": "^4.8.0",
"sqlite3": "^5.1.7",
"table": "^6.8.2",
"uuid-random": "^1.3.2"
Expand All @@ -45,11 +45,11 @@
"@types/express": "^4.17.21",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"eslint": "^9.10.0",
"eslint": "^9.12.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"jest": "^29.7.0",
"nodemon": "^3.1.4",
"nodemon": "^3.1.7",
"prettier": "^3.3.3"
},
"packageManager": "yarn@1.22.22"
Expand Down
7 changes: 3 additions & 4 deletions app/src/controller/princess/character.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ async function getAllCharacter() {
}

function convertUrl(url, rarity) {
rarity = parseInt(rarity);
// 如果給的 rarity 是 2 或 4,則推算到最近的合法稀有度
if (rarity === 2) {
rarity = 1;
Expand All @@ -219,14 +218,14 @@ function convertUrl(url, rarity) {
let parts = url.split("/");
let fileName = parts[parts.length - 1];

// 確保 "icon_unit" 不受影響,只修改角色編號部分
let characterId = fileName.replace("icon_unit_", "").split(".")[0];
// 提取角色編號部分 (例如 100511),去除圖片的副檔名 .png
let characterId = fileName.split(".")[0];

// 修改角色編號的第五碼,假設是從 0 開始的第 4 個字元
let newCharacterId = characterId.slice(0, 4) + rarity + characterId.slice(5);

// 構建新的檔案名稱
let newFileName = "icon_unit_" + newCharacterId + ".png";
let newFileName = newCharacterId + ".png";

// 替換新的檔案名稱
parts[parts.length - 1] = newFileName;
Expand Down
2 changes: 1 addition & 1 deletion app/src/controller/princess/gacha.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ async function gacha(context, { match, pickup, ensure = false, europe = false })
const now = moment();
const month = now.month() + 1;
const date = now.date();
const isEventTime = month === 8 && date >= 19 && date <= 20;
const isEventTime = month === 10 && date >= 6 && date <= 11;

// 只有 12/31~1/1 這兩天才會開放歐洲轉蛋池
if (europe && !isEventTime) {
Expand Down
4 changes: 4 additions & 0 deletions app/src/handler/Market/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const MarketDetailModel = require("../../model/application/MarketDetail");
const TradeHistoryModel = require("../../model/application/TradeHistory");
const { model: GachaPoolModel } = require("../../model/princess/gacha");
const { get, isNull } = require("lodash");
const i18n = require("../../util/i18n");
const { inventory: InventoryModel } = require("../../model/application/Inventory");
Expand Down Expand Up @@ -91,11 +92,14 @@ exports.transaction = async (req, res) => {
const delResult = await InventoryModel.deleteUserItem(sellerId, itemId);
if (!delResult) throw i18n.__("api.error.transaction.removeItemFailed");

const character = await GachaPoolModel.find(itemId);

// 2. 從買方身上新增該商品
const invId = await InventoryModel.create({
userId,
itemId,
itemAmount: 1,
attributes: JSON.stringify([{ key: "star", value: get(character, "star", 1) }]),
});
if (!invId) throw i18n.__("api.error.transaction.addItemFailed");

Expand Down
2 changes: 1 addition & 1 deletion app/src/model/application/Inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,5 @@ class Inventory extends base {

exports.inventory = new Inventory({
table: "Inventory",
fillable: ["userId", "itemId", "itemAmount", "note"],
fillable: ["userId", "itemId", "itemAmount", "attributes", "note"],
});
7 changes: 7 additions & 0 deletions app/src/model/princess/gacha/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { getClient } = require("bottender");
const LineClient = getClient("line");
const redis = require("../../../util/redis");
const { get } = require("lodash");
const base = require("../../base");
exports.table = "GachaPool";

exports.all = async (options = {}) => {
Expand Down Expand Up @@ -189,3 +190,9 @@ exports.getCollectedRank = async options => {

return rankDatas;
};

class GachaPool extends base {}

exports.model = new GachaPool({
table: "GachaPool",
});
Loading

0 comments on commit f380ff6

Please sign in to comment.