fix: 分数缓存逻辑调整

This commit is contained in:
streakingman 2022-10-12 23:02:30 +08:00
parent 9682d31c49
commit a8a6312e2a
6 changed files with 61 additions and 19 deletions

View File

@ -6,6 +6,7 @@ import {
LAST_SCORE_STORAGE_KEY, LAST_SCORE_STORAGE_KEY,
LAST_TIME_STORAGE_KEY, LAST_TIME_STORAGE_KEY,
PLAYING_THEME_ID_STORAGE_KEY, PLAYING_THEME_ID_STORAGE_KEY,
resetScoreStorage,
wrapThemeDefaultSounds, wrapThemeDefaultSounds,
} from './utils'; } from './utils';
import { Theme } from './themes/interface'; import { Theme } from './themes/interface';
@ -18,11 +19,6 @@ const ThemeChanger = React.lazy(() => import('./components/ThemeChanger'));
const ConfigDialog = React.lazy(() => import('./components/ConfigDialog')); const ConfigDialog = React.lazy(() => import('./components/ConfigDialog'));
const WxQrCode = React.lazy(() => import('./components/WxQrCode')); const WxQrCode = React.lazy(() => import('./components/WxQrCode'));
// 读取缓存关卡得分
const initLevel = Number(localStorage.getItem(LAST_LEVEL_STORAGE_KEY) || '1');
const initScore = Number(localStorage.getItem(LAST_SCORE_STORAGE_KEY) || '0');
const initTime = Number(localStorage.getItem(LAST_TIME_STORAGE_KEY) || '0');
const App: FC<{ theme: Theme<any> }> = ({ theme: initTheme }) => { const App: FC<{ theme: Theme<any> }> = ({ theme: initTheme }) => {
console.log('initTheme', initTheme); console.log('initTheme', initTheme);
// console.log(JSON.stringify(theme)); // console.log(JSON.stringify(theme));
@ -30,8 +26,23 @@ const App: FC<{ theme: Theme<any> }> = ({ theme: initTheme }) => {
const [theme, setTheme] = useState<Theme<any>>(initTheme); const [theme, setTheme] = useState<Theme<any>>(initTheme);
const [diyDialogShow, setDiyDialogShow] = useState<boolean>(false); const [diyDialogShow, setDiyDialogShow] = useState<boolean>(false);
// 读取缓存关卡得分
const [initLevel, setInitLevel] = useState<number>(
Number(localStorage.getItem(LAST_LEVEL_STORAGE_KEY) || '1')
);
const [initScore, setInitScore] = useState<number>(
Number(localStorage.getItem(LAST_SCORE_STORAGE_KEY) || '0')
);
const [initTime, setInitTime] = useState<number>(
Number(localStorage.getItem(LAST_TIME_STORAGE_KEY) || '0')
);
const changeTheme = (theme: Theme<any>) => { const changeTheme = (theme: Theme<any>) => {
sessionStorage.setItem(PLAYING_THEME_ID_STORAGE_KEY, theme.title); localStorage.setItem(PLAYING_THEME_ID_STORAGE_KEY, theme.title);
setInitLevel(1);
setInitScore(0);
setInitTime(0);
resetScoreStorage();
wrapThemeDefaultSounds(theme); wrapThemeDefaultSounds(theme);
domRelatedOptForTheme(theme); domRelatedOptForTheme(theme);
setTheme({ ...theme }); setTheme({ ...theme });

View File

@ -12,6 +12,7 @@ import {
LAST_SCORE_STORAGE_KEY, LAST_SCORE_STORAGE_KEY,
LAST_TIME_STORAGE_KEY, LAST_TIME_STORAGE_KEY,
randomString, randomString,
resetScoreStorage,
timestampToUsedTimeString, timestampToUsedTimeString,
waitTimeout, waitTimeout,
} from '../utils'; } from '../utils';
@ -170,7 +171,6 @@ const Game: FC<{
Record<MySymbol['id'], number> Record<MySymbol['id'], number>
>({}); >({});
const [finished, setFinished] = useState<boolean>(false); const [finished, setFinished] = useState<boolean>(false);
const [tipText, setTipText] = useState<string>('');
const [animating, setAnimating] = useState<boolean>(false); const [animating, setAnimating] = useState<boolean>(false);
// 音效 // 音效
@ -394,14 +394,12 @@ const Game: FC<{
// 输了 // 输了
if (updateQueue.length === 7) { if (updateQueue.length === 7) {
setTipText('失败了');
setFinished(true); setFinished(true);
} }
if (!updateScene.find((s) => s.status !== 2)) { if (!updateScene.find((s) => s.status !== 2)) {
// 胜利 // 胜利
if (level === maxLevel) { if (level === maxLevel) {
setTipText('完成挑战');
setFinished(true); setFinished(true);
return; return;
} }
@ -428,9 +426,7 @@ const Game: FC<{
useEffect(() => { useEffect(() => {
if (finished) { if (finished) {
intervalRef.current && clearInterval(intervalRef.current); intervalRef.current && clearInterval(intervalRef.current);
localStorage.setItem(LAST_LEVEL_STORAGE_KEY, '1'); resetScoreStorage();
localStorage.setItem(LAST_SCORE_STORAGE_KEY, '0');
localStorage.setItem(LAST_TIME_STORAGE_KEY, '0');
} }
}, [finished]); }, [finished]);
// 更新使用时间 // 更新使用时间

View File

@ -52,7 +52,7 @@ const Score: FC<{
// 综合评分 // 综合评分
const rating = Math.max(0, score) * 100 - Math.round(time / 1000); const rating = Math.max(0, score) * 100 - Math.round(time / 1000);
// 分主题排行 // 分主题排行
const themeId = sessionStorage.getItem(PLAYING_THEME_ID_STORAGE_KEY); const themeId = localStorage.getItem(PLAYING_THEME_ID_STORAGE_KEY);
const uploadRankInfo = (id?: string) => { const uploadRankInfo = (id?: string) => {
const _userId = localStorage.getItem(USER_ID_STORAGE_KEY); const _userId = localStorage.getItem(USER_ID_STORAGE_KEY);
@ -93,6 +93,13 @@ const Score: FC<{
.then((res) => { .then((res) => {
setRankList(res as any); setRankList(res as any);
cb && cb(res as any); cb && cb(res as any);
const _userId = localStorage.getItem(USER_ID_STORAGE_KEY);
if (_userId) {
setTimeout(() => {
const rankEl = document.getElementById(_userId + 'el');
rankEl?.scrollIntoView({ behavior: 'smooth' });
});
}
}) })
.catch((e) => { .catch((e) => {
console.log(e); console.log(e);
@ -219,7 +226,7 @@ const Score: FC<{
<tr> <tr>
<th></th> <th></th>
<th></th> <th></th>
{/*<th>通关数</th>*/} <th></th>
{/*<th>用时</th>*/} {/*<th>用时</th>*/}
{/*<th>得分</th>*/} {/*<th>得分</th>*/}
<th></th> <th></th>
@ -227,14 +234,23 @@ const Score: FC<{
</thead> </thead>
<tbody> <tbody>
{rankList.map((rank, idx) => ( {rankList.map((rank, idx) => (
<tr key={idx}> <tr
key={idx}
id={rank.userId}
style={{
background:
rank.userId === userId
? 'rgb(0 0 0 / 20%)'
: '',
}}
>
<td>{idx + 1}</td> <td>{idx + 1}</td>
<td> <td>
{rank.username} {rank.username}
{rank.userId === userId && {rank.userId === userId &&
'(你)'} '(你)'}
</td> </td>
{/*<td>{rank.level}</td>*/} <td>{rank.level}</td>
{/*<td>*/} {/*<td>*/}
{/* {timestampToUsedTimeString(*/} {/* {timestampToUsedTimeString(*/}
{/* rank.time*/} {/* rank.time*/}

View File

@ -7,8 +7,12 @@ import Bmob from 'hydrogen-js-sdk';
import { import {
DEFAULT_BGM_STORAGE_KEY, DEFAULT_BGM_STORAGE_KEY,
domRelatedOptForTheme, domRelatedOptForTheme,
LAST_LEVEL_STORAGE_KEY,
LAST_SCORE_STORAGE_KEY,
LAST_TIME_STORAGE_KEY,
parsePathCustomThemeId, parsePathCustomThemeId,
PLAYING_THEME_ID_STORAGE_KEY, PLAYING_THEME_ID_STORAGE_KEY,
resetScoreStorage,
wrapThemeDefaultSounds, wrapThemeDefaultSounds,
} from './utils'; } from './utils';
import { getDefaultTheme } from './themes/default'; import { getDefaultTheme } from './themes/default';
@ -34,7 +38,17 @@ const errorTip = (tip: string) => {
// 加载成功后数据转换runtime以及转场 // 加载成功后数据转换runtime以及转场
const successTrans = (theme: Theme<any>) => { const successTrans = (theme: Theme<any>) => {
sessionStorage.setItem( // 如果上次玩的不是这个主题,清除缓存分数
const lastPlayTheme = localStorage.getItem(PLAYING_THEME_ID_STORAGE_KEY);
if (
!lastPlayTheme ||
![customThemeIdFromPath, theme.title].includes(lastPlayTheme)
) {
resetScoreStorage();
}
// 缓存当前主题id
localStorage.setItem(
PLAYING_THEME_ID_STORAGE_KEY, PLAYING_THEME_ID_STORAGE_KEY,
customThemeIdFromPath || theme.title customThemeIdFromPath || theme.title
); );

View File

@ -25,7 +25,7 @@ export const getDefaultTheme: () => Theme<DefaultSoundNames> = () => {
title: '有解的羊了个羊', title: '有解的羊了个羊',
desc: '真的可以通关~', desc: '真的可以通关~',
dark: true, dark: true,
maxLevel: 5, maxLevel: 10,
backgroundColor: '#8dac85', backgroundColor: '#8dac85',
icons: icons.map((icon) => ({ icons: icons.map((icon) => ({
name: icon, name: icon,

View File

@ -14,11 +14,16 @@ export const DEFAULT_TRIPLE_SOUND_STORAGE_KEY = 'defaultTripleSound';
export const DEFAULT_CLICK_SOUND_STORAGE_KEY = 'defaultClickSound'; export const DEFAULT_CLICK_SOUND_STORAGE_KEY = 'defaultClickSound';
export const USER_NAME_STORAGE_KEY = 'username'; export const USER_NAME_STORAGE_KEY = 'username';
export const USER_ID_STORAGE_KEY = 'userId'; export const USER_ID_STORAGE_KEY = 'userId';
// session
export const PLAYING_THEME_ID_STORAGE_KEY = 'playingThemeId'; export const PLAYING_THEME_ID_STORAGE_KEY = 'playingThemeId';
export const linkReg = /^(https|data):+/; export const linkReg = /^(https|data):+/;
export const resetScoreStorage = () => {
localStorage.setItem(LAST_LEVEL_STORAGE_KEY, '1');
localStorage.setItem(LAST_SCORE_STORAGE_KEY, '0');
localStorage.setItem(LAST_TIME_STORAGE_KEY, '0');
};
export const randomString: (len: number) => string = (len) => { export const randomString: (len: number) => string = (len) => {
const pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; const pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
let res = ''; let res = '';