|
|
@@ -45,6 +45,7 @@ import { RoomService } from 'src/room/room.service'
|
|
|
import { InitGame } from './dto/init-game.dto'
|
|
|
import { PlotOption } from './models/plot-option.model'
|
|
|
import { VoteCallback } from './types'
|
|
|
+import { Survival } from './entities/survival.entity'
|
|
|
|
|
|
@Injectable()
|
|
|
export class GameService implements OnModuleInit {
|
|
|
@@ -66,6 +67,8 @@ export class GameService implements OnModuleInit {
|
|
|
private readonly gameStateRepository: Repository<GameState>,
|
|
|
@InjectRepository(Charactor)
|
|
|
private readonly charactorRepository: Repository<Charactor>,
|
|
|
+ @InjectRepository(Survival)
|
|
|
+ private readonly survivalRepository: Repository<Survival>,
|
|
|
@Inject(forwardRef(() => EventsGateway))
|
|
|
private readonly eventsGateway: EventsGateway,
|
|
|
private readonly promptService: PromptService,
|
|
|
@@ -332,12 +335,14 @@ export class GameService implements OnModuleInit {
|
|
|
})
|
|
|
|
|
|
gameState.charactors = [...game.charactors].map((i) => {
|
|
|
- i.hp = 100
|
|
|
- i.joinAt = initGame.date
|
|
|
- i.survival = 0
|
|
|
- i.dead = false
|
|
|
-
|
|
|
- return i
|
|
|
+ return {
|
|
|
+ ...i,
|
|
|
+ gameId: id,
|
|
|
+ roomId: game.roomId,
|
|
|
+ hp: 100,
|
|
|
+ joinAt: initGame.date,
|
|
|
+ dead: false
|
|
|
+ }
|
|
|
})
|
|
|
|
|
|
if (initGame.plot) {
|
|
|
@@ -420,8 +425,11 @@ export class GameService implements OnModuleInit {
|
|
|
} else {
|
|
|
newCharactor = new Charactor({
|
|
|
...newCharactor,
|
|
|
+ gameId: id,
|
|
|
+ roomId: game.roomId,
|
|
|
hp: 100,
|
|
|
- joinAt: date
|
|
|
+ joinAt: date,
|
|
|
+ dead: false
|
|
|
})
|
|
|
this.send(`${id}`, {
|
|
|
type: 'newCharactor',
|
|
|
@@ -469,6 +477,8 @@ export class GameService implements OnModuleInit {
|
|
|
charactorsAlive.push(newCharactor)
|
|
|
}
|
|
|
|
|
|
+ await this.addSurvival(charactorsAlive)
|
|
|
+
|
|
|
const willEnd = charactorsAlive.filter((i) => !i.dead).length === 0
|
|
|
|
|
|
if (!willEnd && createOptions) {
|
|
|
@@ -783,12 +793,14 @@ export class GameService implements OnModuleInit {
|
|
|
try {
|
|
|
const addCharactor = danmu.content.replace(/^我是/, '')
|
|
|
const danmuUser = await this.danmuService.findDanmuUser(danmu.danmuUserId)
|
|
|
- const charactor = await this.createNewCharactor(id, addCharactor)
|
|
|
- this.logger.log(`已创建角色: ${JSON.stringify(charactor)}`)
|
|
|
- charactor.name = danmuUser.name
|
|
|
- charactor.avatar = danmuUser.avatar
|
|
|
- charactor.danmuUserId = danmu.danmuUserId
|
|
|
- return charactor
|
|
|
+ if (danmuUser) {
|
|
|
+ const charactor = await this.createNewCharactor(id, addCharactor)
|
|
|
+ this.logger.log(`已创建角色: ${JSON.stringify(charactor)}`)
|
|
|
+ charactor.name = danmuUser.name
|
|
|
+ charactor.avatar = danmuUser.avatar
|
|
|
+ charactor.danmuUserId = danmu.danmuUserId
|
|
|
+ return charactor
|
|
|
+ }
|
|
|
} catch (error) {
|
|
|
this.logger.error(error)
|
|
|
}
|
|
|
@@ -959,4 +971,47 @@ export class GameService implements OnModuleInit {
|
|
|
}
|
|
|
return death
|
|
|
}
|
|
|
+
|
|
|
+ async addSurvival(charactors: Charactor[]) {
|
|
|
+ for (const c of charactors) {
|
|
|
+ if (c.danmuUserId) {
|
|
|
+ const damuUser = await this.danmuService.findDanmuUser(c.danmuUserId)
|
|
|
+ if (damuUser) {
|
|
|
+ let s = await this.survivalRepository.findOneBy({
|
|
|
+ roomId: c.roomId,
|
|
|
+ danmuUserId: c.danmuUserId
|
|
|
+ })
|
|
|
+ if (!s) {
|
|
|
+ s = new Survival({
|
|
|
+ roomId: c.roomId,
|
|
|
+ danmuUserId: c.danmuUserId,
|
|
|
+ survival: 0
|
|
|
+ })
|
|
|
+ }
|
|
|
+ s.survival++
|
|
|
+ await this.survivalRepository.save(s)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async survivalRank(roomId: number) {
|
|
|
+ const rank = await this.survivalRepository.find({
|
|
|
+ where: {
|
|
|
+ roomId
|
|
|
+ },
|
|
|
+ order: {
|
|
|
+ survival: 'DESC'
|
|
|
+ },
|
|
|
+ take: 10
|
|
|
+ })
|
|
|
+
|
|
|
+ for (const r of rank) {
|
|
|
+ const danmuUser = await this.danmuService.findDanmuUser(r.danmuUserId)
|
|
|
+ if (danmuUser) {
|
|
|
+ r.userInfo = danmuUser
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return rank
|
|
|
+ }
|
|
|
}
|