检查机器人是否在游戏中改为本地方法
This commit is contained in:
parent
26ccfe0ea2
commit
00f793ec49
@ -13,6 +13,7 @@ import {
|
|||||||
import { fetchAccount } from '../dao/AccountDao'
|
import { fetchAccount } from '../dao/AccountDao'
|
||||||
import { generateId } from '../utils/security.util'
|
import { generateId } from '../utils/security.util'
|
||||||
import { getRandom } from '../utils/number.util'
|
import { getRandom } from '../utils/number.util'
|
||||||
|
import { RobotDao } from '../dao/RobotDao'
|
||||||
const isProd = process.env.NODE_ENV === 'production'
|
const isProd = process.env.NODE_ENV === 'production'
|
||||||
const DEFAULT_NICKNAME = '匿名玩家'
|
const DEFAULT_NICKNAME = '匿名玩家'
|
||||||
|
|
||||||
@ -72,23 +73,26 @@ export default class AccountController extends BaseController {
|
|||||||
let accountid, targetScore
|
let accountid, targetScore
|
||||||
if (datas.length > 0) {
|
if (datas.length > 0) {
|
||||||
for (let i = 0; i < datas.length; i += 2) {
|
for (let i = 0; i < datas.length; i += 2) {
|
||||||
let gameing = await checkGameing(datas[i])
|
|
||||||
if (gameing) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if (!datas[i].startsWith(BaseConst.ROBOT_PREFIX)) {
|
if (!datas[i].startsWith(BaseConst.ROBOT_PREFIX)) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if (!accountSet.has(datas[i]) ) {
|
if (accountSet.has(datas[i])) {
|
||||||
accountid = datas[i]
|
continue
|
||||||
targetScore = datas[i + 1] | 0
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
let gameing = new RobotDao().checkGameing(datas[i])
|
||||||
|
if (gameing) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
accountid = datas[i]
|
||||||
|
targetScore = datas[i + 1] | 0
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!accountid) {
|
if (!accountid) {
|
||||||
accountid = BaseConst.ROBOT_PREFIX + generateId()
|
accountid = BaseConst.ROBOT_PREFIX + generateId()
|
||||||
targetScore = getRandom(min, max) | 0
|
targetScore = getRandom(min, max) | 0
|
||||||
|
new RobotDao().setGameing(accountid)
|
||||||
}
|
}
|
||||||
let account = await fetchAccount({accountid, robot: true})
|
let account = await fetchAccount({accountid, robot: true})
|
||||||
if (account.isnew) {
|
if (account.isnew) {
|
||||||
@ -103,7 +107,6 @@ export default class AccountController extends BaseController {
|
|||||||
account.isnew = 0
|
account.isnew = 0
|
||||||
await account.save()
|
await account.save()
|
||||||
}
|
}
|
||||||
await setGameing(accountid, 30)
|
|
||||||
return {
|
return {
|
||||||
accountid: account._id,
|
accountid: account._id,
|
||||||
nickname: account.nickname || DEFAULT_NICKNAME,
|
nickname: account.nickname || DEFAULT_NICKNAME,
|
||||||
|
28
src/dao/RobotDao.ts
Normal file
28
src/dao/RobotDao.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import { singleton } from '../decorators/singleton.decorator'
|
||||||
|
|
||||||
|
const MAX_SEC = 30
|
||||||
|
@singleton
|
||||||
|
export class RobotDao {
|
||||||
|
accountSet: Map<string, number>
|
||||||
|
constructor() {
|
||||||
|
this.accountSet = new Map()
|
||||||
|
}
|
||||||
|
|
||||||
|
setGameing(key: string) {
|
||||||
|
let now = Date.now() / 1000 | 0
|
||||||
|
this.accountSet.set(key, now)
|
||||||
|
}
|
||||||
|
checkGameing(key: string) {
|
||||||
|
let now = Date.now() / 1000 | 0
|
||||||
|
for(let [key, sec] of this.accountSet) {
|
||||||
|
if (now - sec >= MAX_SEC) {
|
||||||
|
this.accountSet.delete(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let result = this.accountSet.has(key)
|
||||||
|
if (!result) {
|
||||||
|
this.accountSet.set(key, now)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user