Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- console.log(__dirname)
- const ITEMS = require(__dirname + '/items.json');
- const RPG = require(__dirname + '/library');
- const DATA = {};
- const INV = {};
- const PLAYER_DATA = new RPG.DATABASE("data", "Player Data")
- function findItem(name) {
- if (!name) return;
- const item = name.toLowerCase();
- for (let index of Object.keys(ITEMS)) {
- if (index.toLowerCase().indexOf(item) == 0) {
- return index;
- };
- };
- };
- function unequip(id) {
- if (!id) return;
- if (!PLAYER_DATA.CHECK(id)) return;
- const equip_data = PLAYER_DATA.GET_DATA(`${id}.equip`)
- if (!typeof equip_data === "undefined", equip_data.equipped) return;
- return {
- item: PLAYER_DATA.SET_DATA(undefined, `${id}.equip.item`),
- equip: PLAYER_DATA.SET_DATA(false, `${id}.equip.equipped`)
- }
- }
- function tax(price, player) {
- if (!price || isNaN(price)) return {
- err: Error("Price is NaN"),
- noti: RPG.notification("Inv", "This item does not have a sell price!", player, "#0000FF")
- };
- return Math.floor(price * 0.8) // 80% of buy price
- }
- function invAdd(item, plr) {
- const id = plr.userId;
- const name = String(item);
- if (PLAYER_DATA.GET_DATA(`${id}.inv.${name}`)) {
- let quan = PLAYER_DATA.GET_DATA(`${id}.inv.${name}.quantity`)
- return PLAYER_DATA.SET_DATA(quan += 1, `${id}.inv.${name}.quantity`);
- }
- INV[item] = {
- quantity: 1
- }
- return PLAYER_DATA.SET_DATA(INV[item], `${id}.inv.${name}`)
- }
- Game.on('playerJoin', player => {
- if (PLAYER_DATA.CHECK(player.userId)) { // load their data in
- const data = PLAYER_DATA.GET_DATA(player.userId);
- player.score = data.coins;
- }
- DATA[player.userId] = {
- "id": player.userId,
- "name": player.username,
- "coins": 100,
- "inv": {},
- "bank": [],
- "equip": {
- equipped: false,
- item: undefined
- }
- };
- return {
- DATA: PLAYER_DATA.SET_DATA(DATA[player.userId], player.userId),
- SET_SCORE: player.score = PLAYER_DATA.GET_DATA(`${player.userId}.coins`)
- }
- });
- Game.on(`playerLeave`, player => {
- if (!PLAYER_DATA.CHECK(player.userId)) return; // just in case something erros and they don't have any data
- return unequip(player.userId); // unequips if the player leaves
- })
- Game.on('coinsCmd', player => {
- const coins = player.score;
- if (coins > 1) {
- return RPG.notification("Inv", `You have ${player.score} coins!`, player, "#0000FF");
- } else {
- return RPG.notification("Inv", `You have ${player.score} coin!`, player, "#0000FF"); // need correct grammar
- };
- });
- Game.on("giveCmd", (player, msg) => {
- if (!RPG.checkAdmins(player.userId)) return;
- const item = findItem(msg);
- if (item) {
- invAdd(item, player)
- RPG.notification("Inv", `You gave yourself one ${item}`, player, "#0000FF")
- }
- })
- Game.on('sellCmd', (player, msg) => {
- if (!msg) return;
- const item = findItem(msg);
- if (item) {
- const data = PLAYER_DATA.GET_DATA(player.userId);
- let coins = data.coins;
- if (data) {
- 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");
- for (let index of Object.keys(data.inv)) {
- if (String(index).toLowerCase() === item.toLowerCase()) {
- let new_coins = coins += tax(ITEMS[index].cost_price, player)
- if (data.equip.item == item) { // checks for if the player is selling the item he has equipped
- if (data.inv[index].quantity === 1) {
- PLAYER_DATA.SET_DATA(undefined, `${player.userId}.equip.item`) // unequipping the item if they have more than 1
- PLAYER_DATA.SET_DATA(false, `${player.userId}.equip.equipped`)
- return { // then sells the item
- coins: PLAYER_DATA.SET_DATA(new_coins, `${player.userId}.coins`),
- set_score: player.score = new_coins,
- del: PLAYER_DATA.DELETE(`${player.userId}.inv.${item}`),
- noti: RPG.notification("Inv", `You sold the rest of your ${item}s!`, player, "#0000FF")
- }
- }
- };
- if (data.inv[index].quantity > 1) {
- return {
- quan: PLAYER_DATA.SET_DATA(data.inv[index].quantity -= 1, `${player.userId}.inv.${index}.quantity`),
- coins: PLAYER_DATA.SET_DATA(new_coins, `${player.userId}.coins`),
- set_score: player.score = new_coins,
- noti: RPG.notification("Inv", `You sold 1 ${item}`, player, "#0000FF")
- }
- }
- return {
- coins: PLAYER_DATA.SET_DATA(new_coins, `${player.userId}.coins`),
- set_score: player.score = new_coins,
- del: PLAYER_DATA.DELETE(`${player.userId}.inv.${item}`),
- noti: RPG.notification("Inv", `You sold the rest of your ${item}s!`, player, "#0000FF")
- };
- };
- };
- };
- }
- });
- Game.on('invCmd', player => {
- const data = PLAYER_DATA.GET_DATA(player.userId);
- if (data) {
- if (!data.inv.constructor === "object" || Object.keys(data.inv).length === 0) return RPG.notification("Inv", "Your inventory is empty, start begging!", player, "#0000FF")
- for (let index of Object.keys(data.inv)) {
- RPG.notification("Inv", `${index} ${data.inv[index].quantity}x.`, player, "#0000FF")
- }
- }
- });
- Game.on('equipCmd', (player, msg) => {
- const data = PLAYER_DATA.GET_DATA(player.userId);
- if (!data) return;
- if (!msg) return RPG.notification("Inv", "You can't equip nothing!", player, "#0000FF");
- const item = findItem(msg);
- if (item) {
- if (!data.inv[item]) return RPG.notification("Inv", "You can't equip something you don't own!", player, "#0000FF");
- if (data.equip.equipped || data.equip.item !== undefined) {
- let OLD_ITEM = data.equip.item;
- return {
- set_item: PLAYER_DATA.SET_DATA(item, `${player.userId}.equip.item`),
- set_value: PLAYER_DATA.SET_DATA(true, `${player.userId}.equip.equipped`),
- noti: RPG.alert("inv", `You unequipped ${OLD_ITEM} and equipped ${PLAYER_DATA.GET_DATA(`${player.userId}.equip.item`)}!`, player)
- }
- } else {
- return {
- set: PLAYER_DATA.SET_DATA(item, `${player.userId}.equip.item`),
- set_value: PLAYER_DATA.SET_DATA(true, `${player.userId}.equip.equipped`),
- noti: RPG.alert("inv", `You equipped ${item}!`, player)
- }
- }
- }
- })
- Game.on('unequipCmd', player => {
- const data = PLAYER_DATA.GET_DATA(player.userId);
- if (!data) return Error(`Can't find data for user ${player.userId}`);
- if (!data.equip.equipped || data.equip.item === undefined) return RPG.alert("inv", "You can't unequip something that isn't equipped!", player)
- const item = data.equip.item;
- return {
- set_item: PLAYER_DATA.SET_DATA(undefined, `${player.userId}.equip.item`),
- set_value: PLAYER_DATA.SET_DATA(false, `${player.userId}.equip.equipped`),
- noti: RPG.notification("Inv", `You unequipped ${item}.`, player, "#0000FF")
- }
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement