Banrunner (#212)

* 逃跑者给予封禁20分钟的处罚
* 屏蔽其他玩家发言
* 修一些或许是bug的代码
This commit is contained in:
notify 2023-06-29 20:42:07 +08:00 committed by GitHub
parent 3522ea81b6
commit fde7d41e19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 107 additions and 37 deletions

View File

@ -20,6 +20,31 @@ Flickable {
width: parent.width - 40
x: 20
// TODO: player details
Text {
id: screenName
font.pixelSize: 18
}
Text {
id: playerGameData
Layout.fillWidth: true
font.pixelSize: 18
}
TextEdit {
id: skillDesc
Layout.fillWidth: true
font.pixelSize: 18
readOnly: true
selectByKeyboard: true
selectByMouse: false
wrapMode: TextEdit.WordWrap
textFormat: TextEdit.RichText
}
RowLayout {
Button {
text: Backend.translate("Give Flower")
@ -62,7 +87,22 @@ Flickable {
root.finish();
}
}
}
RowLayout {
Button {
text: config.blockedUsers.indexOf(screenName.text) === -1 ? Backend.translate("Block Chatter") : Backend.translate("Unblock Chatter")
enabled: pid !== Self.id && pid > 0
onClicked: {
const idx = config.blockedUsers.indexOf(screenName.text);
if (idx === -1) {
config.blockedUsers.push(screenName.text);
} else {
config.blockedUsers.splice(idx, 1);
}
config.blockedUsersChanged();
}
}
Button {
text: Backend.translate("Kick From Room")
visible: !roomScene.isStarted && roomScene.isOwner
@ -73,34 +113,6 @@ Flickable {
}
}
}
// TODO: player details
RowLayout {
spacing: 16
Text {
id: screenName
font.pixelSize: 18
}
Text {
id: playerGameData
Layout.fillWidth: true
font.pixelSize: 18
}
}
TextEdit {
id: skillDesc
Layout.fillWidth: true
font.pixelSize: 18
readOnly: true
selectByKeyboard: true
selectByMouse: false
wrapMode: TextEdit.WordWrap
textFormat: TextEdit.RichText
}
}
function givePresent(p) {
@ -132,7 +144,8 @@ Flickable {
const run = gamedata[2];
const winRate = (win / total) * 100;
const runRate = (run / total) * 100;
playerGameData.text = total === 0 ? Backend.translate("Newbie") : "<br/>" + Backend.translate("Win=%1 Run=%2 Total=%3").arg(winRate.toFixed(2))
playerGameData.text = total === 0 ? Backend.translate("Newbie") :
Backend.translate("Win=%1 Run=%2 Total=%3").arg(winRate.toFixed(2))
.arg(runRate.toFixed(2)).arg(total);
}

View File

@ -40,6 +40,7 @@ QtObject {
property int roomTimeout: 0
property bool enableFreeAssign: false
property bool observing: false
property var blockedUsers: []
function loadConf() {
conf = JSON.parse(Backend.loadConf());

View File

@ -142,6 +142,10 @@ callbacks["Chat"] = (jsonData) => {
const time = data.time;
const msg = data.msg;
if (config.blockedUsers.indexOf(userName) !== -1) {
return;
}
if (general === "")
current.addToChat(pid, data, `[${time}] ${userName}: ${msg}`);
else

View File

@ -150,6 +150,7 @@ Rectangle {
PropertyAction { target: splash; property: "loading"; value: false }
}
/**
Text {
text: "常用联机IP175.178.66.93\n新月杀联机交流群531553435"
font.pixelSize: 20
@ -159,6 +160,7 @@ Rectangle {
anchors.rightMargin: 20
horizontalAlignment: Text.AlignRight
}
**/
//--------------------Disappear--------------
Behavior on opacity {

View File

@ -251,6 +251,10 @@
<source>you have been banned!</source>
<translation></translation>
</message>
<message>
<source>you have been temporarily banned!</source>
<translation></translation>
</message>
</context>
<context>

View File

@ -54,6 +54,8 @@ Fk:loadTranslationTable{
["Give Egg"] = "砸蛋",
["Give Wine"] = "酒杯",
["Give Shoe"] = "拖鞋",
["Block Chatter"] = "屏蔽发言",
["Unblock Chatter"] = "解除屏蔽",
["Kick From Room"] = "踢出房间",
["Newbie"] = "新手保护ing",
["Win=%1 Run=%2 Total=%3"] = "胜率%1% 逃率%2% 总场次%3",

View File

@ -315,7 +315,10 @@ void Router::handlePacket(const QByteArray &rawPacket) {
ServerPlayer *player = qobject_cast<ServerPlayer *>(parent());
player->setThinking(false);
// qDebug() << "wake up!";
player->getRoom()->getThread()->wakeUp();
auto room = player->getRoom();
if (room->getThread()) {
room->getThread()->wakeUp();
}
if (requestId != this->expectedReplyId)
return;

View File

@ -5,6 +5,7 @@
#include <qjsonarray.h>
#include <qjsondocument.h>
#include "client_socket.h"
#include "roomthread.h"
#include "server.h"
#include "serverplayer.h"
@ -265,6 +266,11 @@ void Room::removePlayer(ServerPlayer *player) {
// 原先的跑路机器人会在游戏结束后自动销毁掉
server->addPlayer(runner);
// 如果走小道的人不是单机启动玩家 那么直接ban
if (!runner->getSocket()->peerAddress().contains("127.0.0.1")) {
server->temporarilyBan(runner->getId());
}
m_thread->wakeUp();
// 发出信号,让大厅添加这个人

View File

@ -86,7 +86,7 @@ bool RoomThread::hasRequest() {
void RoomThread::trySleep(int ms) {
if (sema_wake.available() > 0) {
sema_wake.acquire(sema_wake.available());
sema_wake.tryAcquire(sema_wake.available(), ms);
return;
}

View File

@ -210,13 +210,22 @@ void Server::processNewConnection(ClientSocket *client) {
qInfo() << addr << "connected";
auto result = SelectFromDatabase(
db, QString("SELECT * FROM banip WHERE ip='%1';").arg(addr));
auto errmsg = QString();
if (!result.isEmpty()) {
errmsg = "you have been banned!";
} else if (temp_banlist.contains(addr)) {
errmsg = "you have been temporarily banned!";
}
if (!errmsg.isEmpty()) {
QJsonArray body;
body << -2;
body << (Router::TYPE_NOTIFICATION | Router::SRC_SERVER |
Router::DEST_CLIENT);
body << "ErrorMsg";
body << "you have been banned!";
body << errmsg;
client->send(JsonArray2Bytes(body));
qInfo() << "Refused banned IP:" << addr;
client->disconnectFromHost();
@ -552,15 +561,21 @@ RSA *Server::initServerRSA() {
return rsa;
}
#define SET_DEFAULT_CONFIG(k, v) do {\
if (config.value(k).isUndefined()) { \
config[k] = (v); \
} } while (0)
void Server::readConfig() {
QFile file("freekill.server.config.json");
if (!file.open(QIODevice::ReadOnly)) {
return;
QByteArray json = "{}";
if (file.open(QIODevice::ReadOnly)) {
json = file.readAll();
}
auto json = file.readAll();
config = QJsonDocument::fromJson(json).object();
// defaults
SET_DEFAULT_CONFIG("tempBanTime", 20);
}
QJsonValue Server::getConfig(const QString &key) { return config.value(key); }
@ -578,3 +593,20 @@ bool Server::checkBanWord(const QString &str) {
}
return true;
}
void Server::temporarilyBan(int playerId) {
auto player = findPlayer(playerId);
if (!player) return;
auto socket = player->getSocket();
if (!socket) return;
auto addr = socket->peerAddress();
temp_banlist.append(addr);
auto time = getConfig("tempBanTime").toInt();
QTimer::singleShot(time * 60000, this, [=]() {
temp_banlist.removeOne(addr);
});
emit player->kicked();
}

View File

@ -44,6 +44,8 @@ public:
QJsonValue getConfig(const QString &command);
bool checkBanWord(const QString &str);
void temporarilyBan(int playerId);
signals:
void roomCreated(Room *room);
void playerAdded(ServerPlayer *player);
@ -67,6 +69,7 @@ private:
int nextRoomId;
friend Room::Room(RoomThread *m_thread);
QHash<int, ServerPlayer *> players;
QList<QString> temp_banlist;
RSA *rsa;
QString public_key;

View File

@ -110,8 +110,8 @@ void QmlBackend::joinServer(QString address) {
void QmlBackend::quitLobby(bool close) {
if (ClientInstance)
delete ClientInstance;
if (ServerInstance && close)
ServerInstance->deleteLater();
// if (ServerInstance && close)
// ServerInstance->deleteLater();
}
void QmlBackend::emitNotifyUI(const QString &command, const QString &jsonData) {