1
0
mirror of https://dev.s-ul.net/Galexion/MaiMaiDXNet.git synced 2024-11-24 05:50:12 +01:00
MaiMaiDXNet/dbhandler.js

333 lines
12 KiB
JavaScript

const { serverType, ArtConnSettings, aquaSrvDir } = require('./config.json');
const sqlite3 = require('sqlite3').verbose();
var db = undefined;
if (serverType === 0) {
db = new sqlite3.Database(aquaSrvDir + '\\db.sqlite');
}
const mysql = require('mysql');
//var con = mysql.createConnection(ArtConnSettings);
var pool = mysql.createPool(ArtConnSettings);
if (serverType === 1) {
function establishConnection() {
return new Promise((resolve, reject) => {
pool.getConnection(function(err, con) {
if (err) console.error("ERR: You lost a connection.\n" + err ); // not connected!
resolve(con);
});
});
}
}
// index.js route '/'
async function getUserCount() {
if (serverType === 0) {
return new Promise((resolve, reject) => {
db.all('SELECT * FROM sega_card', (err, rows) => {
if (err) {
console.error(err);
res.render('error', { error: err });
reject(err)
} else {
resolve(rows.length)
}
});
});
} else {
var con = await establishConnection();
return new Promise((resolve, reject) => {
con.query("SELECT * FROM aime.mai2_profile_detail", function (err, result, fields) {
if (err) {
reject(err)
throw err
};
con.release();
resolve(JSON.parse(JSON.stringify(result)).length);
});
});
}
}
// index.js & api.js /getUserData/
async function getUserData(req) {
if (serverType === 0) {
return new Promise((resolve, reject) => {
const cookies = req.cookies
db.all('SELECT * FROM maimai2_user_detail', (err, rows) => {
if (err) {
console.error(err);
// Return a Failed Message.
reject(err)
} else {
// Make the Request easier to get to.
var mUser = undefined // Leave the Matched User undefined until the user is found.
for (user of rows) { // For Each User in Rows
if (cookies.aime_card_id === user.aime_card_id.toString()) { // If the Access Code for the card is in the system,
mUser = user; // set the Matched User Variable to the User, and break the for Loop.
break;
}
}
if (mUser) {
// Return a Response with the whole identifiable user data. the EXT_ID will be used later to identify images and other things and match them to the user's profile.
resolve(mUser);
} else {
reject(new Error('User not found'));
}
}
});
}); // function is required to obtain the User Data, and create the user profile. I'm going to bed.
} else { // 07-23 oh god im going to have to recreate this function again aren't i
var con = await establishConnection();
return new Promise((resolve, reject) => {
con.query("SELECT * FROM aime.mai2_profile_detail", function (err, result, fields) {
if (err) {
reject(err)
throw err
};
// Make the Request easier to get to.
const cookies = req.cookies
var mUser = undefined // Leave the Matched User undefined until the user is found.
for (user of result) { // For Each User in Rows
if (cookies.aime_card_id === user.user.toString()) { // If the Access Code for the card is in the system,
mUser = user; // set the Matched User Variable to the User, and break the for Loop.
break;
}
}
if (mUser) {
// Return a Response with the whole identifiable user data. the EXT_ID will be used later to identify images and other things and match them to the user's profile.
con.release();
resolve(mUser);
} else {
reject(new Error('User not found'));
}
})
});
};
}
// api.js /getExtId/
async function getExtId(req) {
if (serverType === 0) {
return new Promise((resolve, reject) => {
db.all('SELECT * FROM sega_card', (err, rows) => {
if (err) {
console.error(err);
// Return a Failed Message.
reject(err)
} else {
// Make the Request easier to get to.
var request = req.body;
var mUser = undefined // Leave the Matched User undefined until the user is found.
for (user of rows) { // For Each User in Rows
if (request.input === user.luid) { // If the Access Code for the card is in the system,
mUser = user; // set the Matched User Variable to the User, and break the for Loop.
break
}
}
// Return a Response with the whole identifiable user data. the EXT_ID will be used later to identify images and other things and match them to the user's profile.
resolve(mUser)
}
});
});
} else {
var con = await establishConnection();
return new Promise((resolve, reject) => {
con.query("SELECT * FROM aime.aime_card", function (err, result, fields) {
if (err) {
reject(err)
throw err
};
// Make the Request easier to get to.
var request = req.body;
var mUser = undefined // Leave the Matched User undefined until the user is found.
for (user of result) { // For Each User in Rows
if (request.input === user.access_code) { // If the Access Code for the card is in the system,
mUser = user; // set the Matched User Variable to the User, and break the for Loop.
break;
}
}
if (mUser) {
// Return a Response with the whole identifiable user data. the EXT_ID will be used later to identify images and other things and match them to the user's profile.
con.release();
resolve(mUser);
} else {
reject(new Error('User not found'));
}
})
});
}
}
// api /getUserScores/
async function getUserScores(req) {
if (serverType === 0) {
return new Promise((resolve, reject) => {
db.all('SELECT * FROM maimai2_user_playlog', (err, rows) => {
if (err) {
console.error(err);
reject(err)
// Return a Failed Message.
} else {
// Make the Request easier to get to.
var request = req.body;
var mUser = new Array(); // Leave the Matched User's Scores undefined until the user is found.
for (score of rows) { // For Each Score in Rows
if (request.input == score.user_id) { // If the inputed User ID and Score's User ID Matches,
mUser.push(score) // add that score into the array.
}
}
// Return a Response with all the Scores listed under that user.
resolve(mUser)
}
});
});
} else {
var con = await establishConnection();
return new Promise((resolve, reject) => {
con.query("SELECT * FROM aime.mai2_playlog", function (err, result, fields) {
if (err) {
reject(err)
throw err
};
// Make the Request easier to get to.
const cookies = req.cookies
var mUser = undefined // Leave the Matched User undefined until the user is found.
// Make the Request easier to get to.
var request = req.body;
var mUser = new Array(); // Leave the Matched User's Scores undefined until the user is found.
for (score of result) { // For Each Score in Rows
if (request.input == score.user) { // If the inputed User ID and Score's User ID Matches,
mUser.push(score) // add that score into the array.
}
}
// Return a Response with all the Scores listed under that user.
con.release();
resolve(mUser);
})
});
}
}
// api /getUserArea/
async function getUserArea(req) {
if (serverType === 0) {
return new Promise((resolve, reject) => {
db.all('SELECT * FROM maimai2_user_map', (err, rows) => {
if (err) {
console.error(err);
// Return a Failed Message.
reject(err)
} else {
// Make the Request easier to get to.
var request = req.body;
var mUser = new Array(); // Leave the Matched User's Areas undefined until the user is found.
for (map of rows) { // For Each Area in Rows
if (request.input == map.user_id) { // If the inputed User ID and Score's User ID Matches,
mUser.push(map) // add that Area into the array.
}
}
// Return a Response with all the Areas listed under that user.
resolve(mUser)
}
});
});
} else {
var con = await establishConnection();
return new Promise((resolve, reject) => {
con.query("SELECT * FROM aime.mai2_item_map", function (err, result, fields) {
if (err) {
reject(err)
throw err
};
// Make the Request easier to get to.
const cookies = req.cookies
var mUser = undefined // Leave the Matched User undefined until the user is found.
// Make the Request easier to get to.
var request = req.body;
var mUser = new Array(); // Leave the Matched User's Scores undefined until the user is found.
for (score of result) { // For Each Score in Rows
console.log(request.input)
if (request.input == score.user) { // If the inputed User ID and Score's User ID Matches,
mUser.push(score) // add that score into the array.
}
}
// Return a Response with all the Scores listed under that user.
con.release();
resolve(mUser)
})
});
}
}
/* side note: User Banned & User Unbanned should be tied into 1 commnand that toggles it but maybe not, i dont really know */
// api /getUserBanned/
async function getUserBanned(req) {
if (serverType === 0) {
return new Promise((resolve, reject) => {
var request = req.body;
if (request.input === undefined) {
return reject('Failed to update user ban state, insufficent paramaters');
}
db.run('UPDATE maimai2_user_detail SET ban_state = ? WHERE id = ?', [2, request.input], function (err) {
if (err) {
console.error(err.message);
reject({ "status": "failure", "message": "Unable to ban user, see reason", "reason": err });
} else {
resolve({ "status": "Success", "message": `User ${request.input} banned.` })
}
});
});
} else {
var con = await establishConnection();
return new Promise((resolve, reject) => {
var request = req.body;
con.query(`UPDATE aime.mai2_profile_detail SET banState = 2 WHERE user = ${request.input}`, function (err, result, fields) {
if (err) {
reject({ "status": "failure", "message": "Unable to ban user, see reason", "reason": err })
throw err
};
con.release();
resolve({ "status": "Success", "message": `User ${request.input} banned.` })
})
});
}
}
// api /getUserUnbanned/
async function getUserUnbanned(req) {
if (serverType === 0) {
return new Promise((resolve, reject) => {
var request = req.body;
if (request.input === undefined) {
return reject('Failed to update user ban state, insufficent paramaters')
return
}
db.run('UPDATE maimai2_user_detail SET ban_state = ? WHERE id = ?', [0, request.input], function (err) {
if (err) {
console.error(err.message);
reject({ "status": "failure", "message": "Unable to ban user, see reason", "reason": err });
} else {
resolve({ "status": "Success", "message": `User ${request.input} banned.` })
}
});
});
} else {
var con = await establishConnection();
return new Promise((resolve, reject) => {
var request = req.body;
con.query(`UPDATE aime.mai2_profile_detail SET banState = 0 WHERE user = ${request.input}`, function (err, result, fields) {
if (err) {
reject({ "status": "failure", "message": "Unable to ban user, see reason", "reason": err })
throw err
};
con.release();
resolve({ "status": "Success", "message": `User ${request.input} banned.` })
})
});
}
}
module.exports = { getUserCount, getUserData, getUserScores, getUserArea, getUserBanned, getUserUnbanned, getExtId };