Remove console.logs.
This commit is contained in:
17
Scene.ts
17
Scene.ts
@ -1,18 +1,31 @@
|
|||||||
import { GameData, Item } from "./data/data.ts";
|
import { GameData, Item } from "./data/data.ts";
|
||||||
|
import User from "./User.ts";
|
||||||
|
|
||||||
export default class Scene {
|
export default class Scene {
|
||||||
#items: Item[];
|
#items: Item[];
|
||||||
|
#user: User;
|
||||||
|
|
||||||
constructor(gameData: GameData) {
|
constructor(gameData: GameData) {
|
||||||
this.#items = gameData.items;
|
this.#items = gameData.items;
|
||||||
|
this.#user = new User();
|
||||||
}
|
}
|
||||||
|
|
||||||
take(target: string): Item | null {
|
async look() {
|
||||||
|
const description = this.#items
|
||||||
|
.map(({ description }) => description)
|
||||||
|
.join(" ");
|
||||||
|
|
||||||
|
await this.#user.tell(description);
|
||||||
|
}
|
||||||
|
|
||||||
|
async take(target: string): Promise<Item | null> {
|
||||||
const idx = this.#items.findIndex(({ name }) => name === target);
|
const idx = this.#items.findIndex(({ name }) => name === target);
|
||||||
|
|
||||||
if (idx >= 0) {
|
if (idx >= 0) {
|
||||||
const item = this.#items[idx];
|
const item = this.#items[idx];
|
||||||
this.#items = this.#items.splice(idx, 1);
|
this.#items.splice(idx, 1);
|
||||||
|
|
||||||
|
await this.#user.tell("Taken.");
|
||||||
|
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
28
User.ts
Normal file
28
User.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
const DEFAULT_PROMPT = ">";
|
||||||
|
|
||||||
|
export default class User {
|
||||||
|
#prompt: string;
|
||||||
|
#out: (text: string) => Promise<void>;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.#prompt = DEFAULT_PROMPT;
|
||||||
|
this.#out = async (text: string) => {
|
||||||
|
const data = new TextEncoder().encode(text);
|
||||||
|
await Deno.stdout.write(data);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
ask(question: string): string {
|
||||||
|
const answer = prompt(`${question}\n${this.#prompt} `);
|
||||||
|
|
||||||
|
if (!answer) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
|
|
||||||
|
async tell(statement: string): Promise<void> {
|
||||||
|
await this.#out(`\n${statement}\n`);
|
||||||
|
}
|
||||||
|
}
|
19
console.ts
19
console.ts
@ -1,19 +0,0 @@
|
|||||||
const DEFAULT_PROMPT = ">";
|
|
||||||
|
|
||||||
export default class Console {
|
|
||||||
#prompt: string;
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
this.#prompt = DEFAULT_PROMPT;
|
|
||||||
}
|
|
||||||
|
|
||||||
ask(question: string): string {
|
|
||||||
const answer = prompt(`${question}\n${this.#prompt} `);
|
|
||||||
|
|
||||||
if (!answer) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
return answer;
|
|
||||||
}
|
|
||||||
}
|
|
24
main.ts
24
main.ts
@ -1,26 +1,34 @@
|
|||||||
import Console from "./console.ts";
|
import User from "./User.ts";
|
||||||
|
import Scene from "./Scene.ts";
|
||||||
|
import { hall } from "./data/data.ts";
|
||||||
import parseCommand from "./parseCommand.ts";
|
import parseCommand from "./parseCommand.ts";
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const c = new Console();
|
const user = new User();
|
||||||
const question = "Are you a butthole?";
|
const scene = new Scene(hall);
|
||||||
|
const question = "";
|
||||||
let running = true;
|
let running = true;
|
||||||
let statement = "";
|
let statement = "";
|
||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
const prompts = `${statement}${question}`;
|
const prompts = `${statement}${question}`;
|
||||||
const answer = await c.ask(prompts);
|
const answer = await user.ask(prompts);
|
||||||
|
|
||||||
const { action } = parseCommand(answer);
|
const { action, target } = parseCommand(answer);
|
||||||
|
|
||||||
statement = "";
|
statement = "";
|
||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case "quit":
|
case "quit":
|
||||||
running = quit(c);
|
running = quit(user);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "look":
|
||||||
|
await scene.look();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "take":
|
case "take":
|
||||||
|
await scene.take(target);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -31,8 +39,8 @@ async function main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function quit(c: Console): boolean {
|
function quit(user: User): boolean {
|
||||||
const confirmQuit = c.ask("Are you sure you want to quit?");
|
const confirmQuit = user.ask("Are you sure you want to quit?\n");
|
||||||
|
|
||||||
if (confirmQuit[0] === "y") {
|
if (confirmQuit[0] === "y") {
|
||||||
return false;
|
return false;
|
||||||
|
Reference in New Issue
Block a user