Skip to content

Commit

Permalink
Merge pull request #526 from hanshino:feature/attack-message
Browse files Browse the repository at this point in the history
Add attack message template tags functionality
  • Loading branch information
hanshino authored Sep 16, 2024
2 parents 1ffb356 + a090ecc commit 920746e
Show file tree
Hide file tree
Showing 9 changed files with 444 additions and 279 deletions.
10 changes: 0 additions & 10 deletions app/_error.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
const Sentry = require("@sentry/node");
// or use es6 import statements
// import * as Sentry from '@sentry/node';

Sentry.init({ dsn: process.env.SENTRY_DSN });

module.exports = async function HandleError(context, props) {
console.error(props.error.stack);

if (process.env.NODE_ENV === "production") {
Sentry.captureException(props.error);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function (knex) {
return knex.schema.createTable("attack_message_has_tags", table => {
table.increments("id").primary();
table.integer("attack_message_id").unsigned().notNullable();
table.string("tag").notNullable();
});
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function (knex) {
return knex.schema.dropTable("attack_message_has_tags");
};
8 changes: 4 additions & 4 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.28.0",
"@sentry/node": "^8.30.0",
"ajv": "^8.17.1",
"ajv-formats": "^3.0.1",
"axios": "1.7.7",
Expand All @@ -22,7 +22,7 @@
"config": "^3.3.12",
"cron": "^3.1.7",
"date-format": "^4.0.14",
"express": "^4.19.2",
"express": "^4.21.0",
"express-rate-limit": "^7.4.0",
"human-number": "^2.0.4",
"i18n": "^0.15.1",
Expand All @@ -34,7 +34,7 @@
"md5": "^2.3.0",
"minimist": "^1.2.8",
"moment": "^2.30.1",
"mysql2": "^3.11.0",
"mysql2": "^3.11.3",
"redis": "^4.7.0",
"socket.io": "^4.7.5",
"sqlite3": "^5.1.7",
Expand All @@ -52,5 +52,5 @@
"nodemon": "^3.1.4",
"prettier": "^3.3.3"
},
"packageManager": "yarn@1.22.19+sha1.4ba7fc5c6e704fce2066ecbfb0b0d8976fe62447"
"packageManager": "yarn@1.22.22"
}
16 changes: 11 additions & 5 deletions app/src/controller/application/WorldBossController.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ async function revokeAttack(context) {
}

// 確認是否已出完刀
const todayLogs = await worldBossEventLogService.getTodayLogs(id);
if (todayLogs.length !== 10) {
const result = await worldBossEventLogService.getTodayCost(id);
const totalCost = isNull(result.totalCost) ? 0 : parseInt(result.totalCost);
if (totalCost < config.get("worldboss.daily_limit")) {
context.replyText(i18n.__("message.world_boss.revoke_attack_not_enough_times"), {
quoteToken,
});
Expand Down Expand Up @@ -201,7 +202,7 @@ async function attack(context, { attackType = "normal" }) {

/**
* 取得正在進行中的世界事件的 ID。
* @returns {string|null} 正在進行中的世界事件的 ID,若無則返回 null。
* @returns {Promise<Number>} 世界事件 ID
*/
async function getHoldingEventId() {
// 取得正在進行中的世界事件
Expand Down Expand Up @@ -515,8 +516,13 @@ const attackOnBoss = async (context, props) => {
await worldBossEventLogService.create(attributes);

// 隨機取得此次攻擊的訊息樣板
let messageTemplates = await worldBossUserAttackMessageService.all();
let templateData = messageTemplates[Math.floor(Math.random() * messageTemplates.length)];
const messageTemplates = await worldBossUserAttackMessageService.all();
const tags = await worldBossUserAttackMessageService.getTags();
// 先抽取 tag
const tag = sample(tags);
// 再根據 tag 抽取訊息樣板
const templateData = sample(messageTemplates.filter(data => data.tag === tag));

let causedDamagePercent = calculateDamagePercentage(eventBoss.hp, damage);
let earnedExp = (eventBoss.exp * causedDamagePercent) / 100;
// 計算因等級差距的關係是否進行經驗值懲罰
Expand Down
15 changes: 15 additions & 0 deletions app/src/model/application/AttackMessageTags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const base = require("../base");

class AttackMessageTags extends base {
/**
* Get all tags
* @returns {Promise<string[]>}
*/
async getTags() {
return this.knex.distinct("tag").then(result => result.map(item => item.tag));
}
}

module.exports = new AttackMessageTags({
table: "attack_message_has_tags",
});
9 changes: 8 additions & 1 deletion app/src/model/application/WorldBossUserAttackMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ const pick = require("lodash/pick");
exports.table = TABLE;

exports.all = async () => {
return await mysql.select("*").from(TABLE);
return await mysql
.select("*")
.from(TABLE)
.join(
"attack_message_has_tags",
"world_boss_user_attack_message.id",
"attack_message_has_tags.attack_message_id"
);
};

exports.find = async id => {
Expand Down
18 changes: 18 additions & 0 deletions app/src/service/WorldBossUserAttackMessageService.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const worldBossUserAttackMessageModel = require("../model/application/WorldBossUserAttackMessage");
const AttackMessageTags = require("../model/application/AttackMessageTags");
const redis = require("../util/redis");

exports.all = async (cache = true) => {
Expand All @@ -13,6 +14,23 @@ exports.all = async (cache = true) => {
return result;
};

/**
* Get all tags
* @param {Boolean} cache
* @returns {Promise<string[]>}
*/
exports.getTags = async (cache = true) => {
let key = "attackMessageTags";
let data = await redis.get(key);
if (data && cache) {
return JSON.parse(data);
}

let result = await AttackMessageTags.getTags();
redis.set(key, JSON.stringify(result));
return result;
};

exports.find = worldBossUserAttackMessageModel.find;

exports.delete = async id => {
Expand Down
Loading

0 comments on commit 920746e

Please sign in to comment.