diff --git a/src/app.js b/src/app.js index d105b4c..f0c2b40 100644 --- a/src/app.js +++ b/src/app.js @@ -14,6 +14,7 @@ // ============================================================================= const express = require("express"); +const path = require("path"); const swaggerUi = require("swagger-ui-express"); const YAML = require("yamljs"); const cors = require("cors"); //Cross_origin Resource Sharing. Allows frontend to call backend on localhost with different ports. @@ -50,6 +51,7 @@ app.get("/api/health", (req, res) => { //Endpoint route used to check the backen }); app.use(express.static("public")); +app.use("/assets", express.static(path.join(__dirname, "assets"))); app.use("/api/auth", authRouter); app.use("/api/egg", eggRouter); diff --git a/src/models/eggModel.js b/src/models/eggModel.js index 4a6cac5..b6ecc1b 100644 --- a/src/models/eggModel.js +++ b/src/models/eggModel.js @@ -64,7 +64,7 @@ async function initDb(db){ weight: 12, active_background_id: 1, active_music_id: null, - active_cosmetic_id: 2, + active_cosmetic_id: 8, updated_at: new Date().toISOString().split('T')[0] }, { @@ -74,8 +74,8 @@ async function initDb(db){ warmth: 65, weight: 25, active_background_id: 2, - active_music_id: 1, - active_cosmetic_id: 3, + active_music_id: 5, + active_cosmetic_id: 11, updated_at: new Date().toISOString().split('T')[0] } ]; @@ -184,4 +184,4 @@ async function update({egg_id, user_id, stage, glow, warmth, weight, active_back return result.changes > 0; } -module.exports = {initDb, findById, create, remove, update}; \ No newline at end of file +module.exports = {initDb, findById, create, remove, update}; diff --git a/src/models/shopItemModel.js b/src/models/shopItemModel.js index 1238e50..d7048d8 100644 --- a/src/models/shopItemModel.js +++ b/src/models/shopItemModel.js @@ -47,62 +47,172 @@ async function initDb(db) { if (shopItemsCount.count === 0) { const shopItemMockData = [ { - name: "Sunny Garden Background", + name: "default", item_type: "background", - description: "A bright garden background for your egg.", - price: 100, + description: "The original quiet background for a calm memory egg.", + price: 0, effect_type: "warmth", - effect_value: "5", + effect_value: "1", asset_url: null, is_active: 1 }, { - name: "Night Sky Background", + name: "fall_bg", item_type: "background", - description: "A calm night sky background with stars.", + description: "A crisp autumn background with falling leaves and soft amber light.", price: 150, + effect_type: "warmth", + effect_value: "6", + asset_url: "/assets/background/fall-bg.PNG", + is_active: 1 + }, + { + name: "grass_bg", + item_type: "background", + description: "A fresh green field background that makes the egg feel light and peaceful.", + price: 140, + effect_type: "warmth", + effect_value: "4", + asset_url: "/assets/background/grass-bg.PNG", + is_active: 1 + }, + { + name: "nightstreet_bg", + item_type: "background", + description: "A quiet night street background lit by city lamps and reflections.", + price: 180, effect_type: "glow", - effect_value: "8", - asset_url: null, + effect_value: "7", + asset_url: "/assets/background/nightstreet-bg.png", is_active: 1 }, { - name: "Soft Piano Music", + name: "eternity_in_moments", item_type: "music", - description: "A peaceful piano track for the egg room.", - price: 120, + description: "A gentle track that stretches small memories into something lasting.", + price: 160, effect_type: "warmth", + effect_value: "5", + asset_url: "/assets/music/eternity-in-moments.m4a", + is_active: 1 + }, + { + name: "gold_phenomenon", + item_type: "music", + description: "A bright, shimmering track that gives the egg a golden pulse.", + price: 170, + effect_type: "glow", + effect_value: "6", + asset_url: "/assets/music/gold-phenomenon.m4a", + is_active: 1 + }, + { + name: "mi_querido", + item_type: "music", + description: "A warm melodic track with a tender and familiar mood.", + price: 170, + effect_type: "warmth", + effect_value: "6", + asset_url: "/assets/music/src_assets_mi-querido.m4a", + is_active: 1 + }, + { + name: "angelic", + item_type: "cosmetic", + description: "A soft halo effect that gives the egg a serene angelic glow.", + price: 220, + effect_type: "glow", + effect_value: "10", + asset_url: null, + is_active: 1 + }, + { + name: "beard", + item_type: "cosmetic", + description: "A playful beard that makes the egg look wise and patient.", + price: 120, + effect_type: "weight", effect_value: "4", asset_url: null, is_active: 1 }, { - name: "Forest Ambience Music", - item_type: "music", - description: "Relaxing forest ambience with birds and wind.", + name: "dirty_boots", + item_type: "cosmetic", + description: "Muddy little boots for an egg that has been out gathering memories.", price: 130, effect_type: "weight", - effect_value: "3", + effect_value: "5", asset_url: null, is_active: 1 }, { - name: "Tiny Crown Cosmetic", + name: "flower_crown", item_type: "cosmetic", - description: "A small crown decoration for your egg.", - price: 200, + description: "A crown of small flowers that adds a fresh and affectionate touch.", + price: 160, + effect_type: "warmth", + effect_value: "7", + asset_url: null, + is_active: 1 + }, + { + name: "glasses", + item_type: "cosmetic", + description: "Round glasses that give the egg a thoughtful, curious look.", + price: 140, effect_type: "glow", - effect_value: "10", + effect_value: "4", asset_url: null, is_active: 1 }, { - name: "Ribbon Cosmetic", + name: "life_buoy", item_type: "cosmetic", - description: "A cute ribbon decoration for your egg.", - price: 180, + description: "A bright life buoy that helps the egg feel safe and supported.", + price: 150, effect_type: "warmth", - effect_value: "7", + effect_value: "5", + asset_url: null, + is_active: 1 + }, + { + name: "on_fire", + item_type: "cosmetic", + description: "A lively flame effect for an egg filled with intense energy.", + price: 210, + effect_type: "glow", + effect_value: "12", + asset_url: null, + is_active: 1 + }, + { + name: "spinning_hat", + item_type: "cosmetic", + description: "A spinning hat that adds motion and a cheerful spark to the egg.", + price: 190, + effect_type: "glow", + effect_value: "8", + asset_url: null, + is_active: 1 + }, + { + name: "top_hat", + item_type: "cosmetic", + description: "A neat top hat that gives the egg a polished, classic style.", + price: 180, + effect_type: "weight", + effect_value: "3", + asset_url: null, + is_active: 1 + }, + { + name: "work_overall", + item_type: "cosmetic", + description: "Sturdy work overalls for an egg ready to build new memories.", + price: 170, + effect_type: "weight", + effect_value: "6", asset_url: null, is_active: 1 } @@ -219,4 +329,4 @@ async function update({ item_id, name, item_type, description, price, effect_typ return result.changes > 0; } -module.exports = {initDb, getAll, findById, findByName, create, remove, update}; \ No newline at end of file +module.exports = {initDb, getAll, findById, findByName, create, remove, update}; diff --git a/src/services/authService.js b/src/services/authService.js index 2d379f2..f6cfb70 100644 --- a/src/services/authService.js +++ b/src/services/authService.js @@ -152,7 +152,9 @@ async function getCurrentUser(user_id) { async function getInventory(user_id) { const userItems = await userItemModel.getAll(user_id); let items = []; - userItems.forEach(async (userItem) => { + let userItem; + for(let i = 0; i < userItems.length; i++){ + userItem = userItems[i]; let itemInfo = await shopItemModel.findById(userItem.item_id); if (itemInfo === undefined) { const error = new Error("Item is not found in Inventory"); @@ -161,8 +163,7 @@ async function getInventory(user_id) { return; } items.push(itemInfo); - }); - + } return { userItems, items }; } diff --git a/src/services/eggService.js b/src/services/eggService.js index 914f7a2..98ce50d 100644 --- a/src/services/eggService.js +++ b/src/services/eggService.js @@ -37,7 +37,7 @@ async function equip({ user_id, item_id }) { let prior = egg[`active_${itemType}_id`]; // if there is an item which is already equipped, unequip - if (prior != null && prior !== item_id) { + if (prior != null && prior != item_id) { let priorInventory = await userItemModel.findByIds(user_id, prior); if (priorInventory) { @@ -45,10 +45,12 @@ async function equip({ user_id, item_id }) { await userItemModel.update(priorInventory); } } - + if (prior != item_id){ + applyItemEffect(egg, await shopItemModel.findById(prior), 0); + applyItemEffect(egg, item, 1); + } egg[`active_${itemType}_id`] = itemInventory.item_id; itemInventory.is_equipped = 1; - // equip item let flagEgg = await eggModel.update(egg); let flagUserItem = await userItemModel.update(itemInventory); @@ -112,8 +114,8 @@ async function unequip({ user_id, item_id }) { egg[`active_${itemType}_id`] = null; itemInventory.is_equipped = 0; - - // equip item + // unequip item + applyItemEffect(egg, item, 1); let flagEgg = await eggModel.update(egg); let flagUserItem = await userItemModel.update(itemInventory); @@ -127,5 +129,15 @@ async function unequip({ user_id, item_id }) { } } +function applyItemEffect(egg, item, is_euip){ + let itemEffectType = item["effect_type"]; + let itemEffectValue = Number(item["effect_value"]); + if(is_euip){ + egg[itemEffectType] += itemEffectValue; + } else { + egg[itemEffectType] -= itemEffectValue; + } + return egg; +} module.exports = { equip, unequip }; diff --git a/src/services/shopService.js b/src/services/shopService.js index ca668a4..9ca7f96 100644 --- a/src/services/shopService.js +++ b/src/services/shopService.js @@ -68,10 +68,9 @@ async function purchaseItem({ user_id, item_id }) { transactionStarted = false; return { - user_id, - item_id, - item_name: item.name, - price: item.price + "userItems" : await userItemModel.findByIds(user_id, item_id), + "item" : item, + "willBalance": user.will_balance }; } catch (error) {