Skip to content

Instantly share code, notes, and snippets.

@riaf
Last active December 12, 2022 12:07
Show Gist options
  • Save riaf/6d0c7b85e62d94bd06d6325ba90455b7 to your computer and use it in GitHub Desktop.
Save riaf/6d0c7b85e62d94bd06d6325ba90455b7 to your computer and use it in GitHub Desktop.
サンプル(動作未確認)
import {
CellTypes,
findPlayersByPosition,
Game,
parseLegacyMap,
State,
} from "https://deno.land/x/[email protected]/mod.ts";
import { LegacyServer } from "https://deno.land/x/[email protected]/examples/legacy_server.ts";
const CELLCHARS = {
[CellTypes.Floor]: "⬛️",
[CellTypes.Block]: "⬜️",
[CellTypes.Item]: "⭐️",
};
const legacyMap = `N:example
T:200
S:15,17
D:0,0,0,0,3,0,0,3,0,0,0,3,3,0,0
D:0,0,0,0,0,3,0,0,0,0,3,0,0,0,0
D:3,0,0,3,3,0,0,3,0,0,0,0,0,3,0
D:0,3,0,3,0,0,0,3,2,0,0,0,0,3,0
D:0,0,0,0,0,0,0,0,2,0,0,0,0,0,0
D:0,0,0,0,0,0,2,0,2,0,0,3,0,3,0
D:0,3,0,0,0,0,2,0,2,3,2,2,2,0,0
D:0,2,2,2,2,0,2,0,3,3,0,0,0,3,0
D:0,0,0,3,3,0,2,3,2,0,3,3,0,0,0
D:0,3,0,0,0,3,3,0,2,0,2,2,2,2,0
D:0,0,2,2,2,3,2,0,2,0,0,0,0,3,0
D:0,3,0,3,0,0,2,0,2,0,0,0,0,0,0
D:0,0,0,0,0,0,2,0,0,0,0,0,0,0,0
D:0,3,0,0,0,0,2,3,0,0,0,3,0,3,0
D:0,3,0,0,0,0,0,3,0,0,3,3,0,0,3
D:0,0,0,0,3,0,0,0,0,3,0,0,0,0,0
D:0,0,3,3,0,0,0,3,0,0,3,0,0,0,0
H:7,15
C:7,1`;
const { map, maxTurn, cool, hot, playerPositions } = parseLegacyMap(legacyMap);
const game = new Game({ map, maxTurn, players: [cool, hot], playerPositions });
const server = new LegacyServer({ game, coolPort: 2009, hotPort: 2010 });
server.stateEvent.on(renderState);
await server.listen();
function renderState(state: State) {
if (state.isFinish) {
console.log(CELLCHARS[CellTypes.Block].repeat(80));
console.log(`Game finished: ${state.id}`);
for (const player of state.players) {
console.log(`${player.name}: ${state.score[player.id]}`);
}
console.log(
`Dead: ${[...state.deadPlayers].map((p) => p.name).join(", ")}`,
);
console.log(
`Kills: ${
state.killContexts.map((kc) => `${kc.killer.name} -> ${kc.target.name}`)
.join(", ")
}`,
);
return;
}
const playerSymbols = {
[state.players[0].id]: "⛄️",
[state.players[1].id]: "🔥",
};
console.log("=".repeat(80));
console.log(`State: ${state.id}`);
for (const player of state.players) {
console.log(`${player.name}: ${state.score[player.id]}`);
}
if (state.lastAction) {
console.log(
`Last Action By: ${state.lastAction.actor.name}, Command: ${state.lastAction.command}`,
);
}
console.log(CELLCHARS[CellTypes.Block].repeat(map.cells[0].length + 2));
for (const [y, mapy] of state.map.cells.entries()) {
console.log([
CELLCHARS[CellTypes.Block],
...mapy.map((cell, x) => {
const players = findPlayersByPosition(state, { x, y });
if (players.length > 0) {
return playerSymbols[players[0].id];
}
return CELLCHARS[cell.type];
}),
CELLCHARS[CellTypes.Block],
].join(""));
}
console.log(CELLCHARS[CellTypes.Block].repeat(map.cells[0].length + 2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment