diff --git a/backend/database.db b/backend/database.db index 98c3653..47b36cc 100644 Binary files a/backend/database.db and b/backend/database.db differ diff --git a/backend/server.js b/backend/server.js index 40d6087..8327bf0 100644 --- a/backend/server.js +++ b/backend/server.js @@ -362,6 +362,44 @@ app.put('/games/:gameId/:userId/character', (req, res) => { stmt.finalize(); }); +// Update Player Character +app.put('/games/character/:charId', async (req, res) => { + const charId = req.params.charId; + const { + charName, race, sex, age, job, description, + maxHealth, currentHealth, maxMana, currentMana, + strength, dexterity, agility, endurance, + level, gold + } = req.body; + + const stmt = db.prepare(` + UPDATE PlayerCharacter + SET CharName = ?, Race = ?, Sex = ?, Age = ?, Job = ?, + Description = ?, MaxHealth = ?, CurrentHealth = ?, + MaxMana = ?, CurrentMana = ?, Strength = ?, + Dexterity = ?, Agility = ?, Endurance = ?, + Level = ?, Gold = ? + WHERE CharID = ? + `); + + stmt.run([ + charName, race, sex, age, job, description, + maxHealth, currentHealth, maxMana, currentMana, + strength, dexterity, agility, endurance, + level, gold, charId + ], function(err) { + if (err) { + console.error('Database error:', err); + return res.status(500).json({ error: 'Internal server error' }); + } + if (this.changes === 0) { + return res.status(404).json({ error: 'Character not found' }); + } + res.json({ message: 'Character updated successfully!' }); + }); + stmt.finalize(); +}); + // Fetch Player Items app.get('/games/:gameId/:charId/items', (req, res) => { const gameId = req.params.gameId; @@ -424,13 +462,18 @@ app.post('/games/:charId/:itemId/owner', (req, res) => { }) // Update Item details including owner -app.put('/games/item/:itemId', async (req, res) => { +app.put('/games/item/:itemId', (req, res) => { const itemId = req.params.itemId; const { ItemName, Type, Art, Rarity, MaxDurability, CurrentDurability, GoldValue, Abilities, OwnerID, AP } = req.body; - + + // Validate required fields + if (!ItemName) { + return res.status(400).json({ error: 'Item name is required' }); + } + const stmt = db.prepare(` UPDATE Item SET ItemName = ?, Type = ?, Art = ?, Rarity = ?, @@ -440,8 +483,9 @@ app.put('/games/item/:itemId', async (req, res) => { `); stmt.run([ - ItemName, Type, Art, Rarity, MaxDurability, CurrentDurability, - GoldValue, Abilities, OwnerID, AP, itemId + ItemName, Type, Art, Rarity, + MaxDurability, CurrentDurability, GoldValue, + Abilities, OwnerID, AP, itemId ], function(err) { if (err) { console.error('Database error:', err); diff --git a/frontend/src/pages/gameMasterPage.jsx b/frontend/src/pages/gameMasterPage.jsx index afad57e..803c7a6 100644 --- a/frontend/src/pages/gameMasterPage.jsx +++ b/frontend/src/pages/gameMasterPage.jsx @@ -105,44 +105,102 @@ const GameMasterPage = () => { setIsEditing(true); }; - // Update handleUpdate to fetch once after saving - const handleUpdate = async () => { - try { - let response; - if (editType === 'item') { - response = await axios.put(`http://localhost:5000/games/item/${selectedItem.ItemID}`, formData); - } else if (editType === 'npc') { - response = await axios.put(`http://localhost:5000/games/npc/${selectedItem.NPCID}`, formData); - } else if (editType === 'character') { - response = await axios.put(`http://localhost:5000/games/character/${selectedItem.CharID}`, formData); - } - - if (response.status === 200) { - const fetchData = async () => { - try { - const pcsResponse = await axios.get(`http://localhost:5000/games/${gameId}/playerchars`); - const processedPCs = Array.isArray(pcsResponse.data) ? pcsResponse.data : [pcsResponse.data]; - setPlayerCharacters(processedPCs.filter(pc => pc !== null)); - - const npcsResponse = await axios.get(`http://localhost:5000/games/${gameId}/npcs`); - const processedNPCs = Array.isArray(npcsResponse.data) ? npcsResponse.data : [npcsResponse.data]; - setNpcs(processedNPCs.filter(npc => npc !== null)); - - const itemsResponse = await axios.get(`http://localhost:5000/games/${gameId}/items`); - const processedItems = Array.isArray(itemsResponse.data) ? itemsResponse.data : [itemsResponse.data]; - setItems(processedItems.filter(item => item !== null)); - } catch (error) { - console.error('Error fetching data:', error); - } - }; - await fetchData(); // Fetch once after successful update - setEditModalOpen(false); - setFormData(null); - } - } catch (error) { - console.error('Error updating:', error); + // Update the handleUpdate function +const handleUpdate = async () => { + try { + let response; + if (editType === 'item') { + const itemData = { + ItemName: formData.ItemName, + Type: formData.Type, + Art: formData.Art, + Rarity: formData.Rarity, + MaxDurability: formData.MaxDurability, + CurrentDurability: formData.CurrentDurability, + GoldValue: formData.GoldValue, + Abilities: formData.Abilities, + OwnerID: formData.OwnerID, + AP: formData.AP + }; + response = await axios.put(`http://localhost:5000/games/item/${selectedItem.ItemID}`, itemData); + } else if (editType === 'npc') { + // Update NPC data + const npcData = { + Name: formData.Name, + Race: formData.Race, + Sex: formData.Sex, + Age: formData.Age, + Job: formData.Job, + Description: formData.Description, + MaxHealth: formData.MaxHealth, + CurrentHealth: formData.CurrentHealth, + MaxMana: formData.MaxMana, + CurrentMana: formData.CurrentMana, + Strength: formData.Strength, + Dexterity: formData.Dexterity, + Agility: formData.Agility, + Endurance: formData.Endurance, + Level: formData.Level, + Allied: formData.Allied + }; + response = await axios.put(`http://localhost:5000/games/npc/${selectedItem.NpcID}`, npcData); + } else if (editType === 'character') { + // Update Player Character data + const charData = { + charName: formData.CharName, + race: formData.Race, + sex: formData.Sex, + age: formData.Age, + job: formData.Job, + description: formData.Description, + maxHealth: formData.MaxHealth, + currentHealth: formData.CurrentHealth, + maxMana: formData.MaxMana, + currentMana: formData.CurrentMana, + strength: formData.Strength, + dexterity: formData.Dexterity, + agility: formData.Agility, + endurance: formData.Endurance, + level: formData.Level, + gold: formData.Gold + }; + response = await axios.put(`http://localhost:5000/games/character/${selectedItem.CharID}`, charData); } - }; + + if (response.status === 200) { + // Fetch updated data after successful update + const fetchData = async () => { + try { + // Fetch player characters + const pcsResponse = await axios.get(`http://localhost:5000/games/${gameId}/playerchars`); + setPlayerCharacters( + Array.isArray(pcsResponse.data) ? pcsResponse.data : [pcsResponse.data] + ); + + // Fetch NPCs + const npcsResponse = await axios.get(`http://localhost:5000/games/${gameId}/npcs`); + setNpcs( + Array.isArray(npcsResponse.data) ? npcsResponse.data : [npcsResponse.data] + ); + + // Fetch items + const itemsResponse = await axios.get(`http://localhost:5000/games/${gameId}/items`); + setItems( + Array.isArray(itemsResponse.data) ? itemsResponse.data : [itemsResponse.data] + ); + } catch (error) { + console.error('Error fetching updated data:', error); + } + }; + + await fetchData(); + setEditModalOpen(false); + setFormData(null); + } + } catch (error) { + console.error('Error updating:', error); + } +}; const handleModalClose = () => { setEditModalOpen(false);