diff --git a/backend/database.db b/backend/database.db
index e5224fb..8f87ded 100644
Binary files a/backend/database.db and b/backend/database.db differ
diff --git a/backend/server.js b/backend/server.js
index a02b803..e3942d0 100644
--- a/backend/server.js
+++ b/backend/server.js
@@ -6,14 +6,20 @@ const bodyParser = require('body-parser');
const crypto = require('crypto');
const multer = require('multer'); // Add this to your dependencies
const storage = multer.memoryStorage();
-const upload = multer({ storage: storage });
+const upload = multer({
+ storage: storage,
+ limits: {
+ fileSize: 50 * 1024 * 1024 // 50MB limit
+ }
+});
const app = express();
const port = 5000;
// Middleware
app.use(cors());
-app.use(bodyParser.json());
+app.use(bodyParser.json({limit: '50mb'})); // Increase JSON payload limit
+app.use(bodyParser.urlencoded({limit: '50mb', extended: true})); // Increase URL-encoded payload limit
// SQLite database setup
const db = new sqlite3.Database('./database.db', (err) => {
@@ -195,31 +201,46 @@ app.get('/games/:gameId/playerchars', (req, res) => {
// Fetch game NPCs
app.get('/games/:gameId/npcs', (req, res) => {
const gameId = req.params.gameId;
- db.all('SELECT * FROM NPC WHERE GameID = ?', [gameId], (err, row) => {
+ db.all('SELECT * FROM NPC WHERE GameID = ?', [gameId], (err, rows) => {
if (err) {
return res.status(500).json({ error: 'Internal server error' });
}
- res.json(row);
+ // Process each row and convert BLOB to base64 if image exists
+ const processedRows = rows.map(row => {
+ if (row.Img) {
+ row.Img = `data:image/jpeg;base64,${row.Img.toString('base64')}`;
+ }
+ return row;
+ });
+ res.json(processedRows);
});
-})
+});
// Fetch game Items
app.get('/games/:gameId/items', (req, res) => {
const gameId = req.params.gameId;
db.all(`
- SELECT Item.*, PlayerCharacter.CharName as OwnerName
- FROM Item
- LEFT JOIN PlayerCharacter ON Item.OwnerID = PlayerCharacter.CharID
- WHERE Item.GameID = ?`,
+ SELECT i.*,
+ COALESCE(pc.CharName, n.Name) as OwnerName
+ FROM Item i
+ LEFT JOIN PlayerCharacter pc ON i.OwnerID = pc.CharID
+ LEFT JOIN NPC n ON i.OwnerID = n.NPCID
+ WHERE i.GameID = ?`,
[gameId],
(err, rows) => {
if (err) {
return res.status(500).json({ error: 'Internal server error' });
}
- res.json(rows);
+ const processedRows = rows.map(row => {
+ if (row.img) {
+ row.Img = `data:image/jpeg;base64,${row.img.toString('base64')}`;
+ }
+ return row;
+ });
+ res.json(processedRows);
}
);
-})
+});
// Player Part
@@ -365,7 +386,7 @@ app.post('/games/item/create', upload.single('image'), (req, res) => {
const stmt = db.prepare(`
INSERT INTO Item (
ItemName, Type, Art, Rarity, MaxDurability, CurrentDurability,
- GoldValue, Abilities, GameID, OwnerID, img, AP
+ GoldValue, Abilities, GameID, OwnerID, img, AP
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?)
`);
@@ -398,6 +419,165 @@ app.post('/games/:charId/:itemId/owner', (req, res) => {
});
})
+// Update Item details including owner
+app.put('/games/item/:itemId', async (req, res) => {
+ const itemId = req.params.itemId;
+ const {
+ ItemName, Type, Art, Rarity, MaxDurability, CurrentDurability,
+ GoldValue, Abilities, OwnerID, AP
+ } = req.body;
+
+ const stmt = db.prepare(`
+ UPDATE Item
+ SET ItemName = ?, Type = ?, Art = ?, Rarity = ?,
+ MaxDurability = ?, CurrentDurability = ?, GoldValue = ?,
+ Abilities = ?, OwnerID = ?, AP = ?
+ WHERE ItemID = ?
+ `);
+
+ stmt.run([
+ ItemName, Type, Art, Rarity, MaxDurability, CurrentDurability,
+ GoldValue, Abilities, OwnerID, AP, itemId
+ ], 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: 'Item not found' });
+ }
+ res.json({ message: 'Item updated successfully!' });
+ });
+ stmt.finalize();
+});
+
+//NPC part
+
+// Create NPC
+app.post('/games/npc/create', upload.single('image'), (req, res) => {
+ const {
+ Name, Race, Sex, Age, Job, Description,
+ MaxHealth, MaxMana, Strength, Dexterity,
+ Agility, Endurance, Allied, Level, GameID
+ } = req.body;
+
+ const imageBuffer = req.file ? req.file.buffer : null;
+
+ const stmt = db.prepare(`
+ INSERT INTO NPC (
+ GameID, Name, Race, Sex, Age, Job, Description,
+ MaxHealth, CurrentHealth, MaxMana, CurrentMana,
+ Strength, Dexterity, Agility, Endurance,
+ Level, Allied, Img
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
+ `);
+
+ stmt.run([
+ GameID, Name, Race, Sex, Age, Job, Description,
+ MaxHealth, MaxHealth, MaxMana, MaxMana,
+ Strength, Dexterity, Agility, Endurance,
+ Level, Allied, imageBuffer
+ ], function(err) {
+ if (err) {
+ console.error('Database error:', err);
+ return res.status(500).json({ error: 'Internal server error' });
+ }
+ res.status(201).json({
+ message: 'NPC created successfully!',
+ npcId: this.lastID
+ });
+ });
+ stmt.finalize();
+});
+
+// Update NPC details
+app.put('/games/npc/:npcId', upload.single('image'), (req, res) => {
+ const npcId = req.params.npcId;
+ const {
+ Name, Race, Sex, Age, Job, Description,
+ MaxHealth, CurrentHealth, MaxMana, CurrentMana,
+ Strength, Dexterity, Agility, Endurance,
+ Level, Allied
+ } = req.body;
+
+ let updateQuery = `
+ UPDATE NPC SET
+ Name = ?, Race = ?, Sex = ?, Age = ?, Job = ?,
+ Description = ?, MaxHealth = ?, CurrentHealth = ?,
+ MaxMana = ?, CurrentMana = ?, Strength = ?,
+ Dexterity = ?, Agility = ?, Endurance = ?,
+ Level = ?, Allied = ?
+ `;
+
+ let params = [
+ Name, Race, Sex, Age, Job, Description,
+ MaxHealth, CurrentHealth, MaxMana, CurrentMana,
+ Strength, Dexterity, Agility, Endurance,
+ Level, Allied
+ ];
+
+ // If new image is uploaded, add it to the update
+ if (req.file) {
+ updateQuery += `, Img = ?`;
+ params.push(req.file.buffer);
+ }
+
+ updateQuery += ` WHERE NPCID = ?`;
+ params.push(npcId);
+
+ const stmt = db.prepare(updateQuery);
+ stmt.run(params, 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: 'NPC not found' });
+ }
+ res.json({ message: 'NPC updated successfully!' });
+ });
+ stmt.finalize();
+});
+
+// Get NPC details
+app.get('/games/npc/:npcId', (req, res) => {
+ const npcId = req.params.npcId;
+ db.get('SELECT * FROM NPC WHERE NPCID = ?', [npcId], (err, row) => {
+ if (err) {
+ return res.status(500).json({ error: 'Internal server error' });
+ }
+ if (!row) {
+ return res.status(404).json({ error: 'NPC not found' });
+ }
+
+ // Convert BLOB to base64 string if image exists
+ if (row.Img) {
+ row.Img = `data:image/jpeg;base64,${row.Img.toString('base64')}`;
+ }
+
+ res.json(row);
+ });
+});
+
+// Update NPC image
+app.put('/games/npc/:npcId/image', upload.single('image'), (req, res) => {
+ const npcId = req.params.npcId;
+ const imageBuffer = req.file.buffer;
+
+ const stmt = db.prepare('UPDATE NPC SET Img = ? WHERE NPCID = ?');
+ stmt.run([imageBuffer, npcId], 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: 'NPC not found' });
+ }
+ res.json({ message: 'NPC image updated successfully!' });
+ });
+ stmt.finalize();
+});
+
diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx
index 08b941e..8da0223 100644
--- a/frontend/src/App.jsx
+++ b/frontend/src/App.jsx
@@ -13,6 +13,7 @@ import Games from './pages/games.jsx';
import GameMasterPage from './pages/gameMasterPage.jsx';
import CreateItem from './pages/createItem.jsx';
import CreateCharacter from './pages/createCharacter.jsx';
+import CreateNpc from './pages/createNpc.jsx';
import { UserProvider } from './context/UserContext.jsx';
function App() {
@@ -79,7 +80,11 @@ function App() {
path='/create-item'
element={}
/>
-
+ }
+ />
+
diff --git a/frontend/src/pages/createItem.jsx b/frontend/src/pages/createItem.jsx
index a132c1d..ad00689 100644
--- a/frontend/src/pages/createItem.jsx
+++ b/frontend/src/pages/createItem.jsx
@@ -89,6 +89,18 @@ const CreateItem = () => {
return (
+
+