xiongzhu vor 2 Jahren
Ursprung
Commit
24868de4be
4 geänderte Dateien mit 47 neuen und 11 gelöschten Zeilen
  1. 2 1
      src/game/game.module.ts
  2. 11 5
      src/game/game.service.ts
  3. 2 1
      src/game/types.d.ts
  4. 32 4
      src/sys-config/sys-config.service.ts

+ 2 - 1
src/game/game.module.ts

@@ -19,7 +19,8 @@ import { Survival } from './entities/survival.entity'
         PromptModule,
         SysConfigModule,
         DanmuModule,
-        RoomModule
+        RoomModule,
+        SysConfigModule
     ],
     controllers: [GameController],
     providers: [GameService],

+ 11 - 5
src/game/game.service.ts

@@ -74,7 +74,8 @@ export class GameService implements OnModuleInit {
         private readonly promptService: PromptService,
         private readonly sysConfigService: SysConfigService,
         private readonly danmuService: DanmuService,
-        private readonly roomService: RoomService
+        private readonly roomService: RoomService,
+        private readonly sysConfig: SysConfigService
     ) {}
 
     async onModuleInit() {
@@ -383,7 +384,7 @@ export class GameService implements OnModuleInit {
         let date = null,
             time = null,
             plot = null,
-            options = []
+            options: PlotOption[] = []
         lastState.choice = choice || lastState.choice || null
 
         await this.gameStateRepository.update(lastState.id, lastState)
@@ -482,7 +483,12 @@ export class GameService implements OnModuleInit {
         const willEnd = charactorsAlive.filter((i) => !i.dead).length === 0
 
         if (!willEnd && createOptions) {
-            options = await this.createOptions(charactors, `${summary}\n\n${formatedDatatime}\n${plot}`)
+            const voteDelay = await this.sysConfig.getNumber('vote_delay', 0)
+            const delay = voteDelay > 0 ? setTimeout(voteDelay * 1000) : Promise.resolve()
+
+            options = (
+                await Promise.all([this.createOptions(charactors, `${summary}\n\n${formatedDatatime}\n${plot}`), delay])
+            )[0]
             this.send(`${id}`, {
                 type: 'options',
                 data: options
@@ -724,7 +730,7 @@ export class GameService implements OnModuleInit {
         try {
             this.logger.log('发起投票')
             let s = differenceInSeconds(new Date(), startTime)
-            while (s < 30) {
+            while (s < (await this.sysConfigService.getNumber('vote_wait_time', 30))) {
                 await setTimeout(1000)
                 const votes = await this.danmuService.collectVotes(gameId, startTime, options.length)
                 cb?.onProgress(votes)
@@ -778,7 +784,7 @@ export class GameService implements OnModuleInit {
                     i.modifyHp = i.modifyHp.filter(
                         (j) => j.changeValue !== 0 && charactors.find((k) => k.name === j.name)
                     )
-                    return i
+                    return i as PlotOption
                 })
             } catch (error) {
                 this.logger.error(error)

+ 2 - 1
src/game/types.d.ts

@@ -7,4 +7,5 @@ declare class GameEvent {
 declare interface VoteCallback {
     onProgress?: (p: number[]) => void
     onResult?: (choice: PlotOption) => void
-}
+}
+

+ 32 - 4
src/sys-config/sys-config.service.ts

@@ -1,18 +1,36 @@
-import { Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common'
+import { Injectable, InternalServerErrorException, NotFoundException, OnModuleInit } from '@nestjs/common'
 import { InjectRepository } from '@nestjs/typeorm'
 import { Repository } from 'typeorm'
-import { SysConfig } from './entities/sys-config.entity'
-import { PageRequest } from 'src/common/dto/page-request'
+import { SysConfig, SysConfigType } from './entities/sys-config.entity'
+import { PageRequest } from '../common/dto/page-request'
 import { Pagination, paginate } from 'nestjs-typeorm-paginate'
 
 @Injectable()
-export class SysConfigService {
+export class SysConfigService implements OnModuleInit {
     constructor(
         @InjectRepository(SysConfig)
         private readonly sysConfigRepository: Repository<SysConfig>
     ) {
         ;(async function init() {})()
     }
+    async onModuleInit() {
+        if (!(await this.sysConfigRepository.findOneBy({ name: 'vote_delay' }))) {
+            await this.sysConfigRepository.save({
+                name: 'vote_delay',
+                value: '0',
+                type: SysConfigType.Number,
+                remark: '投票延迟时间'
+            })
+        }
+        if (!(await this.sysConfigRepository.findOneBy({ name: 'vote_wait_time' }))) {
+            await this.sysConfigRepository.save({
+                name: 'vote_wait_time',
+                value: '30',
+                type: SysConfigType.Number,
+                remark: '投票等待时间'
+            })
+        }
+    }
 
     async findAll(req: PageRequest<SysConfig>) {
         try {
@@ -41,4 +59,14 @@ export class SysConfigService {
             throw new InternalServerErrorException(error.message)
         }
     }
+
+    async getNumber(name: string, defaultValue: number): Promise<number> {
+        const conf = await this.sysConfigRepository.findOneBy({ name })
+        return parseInt(conf.value) || defaultValue
+    }
+
+    async getString(name: string, defaultValue: string): Promise<string> {
+        const conf = await this.sysConfigRepository.findOneBy({ name })
+        return conf.value || defaultValue
+    }
 }