Untitled
unknown
plain_text
a year ago
23 kB
10
Indexable
export const getDataPlantCareAllNew_V3 = async (req: Request, res: Response, next: NextFunction) => { try { const { seedId, listLandId, typeLand, type, supportId } = req.body if (!listLandId || !type || listLandId.length <= 0 || !typeLand || typeof typeLand !== 'number' || typeLand <= 0 || typeLand > 2) { return next(await getCustomErrorMessage(res.locals.lang, 1)) } const validTypes = ['plant', 'harvest', 'watered', 'worm', 'fertilizer'] if (!validTypes.includes(type)) { return next(await getCustomErrorMessage(res.locals.lang, 1)) } const userData = res.locals.user const now = new Date().getTime() const now1 = new Date() var userPlants; // Acquire a lock. let lock = await getLockStorage(userData.id); if (typeLand == 2) { userPlants = await getUserPlantsLongTermUtils(userData.id, userData) } else { userPlants = await getUserPlantsUtils(userData.id, userData.slots, userData) } const seasonAndWeather = await postRequest(BASE_DB_API + '/getSeason', {}) // console.log(seasonAndWeather) // Trồng cây - complete if (seedId && type === 'plant') { // Release the lock. await lock.unlock(); // let userSeeds: any = await getUserSeedsUtils(userData.id) // const findIndex = userSeeds.findIndex(seed => seed.seedId === seedId) let Seeds: any = await getListPlantRedis(); const findIndex = Seeds.findIndex(seed => seed.id === seedId) if (findIndex === -1) { return next(await getCustomErrorMessage(res.locals.lang, 217)) } const plantInfo = await getPlantInfoRedis(seedId); let itemRes = [] for (const itemId of listLandId) { // for (const item of userPlants[0].data) { if (!itemId) continue const item = userPlants.find(i => i.id === itemId) // if (!listLandId.includes(item.id)) continue if (!item) continue else { if (item.plantId) { continue } // if (userSeeds[findIndex].quantity <= 0) { // continue // } if (userData.gold < Seeds[findIndex].buyPrice) { continue } let extra; try { extra = await processPlantCare(plantInfo, seasonAndWeather) } catch (e) { return next(await getCustomErrorMessage(res.locals.lang, 253)) } const newPlant = createNewUserPlantNew(plantInfo, extra) item.plantId = newPlant.plantId item.growthAt = newPlant.growthAt item.middleStatusAt = newPlant.middleStatusAt item.waterTimes = newPlant.waterTimes item.nextWater = newPlant.nextWater item.bugTimes = newPlant.bugTimes item.nextWorm = newPlant.nextWorm item.isTree = 0 item.isWater = false item.isWorm = false item.properties = plantInfo // userSeeds[findIndex].quantity -= 1 userData.gold -= Seeds[findIndex].buyPrice itemRes.push(item) } } if (itemRes.length > 0) { // if (userSeeds[findIndex].quantity <= 0) { // userSeeds.splice(findIndex, 1) // } // await updateUserSeeds(userData.id, JSON.stringify(userSeeds)) // postRequest("/UserSeed/save", userSeeds).catch(err => console.error(err)) userData.lastUpdatedAt = new Date() const updateUser = await postRequest(BASE_DB_API + '/user/update', userData) if (!updateUser || updateUser.errorStatus === 1) { // Release the lock. await lock.unlock(); return next(await getCustomErrorMessage(res.locals.lang, 500)) } if (typeLand == 2) { // await updateUserPlantsLongTerm(userData.id, JSON.stringify(userPlants)) itemRes = await convertlandLongdata2(itemRes) await postRequest("/UserLands/save", userPlants).catch(err => console.error(err)) } else { // await updateUserPlants(userData.id, JSON.stringify(userPlants)) itemRes = await convertlandShortdata(itemRes) await postRequest("/UserLands/save", userPlants).catch(err => console.error(err)) } } return res.json(BaseResponse(102, { content: await getCustomSuccessMessage(res.locals.lang, 16, []), tree: itemRes }, 'Trồng cây')) } // Thu hoạch - process if (type === 'harvest') { let capacityCurr = await getTotalCapacityUserStorage(userData.id) const DataStoreConfig = await getStoreInfoRedis(userData.levelStore) let userStorage: any = await getUserStorageUtils(userData.id) // userStorage = JSON.parse(userStorage) || [] let itemRes = [] let eventRank = [] for (const itemId of listLandId) { // for (const item of userPlants[0].data) { if (!itemId) continue const item = userPlants.find(i => i.id === itemId) // if (!listLandId.includes(item.id)) continue if (!item) continue else { if (!item.plantId) { continue } if (item.isTree !== 2) { continue } // const productName = item.properties.nameFruit // const extra = await processPlantCareHarvest(item.tree.properties, seasonAndWeather) // let productQuantity = Math.floor(item.tree.properties.quantity * (1 - totalDecreased)) + extra let productQuantity = item.properties.quantity if (DataStoreConfig.capacity < (productQuantity + capacityCurr)) { continue } let expReceive = item.properties.expGain userData.exp += expReceive if (productQuantity > 0) { const findIndex = userStorage.findIndex(item2 => item2.typeOfStorage === `PLANT_${item.properties.id}`) if (findIndex === -1) { userStorage.push({ userId: userData.id, typeOfStorage: `PLANT_${item.properties.id}`, quantity: productQuantity }) } else { userStorage[findIndex].quantity += productQuantity } const rankIndex = eventRank.findIndex(item2 => item2.type === item.properties.type) if (rankIndex === -1) eventRank.push({ type: item.properties.type, quantity: productQuantity }) else eventRank[rankIndex] = { type: item.properties.type, quantity: eventRank[rankIndex].quantity + productQuantity } } if (typeLand == 2) { // thêm code thời tiết. // if (!item.tree.inSeason) { // item.properties = await getPlantInfoRedis(item.properties.id); // item.properties.wateringTime = JSON.parse(item.properties.wateringTime) // item.waterDuration = (item.properties.wateringTime[1]) ? secondToMillisecond(item.properties.wateringTime[1]) : 24 * 3600 * 1000 // } // if (!item.tree.inFavorableWeather && !item.tree.inUnfavorableWeather) { // item.tree.properties = await getPlantInfoRedis(item.tree.properties.id); // } // const extraInfo = await processPlantCare(item.tree.properties, seasonAndWeather) // item.tree.properties.waterTimes += extraInfo.extraWaterTimes // item.tree.properties.bugTimes += extraInfo.extraBugTimes // item.tree.properties.growthTime += (Math.ceil((item.tree.properties.growthTime * extraInfo.extraHarvestPeriod) / 100)) item.growthAt = now + secondToMillisecond(item.properties.growthTime) //now + Math.ceil(secondToMillisecond(item.tree.properties.growthTime) / 2) item.middleStatusAt = Math.floor(now + secondToMillisecond(item.properties.growthTime) / 2) //now // item.tree.nextWater = Math.ceil(now + secondToMillisecond(item.tree.properties.growthTime) / (item.tree.properties.waterTimes + 1)) item.nextWater = (item.properties.wateringTime[0]) ? Math.ceil(now + secondToMillisecond(item.properties.wateringTime[0])) : Math.ceil(now + 24 * 3600 * 1000) item.nextWorm = Math.ceil(now + secondToMillisecond(item.properties.growthTime) / (item.properties.bugTimes + 1)) item.isTree = 1 // item.decreased = 0 item.waterTimes = 0 item.bugTimes = 0 } else { // item.tree = null delete item.properties delete item.supportItem item.plantId = null item.growthAt = null item.middleStatusAt = null item.waterTimes = null item.nextWater = null item.bugTimes = null item.nextWorm = null item.supportItemId = null item.supportItemLimit = null } let dataItem = { ...item } dataItem.count = productQuantity itemRes.push(dataItem) // console.log(userStorage) } } const IuserInfo = await getCurrUserInfo(userData) if (itemRes.length > 0) { // console.log(userStorage) // await updateUserStorage(userData.id, JSON.stringify(userStorage)) await postRequest("/UserStorage/save", userStorage).catch(err => console.error(err)) //new imcEventRank(userData, eventRank).catch(err => console.error(err)) doMissionWatering(userData, "Harvest", eventRank).catch(err => console.error(err)) doMissionDecoration(userData, "Harvest", eventRank).catch(err => console.error(err)) saveLogHarvest(userData, eventRank).catch(err => console.error(err)) userData.lastUpdatedAt = new Date() const updateUser = await postRequest(BASE_DB_API + '/user/update', userData) if (!updateUser || updateUser.errorStatus === 1) { // Release the lock. await lock.unlock(); return next(await getCustomErrorMessage(res.locals.lang, 500)) } if (typeLand == 2) { // await updateUserPlantsLongTerm(userData.id, JSON.stringify(userPlants)) await postRequest("/UserLands/save", userPlants).catch(err => console.error(err)) itemRes = await convertlandLongdata2(itemRes) } else { // await updateUserPlants(userData.id, JSON.stringify(userPlants)) await postRequest("/UserLands/save", userPlants).catch(err => console.error(err)) itemRes = await convertlandShortdata(itemRes) } } // Release the lock. await lock.unlock(); return res.json(BaseResponse(105, { content: await getCustomSuccessMessage(res.locals.lang, 7, []), tree: itemRes, dataPlayer: IuserInfo, // count: productQuantity }, 'Thu hoạch')) } // Release the lock. await lock.unlock(); // Bắt sâu - complete if (type === 'worm') { let itemRes = [] let totalWorm = 0 const listConfig = await getListGameConfigRedis() const priceCatchBug = listConfig.find(item => item.key === "priceCatchBug") // for (const item of userPlants[0].data) { for (const itemId of listLandId) { if (!itemId) continue const item = userPlants.find(i => i.id === itemId) // if (!listLandId.includes(item.id)) continue if (!item) continue else { if (!item.plantId) { continue } if (!item.isWorm) { continue } // if (!item.tree.wormDuration) item.tree.wormDuration = Math.ceil(secondToMillisecond(item.tree.properties.growthTime) / (item.tree.properties.bugTimes + 1)) while (item.nextWorm <= now) { item.nextWorm += Math.ceil(secondToMillisecond(item.properties.growthTime) / (item.properties.bugTimes + 1)) } if (userData.gold < parseInt(priceCatchBug.value)) { continue } item.bugTimes += 1 totalWorm++ item.isWorm = (now > item.nextWorm) userData.gold -= parseInt(priceCatchBug.value) itemRes.push(item) } } const IuserInfo = await getCurrUserInfo(userData) if (itemRes.length > 0) { userData.lastUpdatedAt = new Date() let updateUser = await postRequest(BASE_DB_API + '/user/update', userData) if (!updateUser || updateUser.errorStatus === 1) { return next(await getCustomErrorMessage(res.locals.lang, 500)) } if (typeLand == 2) { // await updateUserPlantsLongTerm(userData.id, JSON.stringify(userPlants)); await postRequest("/UserLands/save", userPlants).catch(err => console.error(err)) itemRes = await convertlandLongdata2(itemRes) } else { // await updateUserPlants(userData.id, JSON.stringify(userPlants)) await postRequest("/UserLands/save", userPlants).catch(err => console.error(err)) itemRes = await convertlandShortdata(itemRes) } doMissionWatering(userData, "CatchWorm", { quantity: totalWorm }).catch(err => console.error(err)) } return res.json(BaseResponse(103, { content: await getCustomSuccessMessage(res.locals.lang, 8, []), tree: itemRes, dataPlayer: IuserInfo }, 'Bắt sâu')) } // Tưới nước - complete if (type === 'watered') { let itemRes = [] // const listConfig = await getListGameConfigRedis() // const priceForWateringPlant = listConfig.find(item => item.key === "priceForWateringPlant") let totalWater = 0 for (const itemId of listLandId) { if (!itemId) continue const item = userPlants.find(i => i.id === itemId) // if (!listLandId.includes(item.id)) continue if (!item) continue else { if (!item.plantId) { continue } if (!item.isWater) { continue } // if (!item.waterDuration) item.waterDuration = 24 * 3600 * 1000 if (item.nextWater <= now) { const wateringTime = JSON.parse(item.properties.wateringTime).map(i => { return (Math.ceil((i))) }) // console.log(now) // console.log((wateringTime[item.waterTimes + 1]) ? secondToMillisecond(wateringTime[item.waterTimes + 1]) : (24 * 3600 * 1000)) const time = (wateringTime[item.waterTimes + 1]) ? secondToMillisecond(wateringTime[item.waterTimes + 1]) : (24 * 3600 * 1000) item.nextWater = now + time } // console.log(item) // if (userData.gold < parseInt(priceForWateringPlant.value)) { // continue // } if (!userData.water || userData.water.totalWater <= 0) { continue } item.waterTimes += 1 item.isWater = (now > item.nextWater) //new // item.tree.waterDuration = (item.tree.properties.wateringTime[item.tree.waterTimes + 1]) ? secondToMillisecond(item.tree.properties.wateringTime[item.tree.waterTimes + 1]) : 24 * 3600 * 1000 item.growthAt -= Math.floor(0.1 * (item.growthAt - now)) // userData.gold -= parseInt(priceForWateringPlant.value) userData.water.totalWater -= 1 totalWater++ itemRes.push(item) } } if (itemRes.length > 0) { // let updateUser = await postRequest(BASE_DB_API + '/user/update', userData) // if (!updateUser || updateUser.errorStatus === 1) { // return next(await getCustomErrorMessage(res.locals.lang, 500)) // } await postRequest("/water/save", userData.water) //new doMissionWatering(userData, "Watering", { quantity: totalWater }).catch(err => console.error(err)) if (typeLand == 2) { // await updateUserPlantsLongTerm(userData.id, JSON.stringify(userPlants)) await postRequest("/UserLands/save", userPlants).catch(err => console.error(err)) itemRes = await convertlandLongdata2(itemRes) } else { // await updateUserPlants(userData.id, JSON.stringify(userPlants)) await postRequest("/UserLands/save", userPlants).catch(err => console.error(err)) itemRes = await convertlandShortdata(itemRes) } } const IuserInfo = await getCurrUserInfo(userData) return res.json(BaseResponse(104, { content: await getCustomSuccessMessage(res.locals.lang, 9, []), tree: itemRes, dataPlayer: IuserInfo }, 'Tưới nước')) } // bón phân - complete if (supportId && type === 'fertilizer') { let itemRes = [] // const currentWeather = await getWeatherInfoRedis(seasonAndWeather.currentWeather) const SupportInfo = await getSupprtItemInfoRedis(supportId); if (SupportInfo.type == 2) { return next(await getCustomErrorMessage(res.locals.lang, 237)) } let UserSupport: any = await getUserSupportItemUtils(userData.id) // UserSupport = JSON.parse(UserSupport) || [] const findIndex = UserSupport.findIndex(item => item.supportItemId === supportId) if (findIndex === -1) { return next(await getCustomErrorMessage(res.locals.lang, 239)) } // for (const item of userPlants[0].data) { for (const itemId of listLandId) { if (!itemId) continue const item = userPlants.find(i => i.id === itemId) // if (!listLandId.includes(item.id)) continue if (!item) continue else { if (!item.plantId) { continue } if (item.growthAt <= now) { continue } if (UserSupport[findIndex].quantity === 0) continue let limit = 0; if (!item.supportItemId) { item.supportItemId = SupportInfo.id item.supportItemLimit = 0 } switch (SupportInfo.typeProduct) { case 'fertilizer5': { limit = item.supportItemLimit break; } case 'fertilizer15': { limit = item.supportItemLimit break; } case 'fertilizer60': { limit = item.supportItemLimit break; } } if (item.supportItemId && supportId != item.supportItemId) continue // if (item.tree.supportItem.nextSp && new Date(item.tree.supportItem.nextSp).getTime() > new Date().getTime()) continue if (limit >= SupportInfo.limitUseForDay) continue if (!item.supportItemId) { item.supportItemId = SupportInfo.id item.supportItem = SupportInfo } switch (SupportInfo.typeProduct) { case 'fertilizer5': { item.supportItemLimit += 1 // item.tree.supportItem.nextSp = new Date().getTime() + SupportInfo.fertilizingTime * 1000 break; } case 'fertilizer15': { item.supportItemLimit += 1 // item.tree.supportItem.nextSp = new Date().getTime() + SupportInfo.fertilizingTime * 1000 break; } case 'fertilizer60': { item.supportItemLimit += 1 // item.tree.supportItem.nextSp = new Date().getTime() + SupportInfo.fertilizingTime * 1000 break; } } // if (!currentWeather.disableSupportItem) item.growthAt -= Math.floor((SupportInfo.value / 100) * (item.growthAt - now)) item.growthAt -= Math.floor((SupportInfo.value / 100) * (item.growthAt - now)) const timeToGrowth = item.growthAt - now item.isTree = (timeToGrowth <= 0) ? 2 : (2 - Math.ceil(timeToGrowth / (secondToMillisecond(item.properties.growthTime) / 2))) item.isTree = item.isTree < 0 ? 0 : item.isTree // if (typeLand == 2) { // // await updateUserPlantsLongTerm(userData.id, JSON.stringify(userPlants)); // await postRequest("/UserLands/save", userPlants).catch(err => console.error(err)) // } else { // // await updateUserPlants(userData.id, JSON.stringify(userPlants)) // await postRequest("/UserLands/save", userPlants).catch(err => console.error(err)) // } UserSupport[findIndex].quantity -= 1 item.waterTimes += 1 item.isWater = (now > item.nextWater) //new // item.waterDuration = (item.properties.wateringTime[item.waterTimes + 1]) ? secondToMillisecond(item.properties.wateringTime[item.waterTimes + 1]) : 24 * 3600 * 1000 item.growthAt -= Math.floor(0.1 * (item.growthAt - now)) // totalWater++ itemRes.push(item) } } if (itemRes.length > 0) { // await updateUserSupportItem(userData.id, JSON.stringify(UserSupport)) await postRequest("/UserSupportItem/save", UserSupport).catch(err => console.error(err)) //new // doMission(userData, "Watering", { quantity: totalWater }).catch(err => console.error(err)) if (typeLand == 2) { // await updateUserPlantsLongTerm(userData.id, JSON.stringify(userPlants)) await postRequest("/UserLands/save", userPlants).catch(err => console.error(err)) itemRes = await convertlandLongdata2(itemRes) } else { // await updateUserPlants(userData.id, JSON.stringify(userPlants)) await postRequest("/UserLands/save", userPlants).catch(err => console.error(err)) itemRes = await convertlandShortdata(itemRes) } } const IuserInfo = await getCurrUserInfo(userData) return res.json(BaseResponse(108, { content: await getCustomSuccessMessage(res.locals.lang, 9, []), tree: itemRes, dataPlayer: IuserInfo }, 'bón phân')) } return next(await getCustomErrorMessage(res.locals.lang, 218)) } catch (error) { console.log(error) const response = await getCustomErrorMessage(res.locals.lang, 500) return next(response) } }
Editor is loading...
Leave a Comment