GameServerRegister.java 8.3 KB

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