feat(framework): core 座位↔视图转换
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
/** 绝对座位 → 以 mySeat 为视角的视图位(自己=0,其余环形顺延)。 */
|
||||
export function toView(mySeat: number, targetSeat: number, seatCount: number): number {
|
||||
return ((targetSeat - mySeat) % seatCount + seatCount) % seatCount;
|
||||
}
|
||||
|
||||
/** 视图位 → 绝对座位(toView 的逆运算)。 */
|
||||
export function fromView(mySeat: number, viewSeat: number, seatCount: number): number {
|
||||
return ((viewSeat + mySeat) % seatCount + seatCount) % seatCount;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { toView, fromView } from '../../YouleNexus/assets/framework/core/seat.ts';
|
||||
|
||||
test('toView:自己永远是视图 0 号位', () => {
|
||||
assert.equal(toView(2, 2, 4), 0); // mySeat=2, target=2 → 0
|
||||
});
|
||||
|
||||
test('toView:他人按环形相对自己排布', () => {
|
||||
// 4 人桌,我在 2 号位:座位 3→视图1,座位 0→视图2,座位 1→视图3
|
||||
assert.equal(toView(2, 3, 4), 1);
|
||||
assert.equal(toView(2, 0, 4), 2);
|
||||
assert.equal(toView(2, 1, 4), 3);
|
||||
});
|
||||
|
||||
test('fromView 是 toView 的逆运算', () => {
|
||||
const seatCount = 4, mySeat = 2;
|
||||
for (let s = 0; s < seatCount; s++) {
|
||||
assert.equal(fromView(mySeat, toView(mySeat, s, seatCount), seatCount), s);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user