const [user] = await sqlQuery("SELECT * FROM user WHERE id = ? ", [rows[0].userId])
user.mats = JSON.parse(user.mats)
user.items = JSON.parse(user.items)
const itemObject = itemBase[data.itemBaseId - 1]
var canComplete = true
for (var i = 0; i <= itemObject.materials.length - 1; i++) {
const findMatIndex = user.mats.findIndex(obj => obj.id == itemObject.materials[i].id)
if (user.mats[findMatIndex] === undefined) { canComplete = false; break }
if (user.mats[findMatIndex].amount - itemObject.materials[i].amount < 0) { canComplete = false; break }
user.mats[findMatIndex].amount -= itemObject.materials[i].amount
}
if (canComplete === false) { targetConnection.emit('itemCraftResponse', { data: { message: 'not enought materials' } }); return }
const rows1 = await sqlQuery(
"INSERT INTO items (userId, name, image, rarity, slot, modifiers, sharpModifiers, sharp) VALUES (?,?,?,?,?,?,?,?)",
[user.id, itemObject.name, itemObject.image, itemObject.rarity, itemObject.slot, JSON.stringify(itemObject.modifiers), JSON.stringify(itemObject.sharpModifiers), 0]
)
user.items.push({ id: rows1.insertId })
console.log("userItems: " + JSON.stringify(user.items))
await sqlQuery("UPDATE user SET items = ?, mats = ? where id = ?", [JSON.stringify(user.items), JSON.stringify(user.mats), user.id])
targetConnection.emit('itemCraftResponse', { data: { message: 'itemCraft complete' } })
}
for (const itemMat of itemObject.materials) { const itemMat = itemObject.materials[i] const userMat = user.mats.find(obj => obj.id == itemMat.id) if (!userMat || (userMat.amount - itemMat.amount < 0)) { canComplete = false break } userMat.amount -= itemMat.amount }
Обсуждают сегодня