Skip to content

Commit

Permalink
修复一键开火时仪表盘强度没有变化的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperzlib committed Aug 7, 2024
1 parent 9c34fdb commit 545f346
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 24 deletions.
5 changes: 3 additions & 2 deletions frontend/src/apis/socketApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ export type PulseItemResponse = {
name: string;
};

export type StrengthInfo = {
export type GameStrengthInfo = {
strength: number;
limit: number;
tempStrength: number;
};

export type GameStrengthConfig = {
Expand All @@ -51,7 +52,7 @@ export interface SocketApiEventListeners extends EventDef {
gameInitialized: [];
gameStarted: [];
gameStopped: [];
strengthChanged: [strength: StrengthInfo];
strengthChanged: [strength: GameStrengthInfo];
configUpdated: [config: CoyoteLiveGameConfig];
}

Expand Down
7 changes: 5 additions & 2 deletions frontend/src/pages/Controller.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const state = reactive({
randomStrengthVal: 5,
strengthLimit: 20,
tempStrength: 0,
randomFreqLow: 10,
randomFreqHigh: 15,
Expand Down Expand Up @@ -65,8 +67,8 @@ const gameConfig = computed<CoyoteLiveGameConfig>({
});
const chartVal = computed(() => ({
valLow: state.strengthVal,
valHigh: Math.min(state.strengthVal + state.randomStrengthVal, state.strengthLimit),
valLow: state.strengthVal + state.tempStrength,
valHigh: Math.min(state.strengthVal + state.tempStrength + state.randomStrengthVal, state.strengthLimit),
valLimit: state.strengthLimit,
}))
Expand Down Expand Up @@ -152,6 +154,7 @@ const initWebSocket = async () => {
wsClient.on('strengthChanged', (strength) => {
state.strengthLimit = strength.limit;
state.tempStrength = strength.tempStrength;
});
wsClient.on('configUpdated', (config) => {
Expand Down
30 changes: 19 additions & 11 deletions frontend/src/pages/Viewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { ServerInfoResData, webApi } from '../apis/webApi';
import { handleApiResponse } from '../utils/response';
const state = reactive({
valLow: 0,
valHigh: 0,
valLimit: 50,
strength: 0,
randomStrength: 0,
strengthLimit: 50,
tempStrength: 0,
clientId: '',
Expand All @@ -18,6 +20,12 @@ const state = reactive({
let serverInfo: ServerInfoResData;
let wsClient: SocketApi;
const chartVal = computed(() => ({
valLow: state.strength + state.tempStrength,
valHigh: Math.min(state.strength + state.tempStrength + state.randomStrength, state.strengthLimit),
valLimit: state.strengthLimit,
}))
const initServerInfo = async () => {
try {
let serverInfoRes = await webApi.getServerInfo();
Expand All @@ -41,15 +49,15 @@ const initWebSocket = async () => {
});
wsClient.on('strengthChanged', (strength) => {
state.valLimit = strength.limit;
state.strengthLimit = strength.limit;
state.tempStrength = strength.tempStrength;
});
wsClient.on('configUpdated', (config) => {
state.valLow = config.strength.strength;
state.valHigh = config.strength.randomStrength;
state.strength = config.strength.strength;
state.randomStrength = config.strength.randomStrength;
state.valLow = Math.min(state.valLow, state.valLimit);
state.valHigh = Math.min(state.valLow + state.valHigh, state.valLimit);
state.strength = Math.min(state.strength, state.strengthLimit); // 限制当前值不超过上限
});
wsClient.on('gameStarted', () => {
Expand Down Expand Up @@ -89,9 +97,9 @@ onMounted(async () => {
<template>
<div class="w-full h-full">
<StatusChart
:val-low="state.valLow"
:val-high="state.valHigh"
:val-limit="state.valLimit"
:val-low="chartVal.valLow"
:val-high="chartVal.valHigh"
:val-limit="chartVal.valLimit"
:running="state.gameStarted"
readonly
/>
Expand Down
31 changes: 25 additions & 6 deletions server/src/controllers/game/CoyoteLiveGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import { EventStore } from '../../utils/EventStore';
import { CoyoteLiveGameManager } from '../../managers/CoyoteLiveGameManager';
import { CoyoteLiveGameConfig, GameStrengthConfig } from '../../types/game';
import { DGLabPulseBaseInfo } from '../../services/DGLabPulse';
import { str } from 'ajv';

export type GameStrengthInfo = StrengthInfo & {
tempStrength: number;
};

export interface CoyoteLiveGameEvents {
close: [];
pulseListUpdated: [pulseList: DGLabPulseBaseInfo[]];
strengthChanged: [strength: StrengthInfo];
strengthChanged: [strength: GameStrengthInfo];
configUpdated: [config: CoyoteLiveGameConfig];
gameStarted: [];
gameStopped: [];
Expand Down Expand Up @@ -60,6 +63,13 @@ export class CoyoteLiveGame {
return this.client.strength;
}

public get gameStrength(): GameStrengthInfo {
return {
...this.client.strength,
tempStrength: this.fireStrength,
};
}

constructor(client: DGLabWSClient) {
this.client = client;

Expand Down Expand Up @@ -115,7 +125,7 @@ export class CoyoteLiveGame {
configUpdated = true;
}

this.events.emit('strengthChanged', strength);
this.events.emit('strengthChanged', this.gameStrength);

if (configUpdated) {
this.handleConfigUpdated();
Expand Down Expand Up @@ -170,6 +180,9 @@ export class CoyoteLiveGame {
this.fireStrength = strength;
this.fireEndTimestamp = Date.now() + duration;

// 通知强度变化
this.events.emit('strengthChanged', this.gameStrength);

this.restartGame().catch((err: any) => {
console.error('Failed to restart game:', err);
}); // 一键开火时需要重启游戏Task
Expand All @@ -180,6 +193,14 @@ export class CoyoteLiveGame {
}
}

private onFireEnd() {
this.fireStrength = 0;
this.fireEndTimestamp = 0;

// 通知强度变化
this.events.emit('strengthChanged', this.gameStrength);
}

private handleConfigUpdated(): void {
this.events.emit('configUpdated', this.gameConfig);
}
Expand Down Expand Up @@ -208,9 +229,7 @@ export class CoyoteLiveGame {
let nextStrength: number | null = null;
if (this.fireStrength) {
if (Date.now() > this.fireEndTimestamp) { // 一键开火结束
this.fireStrength = 0;
this.fireEndTimestamp = 0;

this.onFireEnd();
nextStrength = this.strengthConfig.strength; // 一键开火结束后恢复到初始强度
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions server/src/controllers/ws/WebWS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export class WebWSClient {

await this.send({
event: 'strengthChanged',
data: gameInstance.clientStrength,
data: gameInstance.gameStrength,
});

await this.send({
Expand Down Expand Up @@ -301,7 +301,7 @@ export class WebWSClient {

await this.send({
event: 'strengthChanged',
data: gameInstance.clientStrength,
data: gameInstance.gameStrength,
});
} else {
// 如果游戏已经开始,发送游戏开始事件
Expand All @@ -314,7 +314,7 @@ export class WebWSClient {
// 使用服务器端的强度和配置覆盖客户端的信息
await this.send({
event: 'strengthChanged',
data: gameInstance.clientStrength,
data: gameInstance.gameStrength,
});

await this.send({
Expand Down

0 comments on commit 545f346

Please sign in to comment.