mirror of
https://github.com/unanmed/HumanBreak.git
synced 2026-11-01 07:24:30 +08:00
完成度成就
This commit is contained in:
1 parent
5dbe7654a4
commit
2ea95671a5
22 files changed
+279
-66
No files matched your search
@@ -0,0 +1,108 @@
|
||||
import {
|
||||
AchievementType,
|
||||
completeAchievement,
|
||||
hasCompletedAchievement
|
||||
} from './ui/achievement';
|
||||
import { changeLocalStorage } from './utils';
|
||||
import list from '../data/achievement.json';
|
||||
|
||||
export default function init() {
|
||||
return { getChapterCompletion, checkVisitedFloor };
|
||||
}
|
||||
|
||||
export const floors: Record<number, FloorIds[]> = {
|
||||
1: ['MT0', 'tower7']
|
||||
};
|
||||
const achis: Record<number, Record<AchievementType, number[]>> = {
|
||||
1: {
|
||||
normal: [0, 1],
|
||||
challenge: [0],
|
||||
explore: [1]
|
||||
}
|
||||
};
|
||||
|
||||
export const achiDict: Record<number, number> = {
|
||||
1: 0
|
||||
};
|
||||
|
||||
/**
|
||||
* 检查所有到达过的楼层,用于成就的计算
|
||||
*/
|
||||
export function checkVisitedFloor() {
|
||||
changeLocalStorage<Partial<Record<FloorIds, boolean>>>(
|
||||
'visitedFloor',
|
||||
data => {
|
||||
let needUpdate = false;
|
||||
core.floorIds.forEach(v => {
|
||||
if (core.hasVisitedFloor(v)) {
|
||||
data[v] = true;
|
||||
needUpdate = true;
|
||||
}
|
||||
});
|
||||
if (needUpdate) {
|
||||
checkCompletionAchievement();
|
||||
}
|
||||
|
||||
return data;
|
||||
},
|
||||
{}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个章节的完成度
|
||||
* @param num 章节
|
||||
*/
|
||||
export function getChapterCompletion(num: number) {
|
||||
if (!achis[num]) return 0;
|
||||
let res = 0;
|
||||
const all = floors[num];
|
||||
const achiNum = Object.values(achis[num]).reduce(
|
||||
(pre, cur) => pre + cur.length,
|
||||
0
|
||||
);
|
||||
|
||||
// 计算到达过的楼层
|
||||
let visitedFloor = 0;
|
||||
const visited = core.getLocalStorage<Partial<Record<FloorIds, boolean>>>(
|
||||
'visitedFloor',
|
||||
{}
|
||||
);
|
||||
all.forEach(v => {
|
||||
if (visited[v]) visitedFloor++;
|
||||
});
|
||||
const floorRatio = all.length / (all.length + achiNum);
|
||||
const floorPoint = (floorRatio * visitedFloor) / all.length;
|
||||
|
||||
let completedPoint = 0;
|
||||
let totalPoint = 0;
|
||||
|
||||
// 计算成就,占比按成就点走
|
||||
for (const [type, achi] of Object.entries(achis[num]) as [
|
||||
AchievementType,
|
||||
number[]
|
||||
][]) {
|
||||
achi.forEach(v => {
|
||||
totalPoint += list[type][v].point;
|
||||
if (hasCompletedAchievement(type, v)) {
|
||||
completedPoint += list[type][v].point;
|
||||
}
|
||||
});
|
||||
}
|
||||
const achiPoint = (completedPoint / totalPoint) * (1 - floorRatio);
|
||||
|
||||
res = floorPoint + achiPoint;
|
||||
|
||||
return Math.floor(res * 100);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查完成度成就是否完成
|
||||
*/
|
||||
export function checkCompletionAchievement() {
|
||||
[1].forEach(v => {
|
||||
if (getChapterCompletion(v) >= 100) {
|
||||
completeAchievement('explore', achiDict[v]);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { ref } from 'vue';
|
||||
import list from '../../data/achievement.json';
|
||||
import { has } from '../utils';
|
||||
import { achiDict, checkCompletionAchievement } from '../completion';
|
||||
import { changeLocalStorage, has } from '../utils';
|
||||
|
||||
type AchievementList = typeof list;
|
||||
export type AchievementType = keyof AchievementList;
|
||||
@@ -17,7 +18,7 @@ export interface Achievement {
|
||||
}
|
||||
|
||||
export default function init() {
|
||||
return {};
|
||||
return { completeAchievement, hasCompletedAchievement, addMountSign };
|
||||
}
|
||||
|
||||
export const showComplete = ref(false);
|
||||
@@ -37,13 +38,22 @@ export const totalPoint = Object.values(list)
|
||||
* @param index 成就索引
|
||||
*/
|
||||
export function completeAchievement(type: AchievementType, index: number) {
|
||||
const now = core.getLocalStorage<AchievementData>('achievement', {
|
||||
normal: [],
|
||||
challenge: [],
|
||||
explore: []
|
||||
});
|
||||
now[type][index] = true;
|
||||
core.setLocalStorage('achievement', now);
|
||||
if (flags.debug) return;
|
||||
changeLocalStorage<AchievementData>(
|
||||
'achievement',
|
||||
data => {
|
||||
data[type][index] = true;
|
||||
return data;
|
||||
},
|
||||
{
|
||||
normal: [],
|
||||
challenge: [],
|
||||
explore: []
|
||||
}
|
||||
);
|
||||
if (type === 'explore' && !Object.values(achiDict).includes(index)) {
|
||||
checkCompletionAchievement();
|
||||
}
|
||||
completeAchi.value = `${type},${index}`;
|
||||
showComplete.value = true;
|
||||
}
|
||||
@@ -81,3 +91,32 @@ export function getNowPoint() {
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// ----- 各个成就相关的函数
|
||||
|
||||
/**
|
||||
* 山路木牌
|
||||
* @param id 木牌id
|
||||
*/
|
||||
export function addMountSign(id: number) {
|
||||
if (flags.debug) return;
|
||||
if (
|
||||
!core.getLocalStorage(`mountSign_${id}`, false) &&
|
||||
!hasCompletedAchievement('explore', 1)
|
||||
) {
|
||||
changeLocalStorage(
|
||||
'mountSign',
|
||||
n => {
|
||||
if (n + 1 >= 5) {
|
||||
completeAchievement('explore', 1);
|
||||
for (const i of [1, 2, 3, 4, 5]) {
|
||||
core.removeLocalStorage(`mountSign_${i}`);
|
||||
}
|
||||
}
|
||||
return n + 1;
|
||||
},
|
||||
0
|
||||
);
|
||||
core.setLocalStorage(`mountSign_${id}`, true);
|
||||
}
|
||||
}
|
||||
@@ -95,7 +95,8 @@ export default function init() {
|
||||
shopOpened,
|
||||
startOpened,
|
||||
startAnimationEnded,
|
||||
loaded
|
||||
loaded,
|
||||
achievementOpened
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
+17
-1
@@ -15,7 +15,7 @@ type CanParseCss = keyof {
|
||||
};
|
||||
|
||||
export default function init() {
|
||||
return { has, getDamageColor, parseCss, tip };
|
||||
return { has, getDamageColor, parseCss, tip, changeLocalStorage };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -212,3 +212,19 @@ export async function doByInterval(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更改一个本地存储
|
||||
* @param name 要更改的信息
|
||||
* @param fn 更改时执行的函数
|
||||
* @param defaultValue 如果不存在时获取的默认值
|
||||
*/
|
||||
export function changeLocalStorage<T>(
|
||||
name: string,
|
||||
fn: (data: T) => T,
|
||||
defaultValue?: T
|
||||
) {
|
||||
const now = core.getLocalStorage(name, defaultValue);
|
||||
const to = fn(now);
|
||||
core.setLocalStorage(name, to);
|
||||
}
|
||||
Reference in new issue
Block a user