Advertisement
Guest User

Untitled

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