Guest User

Untitled

a guest
Mar 18th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. document.addEventListener("DOMContentLoaded", function () {
  2. showStats();
  3. });
  4.  
  5. let player = {
  6. stats: {
  7. "level": 1,
  8. "experience": 0,
  9. "maxHp": 100,
  10. "currentHp": 100,
  11. "maxEnergy": 100,
  12. "currentEnergy": 100,
  13. "maxMana": 100,
  14. "currentMana": 100,
  15. "gold": 100,
  16. "diamonds": 0,
  17. },
  18. skills: {
  19. fighting: {
  20. "lvl": 1,
  21. "exp": 0,
  22. },
  23. spellcasting: {
  24. "lvl": 1,
  25. "exp": 0,
  26. },
  27. archery: {
  28. "lvl": 1,
  29. "exp": 0,
  30. },
  31. fishing: {
  32. "lvl": 1,
  33. "exp": 0,
  34. },
  35. mining: {
  36. "lvl": 1,
  37. "exp": 0,
  38. },
  39. crafting: {
  40. "lvl": 1,
  41. "exp": 0,
  42. },
  43. }
  44. }; // Missing semicolon
  45.  
  46. const skillExp = {
  47. 1: 0,
  48. 2: 500,
  49. 3: 1100,
  50. 4: 2000,
  51. 5: 3500,
  52. 6: 5500,
  53. 7: 8500,
  54. 8: 13000,
  55. 9: 19000,
  56. 10: 27000,
  57. 11: 37000,
  58. }; // Missing semicolon
  59.  
  60. // Because skillExp is an object, not an array, we have to get the keys (as an array), and get the last value.
  61. // Saves performing this lookup (on a static set of values) every time it is needed.
  62. // If you add more levels, this will always return the last one.
  63. const maxSkillLevel = parseInt(Object.keys(skillExp)[Object.keys(skillExp).length - 1]);
  64.  
  65. // Use a shortcut to get to the common things you wish to access; reduces code and easier to read.
  66. // Could reduce further to gold, currentEnergy, etc. if you think it applicable.
  67. let stats = player.stats;
  68. let skills = player.skills;
  69.  
  70. // Get the DOM Elements into variables once; not every time you want to access them.
  71. let elemLevel = document.getElementById('level');
  72. let elemTotalExp = document.getElementById('totalExp');
  73. let elemCurrentHp = document.getElementById('currentHp');
  74. let elemMaxHp = document.getElementById('maxHp');
  75. let elemCurrentEnergy = document.getElementById('currentEnergy');
  76. let elemMaxEnergy = document.getElementById('maxEnergy');
  77. let elemGold = document.getElementById('gold');
  78. let elemDiamonds = document.getElementById('diamonds');
  79. let elemFighting = document.getElementById('fighting');
  80. let elemSpellcasting = document.getElementById('spellcasting');
  81. let elemArchery = document.getElementById('archery');
  82. let elemFishing = document.getElementById('fishing');
  83. let elemMining = document.getElementById('mining');
  84. let elemCrafting = document.getElementById('crafting');
  85. let elemCurrentGold = document.getElementById('current-gold');
  86. let elemInfo = document.getElementById('info');
  87.  
  88. // Remove main function - not required. Call mine function directly.
  89.  
  90. // REFILL PLAYERS ENERGY
  91. function addEnergy() {
  92. stats.currentEnergy = 100;
  93. stats.gold -= 100;
  94. elemInfo.textContent = "";
  95. showStats();
  96. }
  97.  
  98. // MINE ACTION
  99. function mine() {
  100. // Removed minedGold and info variables from global scope, and now using them directly where needed.
  101. // Pass the minedGold and message values on to any function that requires them..
  102. var minedGold = 0;
  103. var message = "";
  104. if (stats.currentEnergy >= 10) {
  105. minedGold = Math.floor((Math.random() * 100) + (skills.mining.lvl * 2));
  106. stats.gold += minedGold;
  107. stats.currentEnergy -= 10;
  108. calculateExp(skills.mining, minedGold);
  109. showStats(); // Update stats when finished mining. No need to call this if mining didn't occur.
  110. }
  111. else {
  112. message = "Not enough energy";
  113. }
  114. // Removed call to mineInfo() from first if block;
  115. // Previously, (without curly braces around second if block),
  116. // the second mineInfo() would be called even if it was called in the first block.
  117. // Only the first line of code after an if/else etc. will be called when matched,
  118. // everything after will run as normal code - because not encased in a block.
  119. mineInfo(message, minedGold);
  120. }
  121.  
  122. // DISPLAY MINING COMUNICATES
  123. function mineInfo(message, minedGold) {
  124. // Pass in any message to display, rather than relying on a global variable (that some other action may change);
  125. // Same for minedGold.
  126. // This way, this function doesn't have to rely on variables outside of its scope,
  127. // that could change or move in the future - Separation Of Concerns.
  128. elemCurrentGold.textContent = minedGold;
  129. elemInfo.textContent = message;
  130. }
  131.  
  132. // ADD EXP AND CHECK FOR LVL UP
  133. function calculateExp(skillName, expGained) {
  134. skillName.exp += expGained;
  135. // Update total experience too.
  136. stats.experience += expGained;
  137. // Remove loop. No need to iterate over every item in skillExp comparing values.
  138.  
  139. // If player has more experience than the next level requires, level up.
  140. // A while loop allows for the case where the player's experience jumps more than one level from an experience gain.
  141. // And an additional check that the player's level is not at max.
  142. while (skillName.lvl < maxSkillLevel && skillName.exp > skillExp[skillName.lvl + 1]) {
  143. skillName.lvl++;
  144. }
  145. }
  146.  
  147. // DISPLAY UPDATED STATS
  148. function showStats() {
  149. // Use textContent rather than innerHTML.
  150. // If you need to place style tags etc. in your text (e.g <b>Hello world</b>), then innerHTML is the way to go.
  151. // But you should try to style the elements themselves rather than the text within.
  152. elemLevel.textContent = stats.level;
  153. elemTotalExp.textContent = stats.experience;
  154. elemCurrentHp.textContent = stats.currentHp;
  155. elemMaxHp.textContent = stats.maxHp;
  156. elemCurrentEnergy.textContent = stats.currentEnergy;
  157. elemMaxEnergy.textContent = stats.maxEnergy;
  158. elemGold.textContent = stats.gold;
  159. elemDiamonds.textContent = stats.diamonds;
  160. elemFighting.textContent = skills.fighting.lvl;
  161. elemSpellcasting.textContent = skills.spellcasting.lvl;
  162. elemArchery.textContent = skills.archery.lvl;
  163. elemFishing.textContent = skills.fishing.lvl;
  164. elemMining.textContent = skills.mining.lvl;
  165. elemCrafting.textContent = skills.crafting.lvl;
  166. }
Add Comment
Please, Sign In to add comment