Commit to Deno project.

This commit is contained in:
2021-04-28 16:55:22 -07:00
parent 2b1f9ffba1
commit c1935e58be
12 changed files with 104 additions and 43 deletions

View File

@ -1,11 +0,0 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"ignorePatterns": ["node_modules", "dist", "*.validator.ts"]
}

3
.gitignore vendored
View File

@ -2,5 +2,4 @@
.DS_Store
# Folders
dist/*
node_modules/*
.vscode

View File

@ -1,3 +0,0 @@
{
"useTabs": true
}

22
Scene.ts Normal file
View File

@ -0,0 +1,22 @@
import { GameData, Item } from "./data/data.ts";
export default class Scene {
#items: Item[];
constructor(gameData: GameData) {
this.#items = gameData.items;
}
take(target: string): Item | null {
const idx = this.#items.findIndex(({ name }) => name === target);
if (idx >= 0) {
const item = this.#items[idx];
this.#items = this.#items.splice(idx, 1);
return item;
}
return null;
}
}

19
console.ts Normal file
View File

@ -0,0 +1,19 @@
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;
}
}

10
data/data.ts Normal file
View File

@ -0,0 +1,10 @@
export * from "./rooms.ts";
export interface Item {
name: string;
description: string;
}
export interface GameData {
items: Item[];
}

3
data/rooms.ts Normal file
View File

@ -0,0 +1,3 @@
export const hall = {
items: [{ name: "flashlight", description: "There is a flashlight." }],
};

44
main.ts Normal file
View File

@ -0,0 +1,44 @@
import Console from "./console.ts";
import parseCommand from "./parseCommand.ts";
async function main() {
const c = new Console();
const question = "Are you a butthole?";
let running = true;
let statement = "";
while (running) {
const prompts = `${statement}${question}`;
const answer = await c.ask(prompts);
const { action } = parseCommand(answer);
statement = "";
switch (action) {
case "quit":
running = quit(c);
break;
case "take":
break;
default:
statement = "I didn't understand that.\n";
break;
}
}
}
function quit(c: Console): boolean {
const confirmQuit = c.ask("Are you sure you want to quit?");
if (confirmQuit[0] === "y") {
return false;
}
return true;
}
main();

View File

@ -1,11 +0,0 @@
{
"name": "detective",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

5
parseCommand.ts Normal file
View File

@ -0,0 +1,5 @@
export default function parseCommand(command: string) {
const [action, target] = command.toLocaleLowerCase().split(" ");
return { action, target };
}

View File

View File

@ -1,16 +0,0 @@
{
"compilerOptions": {
"target": "ES2020",
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "commonjs",
"moduleResolution": "node",
"resolveJsonModule": true,
"outDir": "dist",
"sourceMap": true
},
"include": ["src"]
}