Code:
package org.pokenet.server;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.ini4j.Ini;
import org.ini4j.Ini.Section;
import org.ini4j.InvalidIniFormatException;
import org.pokenet.server.connections.ActiveConnections;
import org.pokenet.server.network.MySqlManager;
public class GameServer {
private static final int SERVER_REVISION = 1910;
private static boolean m_boolGui = false;
private static String m_dbServer;
private static String m_dbName;
private static String m_dbUsername;
private static String m_dbPassword;
private static String m_serverName;
private static GameServer m_instance;
private static int m_maxPlayers = 500;
private static ServiceManager m_serviceManager;
private static int m_port = 7002;
private JFrame m_gui;
private JLabel m_pAmount;
private JLabel m_pHighest;
private JButton m_start;
private JButton m_stop;
private JButton m_set;
private JButton m_exit;
private JPasswordField m_dbP;
private JTextField m_dbS;
private JTextField m_dbN;
private JTextField m_dbU;
private JTextField m_name;
private int m_highest;
public static double RATE_GOLD = 1.0D;
public static double RATE_EXP_POKE = 1.0D;
public static double RATE_EXP_TRAINER = 1.0D;
public static final int MOVEMENT_THREADS = 12;
public GameServer(boolean autorun) {
if (autorun) {
if (m_boolGui) {
createGui();
}
loadSettings();
} else if (m_boolGui) {
loadSettings();
createGui();
} else {
ConsoleReader r = new ConsoleReader();
System.out.println("Load Settings? Y/N");
String answer = r.readToken();
if ((answer.contains("y")) || (answer.contains("Y"))) {
loadSettings();
} else {
getConsoleSettings();
}
}
start();
}
public void updatePlayerCount() {
if (m_boolGui) {
int amount = ActiveConnections.getActiveConnections();
this.m_pAmount.setText(amount + " players online");
if (amount > this.m_highest) {
this.m_highest = amount;
this.m_pHighest.setText("Highest: " + amount);
}
} else {
int amount = ActiveConnections.getActiveConnections();
System.out.println(amount + " players online");
if (amount > this.m_highest) {
this.m_highest = amount;
System.out.println("Highest: " + amount);
}
}
}
public static String getDatabaseHost() {
return m_dbServer;
}
public static String getDatabaseName() {
return m_dbName;
}
public static String getDatabasePassword() {
return m_dbPassword;
}
public static String getDatabaseUsername() {
return m_dbUsername;
}
public static GameServer getInstance() {
return m_instance;
}
public static void initGameServer(boolean autorun) {
m_instance = new GameServer(autorun);
}
public static int getMaxPlayers() {
return m_maxPlayers;
}
public static int getPort() {
return m_port;
}
public static String getServerName() {
return m_serverName;
}
public static ServiceManager getServiceManager() {
return m_serviceManager;
}
public static void main(String[] args) {
try {
PrintStream p = new PrintStream(new File("./errors.txt"));
System.setErr(p);
} catch (Exception e) {
e.printStackTrace();
}
Options options = new Options();
options.addOption("p", "players", true, "Sets the max number of players.");
options.addOption("port", "port", true, "Sets the serverport.");
options.addOption("ng", "nogui", false, "Starts server in headless mode.");
options.addOption("ar", "autorun", false, "Runs without asking a single question.");
options.addOption("h", "help", false, "Shows this menu.");
options.addOption("rates", "serverrates", true, "Gives the file to be used for server rates config");
if (args.length > 0) {
CommandLineParser parser = new GnuParser();
try {
CommandLine line = parser.parse(options, args);
if (line.hasOption("players")) {
m_maxPlayers = Integer.parseInt(line.getOptionValue("players"));
if (m_maxPlayers < 1) {
m_maxPlayers = 500;
}
}
if (line.hasOption("port")) {
m_port = Integer.parseInt(line.getOptionValue("port"));
}
if (line.hasOption("help")) {
HelpFormatter formatter = new HelpFormatter();
System.err.println("Server requires a settings parameter");
formatter.printHelp("java GameServer [param] <args>", options);
}
if (!line.hasOption("nogui")) {
m_boolGui = true;
}
if (line.hasOption("rates")) {
String rates = line.getOptionValue("rates");
Ini ratesIni = new Ini(new FileInputStream(rates));
Ini.Section s = (Ini.Section) ratesIni.get("RATES");
RATE_GOLD = Double.parseDouble((String) s.get("GOLD"));
RATE_EXP_POKE = Double.parseDouble((String) s.get("EXP_POKE"));
RATE_EXP_TRAINER = Double.parseDouble((String) s.get("EXP_TRAINER"));
}
boolean autorun = line.hasOption("autorun");
initGameServer(autorun);
} catch (ParseException exp) {
System.err.println("Parsing failed. Reason: " + exp.getMessage());
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("java GameServer [param] <args>", options);
} catch (InvalidIniFormatException e) {
e.printStackTrace();
System.err.println("Error in server rates format, using default 1.0");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
HelpFormatter formatter = new HelpFormatter();
System.err.println("Server requires a settings parameter");
formatter.printHelp("java GameServer [param] <args>", options);
}
}
public void start() {
if (m_boolGui) {
m_dbServer = this.m_dbS.getText();
m_dbName = this.m_dbN.getText();
m_dbUsername = this.m_dbU.getText();
m_dbPassword = new String(this.m_dbP.getPassword());
m_serverName = this.m_name.getText();
this.m_start.setEnabled(false);
this.m_stop.setEnabled(true);
}
MySqlManager.getInstance();
m_serviceManager = new ServiceManager();
m_serviceManager.start();
}
public void stop() {
m_serviceManager.stop();
if (m_boolGui) {
this.m_start.setEnabled(true);
this.m_stop.setEnabled(false);
} else {
try {
Thread.sleep(10000L);
System.out.println("Exiting server...");
MySqlManager.getInstance().close();
System.exit(0);
} catch (InterruptedException localInterruptedException) {
}
}
}
private void createGui() {
this.m_gui = new JFrame();
this.m_gui.setTitle("Pokenet Server");
this.m_gui.setSize(148, 340);
this.m_gui.setDefaultCloseOperation(0);
this.m_gui.getContentPane().setLayout(null);
this.m_gui.setResizable(false);
this.m_gui.setLocation(32, 32);
this.m_pAmount = new JLabel("0 players online");
this.m_pAmount.setSize(160, 16);
this.m_pAmount.setLocation(4, 4);
this.m_pAmount.setVisible(true);
this.m_gui.getContentPane().add(this.m_pAmount);
this.m_pHighest = new JLabel("[No record]");
this.m_pHighest.setSize(160, 16);
this.m_pHighest.setLocation(4, 24);
this.m_pHighest.setVisible(true);
this.m_gui.getContentPane().add(this.m_pHighest);
this.m_start = new JButton("Start Server");
this.m_start.setSize(128, 24);
this.m_start.setLocation(4, 48);
this.m_start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
GameServer.this.start();
}
});
this.m_gui.getContentPane().add(this.m_start);
this.m_stop = new JButton("Stop Server");
this.m_stop.setSize(128, 24);
this.m_stop.setLocation(4, 74);
this.m_stop.setEnabled(false);
this.m_stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
GameServer.this.stop();
}
});
this.m_gui.getContentPane().add(this.m_stop);
this.m_set = new JButton("Save Settings");
this.m_set.setSize(128, 24);
this.m_set.setLocation(4, 100);
this.m_set.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
GameServer.this.saveSettings();
}
});
this.m_gui.getContentPane().add(this.m_set);
this.m_exit = new JButton("Quit");
this.m_exit.setSize(128, 24);
this.m_exit.setLocation(4, 290);
this.m_exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
GameServer.this.exit();
}
});
this.m_gui.getContentPane().add(this.m_exit);
this.m_dbS = new JTextField();
this.m_dbS.setSize(128, 24);
this.m_dbS.setText("MySQL Host");
this.m_dbS.setLocation(4, 128);
this.m_gui.getContentPane().add(this.m_dbS);
this.m_dbN = new JTextField();
this.m_dbN.setSize(128, 24);
this.m_dbN.setText("MySQL Database Name");
this.m_dbN.setLocation(4, 160);
this.m_gui.getContentPane().add(this.m_dbN);
this.m_dbU = new JTextField();
this.m_dbU.setSize(128, 24);
this.m_dbU.setText("MySQL Username");
this.m_dbU.setLocation(4, 192);
this.m_gui.getContentPane().add(this.m_dbU);
this.m_dbP = new JPasswordField();
this.m_dbP.setSize(128, 24);
this.m_dbP.setText("Pass");
this.m_dbP.setLocation(4, 224);
this.m_gui.getContentPane().add(this.m_dbP);
this.m_name = new JTextField();
this.m_name.setSize(128, 24);
this.m_name.setText("Your Server Name");
this.m_name.setLocation(4, 260);
this.m_gui.getContentPane().add(this.m_name);
File f = new File("res/settings.txt");
if (f.exists()) {
try {
Scanner s = new Scanner(f);
this.m_dbS.setText(s.nextLine());
this.m_dbN.setText(s.nextLine());
this.m_dbU.setText(s.nextLine());
this.m_dbP.setText(s.nextLine());
this.m_name.setText(s.nextLine());
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
this.m_gui.setVisible(true);
}
private void exit() {
if ((m_boolGui) && (this.m_stop.isEnabled())) {
JOptionPane.showMessageDialog(null, "You must stop the server before exiting.");
} else {
MySqlManager.getInstance().close();
System.exit(0);
}
}
private void getConsoleSettings() {
ConsoleReader r = new ConsoleReader();
System.out.println("Please enter the required information.");
System.out.println("Database Server: ");
m_dbServer = r.readToken();
System.out.println("Database Name:");
m_dbName = r.readToken();
System.out.println("Database Username:");
m_dbUsername = r.readToken();
System.out.println("Database Password:");
m_dbPassword = r.readToken();
System.out.println("This server's IP or hostname:");
m_serverName = r.readToken();
System.out.println("Save info? (Y/N)");
String answer = r.readToken();
if ((answer.contains("y")) || (answer.contains("Y"))) {
saveSettings();
}
System.out.println();
System.err.println("WARNING: When using -nogui, the server should only be shut down using a master client");
}
private void loadSettings() {
File settings = new File("res/settings.txt");
if (settings.exists()) {
try {
Scanner s = new Scanner(settings);
m_dbServer = s.nextLine();
m_dbName = s.nextLine();
m_dbUsername = s.nextLine();
m_dbPassword = s.nextLine();
m_serverName = s.nextLine();
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void saveSettings() {
try {
if (m_boolGui) {
m_dbServer = this.m_dbS.getText();
m_dbName = this.m_dbN.getText();
m_dbUsername = this.m_dbU.getText();
m_dbPassword = new String(this.m_dbP.getPassword());
m_serverName = this.m_name.getText();
}
File settings = new File("res/settings.txt");
if (settings.exists()) {
settings.delete();
}
PrintWriter settingsWriter = new PrintWriter(settings);
settingsWriter.println(m_dbServer);
settingsWriter.println(m_dbName);
settingsWriter.println(m_dbUsername);
settingsWriter.println(m_serverName);
settingsWriter.println(" ");
settingsWriter.flush();
settingsWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}