Skip to content

chellongCoder/BE-The-Last-King-Game

Repository files navigation

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Discord Backers on Open Collective Sponsors on Open Collective Donate us Support us Follow us on Twitter

Description

Nest framework TypeScript starter repository.

Project setup

$ npm install

Compile and run the project

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Run tests

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

Deployment

When you're ready to deploy your NestJS application to production, there are some key steps you can take to ensure it runs as efficiently as possible. Check out the deployment documentation for more information.

If you are looking for a cloud-based platform to deploy your NestJS application, check out Mau, our official platform for deploying NestJS applications on AWS. Mau makes deployment straightforward and fast, requiring just a few simple steps:

$ npm install -g @nestjs/mau
$ mau deploy

With Mau, you can deploy your application in just a few clicks, allowing you to focus on building features rather than managing infrastructure.

Resources

Check out a few resources that may come in handy when working with NestJS:

  • Visit the NestJS Documentation to learn more about the framework.
  • For questions and support, please visit our Discord channel.
  • To dive deeper and get more hands-on experience, check out our official video courses.
  • Deploy your application to AWS with the help of NestJS Mau in just a few clicks.
  • Visualize your application graph and interact with the NestJS application in real-time using NestJS Devtools.
  • Need help with your project (part-time to full-time)? Check out our official enterprise support.
  • To stay in the loop and get updates, follow us on X and LinkedIn.
  • Looking for a job, or have a job to offer? Check out our official Jobs board.

The NestJS project with Supabase and Drizzle ORM integration is now set up!

Here are the next steps for you:

  1. Fill in .env: Open the newly created be-the-last-empire/.env file and replace the placeholder values (YOUR_SUPABASE_URL, YOUR_SUPABASE_ANON_KEY, YOUR_SUPABASE_SERVICE_ROLE_KEY, and [YOUR-PASSWORD], [AWS-REGION]) with your actual Supabase project credentials. You can find these in your Supabase project settings under "API" and "Database".

  2. Generate Drizzle Migrations:

    • Once your .env is configured, run npm run drizzle:generate inside the be-the-last-empire directory. This will create a migration file in the ./drizzle folder based on your src/db/schema.ts.
  3. Apply Drizzle Migrations:

    • After generating the migration, run npm run drizzle:push. This command will apply the schema changes to your Supabase PostgreSQL database.
  4. Start the Application:

    • You can start the NestJS application with npm run start:dev.
    • You can then access the example users endpoint at http://localhost:3000/users (after applying migrations and starting the app).
  5. Further Development:

    • You can now extend src/db/schema.ts to define more tables and relationships.
    • Use drizzle-kit generate to create new migrations.
    • Build out more modules, services, and controllers for your application's API.

Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.

Stay in touch

https://supabase.com/dashboard/project/mlydsvrsezrjstlqdliv

License

Nest is MIT licensed.

Plan

  • install langchain
  • create api return data for scenario (tự sinh ra data object scenario, giải thích tại sao lại sinh ra data như vậy với từng value của từng field )

prompt : You are a History expert about vietnam history. -> template Imagine you are current being in Tran dynasty You need create scenario like this object { "id": "intro_1", "text": "Nhà Trần mới thành lập, lòng dân chưa yên. Bạn sẽ làm gì để củng cố quyền lực?", "imagePath": "assets/images/intro_1.png", "leftChoice": { "text": "Giảm thuế cho dân", "effect": { "people": 10, "money": -10, "army": 0, "religion": 0 } }, "rightChoice": { "text": "Chiêu mộ binh lính", "effect": { "army": 10, "money": -5, "people": 0, "religion": 0 } } },

You need create api return Scenario data array , each scenario has left and right choice , text to describe scenario and imagePath to describe scenario.

New Features & Implementation (Updated Feb 2026)

1. Database Normalization

The scenario system has been normalized into three distinct tables for better data integrity:

  • scenarios: Stores the core scenario text and image paths.
  • choices: linked to scenarios (Left/Right types).
  • scenario_effects: Linked to choices, storing numeric impacts on money, army, people, and religion.

2. AI Scenario Generation (LangChain)

Integrated LangChain with Google Gemini 1.5 Flash to generate historical scenarios dynamically.

  • Provider: Google Generative AI
  • Model: gemini-1.5-flash
  • Output: Structured JSON following the game's data model.

3. Real-time Streaming (SSE)

Implemented Server-Sent Events (SSE) to solve latency issues with large AI batches (30+ scenarios).

  • Records are streamed one-by-one as the AI generates them.
  • Initial data appears on mobile in ~1-2 seconds instead of waiting for the full batch.

4. API Endpoints

  • GET /scenarios: Fetch all reconstructed scenarios from the database.
  • POST /scenarios: Manually create a new scenario with nested choices/effects.
  • GET /scenarios/generate: Request a batch of AI-generated scenarios (Wait for completion).
  • Sse /scenarios/generate-stream: Listen for a real-time stream of AI-generated scenarios.

5. Technical Optimizations

  • Prompts: Merged System instructions into Human messages for maximum compatibility with Gemini V1 API.
  • Streaming Logic: Custom implementation using Async Generators (async *) and JsonOutputParser to yield partial records.

6. Endpoint Security & Hardening (New)

Implemented a comprehensive security layer following NestJS best practices:

  • Authentication: Stateless JWT verification using passport-jwt. Tokens issued by Supabase are cryptographically verified using JWT_SECRET (HS256) without needing direct API calls to Supabase for each request.
  • Authorization: Global JwtAuthGuard (Deny-by-default). Public routes marked with @Public().
  • Rate Limiting: Global ThrottlerGuard (10 req/min). AI endpoints restricted to 3 req/min.
  • Hardening:
    • Helmet: Secure HTTP headers.
    • CORS: Restricted to trusted origins.
    • ValidationPipe: Whitelisted DTOs to prevent mass assignment attacks.

About

BE for The last king game app

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors