feat: add tic-tac-toe with rule invoking rule
This commit is contained in:
+28
-13
@@ -1,6 +1,11 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { createRule, type RuleContext } from '../../src/core/rule';
|
||||
import { createRule, type RuleContext, type GameContextLike } from '../../src/core/rule';
|
||||
import { createGameContext } from '../../src/core/context';
|
||||
import type { Command } from '../../src/utils/command';
|
||||
|
||||
function isCommand(value: Command | RuleContext<unknown>): value is Command {
|
||||
return 'name' in value;
|
||||
}
|
||||
|
||||
describe('Rule System', () => {
|
||||
function createTestGame() {
|
||||
@@ -24,11 +29,12 @@ describe('Rule System', () => {
|
||||
});
|
||||
|
||||
it('should create a generator when called', () => {
|
||||
const game = createTestGame();
|
||||
const rule = createRule('<target>', function*(cmd) {
|
||||
return cmd.params[0];
|
||||
});
|
||||
|
||||
const gen = rule.create({ name: 'test', params: ['card1'], flags: {}, options: {} });
|
||||
const gen = rule.create.call(game as unknown as GameContextLike, { name: 'test', params: ['card1'], flags: {}, options: {} });
|
||||
const result = gen.next();
|
||||
expect(result.done).toBe(true);
|
||||
expect(result.value).toBe('card1');
|
||||
@@ -57,7 +63,8 @@ describe('Rule System', () => {
|
||||
|
||||
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
|
||||
const confirm = yield { name: '', params: [], options: [], flags: [] };
|
||||
return { moved: cmd.params[0], confirmed: confirm.name === 'confirm' };
|
||||
const confirmCmd = isCommand(confirm) ? confirm : undefined;
|
||||
return { moved: cmd.params[0], confirmed: confirmCmd?.name === 'confirm' };
|
||||
}));
|
||||
|
||||
game.dispatchCommand('move card1 hand');
|
||||
@@ -132,7 +139,8 @@ describe('Rule System', () => {
|
||||
|
||||
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
|
||||
const response = yield { name: '', params: [], options: [], flags: [] };
|
||||
return { moved: cmd.params[0], response: response.name };
|
||||
const rcmd = isCommand(response) ? response : undefined;
|
||||
return { moved: cmd.params[0], response: rcmd?.name };
|
||||
}));
|
||||
|
||||
game.dispatchCommand('move card1 hand');
|
||||
@@ -147,7 +155,8 @@ describe('Rule System', () => {
|
||||
|
||||
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
|
||||
const response = yield '<item>';
|
||||
return { response: response.params[0] };
|
||||
const rcmd = isCommand(response) ? response : undefined;
|
||||
return { response: rcmd?.params[0] };
|
||||
}));
|
||||
|
||||
game.dispatchCommand('move card1 hand');
|
||||
@@ -162,7 +171,8 @@ describe('Rule System', () => {
|
||||
|
||||
game.registerRule('trade', createRule('<from> <to>', function*(cmd) {
|
||||
const response = yield '<item> [amount: number]';
|
||||
return { traded: response.params[0] };
|
||||
const rcmd = isCommand(response) ? response : undefined;
|
||||
return { traded: rcmd?.params[0] };
|
||||
}));
|
||||
|
||||
game.dispatchCommand('trade player1 player2');
|
||||
@@ -333,7 +343,8 @@ describe('Rule System', () => {
|
||||
|
||||
game.registerRule('test', createRule('<arg>', function*() {
|
||||
const cmd = yield customSchema;
|
||||
return { received: cmd.params[0] };
|
||||
const rcmd = isCommand(cmd) ? cmd : undefined;
|
||||
return { received: rcmd?.params[0] };
|
||||
}));
|
||||
|
||||
game.dispatchCommand('test val1');
|
||||
@@ -349,7 +360,9 @@ describe('Rule System', () => {
|
||||
game.registerRule('multi', createRule('<start>', function*() {
|
||||
const a = yield '<value>';
|
||||
const b = yield '<value>';
|
||||
return { a: a.params[0], b: b.params[0] };
|
||||
const acmd = isCommand(a) ? a : undefined;
|
||||
const bcmd = isCommand(b) ? b : undefined;
|
||||
return { a: acmd?.params[0], b: bcmd?.params[0] };
|
||||
}));
|
||||
|
||||
game.dispatchCommand('multi init');
|
||||
@@ -369,13 +382,15 @@ describe('Rule System', () => {
|
||||
const player = cmd.params[0];
|
||||
const action = yield { name: '', params: [], options: [], flags: [] };
|
||||
|
||||
if (action.name === 'move') {
|
||||
yield '<target>';
|
||||
} else if (action.name === 'attack') {
|
||||
yield '<target> [--power: number]';
|
||||
if (isCommand(action)) {
|
||||
if (action.name === 'move') {
|
||||
yield '<target>';
|
||||
} else if (action.name === 'attack') {
|
||||
yield '<target> [--power: number]';
|
||||
}
|
||||
}
|
||||
|
||||
return { player, action: action.name };
|
||||
return { player, action: isCommand(action) ? action.name : '' };
|
||||
}));
|
||||
|
||||
const ctx1 = game.dispatchCommand('start alice');
|
||||
|
||||
Reference in New Issue
Block a user