/* * MC Report System * Copyright (C) 2026 Sea Network Technology Studio * Author: CangLan * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published * by the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ const express = require('express'); const { query, getRow } = require('../db'); const { authenticate, requireRole } = require('../middleware/auth'); const router = express.Router(); function parseDuration(d) { if (!d) return 0; const m = String(d).match(/(\d+)/); return m ? parseInt(m[1]) : 0; } router.get('/', authenticate, async (req, res) => { const rows = await query('SELECT * FROM bans ORDER BY created_at DESC LIMIT 200'); res.json(rows); }); router.get('/active', authenticate, async (req, res) => { res.json(await query("SELECT * FROM bans WHERE status = 'active' ORDER BY created_at DESC")); }); router.get('/min-duration', authenticate, async (req, res) => { const { player_name, player_uid, type } = req.query; const prev = await getRow('SELECT * FROM bans WHERE (player_name = ? OR player_uid = ?) AND type = ? AND status = ? ORDER BY created_at DESC LIMIT 1', [player_name||'', player_uid||'', type||'ban', 'active']); res.json({ minDays: prev ? parseDuration(prev.duration) : 0 }); }); router.post('/', authenticate, requireRole('owner'), async (req, res) => { try { const { player_name, player_uid, reason, type, duration, ticket_id, expires_at, source } = req.body; if (!player_name) return res.status(400).json({ error: '玩家名为必填' }); if (player_name.length > 50) return res.status(400).json({ error: '玩家名过长' }); if (type && !['ban','mute','warn','other'].includes(type)) return res.status(400).json({ error: '无效的处罚类型' }); const durDays = parseDuration(duration); const prev = await getRow('SELECT * FROM bans WHERE (player_name = ? OR player_uid = ?) AND type = ? AND status = ? ORDER BY created_at DESC LIMIT 1', [player_name, player_uid||'', type||'ban', 'active']); if (prev && durDays > 0 && parseDuration(prev.duration) > durDays) { return res.status(400).json({ error: `该玩家已有${parseDuration(prev.duration)}天处罚记录,新处罚时长不可少于上次(需 ≥ ${parseDuration(prev.duration)}天)` }); } const r = await query('INSERT INTO bans(player_name, player_uid, source, reason, type, duration, ticket_id, created_by, expires_at) VALUES (?,?,?,?,?,?,?,?,?)', [player_name, player_uid||'', source||'', reason||'', type||'ban', duration||'', ticket_id||null, req.user.id, expires_at||null]); if (ticket_id) { await query("INSERT INTO responses(ticket_id, user_id, content, is_staff) VALUES (?,?,?,1)", [ticket_id, req.user.id, `【处罚记录】${type==='ban'?'封禁':type==='mute'?'禁言':type==='warn'?'警告':'其他'}: ${player_name}${duration?' 时长:'+duration:''}${reason?' 原因:'+reason:''}`]); } res.status(201).json({ id: r.insertId }); } catch (err) { console.error('[bans]', err); res.status(500).json({ error: '添加失败: ' + err.message }); } }); router.put('/:id', authenticate, requireRole('owner'), async (req, res) => { const fields = {}; if (req.body.status) { if (!['active','expired','appealed','lifted'].includes(req.body.status)) return res.status(400).json({ error: '无效的状态值' }); fields.status = req.body.status; } if (req.body.reason !== undefined) fields.reason = req.body.reason; if (req.body.duration !== undefined) fields.duration = req.body.duration; if (!Object.keys(fields).length) return res.status(400).json({ error: '无更新内容' }); const sets = Object.keys(fields).map(k => `${k} = ?`).join(', '); await query(`UPDATE bans SET ${sets} WHERE id = ?`, [...Object.values(fields), req.params.id]); res.json({ message: '更新成功' }); }); router.delete('/:id', authenticate, requireRole('owner'), async (req, res) => { const id = parseInt(req.params.id); if (!id) return res.status(400).json({ error: '无效的ID' }); await query('DELETE FROM bans WHERE id = ?', [id]); res.json({ message: '已删除' }); }); module.exports = router;