Skip to content

Instantly share code, notes, and snippets.

@LianSheng197
Created April 1, 2021 17:14
Show Gist options
  • Save LianSheng197/b552bf49a827da38534c19c7f1a48a00 to your computer and use it in GitHub Desktop.
Save LianSheng197/b552bf49a827da38534c19c7f1a48a00 to your computer and use it in GitHub Desktop.
我們的浮游城戰報解析核心邏輯(以玩家爲例)
// 原本想說要開源,結果後來覺得實在寫得太噁爛
// 再加上不久後會有替代的專案(不是我寫的)
// 所以這裡只放上核心片段
class Report {
static year = 2021;
static month = 3;
static day = 5;
static hour = 19;
static saveData = (faction, value) => {
if (this.now.data[faction] === undefined) {
this.now.data[faction] = 0;
}
this.now.data[faction] += value;
}
static storeData = () => {
this.now.date = this.getTargetDate();
let copy = JSON.parse(JSON.stringify(this.now.data));
this.stored[this.now.date] = copy;
}
static getTargetDate = () => {
let y = this.year;
let m = ("0" + this.month).substr(-2);
let d = ("0" + this.day).substr(-2);
let h = ("0" + this.hour).substr(-2);
return `${y}-${m}-${d} ${h}:00`;
}
static isReachTargetDate = timestamp => {
let thisTime = Date.parse(`${this.year}-${this.month}-${this.day} ${this.hour}:00:00`);
let diff = timestamp - thisTime;
return diff >= 60 * 60 * 1000;
}
static addHour = () => {
let ts = Date.parse(`${this.year}-${this.month}-${this.day} ${this.hour}:00:00`);
ts += 60 * 60 * 1000;
let d = new Date(ts);
this.year = d.getFullYear();
this.month = d.getMonth() + 1;
this.day = d.getDate();
this.hour = d.getHours();
}
static parse = json => {
let time = json.report.time;
let aName = json.report.aName;
let bName = json.report.bName;
let aFaction = json.report.aFactionName;
let bFaction = json.report.bFactionName;
let aHp = json.report.messages.stats.a.hp;
let bHp = json.report.messages.stats.b ? json.report.messages.stats.b.hp : undefined;
let aDamage = 0;
let bDamage = 0;
let messages = json.report.messages.messages.map(e => e.m);
if (bHp) {
// PvP
let killed = messages.filter(e => e.includes("被擊殺身亡了,"));
let flee = messages.filter(e => e.includes("被打得落荒而逃了,"));
let tie = messages.filter(e => e.includes("雙方大戰 16 回合不分勝負!"));
if (tie.length > 0) {
let re = new RegExp(`雙方大戰 16 回合不分勝負!${aName}還有 (\\d+) 點體力;${bName}還有 (\\d+) 點體力`);
let match = tie[0].match(re);
aDamage = bHp - match[2];
bDamage = aHp - match[1];
return {
msg: tie[0],
a: {
player: aName,
initHp: aHp,
faction: aFaction,
damage: aDamage
},
b: {
player: bName,
initHp: bHp,
faction: bFaction,
damage: bDamage
}
};
} else if (flee.length > 0) {
let allDamage = messages.filter(e => e.match(/(.+?攻擊.+?造成\ \d+\ 點傷害)|(對.+?造成\ \d+\ 點傷害)|(.+?又站了起來)/));
allDamage.forEach(e => {
let normalAtk = e.match(/(.+?)攻擊.+?造成\ (\d+)\ 點傷害/);
let skillAtk = e.match(/對(.+?)造成\ (\d+)\ 點傷害/);
let revival = e.match(/(.+?)又站了起來/);
if (normalAtk) {
if (normalAtk[1] == aName) {
aDamage += normalAtk[2] - 0;
} else {
bDamage += normalAtk[2] - 0;
}
} else if (skillAtk) {
if (skillAtk[1] == aName) {
bDamage += skillAtk[2] - 0;
} else {
aDamage += skillAtk[2] - 0;
}
} else if (revival) {
if (revival[1] == aName) {
bDamage = 0;
} else {
aDamage = 0;
}
}
});
return {
/*allDamage: allDamage,*/
msg: flee[0],
a: {
player: aName,
initHp: aHp,
faction: aFaction,
damage: aDamage
},
b: {
player: bName,
initHp: bHp,
faction: bFaction,
damage: bDamage
}
};
} else if (killed.length > 0) {
let re = new RegExp(`(.+?)被擊殺身亡了,.+?還有 (\\d+) 點體力`);
let match = killed[0].match(re);
if (match[1] == aName) {
// 被擊殺者是 a
aDamage = bHp - match[2];
bDamage = aHp;
} else {
// 被擊殺者是 b
aDamage = bHp;
bDamage = aHp - match[2];
}
return {
msg: killed[0],
a: {
player: aName,
initHp: aHp,
faction: aFaction,
damage: aDamage
},
b: {
player: bName,
initHp: bHp,
faction: bFaction,
damage: bDamage
}
};
} else {
throw new Error("Unknown Type");
}
} else {
// Castle
return {
msg: messages[0],
a: {
player: aName,
initHp: aHp,
faction: aFaction,
// damage: 0
damage: messages[0].match(/直接攻擊城堡,造成\ (\d+)\ 點傷害/)[1] - 0
},
b: {
player: bName,
initHp: bHp,
faction: bFaction,
damage: bDamage
}
};
}
}
static now = {
date: this.getTargetDate(),
data: {}
}
static stored = {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment