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 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2009-2010 Chris Smith
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.md87.charliebravo.commands;
  23. import com.dmdirc.util.DateUtils;
  24. import com.md87.charliebravo.Command;
  25. import com.md87.charliebravo.CommandOptions;
  26. import com.md87.charliebravo.ConfigCache;
  27. import com.md87.charliebravo.Followup;
  28. import com.md87.charliebravo.InputHandler;
  29. import com.md87.charliebravo.Response;
  30. import uk.co.md87.evetool.api.ApiResponse;
  31. import uk.co.md87.evetool.api.EveApi;
  32. import uk.co.md87.evetool.api.wrappers.SkillInTraining;
  33. import uk.co.md87.evetool.api.wrappers.SkillList;
  34. /**
  35. *
  36. * @author chris
  37. */
  38. @CommandOptions(requireAuthorisation=true, requiredSettings={"eve.apikey","eve.userid","eve.charid"})
  39. public class SkillCommand implements Command {
  40. public void execute(InputHandler handler, Response response, String line) throws Exception {
  41. final String openID = (String) handler.getParser().getClient(response.getSource())
  42. .getMap().get("OpenID");
  43. if (openID == null) {
  44. response.sendMessage("You must be authorised to use this command", true);
  45. } else {
  46. final EveApi api = new EveApi(new ConfigCache(handler.getConfig().getConfigfile()));
  47. api.setApiKey(handler.getConfig().getOption(openID, "eve.apikey"));
  48. api.setUserID(Integer.parseInt(handler.getConfig().getOption(openID, "eve.userid")));
  49. api.setCharID(Integer.parseInt(handler.getConfig().getOption(openID, "eve.charid")));
  50. final ApiResponse<SkillInTraining> res = api.getSkillInTraining();
  51. if (res.wasSuccessful()) {
  52. final SkillInTraining skill = res.getResult();
  53. final ApiResponse<SkillList> res2 = api.getSkillTree();
  54. if (res2.wasSuccessful()) {
  55. if (skill.isInTraining() && System.currentTimeMillis()
  56. < skill.getEndTime().getTime() - 1000) {
  57. skill.setSkill(res2.getResult().getSkillById(skill.getTypeId()));
  58. response.sendMessage("you are currently training "
  59. + skill.getSkill().getName() + " to level "
  60. + skill.getTargetLevel() + ". It will finish in "
  61. + DateUtils.formatDuration((int) (skill.getEndTime()
  62. .getTime() - System.currentTimeMillis()) / 1000));
  63. } else {
  64. response.sendMessage("You are not training anything", true);
  65. }
  66. } else {
  67. response.sendMessage("There was an error retrieving the EVE skill list", true);
  68. response.addFollowup(new ErrorFollowup(res2));
  69. response.addFollowup(new RetryFollowup(this));
  70. }
  71. } else {
  72. response.sendMessage("There was an error retrieving your skill information", true);
  73. response.addFollowup(new ErrorFollowup(res));
  74. response.addFollowup(new RetryFollowup(this));
  75. }
  76. response.addFollowup(new CacheFollowup(res));
  77. }
  78. }
  79. protected static class CacheFollowup implements Followup {
  80. protected final ApiResponse<?> apiresponse;
  81. public CacheFollowup(ApiResponse<?> apiresponse) {
  82. this.apiresponse = apiresponse;
  83. }
  84. public boolean matches(String line) {
  85. return line.equalsIgnoreCase("cache");
  86. }
  87. public void execute(InputHandler handler, Response response, String line) throws Exception {
  88. response.setInheritFollows(true);
  89. response.sendMessage("the result has been cached for " +
  90. DateUtils.formatDuration((int) (System.currentTimeMillis()
  91. - apiresponse.getApiResult().getCachedSince().getTime()) / 1000)
  92. + ", and will expire in " +
  93. DateUtils.formatDuration((int) (apiresponse.getApiResult()
  94. .getCachedUntil().getTime() - System.currentTimeMillis()) / 1000));
  95. }
  96. }
  97. protected static class ErrorFollowup implements Followup {
  98. protected final ApiResponse<?> apiresponse;
  99. public ErrorFollowup(ApiResponse<?> response) {
  100. this.apiresponse = response;
  101. }
  102. public boolean matches(String line) {
  103. return line.equalsIgnoreCase("error");
  104. }
  105. public void execute(InputHandler handler, Response response, String line) throws Exception {
  106. response.setInheritFollows(true);
  107. response.sendMessage("the error message was: " + apiresponse.getError());
  108. }
  109. }
  110. protected static class RetryFollowup implements Followup {
  111. protected final Command command;
  112. public RetryFollowup(Command command) {
  113. this.command = command;
  114. }
  115. public boolean matches(String line) {
  116. return line.equalsIgnoreCase("retry");
  117. }
  118. public void execute(InputHandler handler, Response response, String line) throws Exception {
  119. command.execute(handler, response, line);
  120. }
  121. }
  122. }