|
发表于 2025-7-18 22:23:07
|
显示全部楼层
后端模块,不完善仅供参考
使用语言:js- //反作弊
- const { json } = require('../json');
- const channel = require('./Channel');
- const { tools } = require('../tool');
- let serverTime = Date.now();
- class anti {
- constructor(p) {
- this.player = p;
- this.jump_num = 0;
- this.throw_num = 0;
- this.pos_num = 0;
- this.开挂次数 = 0;
- this.检测阈值 = 3;
- this.异常移动 = false;
- this.old_x = Math.random();
- this.old_y = Math.random();
- this.x = 0;
- this.y = 0;
- this.tick = 0;
- this.old_move_time = 0;
- this.random = { x: this.old_x, y: this.old_y };
- this.无效数据 = false;
- this.old_Animation = '';
- this.timer = setInterval(() => {
- this.tick = 0;
- }, 1000);
- }
- clear() {
- clearInterval(this.timer);
- this.timer = null;
- }
- jump(p) {
- this.jump_num++;
- if (this.jump_num > 50 && this.pos_num == 0) {
- p.send('$alert', {
- t: '请勿多次连跳!'
- })
- p.kick();
- return;
- }
- if (this.y < this.old_y - 3) {
- this.kick(p);
- return;
- }
- }
- pos(p) {
- this.jump_num != 0 && this.pos_num++;
- if (this.jump_num == 1 && this.pos_num == 1) {
- this.jump_num--;
- this.pos_num--;
- }
- }
- pick(p) {
- this.throw_num++;
- if (this.throw_num > 3) {
- this.kick(p);
- }
- setTimeout(() => {
- this.throw_num = 0;
- }, 1000);
- }
- throw(p, t) {
- this.throw_num++;
- if (this.throw_num > 3) {
- this.kick(p);
- }
- setTimeout(() => {
- this.throw_num = 0;
- }, 500);
- if (t.x >= this.x - 120
- && t.x < this.x
- && t.y <= this.y + 90
- && t.y >= this.y - 90
- ) {
- return;
- } else {
- if (t.x <= this.x + 120
- && t.x > this.x
- && t.y <= this.y + 90
- && t.y >= this.y - 90) {
- return;
- } else {
- this.kick(p);
- }
- }
- }
- move(p) {
- this.move_num++;
- if (this.old_move_time == 0) {
- this.old_move_time = serverTime;
- return;
- } else if (this.old_move_time < serverTime) {
- let v = Math.abs(this.x - this.old_x) / ((serverTime - this.old_move_time) / 1000);
- if (v > 140) {
- if (this.jump_num == 0) {
- if (v > 500) this.kick(p);
- this.异常移动 = true;
- this.无效数据 = true;
- }
- } else {
- this.异常移动 = false;
- this.无效数据 = false;
- }
- }
- this.old_move_time = serverTime;
- }
- update(d, p, t = {}) {
- if (p.l_cheat) {
- serverTime = Date.now();
- this.无效数据 = false;
- if (!this) {
- this.kick(p);
- return;
- }
- if (this.old_x == this.random.x && this.old_y == this.random.y) {
- this.old_x = Number(d.v.x);
- this.old_y = Number(d.v.y);
- this.old_Animation = d.v.t;
- return;
- } else {
- this.x = Number(d.v.x);
- this.y = Number(d.v.y);
- }
- if (isNaN(this.old_x) || isNaN(this.x) || isNaN(this.old_y) || isNaN(this.y)) {
- this.kick(p);
- }
- if (this.异常移动) {
- channel.move({ k: 'move', v: { t: 'throw', uuid: p.uuid, dir: 0, target: 0, x: this.old_x, y: this.old_y } }, p);
- }
- switch (d.v.t) {
- case 'jump':
- this.jump(p)
- break;
- case 'pos':
- this.pos(p)
- break;
- case 'pick':
- this.pick(p)
- break;
- case 'throw':
- this.throw(p, t)
- break;
- default:
- if ((this.old_Animation == 'moveRight' || this.old_Animation == 'moveLeft') && d.v.t == 'stop') {
- this.move(p);
- }
- break;
- }
- !this.无效数据 && channel.move(d, p);
- d.v.t != 'jump' && this.tick++;
- if (this.tick > 50) {
- this.kick(p);
- return;
- }
- this.old_x = this.x;
- this.old_y = this.y;
- this.old_Animation = d.v.t;
- } else {
- channel.move(d, p);
- }
- }
- kick(p) {
- this.开挂次数++;
- this.无效数据 = true;
- if (this.开挂次数 >= this.检测阈值) {
- p.send('$alert', {
- t: '数据异常,有问题请找管理员'
- })
- //json.banplayer(p.name, { username: '反作弊' });
- //p.kick();
- //tools.log('反作弊' + ' 封禁了 ' + p.name);
- } else {
- p.send('$broad', {
- text: '反作弊提示,请文明游戏!'
- })
- }
- }
- }
- module.exports = anti;
复制代码 |
|