Skip to content

Commit 6dc1360

Browse files
committed
feat: include TTL in game state formatting for API
1 parent 7e10e03 commit 6dc1360

3 files changed

Lines changed: 8 additions & 12 deletions

File tree

src/services/sandboxService.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,10 @@ export const formatGameStateForAPI = (
330330
// 添加食物、障碍物、宝箱、钥匙
331331
items.forEach((item) => {
332332
const pos = item.getPosition();
333-
const x = Math.floor(pos.x / 20); // 转换为网格坐标
333+
const x = Math.floor(pos.x / 20);
334334
const y = Math.floor(pos.y / 20);
335335

336-
let value;
336+
let value, ttl = -1;
337337
if (item instanceof Food) {
338338
const foodValue = item.getValue();
339339
// 将不同类型的食物转换为API要求的格式
@@ -344,6 +344,7 @@ export const formatGameStateForAPI = (
344344
} else {
345345
value = Number(foodValue); // 普通食物
346346
}
347+
ttl = item.getTTL() ?? -1;
347348
} else if (item instanceof Key) {
348349
value = -3; // 钥匙
349350
} else if (item instanceof TreasureChest) {
@@ -352,12 +353,8 @@ export const formatGameStateForAPI = (
352353
value = -4; // 墙
353354
}
354355

355-
if (y >= 30 || x >= 40) {
356-
console.warn("坐标超出范围:", item);
357-
}
358-
359356
// 注意:输入格式要求 y,x 的顺序(规则里规定的坐标系统与内部实现有差异)
360-
input += `${y} ${x} ${value}\n`;
357+
input += `${y} ${x} ${value} ${ttl}\n`;
361358
});
362359

363360
// 存活的玩家
@@ -379,9 +376,6 @@ export const formatGameStateForAPI = (
379376
snake.getBody().forEach((segment) => {
380377
const x = Math.floor(segment.x / 20);
381378
const y = Math.floor(segment.y / 20);
382-
if (y >= 30 || x >= 40) {
383-
console.warn("坐标超出范围:", segment, snake);
384-
}
385379
input += `${y} ${x}\n`;
386380
});
387381
});

src/utils/gameStateFormatter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export function formatGameStateForAlgorithm(frame: GameRecordingFrame, vortexDat
2828
const y = Math.floor(pos.y / 20);
2929

3030
let value: number;
31+
let ttl: number = -1;
3132
if (item.entityType === EntityType.FOOD) {
3233
const food = item as SerializedFood;
3334
// Convert different food types to API format
@@ -38,6 +39,7 @@ export function formatGameStateForAlgorithm(frame: GameRecordingFrame, vortexDat
3839
} else {
3940
value = Number(food.value); // Normal food
4041
}
42+
ttl = food.ttl ?? -1;
4143
} else if (item.entityType === EntityType.KEY) {
4244
value = -3; // Key (on ground)
4345
} else if (item.entityType === EntityType.TREASURE_CHEST) {
@@ -47,7 +49,7 @@ export function formatGameStateForAlgorithm(frame: GameRecordingFrame, vortexDat
4749
}
4850

4951
// Note: Input format requires y,x order (coordinate system difference)
50-
input += `${y} ${x} ${value}\n`;
52+
input += `${y} ${x} ${value} ${ttl}\n`;
5153
});
5254

5355
// Alive players

src/views/GameRules.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@
210210
<pre class="code-block">
211211
剩余游戏刻
212212
物品总数
213-
y1 x1 v1
213+
y1 x1 v1 t1
214214
...
215215
蛇总数
216216
id1 len1 score1 dir1 shield_cd1 shield_time1 has_key1

0 commit comments

Comments
 (0)