mirror of
https://github.com/SikongJueluo/cc-utils.git
synced 2025-11-29 12:57:50 +08:00
add tui framework but not finish, reconstruct accesscontrol
This commit is contained in:
@@ -1,20 +1,55 @@
|
||||
{
|
||||
"detectRange": 64,
|
||||
"detectRange": 256,
|
||||
"detectInterval": 1,
|
||||
"warnInterval": 7,
|
||||
"watchInterval": 10,
|
||||
"noticeTimes": 2,
|
||||
"isWarn": false,
|
||||
"adminGroupConfig": {
|
||||
"groupName": "Admin",
|
||||
"groupUsers": ["Selcon"],
|
||||
"isAllowed": true,
|
||||
"isNotice": true
|
||||
},
|
||||
"defaultToastConfig": {
|
||||
"usersGroups": [
|
||||
{
|
||||
"groupName": "user",
|
||||
"groupUsers": [],
|
||||
"isAllowed": true,
|
||||
"isNotice": true
|
||||
},
|
||||
{
|
||||
"groupName": "VIP",
|
||||
"groupUsers": [],
|
||||
"isAllowed": true,
|
||||
"isNotice": false
|
||||
},
|
||||
{
|
||||
"groupName": "enemies",
|
||||
"groupUsers": [],
|
||||
"isAllowed": false,
|
||||
"isNotice": false
|
||||
}
|
||||
],
|
||||
"welcomeToastConfig": {
|
||||
"title": {
|
||||
"text": "Welcome",
|
||||
"color": "green"
|
||||
},
|
||||
"msg": {
|
||||
"text": "Hello %groupName% %playerName%",
|
||||
"text": "Hello User %playerName%",
|
||||
"color": "green"
|
||||
},
|
||||
"prefix": "Taohuayuan",
|
||||
"brackets": "[]",
|
||||
"bracketColor": ""
|
||||
},
|
||||
"noticeToastConfig": {
|
||||
"title": {
|
||||
"text": "Welcome",
|
||||
"color": "green"
|
||||
},
|
||||
"msg": {
|
||||
"text": "Hello User %playerName%",
|
||||
"color": "green"
|
||||
},
|
||||
"prefix": "Taohuayuan",
|
||||
@@ -33,35 +68,5 @@
|
||||
"prefix": "Taohuayuan",
|
||||
"brackets": "[]",
|
||||
"bracketColor": ""
|
||||
},
|
||||
"usersGroups": [
|
||||
{
|
||||
"groupName": "user",
|
||||
"groupUsers": [],
|
||||
"isAllowed": true,
|
||||
"isNotice": true
|
||||
},
|
||||
{
|
||||
"groupName": "VIP",
|
||||
"groupUsers": [],
|
||||
"isAllowed": true,
|
||||
"isNotice": false
|
||||
},
|
||||
{
|
||||
"groupName": "enemy",
|
||||
"groupUsers": [],
|
||||
"isAllowed": false,
|
||||
"isNotice": false,
|
||||
"toastConfig": {
|
||||
"title": {
|
||||
"text": "Warn",
|
||||
"color": "red"
|
||||
},
|
||||
"msg": {
|
||||
"text": "Warn %playerName%",
|
||||
"color": "red"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -261,10 +261,10 @@ class SetCommand implements CLICommand {
|
||||
|
||||
switch (option) {
|
||||
case "warnInterval":
|
||||
context.config.warnInterval = value;
|
||||
context.config.watchInterval = value;
|
||||
return {
|
||||
success: true,
|
||||
message: `Set warn interval to ${context.config.warnInterval}`,
|
||||
message: `Set warn interval to ${context.config.watchInterval}`,
|
||||
shouldSaveConfig: true,
|
||||
};
|
||||
|
||||
@@ -549,20 +549,6 @@ class ShowConfigCommand implements CLICommand {
|
||||
groupsMessage += ` Users: [${(group.groupUsers ?? []).join(", ")}]\n`;
|
||||
groupsMessage += ` Allowed: ${group.isAllowed}\n`;
|
||||
groupsMessage += ` Notice: ${group.isNotice}\n`;
|
||||
if (group.toastConfig !== undefined) {
|
||||
groupsMessage += ` Custom Toast Config:\n`;
|
||||
groupsMessage += ` Title: ${group.toastConfig.title.text}\n`;
|
||||
groupsMessage += ` Message: ${group.toastConfig.msg.text}\n`;
|
||||
if (group.toastConfig.prefix !== undefined) {
|
||||
groupsMessage += ` Prefix: ${group.toastConfig.prefix}\n`;
|
||||
}
|
||||
if (group.toastConfig.brackets !== undefined) {
|
||||
groupsMessage += ` Brackets: ${group.toastConfig.brackets}\n`;
|
||||
}
|
||||
if (group.toastConfig.bracketColor !== undefined) {
|
||||
groupsMessage += ` Bracket Color: ${group.toastConfig.bracketColor}\n`;
|
||||
}
|
||||
}
|
||||
groupsMessage += "\n";
|
||||
}
|
||||
|
||||
@@ -574,11 +560,11 @@ class ShowConfigCommand implements CLICommand {
|
||||
|
||||
case "toast": {
|
||||
let toastMessage = "Default Toast Config:\n";
|
||||
toastMessage += ` Title: ${context.config.defaultToastConfig.title.text}\n`;
|
||||
toastMessage += ` Message: ${context.config.defaultToastConfig.msg.text}\n`;
|
||||
toastMessage += ` Prefix: ${context.config.defaultToastConfig.prefix ?? "none"}\n`;
|
||||
toastMessage += ` Brackets: ${context.config.defaultToastConfig.brackets ?? "none"}\n`;
|
||||
toastMessage += ` Bracket Color: ${context.config.defaultToastConfig.bracketColor ?? "none"}\n\n`;
|
||||
toastMessage += ` Title: ${context.config.welcomeToastConfig.title.text}\n`;
|
||||
toastMessage += ` Message: ${context.config.welcomeToastConfig.msg.text}\n`;
|
||||
toastMessage += ` Prefix: ${context.config.welcomeToastConfig.prefix ?? "none"}\n`;
|
||||
toastMessage += ` Brackets: ${context.config.welcomeToastConfig.brackets ?? "none"}\n`;
|
||||
toastMessage += ` Bracket Color: ${context.config.welcomeToastConfig.bracketColor ?? "none"}\n\n`;
|
||||
|
||||
toastMessage += "Warn Toast Config:\n";
|
||||
toastMessage += ` Title: ${context.config.warnToastConfig.title.text}\n`;
|
||||
@@ -596,7 +582,7 @@ class ShowConfigCommand implements CLICommand {
|
||||
case "all": {
|
||||
let allMessage = `Detect Range: ${context.config.detectRange}\n`;
|
||||
allMessage += `Detect Interval: ${context.config.detectInterval}\n`;
|
||||
allMessage += `Warn Interval: ${context.config.warnInterval}\n\n`;
|
||||
allMessage += `Warn Interval: ${context.config.watchInterval}\n\n`;
|
||||
allMessage +=
|
||||
"Use 'showconfig groups' or 'showconfig toast' for detailed view";
|
||||
|
||||
|
||||
@@ -16,28 +16,32 @@ interface UserGroupConfig {
|
||||
isAllowed: boolean;
|
||||
isNotice: boolean;
|
||||
groupUsers: string[];
|
||||
toastConfig?: ToastConfig;
|
||||
}
|
||||
|
||||
interface AccessConfig {
|
||||
detectInterval: number;
|
||||
warnInterval: number;
|
||||
watchInterval: number;
|
||||
noticeTimes: number;
|
||||
detectRange: number;
|
||||
isWarn: boolean;
|
||||
adminGroupConfig: UserGroupConfig;
|
||||
defaultToastConfig: ToastConfig;
|
||||
welcomeToastConfig: ToastConfig;
|
||||
warnToastConfig: ToastConfig;
|
||||
noticeToastConfig: ToastConfig;
|
||||
usersGroups: UserGroupConfig[];
|
||||
}
|
||||
|
||||
const defaultConfig: AccessConfig = {
|
||||
detectRange: 64,
|
||||
detectInterval: 3,
|
||||
warnInterval: 7,
|
||||
detectRange: 256,
|
||||
detectInterval: 1,
|
||||
watchInterval: 10,
|
||||
noticeTimes: 2,
|
||||
isWarn: false,
|
||||
adminGroupConfig: {
|
||||
groupName: "Admin",
|
||||
groupUsers: ["Selcon"],
|
||||
isAllowed: true,
|
||||
isNotice: false,
|
||||
isNotice: true,
|
||||
},
|
||||
usersGroups: [
|
||||
{
|
||||
@@ -57,19 +61,22 @@ const defaultConfig: AccessConfig = {
|
||||
groupUsers: [],
|
||||
isAllowed: false,
|
||||
isNotice: false,
|
||||
toastConfig: {
|
||||
title: {
|
||||
text: "Warn",
|
||||
color: "red",
|
||||
},
|
||||
msg: {
|
||||
text: "Warn %playerName%",
|
||||
color: "red",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
defaultToastConfig: {
|
||||
welcomeToastConfig: {
|
||||
title: {
|
||||
text: "Welcome",
|
||||
color: "green",
|
||||
},
|
||||
msg: {
|
||||
text: "Hello User %playerName%",
|
||||
color: "green",
|
||||
},
|
||||
prefix: "Taohuayuan",
|
||||
brackets: "[]",
|
||||
bracketColor: "",
|
||||
},
|
||||
noticeToastConfig: {
|
||||
title: {
|
||||
text: "Welcome",
|
||||
color: "green",
|
||||
|
||||
@@ -16,12 +16,12 @@ const config = loadConfig(configFilepath);
|
||||
log.info("Load config successfully!");
|
||||
if (DEBUG) log.debug(textutils.serialise(config, { allow_repetitions: true }));
|
||||
const groupNames = config.usersGroups.map((value) => value.groupName);
|
||||
let warnTargetPlayers: string[];
|
||||
let noticeTargetPlayers: string[];
|
||||
const playerDetector = peripheralManager.findByNameRequired("playerDetector");
|
||||
const chatBox = peripheralManager.findByNameRequired("chatBox");
|
||||
|
||||
let inRangePlayers: string[] = [];
|
||||
let notAllowedPlayers: string[] = [];
|
||||
let watchPlayersInfo: { name: string; hasNoticeTimes: number }[] = [];
|
||||
|
||||
function safeParseTextComponent(
|
||||
component: MinecraftTextComponent,
|
||||
@@ -38,43 +38,50 @@ function safeParseTextComponent(
|
||||
return textutils.serialiseJSON(component);
|
||||
}
|
||||
|
||||
function sendToast(
|
||||
toastConfig: ToastConfig,
|
||||
player: string,
|
||||
groupConfig?: UserGroupConfig,
|
||||
) {
|
||||
function sendToast(toastConfig: ToastConfig, targetPlayer: string) {
|
||||
return chatBox.sendFormattedToastToPlayer(
|
||||
safeParseTextComponent(
|
||||
toastConfig.msg ?? config.defaultToastConfig.msg,
|
||||
player,
|
||||
groupConfig?.groupName,
|
||||
textutils.serialiseJSON(toastConfig.msg ?? config.welcomeToastConfig.msg),
|
||||
textutils.serialiseJSON(
|
||||
toastConfig.title ?? config.welcomeToastConfig.title,
|
||||
),
|
||||
safeParseTextComponent(
|
||||
toastConfig.title ?? config.defaultToastConfig.title,
|
||||
player,
|
||||
groupConfig?.groupName,
|
||||
),
|
||||
player,
|
||||
toastConfig.prefix ?? config.defaultToastConfig.prefix,
|
||||
toastConfig.brackets ?? config.defaultToastConfig.brackets,
|
||||
toastConfig.bracketColor ?? config.defaultToastConfig.bracketColor,
|
||||
targetPlayer,
|
||||
toastConfig.prefix ?? config.welcomeToastConfig.prefix,
|
||||
toastConfig.brackets ?? config.welcomeToastConfig.brackets,
|
||||
toastConfig.bracketColor ?? config.welcomeToastConfig.bracketColor,
|
||||
undefined,
|
||||
true,
|
||||
);
|
||||
}
|
||||
|
||||
function sendWarnAndNotice(player: string) {
|
||||
const playerPos = playerDetector.getPlayerPos(player);
|
||||
function sendNotice(player: string, playerInfo?: PlayerInfo) {
|
||||
const onlinePlayers = playerDetector.getOnlinePlayers();
|
||||
warnTargetPlayers = config.adminGroupConfig.groupUsers.concat(
|
||||
noticeTargetPlayers = config.adminGroupConfig.groupUsers.concat(
|
||||
config.usersGroups
|
||||
.filter((value) => value.isNotice)
|
||||
.map((value) => value.groupUsers ?? [])
|
||||
.flat(),
|
||||
);
|
||||
|
||||
const warnMsg = `Not Allowed Player ${player} Break in Home at Position ${playerPos?.x}, ${playerPos?.y}, ${playerPos?.z}`;
|
||||
const toastConfig: ToastConfig = {
|
||||
title: {
|
||||
text: "Notice",
|
||||
color: "red",
|
||||
},
|
||||
msg: {
|
||||
text: `Unfamiliar Player ${player} appeared at\n Position ${playerInfo?.x}, ${playerInfo?.y}, ${playerInfo?.z}`,
|
||||
color: "red",
|
||||
},
|
||||
};
|
||||
for (const targetPlayer of noticeTargetPlayers) {
|
||||
if (!onlinePlayers.includes(targetPlayer)) continue;
|
||||
sendToast(toastConfig, targetPlayer);
|
||||
}
|
||||
}
|
||||
|
||||
function sendWarn(player: string) {
|
||||
const warnMsg = `Not Allowed Player ${player} Break in Home `;
|
||||
log.warn(warnMsg);
|
||||
|
||||
sendToast(config.warnToastConfig, player);
|
||||
chatBox.sendFormattedMessageToPlayer(
|
||||
safeParseTextComponent(config.warnToastConfig.msg, player),
|
||||
@@ -85,37 +92,36 @@ function sendWarnAndNotice(player: string) {
|
||||
undefined,
|
||||
true,
|
||||
);
|
||||
|
||||
for (const targetPlayer of warnTargetPlayers) {
|
||||
if (!onlinePlayers.includes(targetPlayer)) continue;
|
||||
chatBox.sendFormattedMessageToPlayer(
|
||||
textutils.serialise({
|
||||
text: warnMsg,
|
||||
color: "red",
|
||||
} as MinecraftTextComponent),
|
||||
targetPlayer,
|
||||
"AccessControl",
|
||||
"[]",
|
||||
undefined,
|
||||
undefined,
|
||||
true,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function warnLoop() {
|
||||
function watchLoop() {
|
||||
while (true) {
|
||||
for (const player of notAllowedPlayers) {
|
||||
if (inRangePlayers.includes(player)) {
|
||||
// sendWarnAndNotice(player);
|
||||
for (const player of watchPlayersInfo) {
|
||||
if (inRangePlayers.includes(player.name)) {
|
||||
const playerInfo = playerDetector.getPlayerPos(player.name);
|
||||
|
||||
// Notice
|
||||
if (player.hasNoticeTimes < config.noticeTimes) {
|
||||
sendNotice(player.name, playerInfo);
|
||||
player.hasNoticeTimes += 1;
|
||||
}
|
||||
|
||||
// Warn
|
||||
if (config.isWarn) sendWarn(player.name);
|
||||
|
||||
// Record
|
||||
log.warn(
|
||||
`${player.name} appear at ${playerInfo?.x}, ${playerInfo?.y}, ${playerInfo?.z}`,
|
||||
);
|
||||
} else {
|
||||
notAllowedPlayers = notAllowedPlayers.filter(
|
||||
(value) => value != player,
|
||||
// Get rid of player from list
|
||||
watchPlayersInfo = watchPlayersInfo.filter(
|
||||
(value) => value.name != player.name,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
os.sleep(config.warnInterval);
|
||||
os.sleep(config.watchInterval);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,39 +137,36 @@ function mainLoop() {
|
||||
if (inRangePlayers.includes(player)) continue;
|
||||
|
||||
if (config.adminGroupConfig.groupUsers.includes(player)) {
|
||||
log.info(`Admin ${player} enter`);
|
||||
sendToast(
|
||||
config.adminGroupConfig.toastConfig ?? config.defaultToastConfig,
|
||||
player,
|
||||
config.adminGroupConfig,
|
||||
);
|
||||
log.info(`Admin ${player} appear`);
|
||||
continue;
|
||||
}
|
||||
|
||||
let inUserGroup = false;
|
||||
// New player appear
|
||||
const playerInfo = playerDetector.getPlayerPos(player);
|
||||
let groupConfig: UserGroupConfig = {
|
||||
groupName: "Unfamiliar",
|
||||
groupUsers: [],
|
||||
isAllowed: false,
|
||||
isNotice: false,
|
||||
};
|
||||
for (const userGroupConfig of config.usersGroups) {
|
||||
if (userGroupConfig.groupUsers == undefined) continue;
|
||||
if (!userGroupConfig.groupUsers.includes(player)) continue;
|
||||
|
||||
if (!userGroupConfig.isAllowed) {
|
||||
sendWarnAndNotice(player);
|
||||
notAllowedPlayers.push(player);
|
||||
continue;
|
||||
}
|
||||
|
||||
log.info(`${userGroupConfig.groupName} ${player} enter`);
|
||||
sendToast(
|
||||
userGroupConfig.toastConfig ?? config.defaultToastConfig,
|
||||
player,
|
||||
userGroupConfig,
|
||||
groupConfig = userGroupConfig;
|
||||
log.info(
|
||||
`${groupConfig.groupName} ${player} appear at ${playerInfo?.x}, ${playerInfo?.y}, ${playerInfo?.z}`,
|
||||
);
|
||||
|
||||
inUserGroup = true;
|
||||
break;
|
||||
}
|
||||
if (inUserGroup) continue;
|
||||
if (groupConfig.isAllowed) continue;
|
||||
|
||||
sendWarnAndNotice(player);
|
||||
notAllowedPlayers.push(player);
|
||||
log.warn(
|
||||
`${groupConfig.groupName} ${player} appear at ${playerInfo?.x}, ${playerInfo?.y}, ${playerInfo?.z}`,
|
||||
);
|
||||
if (config.isWarn) sendWarn(player);
|
||||
watchPlayersInfo.push({ name: player, hasNoticeTimes: 0 });
|
||||
}
|
||||
|
||||
inRangePlayers = players;
|
||||
@@ -192,7 +195,7 @@ function main(args: string[]) {
|
||||
void cli.startConfigLoop();
|
||||
},
|
||||
() => {
|
||||
warnLoop();
|
||||
watchLoop();
|
||||
},
|
||||
);
|
||||
return;
|
||||
|
||||
0
src/accesscontrol/tui.ts
Normal file
0
src/accesscontrol/tui.ts
Normal file
Reference in New Issue
Block a user