saveGameScore2
parent
9a64b60454
commit
2dec203ad0
|
@ -0,0 +1,13 @@
|
||||||
|
const mongoose = require("mongoose");
|
||||||
|
|
||||||
|
const gameSchema = new mongoose.Schema({
|
||||||
|
gameName: { type: String, required: true },
|
||||||
|
userId: { type: String, required: true },
|
||||||
|
gameID: { type: String, required: true },
|
||||||
|
gameTime: { type: Number, required: true },
|
||||||
|
score: { type: Number, required: true },
|
||||||
|
gameStar: { type: Number, required: true },
|
||||||
|
screenshotUrl: { type: String }, // Store the S3 URL of the screenshot
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = mongoose.model("Game", gameSchema);
|
|
@ -0,0 +1,60 @@
|
||||||
|
const AWS = require("aws-sdk");
|
||||||
|
const Game = require("../../models/gameModel"); // Import the Game model
|
||||||
|
|
||||||
|
// Configure AWS S3
|
||||||
|
const s3 = new AWS.S3({
|
||||||
|
accessKeyId: process.env.AWS_ACCESS_KEY_ID, // Add your AWS Access Key ID
|
||||||
|
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, // Add your AWS Secret Access Key
|
||||||
|
region: process.env.AWS_REGION, // Add your AWS Region
|
||||||
|
});
|
||||||
|
|
||||||
|
const saveGameScore2 = async (req, res) => {
|
||||||
|
const { userId, gameName, gameID, gameTime, score, gameStar, screenShot } = req.body;
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!gameName || !userId || !gameID) {
|
||||||
|
return res.status(400).json({ error: "Missing required fields" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save game data to MongoDB
|
||||||
|
const newGame = new Game({
|
||||||
|
gameName,
|
||||||
|
userId,
|
||||||
|
gameID,
|
||||||
|
gameTime,
|
||||||
|
score,
|
||||||
|
gameStar,
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await newGame.save();
|
||||||
|
|
||||||
|
if (screenShot) {
|
||||||
|
// Decode base64 image
|
||||||
|
const buffer = Buffer.from(screenShot, "base64");
|
||||||
|
|
||||||
|
// Upload to S3
|
||||||
|
const uploadParams = {
|
||||||
|
Bucket: process.env.S3_BUCKET_NAME, // Your S3 bucket name
|
||||||
|
Key: `images/${result._id}.png`, // Key for the image in S3
|
||||||
|
Body: buffer,
|
||||||
|
ContentType: "image/png", // Adjust if using a different image format
|
||||||
|
};
|
||||||
|
|
||||||
|
const s3Response = await s3.upload(uploadParams).promise();
|
||||||
|
|
||||||
|
// Update game document with screenshot URL
|
||||||
|
newGame.screenshotUrl = s3Response.Location;
|
||||||
|
await newGame.save();
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(201).json({
|
||||||
|
message: "Game data saved successfully",
|
||||||
|
data: newGame,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error:", error);
|
||||||
|
res.status(500).json({ error: "Internal Server Error" });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = saveGameScore2;
|
Loading…
Reference in New Issue