跳转到内容

AI地牢游戏

我们将从创建一个新的 monorepo 开始。在目标目录中运行以下命令:

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

这将在 dungeon-adventure 目录中创建一个 NX monorepo,之后你可以在 vscode 中打开它。其文件结构应如下所示:

  • 文件夹.nx/
  • 文件夹.vscode/
  • 文件夹node_modules/
  • 文件夹packages/ 此处将存放你的子项目
  • .gitignore
  • .npmrc
  • .prettierignore
  • .prettierrc
  • nx.json 配置 Nx CLI 和 monorepo 默认值
  • 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 { Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
export class ApplicationStack extends Stack {
constructor(scope: Construct, id: string, props?: 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 引用!

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

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