跳转到内容

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 The workspace is out of sync
[@nx/js:typescript-sync]: Some TypeScript configuration files are missing project references to the projects they depend on or contain outdated project references.
This will result in an error in CI.
? Would you like to sync the identified changes to get your workspace up to date?
Yes, sync the changes and run the tasks
No, run the tasks without syncing the changes

此消息表示 NX 检测到可自动更新的文件。此处指未设置 TypeScript 引用的 tsconfig.json 文件。选择 Yes, sync the changes and run the tasks 继续。你会注意到所有 IDE 相关的导入错误自动解决,因为同步生成器会自动添加缺失的 TypeScript 引用!

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

恭喜!你已创建了开始实现地牢冒险游戏核心所需的所有子项目。🎉🎉🎉