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