Commit 693361fd authored by nanahira's avatar nanahira

random duel ban

parent c8b15204
......@@ -2,6 +2,7 @@
jsconfig.json
coffeelint.json
.vscode/
.idea
password.json
config.*.json
......
......@@ -2,6 +2,7 @@
jsconfig.json
coffeelint.json
.vscode/
.idea
password.json
config.*.json
......
......@@ -9,6 +9,8 @@ const typeorm_1 = require("typeorm");
const CloudReplay_1 = require("./entities/CloudReplay");
const CloudReplayPlayer_1 = require("./entities/CloudReplayPlayer");
const Ban_1 = require("./entities/Ban");
const RandomDuelBan_1 = require("./entities/RandomDuelBan");
const underscore_1 = __importDefault(require("underscore"));
class DataManager {
constructor(config, log) {
this.config = config;
......@@ -125,6 +127,62 @@ class DataManager {
return null;
}
}
async getRandomDuelBan(ip) {
const repo = this.db.getRepository(RandomDuelBan_1.RandomDuelBan);
try {
const ban = await repo.findOne(ip);
//console.log(ip, ban);
return ban;
}
catch (e) {
this.log.warn(`Failed to fetch random duel ban ${ip}: ${e.toString()}`);
return null;
}
}
async updateRandomDuelBan(ban) {
const repo = this.db.getRepository(RandomDuelBan_1.RandomDuelBan);
try {
await repo.save(ban);
}
catch (e) {
this.log.warn(`Failed to update random duel ban ${ban.ip}: ${e.toString()}`);
}
}
async randomDuelBanPlayer(ip, reason, countadd) {
const count = countadd || 1;
const repo = this.db.getRepository(RandomDuelBan_1.RandomDuelBan);
try {
let ban = await repo.findOne(ip);
if (ban) {
ban.count += count;
const banTime = ban.count > 3 ? Math.pow(2, ban.count - 3) * 2 : 0;
const banDate = moment_1.default(ban.time);
if (moment_1.default().isAfter(banDate)) {
ban.time = moment_1.default().add(banTime, 'm').toDate();
}
else {
ban.time = moment_1.default(banDate).add(banTime, 'm').toDate();
}
if (!underscore_1.default.contains(ban.reasons, reason)) {
ban.reasons.push(reason);
}
ban.needTip = 1;
}
else {
ban = new RandomDuelBan_1.RandomDuelBan();
ban.ip = ip;
ban.time = moment_1.default().toDate();
ban.count = count;
ban.reasons = [reason];
ban.needTip = 1;
}
return await repo.save(ban);
}
catch (e) {
this.log.warn(`Failed to update random duel ban ${ip}: ${e.toString()}`);
return null;
}
}
}
exports.DataManager = DataManager;
//# sourceMappingURL=DataManager.js.map
\ No newline at end of file
......@@ -5,6 +5,8 @@ import { Connection, ConnectionOptions, createConnection, Transaction } from "ty
import { CloudReplay } from "./entities/CloudReplay";
import { CloudReplayPlayer } from "./entities/CloudReplayPlayer";
import { Ban } from "./entities/Ban";
import {RandomDuelBan} from "./entities/RandomDuelBan";
import _ from "underscore";
export interface CloudReplayPlayerInfo {
......@@ -134,4 +136,59 @@ export class DataManager {
return null;
}
}
async getRandomDuelBan(ip: string) {
const repo = this.db.getRepository(RandomDuelBan);
try {
const ban = await repo.findOne(ip);
//console.log(ip, ban);
return ban;
} catch (e) {
this.log.warn(`Failed to fetch random duel ban ${ip}: ${e.toString()}`);
return null;
}
}
async updateRandomDuelBan(ban: RandomDuelBan) {
const repo = this.db.getRepository(RandomDuelBan);
try {
await repo.save(ban);
} catch (e) {
this.log.warn(`Failed to update random duel ban ${ban.ip}: ${e.toString()}`);
}
}
async randomDuelBanPlayer(ip: string, reason: string, countadd?: number){
const count = countadd || 1;
const repo = this.db.getRepository(RandomDuelBan);
try {
let ban = await repo.findOne(ip);
if(ban) {
ban.count += count;
const banTime = ban.count > 3 ? Math.pow(2, ban.count - 3) * 2 : 0;
const banDate = moment(ban.time);
if(moment().isAfter(banDate)) {
ban.time = moment().add(banTime, 'm').toDate();
} else {
ban.time = moment(banDate).add(banTime, 'm').toDate();
}
if(!_.contains(ban.reasons, reason)) {
ban.reasons.push(reason);
}
ban.needTip = 1;
} else {
ban = new RandomDuelBan();
ban.ip = ip;
ban.time = moment().toDate();
ban.count = count;
ban.reasons = [reason];
ban.needTip = 1;
}
return await repo.save(ban);
} catch (e) {
this.log.warn(`Failed to update random duel ban ${ip}: ${e.toString()}`);
return null;
}
}
}
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RandomDuelBan = void 0;
const typeorm_1 = require("typeorm");
let RandomDuelBan = /** @class */ (() => {
let RandomDuelBan = class RandomDuelBan {
setNeedTip(need) {
this.needTip = need ? 1 : 0;
}
getNeedTip() {
return this.needTip > 0 ? true : false;
}
};
__decorate([
typeorm_1.PrimaryColumn({ type: "varchar", length: 64 }),
__metadata("design:type", String)
], RandomDuelBan.prototype, "ip", void 0);
__decorate([
typeorm_1.Column("datetime"),
__metadata("design:type", Date)
], RandomDuelBan.prototype, "time", void 0);
__decorate([
typeorm_1.Column("smallint"),
__metadata("design:type", Number)
], RandomDuelBan.prototype, "count", void 0);
__decorate([
typeorm_1.Column({ type: "simple-array" }),
__metadata("design:type", Array)
], RandomDuelBan.prototype, "reasons", void 0);
__decorate([
typeorm_1.Column({ type: "tinyint", unsigned: true }),
__metadata("design:type", Number)
], RandomDuelBan.prototype, "needTip", void 0);
RandomDuelBan = __decorate([
typeorm_1.Entity()
], RandomDuelBan);
return RandomDuelBan;
})();
exports.RandomDuelBan = RandomDuelBan;
//# sourceMappingURL=RandomDuelBan.js.map
\ No newline at end of file
import {Column, Entity, PrimaryColumn} from "typeorm";
@Entity()
export class RandomDuelBan {
@PrimaryColumn({type: "varchar", length: 64})
ip: string;
@Column("datetime")
time: Date;
@Column("smallint")
count: number;
@Column({type: "simple-array"})
reasons: string[]
@Column({type: "tinyint", unsigned: true})
needTip: number;
setNeedTip(need: boolean) {
this.needTip = need ? 1 : 0;
}
getNeedTip() {
return this.needTip > 0 ? true : false;
}
}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment