Skip to content

Commit

Permalink
将一键开火的结束时间改为准确时间
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperzlib committed Aug 19, 2024
1 parent 16f9739 commit f49baf5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
40 changes: 25 additions & 15 deletions server/src/controllers/game/CoyoteLiveGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,17 @@ export class CoyoteLiveGame {
this.events.emit('configUpdated', this.gameConfig);
}

private async setClientStrength(strength: number): Promise<void> {
if (!this.client.active) {
return;
}

await this.client.setStrength(Channel.A, strength);
if (this.strengthConfig.bChannelMultiplier) {
await this.client.setStrength(Channel.B, strength * this.strengthConfig.bChannelMultiplier);
}
}

private async runGameTask(ab: AbortController, harvest: () => void): Promise<void> {
let outputTime = 0;
if (this.fireStrength) {
Expand All @@ -222,31 +233,30 @@ export class CoyoteLiveGame {
await this.client.outputPulse(this.currentPulseId, outputTime, {
abortController: ab,
bChannel: !!(this.strengthConfig && this.strengthConfig.bChannelMultiplier),
onTimeEnd: () => {
if (this.fireStrength && Date.now() > this.fireEndTimestamp) { // 一键开火结束
this.onFireEnd();
// 提前降低强度
this.setClientStrength(this.strengthConfig.strength).catch((error) => {
console.error('Failed to set strength:', error);
});
}
}
});

harvest();

let nextStrength: number | null = null;
if (this.fireStrength) {
if (Date.now() > this.fireEndTimestamp) { // 一键开火结束
this.onFireEnd();
nextStrength = this.strengthConfig.strength; // 一键开火结束后恢复到初始强度
}
} else {
if (this.strengthConfig.randomStrength) {
// 随机强度
nextStrength = this.strengthConfig.strength + randomInt(0, this.strengthConfig.randomStrength);
nextStrength = Math.min(nextStrength, this.clientStrength.limit);
}
if (this.strengthConfig.randomStrength) {
// 随机强度
nextStrength = this.strengthConfig.strength + randomInt(0, this.strengthConfig.randomStrength);
nextStrength = Math.min(nextStrength, this.clientStrength.limit);
}

if (nextStrength !== null) {
setTimeout(async () => {
try {
await this.client.setStrength(Channel.A, nextStrength);
if (this.strengthConfig.bChannelMultiplier) {
await this.client.setStrength(Channel.B, nextStrength * this.strengthConfig.bChannelMultiplier);
}
await this.setClientStrength(nextStrength);
} catch (error) {
console.error('Failed to set strength:', error);
}
Expand Down
16 changes: 12 additions & 4 deletions server/src/controllers/ws/DGLabWS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface StrengthInfo {
export interface OutputPulseOptions {
abortController?: AbortController;
bChannel?: boolean;
onTimeEnd?: () => void;
}

export interface DGLabWSEvents {
Expand All @@ -37,6 +38,7 @@ export class DGLabWSClient {
public strength: StrengthInfo = { strength: 0, limit: 0 };
public strengthChannelB: StrengthInfo = { strength: 0, limit: 0 };
public socket: AsyncWebSocket;
public active: boolean = true;

public pulseList: DGLabPulseBaseInfo[] = [];

Expand Down Expand Up @@ -254,11 +256,16 @@ export class DGLabWSClient {

let netDuration = Date.now() - startTime;

await asleep(time, options.abortController); // 等待时间到达

startTime = Date.now();
options.onTimeEnd?.(); // 时间到达后的回调
let onTimeEndDuration = Date.now() - startTime;

let finished = true;
if (totalDuration < time) {
finished = await asleep(time - totalDuration - netDuration, options.abortController);
} else {
finished = await asleep(totalDuration + 200 - netDuration, options.abortController);
if (totalDuration > time) {
const waitTime = totalDuration - time - onTimeEndDuration - netDuration + 200;
finished = await asleep(waitTime, options.abortController);
}

if (!finished) {
Expand All @@ -275,6 +282,7 @@ export class DGLabWSClient {
}

public async close() {
this.active = false;
if (this.socket.readyState === WebSocket.OPEN) {
try {
await this.send(MessageType.BREAK, RetCode.CLIENT_DISCONNECTED);
Expand Down

0 comments on commit f49baf5

Please sign in to comment.