This commit is contained in:
Marces Zastrow
2025-01-17 14:41:56 +01:00
parent e6f017b0b3
commit 50c7c2c550
3 changed files with 143 additions and 41 deletions
Binary file not shown.
+47 -3
View File
@@ -362,6 +362,44 @@ app.put('/games/:gameId/:userId/character', (req, res) => {
stmt.finalize(); 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 // Fetch Player Items
app.get('/games/:gameId/:charId/items', (req, res) => { app.get('/games/:gameId/:charId/items', (req, res) => {
const gameId = req.params.gameId; const gameId = req.params.gameId;
@@ -424,13 +462,18 @@ app.post('/games/:charId/:itemId/owner', (req, res) => {
}) })
// Update Item details including owner // 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 itemId = req.params.itemId;
const { const {
ItemName, Type, Art, Rarity, MaxDurability, CurrentDurability, ItemName, Type, Art, Rarity, MaxDurability, CurrentDurability,
GoldValue, Abilities, OwnerID, AP GoldValue, Abilities, OwnerID, AP
} = req.body; } = req.body;
// Validate required fields
if (!ItemName) {
return res.status(400).json({ error: 'Item name is required' });
}
const stmt = db.prepare(` const stmt = db.prepare(`
UPDATE Item UPDATE Item
SET ItemName = ?, Type = ?, Art = ?, Rarity = ?, SET ItemName = ?, Type = ?, Art = ?, Rarity = ?,
@@ -440,8 +483,9 @@ app.put('/games/item/:itemId', async (req, res) => {
`); `);
stmt.run([ stmt.run([
ItemName, Type, Art, Rarity, MaxDurability, CurrentDurability, ItemName, Type, Art, Rarity,
GoldValue, Abilities, OwnerID, AP, itemId MaxDurability, CurrentDurability, GoldValue,
Abilities, OwnerID, AP, itemId
], function(err) { ], function(err) {
if (err) { if (err) {
console.error('Database error:', err); console.error('Database error:', err);
+95 -37
View File
@@ -105,44 +105,102 @@ const GameMasterPage = () => {
setIsEditing(true); setIsEditing(true);
}; };
// Update handleUpdate to fetch once after saving // Update the handleUpdate function
const handleUpdate = async () => { const handleUpdate = async () => {
try { try {
let response; let response;
if (editType === 'item') { if (editType === 'item') {
response = await axios.put(`http://localhost:5000/games/item/${selectedItem.ItemID}`, formData); const itemData = {
} else if (editType === 'npc') { ItemName: formData.ItemName,
response = await axios.put(`http://localhost:5000/games/npc/${selectedItem.NPCID}`, formData); Type: formData.Type,
} else if (editType === 'character') { Art: formData.Art,
response = await axios.put(`http://localhost:5000/games/character/${selectedItem.CharID}`, formData); Rarity: formData.Rarity,
} MaxDurability: formData.MaxDurability,
CurrentDurability: formData.CurrentDurability,
if (response.status === 200) { GoldValue: formData.GoldValue,
const fetchData = async () => { Abilities: formData.Abilities,
try { OwnerID: formData.OwnerID,
const pcsResponse = await axios.get(`http://localhost:5000/games/${gameId}/playerchars`); AP: formData.AP
const processedPCs = Array.isArray(pcsResponse.data) ? pcsResponse.data : [pcsResponse.data]; };
setPlayerCharacters(processedPCs.filter(pc => pc !== null)); response = await axios.put(`http://localhost:5000/games/item/${selectedItem.ItemID}`, itemData);
} else if (editType === 'npc') {
const npcsResponse = await axios.get(`http://localhost:5000/games/${gameId}/npcs`); // Update NPC data
const processedNPCs = Array.isArray(npcsResponse.data) ? npcsResponse.data : [npcsResponse.data]; const npcData = {
setNpcs(processedNPCs.filter(npc => npc !== null)); Name: formData.Name,
Race: formData.Race,
const itemsResponse = await axios.get(`http://localhost:5000/games/${gameId}/items`); Sex: formData.Sex,
const processedItems = Array.isArray(itemsResponse.data) ? itemsResponse.data : [itemsResponse.data]; Age: formData.Age,
setItems(processedItems.filter(item => item !== null)); Job: formData.Job,
} catch (error) { Description: formData.Description,
console.error('Error fetching data:', error); MaxHealth: formData.MaxHealth,
} CurrentHealth: formData.CurrentHealth,
}; MaxMana: formData.MaxMana,
await fetchData(); // Fetch once after successful update CurrentMana: formData.CurrentMana,
setEditModalOpen(false); Strength: formData.Strength,
setFormData(null); Dexterity: formData.Dexterity,
} Agility: formData.Agility,
} catch (error) { Endurance: formData.Endurance,
console.error('Error updating:', error); 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 = () => { const handleModalClose = () => {
setEditModalOpen(false); setEditModalOpen(false);