Untitled
unknown
java
8 months ago
11 kB
5
Indexable
package com.nguyenduong.nsocomplete.server;
import java.awt.*;
import java.awt.event.*;
import java.io.InputStream;
import java.util.List;
import javax.swing.ImageIcon;
import com.nguyenduong.nsocomplete.clan.Clan;
import com.nguyenduong.nsocomplete.db.jdbc.DbManager;
import com.nguyenduong.nsocomplete.model.Char;
import com.nguyenduong.nsocomplete.stall.StallManager;
import com.nguyenduong.nsocomplete.util.Log;
import com.nguyenduong.nsocomplete.util.NinjaUtils;
public class NinjaSchool {
private Frame frame;
public static boolean isStop = false;
private static Thread mainThread;
private static volatile boolean isRunning = true;
private static boolean isHeadless;
public NinjaSchool() {
isHeadless = GraphicsEnvironment.isHeadless();
if (!isHeadless) {
initializeGUI();
} else {
initializeHeadless();
}
}
private void initializeGUI() {
try {
frame = new Frame("Quản lý");
final Font font = new Font("Segoe UI", Font.PLAIN, 12);
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("icon.png");
if (inputStream != null) {
byte[] data = new byte[inputStream.available()];
inputStream.read(data);
ImageIcon icon = new ImageIcon(data);
frame.setIconImage(icon.getImage());
}
frame.setSize(300, 500);
frame.setBackground(Color.BLACK);
frame.setResizable(false);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
shutdownServer();
}
});
// Buttons setup
Button[] buttons = createButtons(font);
for (Button btn : buttons) {
frame.add(btn);
}
frame.setLocation(32, 32);
frame.setLayout(null);
frame.setVisible(true);
} catch (Exception e) {
Log.error("Lỗi tạo trình Quản lý GUI!", e);
}
}
private Button[] createButtons(Font font) {
Button[] buttons = new Button[7];
String[][] buttonConfigs = {
{"Bảo trì", "maintain", "60"},
{"Lưu Shinwa", "saveShinwa", "110"},
{"Lưu dữ liệu gia tộc", "saveClan", "160"},
{"Lưu dữ liệu người chơi", "saveUser", "210"},
{"Làm mới bảng xếp hạng", "refreshTop", "260"},
{"Khởi động lại Database", "restartDB", "310"},
{"Gửi Đồ", "sendItem", "360"}
};
for (int i = 0; i < buttons.length; i++) {
Button btn = new Button(buttonConfigs[i][0]);
btn.setFont(font);
btn.setBounds(30, Integer.parseInt(buttonConfigs[i][2]), 140, 30);
btn.setActionCommand(buttonConfigs[i][1]);
btn.addActionListener(this::handleAction);
buttons[i] = btn;
}
return buttons;
}
private void initializeHeadless() {
mainThread = new Thread(() -> {
while (isRunning) {
try {
byte[] buffer = new byte[1024];
int read = System.in.read(buffer);
if (read > 0) {
String command = new String(buffer, 0, read).trim();
processCommand(command);
}
Thread.sleep(100);
} catch (Exception e) {
Log.error("Error reading command", e);
}
}
});
mainThread.setName("CommandListener");
mainThread.start();
}
private void processCommand(String command) {
switch (command.toLowerCase()) {
case "maintain":
case "bao tri":
maintainAction();
break;
case "save shinwa":
case "luu shinwa":
saveShinwaAction();
break;
case "save clan":
case "luu clan":
saveClanAction();
break;
case "save user":
case "luu user":
saveUserAction();
break;
case "refresh top":
case "refresh bxh":
refreshTopAction();
break;
case "restart db":
restartDBAction();
break;
case "stop":
case "exit":
shutdownServer();
break;
case "help":
showHelp();
break;
default:
Log.info("Unknown command. Type 'help' for available commands.");
break;
}
}
private void handleAction(ActionEvent e) {
switch (e.getActionCommand()) {
case "maintain":
maintainAction();
break;
case "saveShinwa":
saveShinwaAction();
break;
case "saveClan":
saveClanAction();
break;
case "saveUser":
saveUserAction();
break;
case "refreshTop":
refreshTopAction();
break;
case "restartDB":
restartDBAction();
break;
case "sendItem":
JFrameSendItem.run();
break;
}
}
public static void main(String[] args) {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
saveShinwaAction();
saveClanAction();
saveUserAction();
refreshTopAction();
if (Server.start) {
Log.info("Đóng máy chủ.");
Server.stop();
}
}));
if (!Config.getInstance().load()) {
Log.error("Vui long kiem tra lai cau hinh!");
return;
}
if (!DbManager.getInstance().start()) {
return;
}
if (NinjaUtils.availablePort(Config.getInstance().getPort())) {
if (!Server.init()) {
Log.error("Khởi tạo thất bại!");
return;
}
new NinjaSchool();
Server.start();
} else {
Log.error("Port " + Config.getInstance().getPort() + " đã được sử dụng!");
}
}
public static void saveShinwaAction() {
if (Server.start) {
Log.info("Lưu Shinwa");
StallManager.getInstance().save();
Log.info("Lưu xong");
} else {
Log.info("Máy chủ chưa bật");
}
}
public static void maintainAction() {
if (Server.start) {
if (!isStop) {
new Thread(() -> {
try {
Server.maintance();
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
} else {
Log.info("Máy chủ chưa bật.");
}
}
public static void saveClanAction() {
Log.info("Lưu dữ liệu gia tộc.");
List<Clan> clans = Clan.getClanDAO().getAll();
synchronized (clans) {
for (Clan clan : clans) {
Clan.getClanDAO().update(clan);
}
}
Log.info("Lưu xong");
}
public static void refreshTopAction() {
List<Char> chars = ServerManager.getChars();
for (Char _char : chars) {
_char.saveData();
}
Log.info("Làm mới bảng xếp hạng");
Ranked.refresh();
}
public static void saveUserAction() {
Log.info("Lưu dữ liệu người chơi");
List<Char> chars = ServerManager.getChars();
for (Char _char : chars) {
try {
if (_char != null && !_char.isCleaned) {
_char.saveData();
if (_char.clone != null && !_char.clone.isCleaned) {
_char.clone.saveData();
}
if (_char.user != null && !_char.user.isCleaned) {
_char.user.saveData();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
Log.info("Lưu xong");
}
public static void restartDBAction() {
Log.info("Bắt đầu khởi động lại!");
DbManager.getInstance().shutdown();
DbManager.getInstance().start();
Log.info("Khởi động xong!");
}
private void shutdownServer() {
isRunning = false;
if (frame != null) {
frame.dispose();
}
if (Server.start) {
Log.info("Đóng máy chủ.");
Server.saveAll();
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Log.error("Error while stopping server", ex);
}
Server.stop();
}
System.exit(0);
}
private void showHelp() {
Log.info("╔════════════════════════════════════════════╗");
Log.info("║ DANH SÁCH LỆNH ║");
Log.info("╠════════════════════════════════════════════╣");
Log.info("║ maintain/bao tri : Bảo trì máy chủ ║");
Log.info("║ save shinwa/luu shinwa: Lưu dữ liệu Shinwa ║");
Log.info("║ save clan/luu clan : Lưu dữ liệu gia tộc ║");
Log.info("║ save user/luu user : Lưu dữ liệu người ║");
Log.info("║ refresh top/bxh : Làm mới bảng xếp hạng║");
Log.info("║ restart db : Khởi động lại DB ║");
Log.info("║ stop/exit : Dừng máy chủ ║");
Log.info("║ clear/cls : Xóa màn hình ║");
Log.info("║ help : Hiện danh sách lệnh ║");
Log.info("╚════════════════════════════════════════════╝");
}
}Editor is loading...
Leave a Comment