跳转到内容

AI地牢游戏

我们将从创建新的单仓库开始。在目标目录中运行以下命令:

Terminal window
npx create-nx-workspace@~21.0.3 dungeon-adventure --pm=pnpm --preset=@aws/nx-plugin --ci=skip

这将在 dungeon-adventure 目录中设置一个 NX 单仓库,随后可以在 vscode 中打开。其结构应如下所示:

  • 文件夹.nx/
  • 文件夹.vscode/
  • 文件夹node_modules/
  • 文件夹packages/ 子项目存放位置
  • .gitignore
  • .npmrc
  • .prettierignore
  • .prettierrc
  • nx.json 配置 Nx CLI 和单仓库默认设置
  • package.json 定义所有 Node 依赖
  • pnpm-lock.yaml 或 bun.lock、yarn.lock、package-lock.json(取决于包管理器)
  • pnpm-workspace.yaml(如果使用 pnpm)
  • README.md
  • tsconfig.base.json 所有基于 Node 的子项目继承此配置
  • tsconfig.json

现在我们可以使用 @aws/nx-plugin 开始创建不同的子项目。

首先创建游戏 API。通过以下步骤创建名为 GameApi 的 tRPC API:

  1. 安装 Nx Console VSCode Plugin 如果您尚未安装
  2. 在VSCode中打开Nx控制台
  3. 点击 Generate (UI) 在"Common Nx Commands"部分
  4. 搜索 @aws/nx-plugin - ts#trpc-api
  5. 填写必需参数
    • name: GameApi
  6. 点击 Generate

您应该看到文件树中出现了新文件。

点击此处查看文件详情。

现在创建故事 API。通过以下步骤创建名为 StoryApi 的 Fast API:

  1. 安装 Nx Console VSCode Plugin 如果您尚未安装
  2. 在VSCode中打开Nx控制台
  3. 点击 Generate (UI) 在"Common Nx Commands"部分
  4. 搜索 @aws/nx-plugin - py#fast-api
  5. 填写必需参数
    • name: StoryApi
    • moduleName: story_api
  6. 点击 Generate

您应该看到文件树中出现了新文件。

点击此处查看文件详情。

现在创建用于与游戏交互的 UI。通过以下步骤创建名为 GameUI 的网站:

  1. 安装 Nx Console VSCode Plugin 如果您尚未安装
  2. 在VSCode中打开Nx控制台
  3. 点击 Generate (UI) 在"Common Nx Commands"部分
  4. 搜索 @aws/nx-plugin - ts#react-website
  5. 填写必需参数
    • name: GameUI
  6. 点击 Generate

您应该看到文件树中出现了新文件。

点击此处查看文件详情。

现在通过以下步骤配置 Game UI 要求通过 Amazon Cognito 进行认证:

  1. 安装 Nx Console VSCode Plugin 如果您尚未安装
  2. 在VSCode中打开Nx控制台
  3. 点击 Generate (UI) 在"Common Nx Commands"部分
  4. 搜索 @aws/nx-plugin - ts#react-website#auth
  5. 填写必需参数
    • cognitoDomain: game-ui
    • project: @dungeon-adventure/game-ui
    • allowSignup: true
  6. 点击 Generate

您应该看到文件树中有文件变更。

点击此处查看文件详情。

现在配置 Game UI 连接之前创建的故事 API:

  1. 安装 Nx Console VSCode Plugin 如果您尚未安装
  2. 在VSCode中打开Nx控制台
  3. 点击 Generate (UI) 在"Common Nx Commands"部分
  4. 搜索 @aws/nx-plugin - api-connection
  5. 填写必需参数
    • sourceProject: @dungeon-adventure/game-ui
    • targetProject: dungeon_adventure.story_api
  6. 点击 Generate

您应该看到文件树中有文件变更。

点击此处查看文件详情。

现在配置 Game UI 连接之前创建的游戏 API:

  1. 安装 Nx Console VSCode Plugin 如果您尚未安装
  2. 在VSCode中打开Nx控制台
  3. 点击 Generate (UI) 在"Common Nx Commands"部分
  4. 搜索 @aws/nx-plugin - api-connection
  5. 填写必需参数
    • sourceProject: @dungeon-adventure/game-ui
    • targetProject: @dungeon-adventure/game-api
  6. 点击 Generate

您应该看到文件树中有文件变更。

点击此处查看文件详情。

最后需要创建 CDK 基础设施子项目。通过以下步骤创建:

  1. 安装 Nx Console VSCode Plugin 如果您尚未安装
  2. 在VSCode中打开Nx控制台
  3. 点击 Generate (UI) 在"Common Nx Commands"部分
  4. 搜索 @aws/nx-plugin - ts#infra
  5. 填写必需参数
    • name: infra
  6. 点击 Generate

您应该看到文件树中有文件变更。

点击此处查看文件详情。

更新 packages/infra/src/stacks/application-stack.ts 以实例化已生成的构造:

import {
GameApi,
GameUI,
StoryApi,
UserIdentity,
} from ':dungeon-adventure/common-constructs';
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
export class ApplicationStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// The code that defines your stack goes here
const userIdentity = new UserIdentity(this, 'UserIdentity');
const gameApi = new GameApi(this, 'GameApi', {
integrations: GameApi.defaultIntegrations(this).build(),
});
const storyApi = new StoryApi(this, 'StoryApi', {
integrations: StoryApi.defaultIntegrations(this).build(),
});
// grant our authenticated role access to invoke our APIs
[storyApi, gameApi].forEach((api) =>
api.grantInvokeAccess(userIdentity.identityPool.authenticatedRole),
);
// Ensure this is instantiated last so our runtime-config.json can be automatically configured
new GameUI(this, 'GameUI');
}
}

注意我们为两个 API 提供了默认集成。默认情况下,API 中的每个操作都映射到独立的 Lambda 函数处理。

现在首次构建代码
Terminal window
pnpm nx run-many --target build --all

您将看到以下提示:

Terminal window
NX 工作区不同步
[@nx/js:typescript-sync]: 某些 TypeScript 配置文件缺少项目引用或包含过时的引用。
这将在 CI 中导致错误。
? 是否要同步变更以使工作区保持最新? …
是,同步变更并运行任务
否,不同步直接运行任务

此消息表示 NX 检测到可自动更新的文件。此处指未设置 Typescript 引用的 tsconfig.json 文件。选择 是,同步变更并运行任务 继续。所有 IDE 导入错误将自动解决,因为同步生成器会自动添加缺失的 Typescript 引用!

所有构建产物现在位于单仓库根目录的 dist/ 文件夹中。这是使用 @aws/nx-plugin 生成项目的标准实践,避免生成文件污染文件树。如需清理,直接删除 dist/ 文件夹即可。

恭喜!您已成功创建地牢冒险游戏所需的所有子项目,可以开始核心功能开发了。🎉🎉🎉