GameServerRegister.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright © 2004-2019 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.tools.gsregistering;
  20. import java.io.IOException;
  21. import java.io.InputStreamReader;
  22. import java.io.LineNumberReader;
  23. import java.sql.SQLException;
  24. import java.util.Map.Entry;
  25. import java.util.ResourceBundle;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.loginserver.GameServerTable;
  28. public class GameServerRegister extends BaseGameServerRegister
  29. {
  30. private LineNumberReader _in;
  31. public static void main(String[] args)
  32. {
  33. // Backwards compatibility, redirect to the new one
  34. BaseGameServerRegister.main(args);
  35. }
  36. /**
  37. * @param bundle
  38. */
  39. public GameServerRegister(ResourceBundle bundle)
  40. {
  41. super(bundle);
  42. load();
  43. int size = GameServerTable.getInstance().getServerNames().size();
  44. if (size == 0)
  45. {
  46. System.out.println(getBundle().getString("noServerNames"));
  47. System.exit(1);
  48. }
  49. }
  50. public void consoleUI() throws IOException
  51. {
  52. _in = new LineNumberReader(new InputStreamReader(System.in));
  53. boolean choiceOk = false;
  54. String choice;
  55. while (true)
  56. {
  57. hr();
  58. System.out.println("GSRegister");
  59. System.out.println(Config.EOL);
  60. System.out.println("1 - " + getBundle().getString("cmdMenuRegister"));
  61. System.out.println("2 - " + getBundle().getString("cmdMenuListNames"));
  62. System.out.println("3 - " + getBundle().getString("cmdMenuRemoveGS"));
  63. System.out.println("4 - " + getBundle().getString("cmdMenuRemoveAll"));
  64. System.out.println("5 - " + getBundle().getString("cmdMenuExit"));
  65. do
  66. {
  67. System.out.print(getBundle().getString("yourChoice") + ' ');
  68. choice = _in.readLine();
  69. try
  70. {
  71. int choiceNumber = Integer.parseInt(choice);
  72. choiceOk = true;
  73. switch (choiceNumber)
  74. {
  75. case 1:
  76. registerNewGS();
  77. break;
  78. case 2:
  79. listGSNames();
  80. break;
  81. case 3:
  82. unregisterSingleGS();
  83. break;
  84. case 4:
  85. unregisterAllGS();
  86. break;
  87. case 5:
  88. System.exit(0);
  89. break;
  90. default:
  91. System.out.printf(getBundle().getString("invalidChoice") + Config.EOL, choice);
  92. choiceOk = false;
  93. }
  94. }
  95. catch (NumberFormatException nfe)
  96. {
  97. System.out.printf(getBundle().getString("invalidChoice") + Config.EOL, choice);
  98. }
  99. }
  100. while (!choiceOk);
  101. }
  102. }
  103. /**
  104. *
  105. */
  106. private void hr()
  107. {
  108. System.out.println("_____________________________________________________" + Config.EOL);
  109. }
  110. /**
  111. *
  112. */
  113. private void listGSNames()
  114. {
  115. int idMaxLen = 0;
  116. int nameMaxLen = 0;
  117. for (Entry<Integer, String> e : GameServerTable.getInstance().getServerNames().entrySet())
  118. {
  119. if (e.getKey().toString().length() > idMaxLen)
  120. {
  121. idMaxLen = e.getKey().toString().length();
  122. }
  123. if (e.getValue().length() > nameMaxLen)
  124. {
  125. nameMaxLen = e.getValue().length();
  126. }
  127. }
  128. idMaxLen += 2;
  129. nameMaxLen += 2;
  130. String id;
  131. boolean inUse;
  132. String gsInUse = getBundle().getString("gsInUse");
  133. String gsFree = getBundle().getString("gsFree");
  134. int gsStatusMaxLen = Math.max(gsInUse.length(), gsFree.length()) + 2;
  135. for (Entry<Integer, String> e : GameServerTable.getInstance().getServerNames().entrySet())
  136. {
  137. id = e.getKey().toString();
  138. System.out.print(id);
  139. for (int i = id.length(); i < idMaxLen; i++)
  140. {
  141. System.out.print(' ');
  142. }
  143. System.out.print("| ");
  144. System.out.print(e.getValue());
  145. for (int i = e.getValue().length(); i < nameMaxLen; i++)
  146. {
  147. System.out.print(' ');
  148. }
  149. System.out.print("| ");
  150. inUse = GameServerTable.getInstance().hasRegisteredGameServerOnId(e.getKey());
  151. String inUseStr = (inUse ? gsInUse : gsFree);
  152. System.out.print(inUseStr);
  153. for (int i = inUseStr.length(); i < gsStatusMaxLen; i++)
  154. {
  155. System.out.print(' ');
  156. }
  157. System.out.println('|');
  158. }
  159. }
  160. /**
  161. * @throws IOException
  162. */
  163. private void unregisterAllGS() throws IOException
  164. {
  165. if (yesNoQuestion(getBundle().getString("confirmRemoveAllText")))
  166. {
  167. try
  168. {
  169. BaseGameServerRegister.unregisterAllGameServers();
  170. System.out.println(getBundle().getString("unregisterAllOk"));
  171. }
  172. catch (SQLException e)
  173. {
  174. showError(getBundle().getString("sqlErrorUnregisterAll"), e);
  175. }
  176. }
  177. }
  178. private boolean yesNoQuestion(String question) throws IOException
  179. {
  180. do
  181. {
  182. hr();
  183. System.out.println(question);
  184. System.out.println("1 - " + getBundle().getString("yes"));
  185. System.out.println("2 - " + getBundle().getString("no"));
  186. System.out.print(getBundle().getString("yourChoice") + ' ');
  187. String choice;
  188. choice = _in.readLine();
  189. if (choice != null)
  190. {
  191. if (choice.equals("1"))
  192. {
  193. return true;
  194. }
  195. else if (choice.equals("2"))
  196. {
  197. return false;
  198. }
  199. else
  200. {
  201. System.out.printf(getBundle().getString("invalidChoice") + Config.EOL, choice);
  202. }
  203. }
  204. }
  205. while (true);
  206. }
  207. /**
  208. * @throws IOException
  209. */
  210. private void unregisterSingleGS() throws IOException
  211. {
  212. String line;
  213. int id = Integer.MIN_VALUE;
  214. do
  215. {
  216. System.out.print(getBundle().getString("enterDesiredId") + ' ');
  217. line = _in.readLine();
  218. try
  219. {
  220. id = Integer.parseInt(line);
  221. }
  222. catch (NumberFormatException e)
  223. {
  224. System.out.printf(getBundle().getString("invalidChoice") + Config.EOL, line);
  225. }
  226. }
  227. while (id == Integer.MIN_VALUE);
  228. String name = GameServerTable.getInstance().getServerNameById(id);
  229. if (name == null)
  230. {
  231. System.out.printf(getBundle().getString("noNameForId") + Config.EOL, id);
  232. }
  233. else
  234. {
  235. if (GameServerTable.getInstance().hasRegisteredGameServerOnId(id))
  236. {
  237. System.out.printf(getBundle().getString("confirmRemoveText") + Config.EOL, id, name);
  238. try
  239. {
  240. BaseGameServerRegister.unregisterGameServer(id);
  241. System.out.printf(getBundle().getString("unregisterOk") + Config.EOL, id);
  242. }
  243. catch (SQLException e)
  244. {
  245. showError(getBundle().getString("sqlErrorUnregister"), e);
  246. }
  247. }
  248. else
  249. {
  250. System.out.printf(getBundle().getString("noServerForId") + Config.EOL, id);
  251. }
  252. }
  253. }
  254. private void registerNewGS() throws IOException
  255. {
  256. String line;
  257. int id = Integer.MIN_VALUE;
  258. do
  259. {
  260. System.out.println(getBundle().getString("enterDesiredId"));
  261. line = _in.readLine();
  262. try
  263. {
  264. id = Integer.parseInt(line);
  265. }
  266. catch (NumberFormatException e)
  267. {
  268. System.out.printf(getBundle().getString("invalidChoice") + Config.EOL, line);
  269. }
  270. }
  271. while (id == Integer.MIN_VALUE);
  272. String name = GameServerTable.getInstance().getServerNameById(id);
  273. if (name == null)
  274. {
  275. System.out.printf(getBundle().getString("noNameForId") + Config.EOL, id);
  276. }
  277. else
  278. {
  279. if (GameServerTable.getInstance().hasRegisteredGameServerOnId(id))
  280. {
  281. System.out.println(getBundle().getString("idIsNotFree"));
  282. }
  283. else
  284. {
  285. try
  286. {
  287. BaseGameServerRegister.registerGameServer(id, ".");
  288. }
  289. catch (IOException e)
  290. {
  291. showError(getBundle().getString("ioErrorRegister"), e);
  292. }
  293. }
  294. }
  295. }
  296. @Override
  297. public void showError(String msg, Throwable t)
  298. {
  299. String title;
  300. if (getBundle() != null)
  301. {
  302. title = getBundle().getString("error");
  303. msg += Config.EOL + getBundle().getString("reason") + ' ' + t.getLocalizedMessage();
  304. }
  305. else
  306. {
  307. title = "Error";
  308. msg += Config.EOL + "Cause: " + t.getLocalizedMessage();
  309. }
  310. System.out.println(title + ": " + msg);
  311. }
  312. }