Java IRC bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SkillCommand.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package com.md87.charliebravo.commands;
  6. import com.dmdirc.ui.messages.Formatter;
  7. import com.md87.charliebravo.Command;
  8. import com.md87.charliebravo.CommandOptions;
  9. import com.md87.charliebravo.ConfigCache;
  10. import com.md87.charliebravo.Followup;
  11. import com.md87.charliebravo.InputHandler;
  12. import com.md87.charliebravo.Response;
  13. import uk.co.md87.evetool.api.ApiResponse;
  14. import uk.co.md87.evetool.api.EveApi;
  15. import uk.co.md87.evetool.api.wrappers.SkillInTraining;
  16. import uk.co.md87.evetool.api.wrappers.SkillList;
  17. /**
  18. *
  19. * @author chris
  20. */
  21. @CommandOptions(requireAuthorisation=true, requiredSettings={"eve.apikey","eve.userid","eve.charid"})
  22. public class SkillCommand implements Command {
  23. public void execute(InputHandler handler, Response response, String line) throws Exception {
  24. final String openID = (String) handler.getParser().getClientInfoOrFake(response.getSource())
  25. .getMap().get("OpenID");
  26. if (openID == null) {
  27. response.sendMessage("You must be authorised to use this command", true);
  28. } else {
  29. final EveApi api = new EveApi(new ConfigCache(handler.getConfig().getConfigfile()));
  30. api.setApiKey(handler.getConfig().getOption(openID, "eve.apikey"));
  31. api.setUserID(Integer.parseInt(handler.getConfig().getOption(openID, "eve.userid")));
  32. api.setCharID(Integer.parseInt(handler.getConfig().getOption(openID, "eve.charid")));
  33. final ApiResponse<SkillInTraining> res = api.getSkillInTraining();
  34. if (res.wasSuccessful()) {
  35. final SkillInTraining skill = res.getResult();
  36. final ApiResponse<SkillList> res2 = api.getSkillTree();
  37. if (res2.wasSuccessful()) {
  38. if (skill.isInTraining() && System.currentTimeMillis()
  39. < skill.getEndTime().getTime() - 1000) {
  40. skill.setSkill(res2.getResult().getSkillById(skill.getTypeId()));
  41. response.sendMessage("you are currently training "
  42. + skill.getSkill().getName() + " to level "
  43. + skill.getTargetLevel() + ". It will finish in "
  44. + Formatter.formatDuration((int) (skill.getEndTime()
  45. .getTime() - System.currentTimeMillis()) / 1000));
  46. } else {
  47. response.sendMessage("You are not training anything", true);
  48. }
  49. } else {
  50. response.sendMessage("There was an error retrieving the EVE skill list", true);
  51. response.addFollowup(new ErrorFollowup(res2));
  52. response.addFollowup(new RetryFollowup(this));
  53. }
  54. } else {
  55. response.sendMessage("There was an error retrieving your skill information", true);
  56. response.addFollowup(new ErrorFollowup(res));
  57. response.addFollowup(new RetryFollowup(this));
  58. }
  59. response.addFollowup(new CacheFollowup(res));
  60. }
  61. }
  62. protected static class CacheFollowup implements Followup {
  63. protected final ApiResponse<?> apiresponse;
  64. public CacheFollowup(ApiResponse<?> apiresponse) {
  65. this.apiresponse = apiresponse;
  66. }
  67. public boolean matches(String line) {
  68. return line.equalsIgnoreCase("cache");
  69. }
  70. public void execute(InputHandler handler, Response response, String line) throws Exception {
  71. response.setInheritFollows(true);
  72. response.sendMessage("the result has been cached for " +
  73. Formatter.formatDuration((int) (System.currentTimeMillis()
  74. - apiresponse.getApiResult().getCachedSince().getTime()) / 1000)
  75. + ", and will expire in " +
  76. Formatter.formatDuration((int) (apiresponse.getApiResult()
  77. .getCachedUntil().getTime() - System.currentTimeMillis()) / 1000));
  78. }
  79. }
  80. protected static class ErrorFollowup implements Followup {
  81. protected final ApiResponse<?> apiresponse;
  82. public ErrorFollowup(ApiResponse<?> response) {
  83. this.apiresponse = response;
  84. }
  85. public boolean matches(String line) {
  86. return line.equalsIgnoreCase("error");
  87. }
  88. public void execute(InputHandler handler, Response response, String line) throws Exception {
  89. response.setInheritFollows(true);
  90. response.sendMessage("the error message was: " + apiresponse.getError());
  91. }
  92. }
  93. protected static class RetryFollowup implements Followup {
  94. protected final Command command;
  95. public RetryFollowup(Command command) {
  96. this.command = command;
  97. }
  98. public boolean matches(String line) {
  99. return line.equalsIgnoreCase("retry");
  100. }
  101. public void execute(InputHandler handler, Response response, String line) throws Exception {
  102. command.execute(handler, response, line);
  103. }
  104. }
  105. }