Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.58 KB | None | 0 0
  1. const ITEMS = require(__dirname + '/user_scripts/items.json');
  2. const RPG = require(__dirname + '/user_scripts/library')
  3. const DATA = {};
  4. const INV = {};
  5.  
  6. const PLAYER_DATA = new RPG.DATABASE("data", "Player Data")
  7.  
  8. function findItem(name) {
  9. if (!name) return;
  10. const item = name.toLowerCase();
  11. for (let index of Object.keys(ITEMS)) {
  12. if (index.toLowerCase().indexOf(item) == 0) {
  13. return index;
  14. };
  15. };
  16. };
  17.  
  18. function unequip(id) {
  19. if (!id) return;
  20. if (!PLAYER_DATA.CHECK(id)) return;
  21.  
  22. const equip_data = PLAYER_DATA.GET_DATA(`${id}.equip`)
  23. if (!typeof equip_data === "undefined", equip_data.equipped) return;
  24. return {
  25. item: PLAYER_DATA.SET_DATA(undefined, `${id}.equip.item`),
  26. equip: PLAYER_DATA.SET_DATA(false, `${id}.equip.equipped`)
  27. }
  28. }
  29.  
  30. function tax(price, player) {
  31. if (!price || isNaN(price)) return {
  32. err: Error("Price is NaN"),
  33. noti: RPG.notification("Inv", "This item does not have a sell price!", player, "#0000FF")
  34. };
  35. return Math.floor(price * 0.8)
  36. }
  37.  
  38. function invAdd(item, plr) {
  39. const id = plr.userId;
  40. const name = String(item);
  41. if (PLAYER_DATA.GET_DATA(`${id}.inv.${name}`)) {
  42. let quan = PLAYER_DATA.GET_DATA(`${id}.inv.${name}.quantity`)
  43. return PLAYER_DATA.SET_DATA(quan += 1, `${id}.inv.${name}.quantity`);
  44. }
  45. INV[item] = {
  46. quantity: 1
  47. }
  48. return PLAYER_DATA.SET_DATA(INV[item], `${id}.inv.${name}`)
  49. }
  50.  
  51. Game.on('playerJoin', player => {
  52. if (PLAYER_DATA.CHECK(player.userId)) { // load their data in
  53. const data = PLAYER_DATA.GET_DATA(player.userId);
  54. player.score = data.coins;
  55.  
  56. }
  57.  
  58. DATA[player.userId] = {
  59. "id": player.userId,
  60. "name": player.username,
  61. "coins": 100,
  62. "inv": {},
  63. "bank": [],
  64. "equip": {
  65. equipped: false,
  66. item: undefined
  67. }
  68. };
  69.  
  70. return {
  71. DATA: PLAYER_DATA.SET_DATA(DATA[player.userId], player.userId),
  72. SET_SCORE: player.score = PLAYER_DATA.GET_DATA(`${player.userId}.coins`)
  73. }
  74. });
  75.  
  76. Game.on(`playerLeave`, player => {
  77. if (!PLAYER_DATA.CHECK(player.userId)) return; // just in case something erros and they don't have any data
  78. return unequip(); // unequips if the player leaves
  79. })
  80.  
  81.  
  82. Game.on('coinsCmd', player => {
  83. const coins = player.score;
  84. if (coins > 1) {
  85. return RPG.notification("Inv", `You have ${player.score} coins!`, player, "#0000FF");
  86. } else {
  87. return RPG.notification("Inv", `You have ${player.score} coin!`, player, "#0000FF"); // need correct grammar
  88. };
  89. });
  90.  
  91. Game.on("giveCmd", (player, msg) => {
  92. if (!RPG.checkAdmins(player.userId)) return;
  93. const item = findItem(msg);
  94. if (item) {
  95. invAdd(item, player)
  96. RPG.notification("Inv", `You gave yourself one ${item}`, player, "#0000FF")
  97. }
  98. })
  99.  
  100. Game.on('sellCmd', (player, msg) => {
  101. if (!msg) return;
  102. const item = findItem(msg);
  103. if (item) {
  104. const data = PLAYER_DATA.GET_DATA(player.userId);
  105. let coins = data.coins;
  106. if (data) {
  107. if (!typeof data.inv === "object" || Object.keys(data.inv).length === 0) return RPG.notification("Inv", "You can sell something you don't have!", player, "#0000FF");
  108. for (let index of Object.keys(data.inv)) {
  109. if (String(index).toLowerCase() === item.toLowerCase()) {
  110. let new_coins = coins += tax(ITEMS[index].cost_price, player)
  111. if (data.equip.item == item) { // checks for if the player is selling the item he has equipped
  112. if (data.inv[index].quantity === 1) {
  113. PLAYER_DATA.SET_DATA(undefined, `${player.userId}.equip.item`) // unequipping the item if they have more than 1
  114. PLAYER_DATA.SET_DATA(false, `${player.userId}.equip.equipped`)
  115. return { // then sells the item
  116. coins: PLAYER_DATA.SET_DATA(new_coins, `${player.userId}.coins`),
  117. set_score: player.score = new_coins,
  118. del: PLAYER_DATA.DELETE(`${player.userId}.inv.${item}`),
  119. noti: RPG.notification("Inv", `You sold the rest of your ${item}s!`, player, "#0000FF")
  120. }
  121. }
  122. };
  123. if (data.inv[index].quantity > 1) {
  124. return {
  125. quan: PLAYER_DATA.SET_DATA(data.inv[index].quantity -= 1, `${player.userId}.inv.${index}.quantity`),
  126. coins: PLAYER_DATA.SET_DATA(new_coins, `${player.userId}.coins`),
  127. set_score: player.score = new_coins,
  128. noti: RPG.notification("Inv", `You sold 1 ${item}`, player, "#0000FF")
  129. }
  130. }
  131. return {
  132. coins: PLAYER_DATA.SET_DATA(new_coins, `${player.userId}.coins`),
  133. set_score: player.score = new_coins,
  134. del: PLAYER_DATA.DELETE(`${player.userId}.inv.${item}`),
  135. noti: RPG.notification("Inv", `You sold the rest of your ${item}s!`, player, "#0000FF")
  136. };
  137. };
  138. };
  139. };
  140. }
  141. });
  142.  
  143. Game.on('invCmd', player => {
  144. const data = PLAYER_DATA.GET_DATA(player.userId);
  145. if (data) {
  146. if (!data.inv.constructor === "object" || Object.keys(data.inv).length === 0) return RPG.notification("Inv", "Your inventory is empty, start begging!", player, "#0000FF")
  147. for (let index of Object.keys(data.inv)) {
  148. RPG.notification("Inv", `${index} ${data.inv[index].quantity}x.`, player, "#0000FF")
  149. }
  150. }
  151. });
  152.  
  153. Game.on('equipCmd', (player, msg) => {
  154. const data = PLAYER_DATA.GET_DATA(player.userId);
  155. if (!data) return;
  156. const item = findItem(msg);
  157. if (item) {
  158. if (!data.inv[item]) return RPG.notification("Inv", "You can't equip something you don't own!", player, "#0000FF");
  159. if (data.equip.equipped || data.equip.item !== undefined) {
  160. let OLD_ITEM = data.equip.item;
  161. return {
  162. set_item: PLAYER_DATA.SET_DATA(item, `${player.userId}.equip.item`),
  163. set_value: PLAYER_DATA.SET_DATA(true, `${player.userId}.equip.equipped`),
  164. noti: RPG.notification("Inv", `You unequipped ${OLD_ITEM} and equipped ${PLAYER_DATA.GET_DATA(`${player.userId}.equip.item`)}!`, player, "#0000FF")
  165. }
  166. } else {
  167. return {
  168. set: PLAYER_DATA.SET_DATA(item, `${player.userId}.equip.item`),
  169. set_value: PLAYER_DATA.SET_DATA(true, `${player.userId}.equip.equipped`),
  170. noti: RPG.notification("Inv", `You equipped ${item}!`, player, "#0000FF")
  171. }
  172. }
  173. }
  174. })
  175.  
  176. Game.on('unequipCmd', player => {
  177. const data = PLAYER_DATA.GET_DATA(player.userId);
  178. if (!data) return Error(`Can't find data for user ${player.userId}`);
  179. if (!data.equip.equipped || data.equip.item === undefined) return RPG.notification("Inv", "You can't unequip something that isn't equipped!", player, "#0000FF")
  180. const item = data.equip.item;
  181. return {
  182. set_item: PLAYER_DATA.SET_DATA(undefined, `${player.userId}.equip.item`),
  183. set_value: PLAYER_DATA.SET_DATA(false, `${player.userId}.equip.equipped`),
  184. noti: RPG.notification("Inv", `You unequipped ${item}.`, player, "#0000FF")
  185. }
  186. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement