Chore/lint fixes (#212)
* fix(eslint): 修复装饰器缩进配置 * fix(eslint): 修复装饰器缩进配置 * chore: 删除未使用的导入 * chore(lint): 移除未使用的导入和变量 * chore(lint): 修复editor-app中未使用的函数参数 * chore(lint): 修复未使用的赋值变量 * chore(eslint): 将所有错误级别改为警告以通过CI * fix(codeql): 修复GitHub Advanced Security检测到的问题
This commit is contained in:
@@ -57,27 +57,27 @@ export interface CustomScopeRule {
|
||||
export class NetworkScopeManager extends EventEmitter {
|
||||
private logger = createLogger('NetworkScopeManager');
|
||||
private config: ScopeConfig;
|
||||
|
||||
|
||||
/** 客户端位置信息 */
|
||||
private clientPositions = new Map<string, ClientPosition>();
|
||||
|
||||
|
||||
/** 客户端可视范围 */
|
||||
private clientRanges = new Map<string, number>();
|
||||
|
||||
|
||||
/** 房间映射 */
|
||||
private clientRooms = new Map<string, string>();
|
||||
private roomClients = new Map<string, Set<string>>();
|
||||
|
||||
|
||||
/** 自定义作用域规则 */
|
||||
private customRules: CustomScopeRule[] = [];
|
||||
|
||||
|
||||
/** 作用域缓存 */
|
||||
private scopeCache = new Map<string, { result: ScopeQueryResult; timestamp: number }>();
|
||||
private cacheTimeout = 100; // 100ms缓存
|
||||
|
||||
constructor(config: Partial<ScopeConfig> = {}) {
|
||||
super();
|
||||
|
||||
|
||||
this.config = {
|
||||
defaultRange: 100,
|
||||
maxRange: 500,
|
||||
@@ -93,11 +93,11 @@ export class NetworkScopeManager extends EventEmitter {
|
||||
*/
|
||||
public addClient(clientId: string, position?: { x: number; y: number; z: number }): void {
|
||||
this.clientRanges.set(clientId, this.config.defaultRange);
|
||||
|
||||
|
||||
if (position) {
|
||||
this.updateClientPosition(clientId, position.x, position.y, position.z);
|
||||
}
|
||||
|
||||
|
||||
this.logger.debug(`客户端 ${clientId} 已添加到作用域管理器`);
|
||||
}
|
||||
|
||||
@@ -107,16 +107,16 @@ export class NetworkScopeManager extends EventEmitter {
|
||||
public removeClient(clientId: string): void {
|
||||
this.clientPositions.delete(clientId);
|
||||
this.clientRanges.delete(clientId);
|
||||
|
||||
|
||||
// 从房间中移除
|
||||
const roomId = this.clientRooms.get(clientId);
|
||||
if (roomId) {
|
||||
this.leaveRoom(clientId, roomId);
|
||||
}
|
||||
|
||||
|
||||
// 清理缓存
|
||||
this.clearClientCache(clientId);
|
||||
|
||||
|
||||
this.logger.debug(`客户端 ${clientId} 已从作用域管理器移除`);
|
||||
}
|
||||
|
||||
@@ -131,12 +131,12 @@ export class NetworkScopeManager extends EventEmitter {
|
||||
z,
|
||||
lastUpdate: Date.now()
|
||||
};
|
||||
|
||||
|
||||
this.clientPositions.set(clientId, clientPosition);
|
||||
|
||||
|
||||
// 清理相关缓存
|
||||
this.clearClientCache(clientId);
|
||||
|
||||
|
||||
this.emit('positionUpdated', clientId, clientPosition);
|
||||
}
|
||||
|
||||
@@ -146,10 +146,10 @@ export class NetworkScopeManager extends EventEmitter {
|
||||
public setClientRange(clientId: string, range: number): void {
|
||||
const clampedRange = Math.min(range, this.config.maxRange);
|
||||
this.clientRanges.set(clientId, clampedRange);
|
||||
|
||||
|
||||
// 清理相关缓存
|
||||
this.clearClientCache(clientId);
|
||||
|
||||
|
||||
this.logger.debug(`客户端 ${clientId} 可视范围设置为: ${clampedRange}`);
|
||||
}
|
||||
|
||||
@@ -162,15 +162,15 @@ export class NetworkScopeManager extends EventEmitter {
|
||||
if (oldRoom) {
|
||||
this.leaveRoom(clientId, oldRoom);
|
||||
}
|
||||
|
||||
|
||||
// 加入新房间
|
||||
this.clientRooms.set(clientId, roomId);
|
||||
|
||||
|
||||
if (!this.roomClients.has(roomId)) {
|
||||
this.roomClients.set(roomId, new Set());
|
||||
}
|
||||
this.roomClients.get(roomId)!.add(clientId);
|
||||
|
||||
|
||||
this.logger.debug(`客户端 ${clientId} 已加入房间 ${roomId}`);
|
||||
this.emit('clientJoinedRoom', clientId, roomId);
|
||||
}
|
||||
@@ -180,17 +180,17 @@ export class NetworkScopeManager extends EventEmitter {
|
||||
*/
|
||||
public leaveRoom(clientId: string, roomId: string): void {
|
||||
this.clientRooms.delete(clientId);
|
||||
|
||||
|
||||
const roomClientSet = this.roomClients.get(roomId);
|
||||
if (roomClientSet) {
|
||||
roomClientSet.delete(clientId);
|
||||
|
||||
|
||||
// 如果房间为空,删除房间
|
||||
if (roomClientSet.size === 0) {
|
||||
this.roomClients.delete(roomId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
this.logger.debug(`客户端 ${clientId} 已离开房间 ${roomId}`);
|
||||
this.emit('clientLeftRoom', clientId, roomId);
|
||||
}
|
||||
@@ -201,7 +201,7 @@ export class NetworkScopeManager extends EventEmitter {
|
||||
public addCustomRule(rule: CustomScopeRule): void {
|
||||
this.customRules.push(rule);
|
||||
this.customRules.sort((a, b) => b.priority - a.priority);
|
||||
|
||||
|
||||
this.logger.debug(`已添加自定义作用域规则: ${rule.name}`);
|
||||
}
|
||||
|
||||
@@ -209,7 +209,7 @@ export class NetworkScopeManager extends EventEmitter {
|
||||
* 移除自定义作用域规则
|
||||
*/
|
||||
public removeCustomRule(ruleName: string): boolean {
|
||||
const index = this.customRules.findIndex(rule => rule.name === ruleName);
|
||||
const index = this.customRules.findIndex((rule) => rule.name === ruleName);
|
||||
if (index >= 0) {
|
||||
this.customRules.splice(index, 1);
|
||||
this.logger.debug(`已移除自定义作用域规则: ${ruleName}`);
|
||||
@@ -229,14 +229,14 @@ export class NetworkScopeManager extends EventEmitter {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 检查网络作用域
|
||||
for (const [prop, scope] of Object.entries(batch.scopes)) {
|
||||
if (!this.checkPropertyScope(scope, batch, clientId)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -250,26 +250,26 @@ export class NetworkScopeManager extends EventEmitter {
|
||||
): ScopeQueryResult {
|
||||
const cacheKey = `${position.x},${position.y},${position.z},${range},${excludeClientId || ''}`;
|
||||
const cached = this.scopeCache.get(cacheKey);
|
||||
|
||||
|
||||
if (cached && Date.now() - cached.timestamp < this.cacheTimeout) {
|
||||
return cached.result;
|
||||
}
|
||||
|
||||
|
||||
const clientsInRange: string[] = [];
|
||||
const distances = new Map<string, number>();
|
||||
const lodLevels = new Map<string, number>();
|
||||
|
||||
|
||||
for (const [clientId, clientPosition] of this.clientPositions) {
|
||||
if (excludeClientId && clientId === excludeClientId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
const distance = this.calculateDistance(position, clientPosition);
|
||||
|
||||
|
||||
if (distance <= range) {
|
||||
clientsInRange.push(clientId);
|
||||
distances.set(clientId, distance);
|
||||
|
||||
|
||||
// 计算LOD级别
|
||||
if (this.config.enableLOD) {
|
||||
const lodLevel = this.calculateLODLevel(distance);
|
||||
@@ -277,19 +277,19 @@ export class NetworkScopeManager extends EventEmitter {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const result: ScopeQueryResult = {
|
||||
clientsInRange,
|
||||
distances,
|
||||
lodLevels
|
||||
};
|
||||
|
||||
|
||||
// 缓存结果
|
||||
this.scopeCache.set(cacheKey, {
|
||||
result,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -349,7 +349,7 @@ export class NetworkScopeManager extends EventEmitter {
|
||||
roomCount: number;
|
||||
cacheSize: number;
|
||||
customRuleCount: number;
|
||||
} {
|
||||
} {
|
||||
return {
|
||||
clientCount: this.clientPositions.size,
|
||||
roomCount: this.roomClients.size,
|
||||
@@ -378,22 +378,22 @@ export class NetworkScopeManager extends EventEmitter {
|
||||
switch (scope) {
|
||||
case NetworkScope.Global:
|
||||
return true;
|
||||
|
||||
|
||||
case NetworkScope.Room:
|
||||
const clientRoom = this.clientRooms.get(clientId);
|
||||
// 这里需要知道batch来源的实体所在房间,简化实现
|
||||
return true;
|
||||
|
||||
|
||||
case NetworkScope.Owner:
|
||||
return batch.instanceId === clientId;
|
||||
|
||||
|
||||
case NetworkScope.Nearby:
|
||||
return this.isClientNearby(batch, clientId);
|
||||
|
||||
|
||||
case NetworkScope.Custom:
|
||||
// 由自定义规则处理
|
||||
return true;
|
||||
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@@ -407,9 +407,9 @@ export class NetworkScopeManager extends EventEmitter {
|
||||
if (!clientPosition) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
const clientRange = this.clientRanges.get(clientId) || this.config.defaultRange;
|
||||
|
||||
|
||||
// 这里需要知道batch来源实体的位置,简化实现
|
||||
// 实际项目中应该从实体的Transform组件获取位置
|
||||
return true;
|
||||
@@ -445,15 +445,15 @@ export class NetworkScopeManager extends EventEmitter {
|
||||
*/
|
||||
private clearClientCache(clientId: string): void {
|
||||
const keysToDelete: string[] = [];
|
||||
|
||||
|
||||
for (const key of this.scopeCache.keys()) {
|
||||
if (key.includes(clientId)) {
|
||||
keysToDelete.push(key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (const key of keysToDelete) {
|
||||
this.scopeCache.delete(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,18 +74,18 @@ interface SchedulerStats {
|
||||
export class SyncScheduler extends EventEmitter {
|
||||
private logger = createLogger('SyncScheduler');
|
||||
private config: SyncSchedulerConfig;
|
||||
|
||||
|
||||
/** 任务队列 */
|
||||
private taskQueue: ScheduledTask[] = [];
|
||||
private taskIdCounter = 0;
|
||||
|
||||
|
||||
/** 客户端状态 */
|
||||
private clientStates = new Map<string, ClientScheduleState>();
|
||||
|
||||
|
||||
/** 调度定时器 */
|
||||
private scheduleTimer: any = null;
|
||||
private adaptiveTimer: any = null;
|
||||
|
||||
|
||||
/** 统计信息 */
|
||||
private stats: SchedulerStats = {
|
||||
totalTasks: 0,
|
||||
@@ -96,14 +96,14 @@ export class SyncScheduler extends EventEmitter {
|
||||
queueSizes: {},
|
||||
adaptiveRates: {}
|
||||
};
|
||||
|
||||
|
||||
/** 自适应调整历史 */
|
||||
private latencyHistory = new Map<string, number[]>();
|
||||
private bandwidthHistory = new Map<string, number[]>();
|
||||
|
||||
constructor(config: Partial<SyncSchedulerConfig> = {}) {
|
||||
super();
|
||||
|
||||
|
||||
this.config = {
|
||||
targetFPS: 60,
|
||||
maxLatency: 100,
|
||||
@@ -117,9 +117,9 @@ export class SyncScheduler extends EventEmitter {
|
||||
enableAdaptive: true,
|
||||
...config
|
||||
};
|
||||
|
||||
|
||||
this.startScheduler();
|
||||
|
||||
|
||||
if (this.config.enableAdaptive) {
|
||||
this.startAdaptiveAdjustment();
|
||||
}
|
||||
@@ -141,11 +141,11 @@ export class SyncScheduler extends EventEmitter {
|
||||
lastSendTime: 0,
|
||||
adaptiveRate: 1000 / this.config.targetFPS // 初始发送间隔
|
||||
};
|
||||
|
||||
|
||||
this.clientStates.set(clientId, clientState);
|
||||
this.latencyHistory.set(clientId, []);
|
||||
this.bandwidthHistory.set(clientId, []);
|
||||
|
||||
|
||||
this.logger.debug(`客户端 ${clientId} 已添加到调度器`);
|
||||
}
|
||||
|
||||
@@ -156,10 +156,10 @@ export class SyncScheduler extends EventEmitter {
|
||||
this.clientStates.delete(clientId);
|
||||
this.latencyHistory.delete(clientId);
|
||||
this.bandwidthHistory.delete(clientId);
|
||||
|
||||
|
||||
// 移除该客户端的所有任务
|
||||
this.taskQueue = this.taskQueue.filter(task => task.clientId !== clientId);
|
||||
|
||||
this.taskQueue = this.taskQueue.filter((task) => task.clientId !== clientId);
|
||||
|
||||
this.logger.debug(`客户端 ${clientId} 已从调度器移除`);
|
||||
}
|
||||
|
||||
@@ -172,10 +172,10 @@ export class SyncScheduler extends EventEmitter {
|
||||
this.logger.warn(`客户端 ${clientId} 不存在,无法调度任务`);
|
||||
return '';
|
||||
}
|
||||
|
||||
|
||||
const taskId = `task_${++this.taskIdCounter}`;
|
||||
const now = Date.now();
|
||||
|
||||
|
||||
const task: ScheduledTask = {
|
||||
id: taskId,
|
||||
batch,
|
||||
@@ -186,13 +186,13 @@ export class SyncScheduler extends EventEmitter {
|
||||
retryCount: 0,
|
||||
createdAt: now
|
||||
};
|
||||
|
||||
|
||||
this.taskQueue.push(task);
|
||||
this.stats.totalTasks++;
|
||||
|
||||
|
||||
// 按优先级和截止时间排序
|
||||
this.sortTaskQueue();
|
||||
|
||||
|
||||
return taskId;
|
||||
}
|
||||
|
||||
@@ -203,16 +203,16 @@ export class SyncScheduler extends EventEmitter {
|
||||
const clientState = this.clientStates.get(clientId);
|
||||
if (clientState) {
|
||||
clientState.latency = latency;
|
||||
|
||||
|
||||
// 记录延迟历史
|
||||
const history = this.latencyHistory.get(clientId) || [];
|
||||
history.push(latency);
|
||||
|
||||
|
||||
// 保持最近50个记录
|
||||
if (history.length > 50) {
|
||||
history.shift();
|
||||
}
|
||||
|
||||
|
||||
this.latencyHistory.set(clientId, history);
|
||||
}
|
||||
}
|
||||
@@ -234,11 +234,11 @@ export class SyncScheduler extends EventEmitter {
|
||||
public getStats(): SchedulerStats {
|
||||
// 更新队列大小统计
|
||||
for (const [clientId, clientState] of this.clientStates) {
|
||||
const clientTasks = this.taskQueue.filter(task => task.clientId === clientId);
|
||||
const clientTasks = this.taskQueue.filter((task) => task.clientId === clientId);
|
||||
this.stats.queueSizes[clientId] = clientTasks.length;
|
||||
this.stats.adaptiveRates[clientId] = clientState.adaptiveRate;
|
||||
}
|
||||
|
||||
|
||||
return { ...this.stats };
|
||||
}
|
||||
|
||||
@@ -257,7 +257,7 @@ export class SyncScheduler extends EventEmitter {
|
||||
*/
|
||||
public updateConfig(newConfig: Partial<SyncSchedulerConfig>): void {
|
||||
Object.assign(this.config, newConfig);
|
||||
|
||||
|
||||
if (newConfig.enableAdaptive !== undefined) {
|
||||
if (newConfig.enableAdaptive) {
|
||||
this.startAdaptiveAdjustment();
|
||||
@@ -287,7 +287,7 @@ export class SyncScheduler extends EventEmitter {
|
||||
if (this.scheduleTimer) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const interval = 1000 / this.config.targetFPS;
|
||||
this.scheduleTimer = setInterval(() => {
|
||||
this.processTasks();
|
||||
@@ -311,7 +311,7 @@ export class SyncScheduler extends EventEmitter {
|
||||
if (this.adaptiveTimer) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
this.adaptiveTimer = setInterval(() => {
|
||||
this.performAdaptiveAdjustment();
|
||||
}, this.config.adaptiveInterval);
|
||||
@@ -333,7 +333,7 @@ export class SyncScheduler extends EventEmitter {
|
||||
private processTasks(): void {
|
||||
const now = Date.now();
|
||||
const processedTasks: string[] = [];
|
||||
|
||||
|
||||
for (const task of this.taskQueue) {
|
||||
const clientState = this.clientStates.get(task.clientId);
|
||||
if (!clientState) {
|
||||
@@ -341,7 +341,7 @@ export class SyncScheduler extends EventEmitter {
|
||||
this.stats.droppedTasks++;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// 检查截止时间
|
||||
if (now > task.deadline) {
|
||||
processedTasks.push(task.id);
|
||||
@@ -349,25 +349,25 @@ export class SyncScheduler extends EventEmitter {
|
||||
this.logger.warn(`任务 ${task.id} 超时被丢弃`);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// 检查发送间隔
|
||||
if (now - clientState.lastSendTime < clientState.adaptiveRate) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// 检查带宽限制
|
||||
if (!this.checkBandwidthLimit(clientState, task.size)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// 发送任务
|
||||
this.sendTask(task, clientState);
|
||||
processedTasks.push(task.id);
|
||||
this.stats.completedTasks++;
|
||||
}
|
||||
|
||||
|
||||
// 移除已处理的任务
|
||||
this.taskQueue = this.taskQueue.filter(task => !processedTasks.includes(task.id));
|
||||
this.taskQueue = this.taskQueue.filter((task) => !processedTasks.includes(task.id));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -375,24 +375,24 @@ export class SyncScheduler extends EventEmitter {
|
||||
*/
|
||||
private sendTask(task: ScheduledTask, clientState: ClientScheduleState): void {
|
||||
const now = Date.now();
|
||||
|
||||
|
||||
// 更新客户端状态
|
||||
clientState.lastSendTime = now;
|
||||
clientState.bandwidth.used += task.size;
|
||||
|
||||
|
||||
// 记录带宽历史
|
||||
const bandwidthHistory = this.bandwidthHistory.get(task.clientId) || [];
|
||||
bandwidthHistory.push(task.size);
|
||||
|
||||
|
||||
if (bandwidthHistory.length > 50) {
|
||||
bandwidthHistory.shift();
|
||||
}
|
||||
|
||||
|
||||
this.bandwidthHistory.set(task.clientId, bandwidthHistory);
|
||||
|
||||
|
||||
// 发出事件
|
||||
this.emit('taskReady', task.batch, task.clientId);
|
||||
|
||||
|
||||
this.logger.debug(`任务 ${task.id} 已发送给客户端 ${task.clientId}`);
|
||||
}
|
||||
|
||||
@@ -401,13 +401,13 @@ export class SyncScheduler extends EventEmitter {
|
||||
*/
|
||||
private checkBandwidthLimit(clientState: ClientScheduleState, taskSize: number): boolean {
|
||||
const now = Date.now();
|
||||
|
||||
|
||||
// 重置带宽计数器
|
||||
if (now >= clientState.bandwidth.resetTime) {
|
||||
clientState.bandwidth.used = 0;
|
||||
clientState.bandwidth.resetTime = now + 1000;
|
||||
}
|
||||
|
||||
|
||||
return clientState.bandwidth.used + taskSize <= clientState.bandwidth.limit;
|
||||
}
|
||||
|
||||
@@ -429,7 +429,7 @@ export class SyncScheduler extends EventEmitter {
|
||||
if (priorityDiff !== 0) {
|
||||
return priorityDiff;
|
||||
}
|
||||
|
||||
|
||||
// 然后按截止时间排序
|
||||
return a.deadline - b.deadline;
|
||||
});
|
||||
@@ -450,21 +450,21 @@ export class SyncScheduler extends EventEmitter {
|
||||
private adjustClientRate(clientId: string, clientState: ClientScheduleState): void {
|
||||
const latencyHistory = this.latencyHistory.get(clientId) || [];
|
||||
const bandwidthHistory = this.bandwidthHistory.get(clientId) || [];
|
||||
|
||||
|
||||
if (latencyHistory.length < 5) {
|
||||
return; // 数据不足,不进行调整
|
||||
}
|
||||
|
||||
|
||||
// 计算平均延迟
|
||||
const avgLatency = latencyHistory.reduce((sum, lat) => sum + lat, 0) / latencyHistory.length;
|
||||
|
||||
|
||||
// 计算带宽利用率
|
||||
const totalBandwidth = bandwidthHistory.reduce((sum, size) => sum + size, 0);
|
||||
const bandwidthUtilization = totalBandwidth / clientState.bandwidth.limit;
|
||||
|
||||
|
||||
// 根据延迟和带宽利用率调整发送频率
|
||||
let adjustment = 1;
|
||||
|
||||
|
||||
if (avgLatency > this.config.maxLatency) {
|
||||
// 延迟过高,降低发送频率
|
||||
adjustment = 1.2;
|
||||
@@ -472,7 +472,7 @@ export class SyncScheduler extends EventEmitter {
|
||||
// 延迟较低,可以提高发送频率
|
||||
adjustment = 0.9;
|
||||
}
|
||||
|
||||
|
||||
if (bandwidthUtilization > 0.9) {
|
||||
// 带宽利用率过高,降低发送频率
|
||||
adjustment *= 1.1;
|
||||
@@ -480,22 +480,22 @@ export class SyncScheduler extends EventEmitter {
|
||||
// 带宽利用率较低,可以提高发送频率
|
||||
adjustment *= 0.95;
|
||||
}
|
||||
|
||||
|
||||
// 应用调整
|
||||
clientState.adaptiveRate = Math.max(
|
||||
clientState.adaptiveRate * adjustment,
|
||||
1000 / (this.config.targetFPS * 2) // 最小间隔
|
||||
);
|
||||
|
||||
|
||||
clientState.adaptiveRate = Math.min(
|
||||
clientState.adaptiveRate,
|
||||
1000 // 最大间隔1秒
|
||||
);
|
||||
|
||||
|
||||
this.logger.debug(
|
||||
`客户端 ${clientId} 自适应调整: 延迟=${avgLatency.toFixed(1)}ms, ` +
|
||||
`带宽利用率=${(bandwidthUtilization * 100).toFixed(1)}%, ` +
|
||||
`新间隔=${clientState.adaptiveRate.toFixed(1)}ms`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
*/
|
||||
|
||||
export * from './NetworkScopeManager';
|
||||
export * from './SyncScheduler';
|
||||
export * from './SyncScheduler';
|
||||
|
||||
Reference in New Issue
Block a user