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 f49baf5 commit d2a5813
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 8 deletions.
69 changes: 69 additions & 0 deletions sdk/auto_hot_key.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
; =============================================================================
; AutoHotKey functions for Coyote Game Hub
; =============================================================================

global CoyoteControllerURL := "http://127.0.0.1:8920"
global CoyoteTargetClientId := "all"

HttpPost(url, body) {
; https://learn.microsoft.com/en-us/windows/win32/winhttp/winhttprequest
web := ComObject('WinHttp.WinHttpRequest.5.1')
web.Open("POST", url, false)
web.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
web.Send(body)
return web.ResponseText
}

CoyoteUpdateGameConfig(paramStr)
{
global CoyoteControllerURL, CoyoteTargetClientId

url := CoyoteControllerURL . "/api/game/" . CoyoteTargetClientId . "/strength_config"
return HttpPost(url, paramStr)
}

CoyoteAddStrength(value)
{
return CoyoteUpdateGameConfig("strength.add=" . value)
}

CoyoteSubStrength(value)
{
return CoyoteUpdateGameConfig("strength.sub=" . value)
}

CoyoteSetStrength(value)
{
return CoyoteUpdateGameConfig("strength.set=" . value)
}

CoyoteAddRandomStrength(value)
{
return CoyoteUpdateGameConfig("randomStrength.add=" . value)
}

CoyoteSubRandomStrength(value)
{
return CoyoteUpdateGameConfig("randomStrength.sub=" . value)
}

CoyoteSetRandomStrength(value)
{
return CoyoteUpdateGameConfig("randomStrength.set=" . value)
}

CoyoteFire(strength, time)
{
global CoyoteControllerURL, CoyoteTargetClientId

timeMs := time * 1000
url := CoyoteControllerURL . "/api/game/" . CoyoteTargetClientId . "/fire"
return HttpPost(url, "strength=" . strength . "&time=" . timeMs)
}

; Example usage:
; F1::
; {
; CoyoteAddStrength(1)
; return
; }
15 changes: 13 additions & 2 deletions server/src/controllers/game/CoyoteLiveGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export class CoyoteLiveGame {
/** 一键开火结束时间 */
public fireEndTimestamp: number = 0;

/** 一键开火波形 */
public firePulseId: string = '';

public currentPulseId: string = '';

private eventStore: EventStore = new EventStore();
Expand Down Expand Up @@ -174,11 +177,13 @@ export class CoyoteLiveGame {
* 一键开火
* @param strength 强度
* @param duration 持续时间(毫秒)
* @param pulseId 一键开火使用的脉冲ID(可选)
*/
public async fire(strength: number, duration: number) {
public async fire(strength: number, duration: number, pulseId?: string): Promise<void> {
if (!this.fireStrength) {
this.fireStrength = strength;
this.fireEndTimestamp = Date.now() + duration;
this.firePulseId = pulseId || '';

// 通知强度变化
this.events.emit('strengthChanged', this.gameStrength);
Expand All @@ -190,6 +195,7 @@ export class CoyoteLiveGame {
// 已经在一键开火状态,使用最大的强度,持续时间增加
this.fireStrength = Math.max(this.fireStrength, strength);
this.fireEndTimestamp += duration;
this.firePulseId = pulseId || '';
}
}

Expand Down Expand Up @@ -218,19 +224,24 @@ export class CoyoteLiveGame {

private async runGameTask(ab: AbortController, harvest: () => void): Promise<void> {
let outputTime = 0;
let pulseId = this.currentPulseId;
if (this.fireStrength) {
if (Date.now() > this.fireEndTimestamp) { // 一键开火结束
this.fireStrength = 0;
this.fireEndTimestamp = 0;
} else {
outputTime = Math.min(this.fireEndTimestamp - Date.now(), 30000); // 单次最多输出30秒
}

if (this.firePulseId) {
pulseId = this.firePulseId;
}
} else { // 随机强度
outputTime = randomInt(this.strengthConfig.minInterval, this.strengthConfig.maxInterval) * 1000;
}

// 输出脉冲,直到下次随机强度时间
await this.client.outputPulse(this.currentPulseId, outputTime, {
await this.client.outputPulse(pulseId, outputTime, {
abortController: ab,
bChannel: !!(this.strengthConfig && this.strengthConfig.bChannelMultiplier),
onTimeEnd: () => {
Expand Down
25 changes: 19 additions & 6 deletions server/src/controllers/http/GameApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type SetStrengthConfigRequest = {
export type FireRequest = {
strength: number;
time?: number;
pulseId?: string;
};

export class GameApiController {
Expand Down Expand Up @@ -322,12 +323,22 @@ export class GameApiController {
};
return;
}

// 是否获取完整的波形信息
let isFullMode = ctx.request.query?.type === 'full';

// 暂且只从配置文件中获取脉冲列表
const pulseList = DGLabPulseService.instance.pulseList.map((pulse) => ({
id: pulse.id,
name: pulse.name,
}));
let pulseList: any[] = [];

if (isFullMode) {
pulseList = DGLabPulseService.instance.pulseList;
} else {
// 只返回基本信息
DGLabPulseService.instance.pulseList.map((pulse) => ({
id: pulse.id,
name: pulse.name,
}));
}

ctx.body = {
status: 1,
Expand Down Expand Up @@ -358,7 +369,7 @@ export class GameApiController {
// fix for x-www-form-urlencoded
const postBody = ctx.request.body;
for (const key in postBody) {
if (postBody[key]) {
if (['strength', 'time'].includes(key) && postBody[key]) {
postBody[key] = parseInt(postBody[key]);
}
}
Expand Down Expand Up @@ -394,6 +405,8 @@ export class GameApiController {
return;
}

const pulseId = req.pulseId ?? undefined;

let gameList: Iterable<CoyoteLiveGame> = [];
if (ctx.params.id === 'all') { // 广播模式,设置所有游戏的强度配置
if (!MainConfig.value.allowBroadcastToClients) {
Expand Down Expand Up @@ -422,7 +435,7 @@ export class GameApiController {

let successClientIds = new Set<string>();
for (const game of gameList) {
game.fire(req.strength, fireTime);
game.fire(req.strength, fireTime, pulseId);

successClientIds.add(game.clientId);
}
Expand Down

0 comments on commit d2a5813

Please sign in to comment.