123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- /*
- * This program is free software: you can redistribute it and/or modify it under
- * the terms of the GNU 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 General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package com.l2jserver.tools.gsregistering;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.LineNumberReader;
- import java.sql.SQLException;
- import java.util.Map.Entry;
- import java.util.ResourceBundle;
- import com.l2jserver.loginserver.GameServerTable;
- public class GameServerRegister extends BaseGameServerRegister
- {
- private LineNumberReader _in;
-
- public static void main(String[] args)
- {
- // Backwards compatibility, redirect to the new one
- BaseGameServerRegister.main(args);
- }
-
- /**
- *
- * @param bundle
- */
- public GameServerRegister(ResourceBundle bundle)
- {
- super(bundle);
- this.load();
-
- int size = GameServerTable.getInstance().getServerNames().size();
- if (size == 0)
- {
- System.out.println(this.getBundle().getString("noServerNames"));
- System.exit(1);
- }
- }
-
- public void consoleUI() throws IOException
- {
- _in = new LineNumberReader(new InputStreamReader(System.in));
- boolean choiceOk = false;
- String choice;
-
- while (true)
- {
- this.hr();
- System.out.println("GSRegister");
- System.out.println('\n');
- System.out.println("1 - "+this.getBundle().getString("cmdMenuRegister"));
- System.out.println("2 - "+this.getBundle().getString("cmdMenuListNames"));
- System.out.println("3 - "+this.getBundle().getString("cmdMenuRemoveGS"));
- System.out.println("4 - "+this.getBundle().getString("cmdMenuRemoveAll"));
- System.out.println("5 - "+this.getBundle().getString("cmdMenuExit"));
-
- do
- {
- System.out.print(this.getBundle().getString("yourChoice")+' ');
- choice = _in.readLine();
- try
- {
- int choiceNumber = Integer.parseInt(choice);
- choiceOk = true;
-
- switch (choiceNumber)
- {
- case 1:
- this.registerNewGS();
- break;
- case 2:
- this.listGSNames();
- break;
- case 3:
- this.unregisterSingleGS();
- break;
- case 4:
- this.unregisterAllGS();
- break;
- case 5:
- System.exit(0);
- break;
- default:
- System.out.printf(this.getBundle().getString("invalidChoice")+'\n', choice);
- choiceOk = false;
- }
-
- }
- catch (NumberFormatException nfe)
- {
- System.out.printf(this.getBundle().getString("invalidChoice")+'\n', choice);
- }
- }
- while (!choiceOk);
- }
- }
-
- /**
- *
- */
- private void hr()
- {
- System.out.println("_____________________________________________________\n");
- }
-
- /**
- *
- */
- private void listGSNames()
- {
- int idMaxLen = 0;
- int nameMaxLen = 0;
- for (Entry<Integer, String> e : GameServerTable.getInstance().getServerNames().entrySet())
- {
- if (e.getKey().toString().length() > idMaxLen)
- {
- idMaxLen = e.getKey().toString().length();
- }
- if (e.getValue().length() > nameMaxLen)
- {
- nameMaxLen = e.getValue().length();
- }
- }
- idMaxLen += 2;
- nameMaxLen += 2;
-
- String id;
- boolean inUse;
- String gsInUse = this.getBundle().getString("gsInUse");
- String gsFree = this.getBundle().getString("gsFree");
- int gsStatusMaxLen = Math.max(gsInUse.length(), gsFree.length()) + 2;
- for (Entry<Integer, String> e : GameServerTable.getInstance().getServerNames().entrySet())
- {
- id = e.getKey().toString();
- System.out.print(id);
-
- for (int i = id.length(); i < idMaxLen; i++)
- {
- System.out.print(' ');
- }
- System.out.print("| ");
-
- System.out.print(e.getValue());
-
- for (int i = e.getValue().length(); i < nameMaxLen; i++)
- {
- System.out.print(' ');
- }
- System.out.print("| ");
-
- inUse = GameServerTable.getInstance().hasRegisteredGameServerOnId(e.getKey());
- String inUseStr = (inUse ? gsInUse : gsFree);
- System.out.print(inUseStr);
-
- for (int i = inUseStr.length(); i < gsStatusMaxLen; i++)
- {
- System.out.print(' ');
- }
- System.out.println('|');
- }
- }
-
- /**
- * @throws IOException
- *
- */
- private void unregisterAllGS() throws IOException
- {
- if (this.yesNoQuestion(this.getBundle().getString("confirmRemoveAllText")))
- {
- try
- {
- BaseGameServerRegister.unregisterAllGameServers();
- System.out.println(this.getBundle().getString("unregisterAllOk"));
- }
- catch (SQLException e)
- {
- this.showError(this.getBundle().getString("sqlErrorUnregisterAll"), e);
- }
- }
- }
-
- private boolean yesNoQuestion(String question) throws IOException
- {
-
- do
- {
- this.hr();
- System.out.println(question);
- System.out.println("1 - "+this.getBundle().getString("yes"));
- System.out.println("2 - "+this.getBundle().getString("no"));
- System.out.print(this.getBundle().getString("yourChoice")+' ');
- String choice;
- choice = _in.readLine();
- if (choice != null)
- {
- if (choice.equals("1"))
- {
- return true;
- }
- else if (choice.equals("2"))
- {
- return false;
- }
- else
- {
- System.out.printf(this.getBundle().getString("invalidChoice")+'\n', choice);
- }
- }
- }
- while (true);
- }
-
- /**
- * @throws IOException
- *
- */
- private void unregisterSingleGS() throws IOException
- {
- String line;
- int id = Integer.MIN_VALUE;
-
- do
- {
- System.out.print(this.getBundle().getString("enterDesiredId")+' ');
- line = _in.readLine();
- try
- {
- id = Integer.parseInt(line);
- }
- catch (NumberFormatException e)
- {
- System.out.printf(this.getBundle().getString("invalidChoice")+'\n', line);
- }
- }
- while (id == Integer.MIN_VALUE);
-
- String name = GameServerTable.getInstance().getServerNameById(id);
- if (name == null)
- {
- System.out.printf(this.getBundle().getString("noNameForId")+'\n', id);
- }
- else
- {
- if (GameServerTable.getInstance().hasRegisteredGameServerOnId(id))
- {
- System.out.printf(this.getBundle().getString("confirmRemoveText")+'\n', id, name);
- try
- {
- BaseGameServerRegister.unregisterGameServer(id);
- System.out.printf(this.getBundle().getString("unregisterOk")+'\n', id);
- }
- catch (SQLException e)
- {
- this.showError(this.getBundle().getString("sqlErrorUnregister"), e);
- }
-
- }
- else
- {
- System.out.printf(this.getBundle().getString("noServerForId")+'\n', id);
- }
- }
-
-
- }
-
- private void registerNewGS() throws IOException
- {
- String line;
- int id = Integer.MIN_VALUE;
-
- do
- {
- System.out.println(this.getBundle().getString("enterDesiredId"));
- line = _in.readLine();
- try
- {
- id = Integer.parseInt(line);
- }
- catch (NumberFormatException e)
- {
- System.out.printf(this.getBundle().getString("invalidChoice")+'\n', line);
- }
- }
- while (id == Integer.MIN_VALUE);
-
-
- String name = GameServerTable.getInstance().getServerNameById(id);
- if (name == null)
- {
- System.out.printf(this.getBundle().getString("noNameForId")+'\n', id);
- }
- else
- {
- if (GameServerTable.getInstance().hasRegisteredGameServerOnId(id))
- {
- System.out.println(this.getBundle().getString("idIsNotFree"));
- }
- else
- {
- try
- {
- BaseGameServerRegister.registerGameServer(id, ".");
- }
- catch (IOException e)
- {
- this.showError(getBundle().getString("ioErrorRegister"), e);
- }
- }
- }
- }
-
- /**
- * @see com.l2jserver.tools.gsregistering.BaseGameServerRegister#showError(java.lang.String, java.lang.Throwable)
- */
- @Override
- public void showError(String msg, Throwable t)
- {
- String title;
- if (this.getBundle() != null)
- {
- title = this.getBundle().getString("error");
- msg += '\n'+this.getBundle().getString("reason")+' '+t.getLocalizedMessage();
- }
- else
- {
- title = "Error";
- msg += "\nCause: "+t.getLocalizedMessage();
- }
- System.out.println(title+": "+msg);
- }
- }
|