+
This commit is contained in:
Binary file not shown.
+192
-12
@@ -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();
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user