Advertisement
Guest User

Untitled

a guest
Mar 10th, 2019
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  1. package com.wurmonline.server.questions;
  2.  
  3. import com.wurmonline.server.DbConnector;
  4. import com.wurmonline.server.Items;
  5. import com.wurmonline.server.Mailer;
  6. import com.wurmonline.server.NoSuchItemException;
  7. import com.wurmonline.server.WurmCalendar;
  8. import com.wurmonline.server.creatures.Communicator;
  9. import com.wurmonline.server.creatures.Creature;
  10. import com.wurmonline.server.economy.Economy;
  11. import com.wurmonline.server.items.Item;
  12. import com.wurmonline.server.items.NotOwnedException;
  13. import com.wurmonline.server.utils.DbUtilities;
  14. import com.wurmonline.server.webinterface.WebInterfaceImpl;
  15. import java.sql.Connection;
  16. import java.sql.PreparedStatement;
  17. import java.sql.SQLException;
  18. import java.util.Properties;
  19. import java.util.Random;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40. public class WishQuestion
  41. extends Question
  42. {
  43. private static final Logger logger = Logger.getLogger(WishQuestion.class.getName());
  44.  
  45. private final long coinId;
  46. private static final String RESPONSE1 = ". Will the gods listen?";
  47. private static final String RESPONSE2 = ". Do you consider yourself lucky?";
  48. private static final String RESPONSE3 = ". Is this your turn?";
  49. private static final String RESPONSE4 = ". You get the feeling that someone listens.";
  50. private static final String RESPONSE5 = ". Good luck!";
  51. private static final String RESPONSE6 = ". Will it come true?";
  52. private static final Random rand = new Random();
  53.  
  54.  
  55.  
  56.  
  57. private static final String INSERT_WISH = "INSERT INTO WISHES (PLAYER,WISH,COIN,TOFULFILL) VALUES(?,?,?,?)";
  58.  
  59.  
  60.  
  61.  
  62.  
  63. public WishQuestion(Creature aResponder, String aTitle, String aQuestion, long aTarget, long coin)
  64. {
  65. super(aResponder, aTitle, aQuestion, 77, aTarget);
  66. coinId = coin;
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75. public void answer(Properties aAnswers)
  76. {
  77. Item coin = null;
  78. Item targetItem = null;
  79. try
  80. {
  81. targetItem = Items.getItem(target);
  82. }
  83. catch (NoSuchItemException nsi)
  84. {
  85. getResponder().getCommunicator().sendNormalServerMessage("You fail to locate the target!");
  86. return;
  87. }
  88. try
  89. {
  90. coin = Items.getItem(coinId);
  91. if ((coin.getOwner() == getResponder().getWurmId()) && (!coin.isBanked()) && (!mailed))
  92. {
  93. String key = "data1";
  94. String val = aAnswers.getProperty("data1");
  95. if ((val != null) && (val.length() > 0))
  96. {
  97. String tstring = ". Will the gods listen?";
  98. int x = rand.nextInt(6);
  99. if (x == 1) {
  100. tstring = ". Do you consider yourself lucky?";
  101. } else if (x == 2) {
  102. tstring = ". Is this your turn?";
  103. } else if (x == 3) {
  104. tstring = ". You get the feeling that someone listens.";
  105. } else if (x == 4) {
  106. tstring = ". Good luck!";
  107. } else if (x == 5)
  108. tstring = ". Will it come true?";
  109. getResponder().getCommunicator().sendNormalServerMessage("You wish for " + val + tstring);
  110. long moneyVal = Economy.getValueFor(coin.getTemplateId());
  111. float chance = (float)moneyVal / 3.0E7F;
  112. float chantLevel = targetItem.getSpellCourierBonus();
  113.  
  114. float timeBonus = WurmCalendar.isNight() ? 1.05F : 1.0F;
  115.  
  116. float newchance = chance * (targetItem.getCurrentQualityLevel() / 100.0F) * (1.0F + chantLevel / 100.0F) * (1.0F + coin.getCurrentQualityLevel() / 1000.0F) * timeBonus;
  117. logger.log(Level.INFO, "New chance=" + newchance + " after coin=" + chance + ", chant=" + chantLevel + " ql=" + targetItem
  118. .getCurrentQualityLevel());
  119. boolean toFulfill = rand.nextFloat() < newchance;
  120. if (getResponder().getPower() >= 5)
  121. toFulfill = true;
  122. Connection dbcon = null;
  123. PreparedStatement ps = null;
  124. try
  125. {
  126. dbcon = DbConnector.getPlayerDbCon();
  127. ps = dbcon.prepareStatement("INSERT INTO WISHES (PLAYER,WISH,COIN,TOFULFILL) VALUES(?,?,?,?)");
  128. ps.setLong(1, getResponder().getWurmId());
  129. ps.setString(2, val);
  130. ps.setLong(3, moneyVal);
  131. ps.setBoolean(4, toFulfill);
  132. ps.executeUpdate();
  133. }
  134. catch (SQLException sqx)
  135. {
  136. logger.log(Level.WARNING, sqx.getMessage(), sqx);
  137. }
  138. finally
  139. {
  140. DbUtilities.closeDatabaseObjects(ps, null);
  141. DbConnector.returnConnection(dbcon);
  142. }
  143. Items.destroyItem(coin.getWurmId());
  144. if (toFulfill)
  145. {
  146. try
  147. {
  148. Mailer.sendMail(WebInterfaceImpl.mailAccount, "[email protected]", getResponder().getName() + " made a wish!",
  149. getResponder().getName() + " wants the wish " + val + " to be fulfilled!");
  150.  
  151. }
  152. catch (Exception ex)
  153. {
  154. logger.log(Level.WARNING, ex.getMessage(), ex);
  155. }
  156. }
  157. }
  158. else {
  159. getResponder().getCommunicator().sendNormalServerMessage("You make no wish this time.");
  160. }
  161. }
  162. else {
  163. getResponder().getCommunicator().sendNormalServerMessage("You are no longer in possesion of the " + coin
  164. .getName() + "!");
  165. return;
  166. }
  167. }
  168. catch (NoSuchItemException nsi)
  169. {
  170. getResponder().getCommunicator().sendNormalServerMessage("You are no longer in possesion of the coin!");
  171. return;
  172. }
  173. catch (NotOwnedException no)
  174. {
  175. getResponder().getCommunicator().sendNormalServerMessage("You are no longer in possesion of the coin!");
  176. return;
  177. }
  178. }
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186. public void sendQuestion()
  187. {
  188. StringBuilder buf = new StringBuilder(getBmlHeader());
  189.  
  190. buf.append("harray{label{text='What is your wish?'};input{maxchars='40';id='data1'; text=''}}");
  191. buf.append("label{text=\"Just leave it blank if you don't want to lose your coin.\"}");
  192.  
  193. buf.append(createAnswerButton2());
  194. getResponder().getCommunicator().sendBml(300, 300, true, true, buf.toString(), 200, 200, 200, title);
  195. }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement