Initial commit
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
const Game = require("../../../Game/Game");
|
||||
|
||||
const MaintenanceScheduledPacket = require("../../../Packets/MaintenanceScheduled");
|
||||
|
||||
const isAuthenticated = require("../../../Utility/isAuthenticated");
|
||||
|
||||
module.exports = async (res, req) => {
|
||||
res.onAborted(() => res.isAborted = true);
|
||||
|
||||
const maintenanceTimeSeconds = req.getQuery("time");
|
||||
const timestamp = req.getQuery("timestamp");
|
||||
|
||||
const hasPermission = await isAuthenticated(res, req);
|
||||
if (!hasPermission) return;
|
||||
|
||||
if (!Game.listeningServer) return res.cork(() => res.writeStatus("400").end("Server is offline!"));
|
||||
if (!maintenanceTimeSeconds) return res.cork(() => res.writeStatus("400").end("No approximate time was provided!"));
|
||||
if (!timestamp) return res.cork(() => res.writeStatus("400").end("No timestamp was provided!"));
|
||||
|
||||
const parsedTimestamp = parseInt(timestamp);
|
||||
if (parsedTimestamp < Date.now()) return res.cork(() => res.writeStatus("400").end("Incorrect timestamp was provided!"));
|
||||
|
||||
const event = {
|
||||
fn: Game.disconnectAll.bind(Game),
|
||||
arguments: []
|
||||
};
|
||||
|
||||
Game.eventsFixed.add(parsedTimestamp, event);
|
||||
|
||||
const packet = MaintenanceScheduledPacket({
|
||||
timestamp: parsedTimestamp / 1000,
|
||||
timeSeconds: maintenanceTimeSeconds
|
||||
});
|
||||
|
||||
Game.maps.forEach(map => app.publish(map.id.toString(), packet, true, true));
|
||||
|
||||
res.cork(() => res
|
||||
.writeStatus("200")
|
||||
.end("Shutdown has been scheduled!"));
|
||||
};
|
||||
@@ -0,0 +1,20 @@
|
||||
const Game = require("../../../Game/Game");
|
||||
|
||||
const isAuthenticated = require("../../../Utility/isAuthenticated");
|
||||
|
||||
module.exports = async (res, req) => {
|
||||
res.onAborted(() => res.isAborted = true);
|
||||
|
||||
const state = req.getQuery("state");
|
||||
|
||||
const hasPermission = await isAuthenticated(res, req);
|
||||
if (!hasPermission) return;
|
||||
|
||||
if (!state) return res.cork(() => res.writeStatus("400").end("Missing new game state!"));
|
||||
|
||||
Game.state = parseInt(state);
|
||||
|
||||
res.cork(() => res
|
||||
.writeStatus("200")
|
||||
.end(`New game state is set to be ${state}`));
|
||||
};
|
||||
@@ -0,0 +1,73 @@
|
||||
const redis = require("../../../Miscellaneous/redis");
|
||||
const stripe = require("../../../Miscellaneous/stripe");
|
||||
|
||||
const entries = require("../../../Data/payment");
|
||||
|
||||
function badRequest(res) {
|
||||
console.trace("asd")
|
||||
res.cork(() => res
|
||||
.writeStatus("400")
|
||||
.end());
|
||||
};
|
||||
|
||||
module.exports = async (res, req) => {
|
||||
const paymentPacketID = req.getQuery("paymentPacketId");
|
||||
console.info("payment_intent");
|
||||
const cookieHeader = req.getHeader("cookie");
|
||||
const cookie = cookieHeader.split("ps=")[1];
|
||||
|
||||
res.onAborted(() => console.log("aborted"));
|
||||
|
||||
if (!cookie) return badRequest(res);
|
||||
|
||||
const paymentPacket = entries[paymentPacketID];
|
||||
if (!paymentPacketID || !paymentPacket) return badRequest(res);
|
||||
|
||||
const formattedCookie = cookie.split(".")[0].slice(4);
|
||||
const cookieInDB = await redis.get(`sess:${formattedCookie}`);
|
||||
|
||||
if (!cookieInDB) return badRequest(res);
|
||||
|
||||
const parsedCookie = JSON.parse(cookieInDB);
|
||||
const { accountID } = parsedCookie;
|
||||
|
||||
const existingClientSecret = await redis.get(`o_${accountID}_${paymentPacketID}`);
|
||||
let clientSecret = null;
|
||||
|
||||
try {
|
||||
const { amount, unit_amount } = paymentPacket;
|
||||
if (!existingClientSecret) {
|
||||
const paymentIntent = await stripe.paymentIntents.create({
|
||||
currency: "EUR",
|
||||
amount: unit_amount,
|
||||
automatic_payment_methods: {
|
||||
enabled: true
|
||||
},
|
||||
metadata: {
|
||||
paymentPacketID,
|
||||
playerID: accountID
|
||||
}
|
||||
});
|
||||
|
||||
clientSecret = paymentIntent.client_secret;
|
||||
await redis.set(`o_${accountID}_${paymentPacketID}`, clientSecret);
|
||||
} else {
|
||||
clientSecret = existingClientSecret;
|
||||
};
|
||||
|
||||
const response = JSON.stringify({
|
||||
clientSecret,
|
||||
amount,
|
||||
price: unit_amount
|
||||
});
|
||||
|
||||
res.cork(() => {
|
||||
res
|
||||
.writeStatus("200")
|
||||
.end(response);
|
||||
});
|
||||
} catch (e) {
|
||||
console.info(e)
|
||||
return badRequest(res);
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,75 @@
|
||||
const RewardPacket = require("../../../Packets/Reward");
|
||||
|
||||
const Game = require("../../../Game/Game");
|
||||
|
||||
const stripe = require("../../../Miscellaneous/stripe");
|
||||
const redis = require("../../../Miscellaneous/redis");
|
||||
|
||||
const entries = require("../../../Data/payment");
|
||||
const STRIPE_IP_COLLECTION = require("../../../Data/stripe");
|
||||
|
||||
require("dotenv").config({ path: "../.env" });
|
||||
|
||||
const textDecoder = new TextDecoder();
|
||||
|
||||
const PAYMENT_INTENT_SUCCEEDED = "payment_intent.succeeded";
|
||||
|
||||
function badRequest(res) {
|
||||
res
|
||||
.writeStatus("400")
|
||||
.end();
|
||||
};
|
||||
|
||||
module.exports = async (res, req) => {
|
||||
const sig = req.getHeader("stripe-signature");
|
||||
if (!sig) return badRequest(res);
|
||||
|
||||
const realIP = req.getHeader("x-real-ip");
|
||||
if (!STRIPE_IP_COLLECTION[realIP]) return badRequest(res);
|
||||
|
||||
res.onAborted(() => console.log("/webhook path aborted connection!"));
|
||||
|
||||
let data;
|
||||
|
||||
// it works because there is no multiple chunks, but should address this issue
|
||||
res.onData(async (raw, isLast) => {
|
||||
const stringData = textDecoder.decode(raw);
|
||||
|
||||
try {
|
||||
data = stripe.webhooks.constructEvent(stringData, sig, process.env.STRIPE_WEBHOOK_ENDPOINT_SECRET);
|
||||
} catch (err) {
|
||||
console.log(err.message)
|
||||
return badRequest(res);
|
||||
};
|
||||
|
||||
if (data.type === PAYMENT_INTENT_SUCCEEDED) {
|
||||
const { paymentPacketID, playerID } = data.data.object.metadata;
|
||||
if (!paymentPacketID || !playerID) return badRequest(res);
|
||||
|
||||
const paymentPackage = entries[paymentPacketID];
|
||||
const { amount } = paymentPackage;
|
||||
|
||||
const player = Game.players[playerID];
|
||||
if (player) {
|
||||
const boughtItem = {
|
||||
categoryID: 1,
|
||||
id: 4,
|
||||
amount
|
||||
};
|
||||
|
||||
player.inventory.refreshSingle(boughtItem);
|
||||
|
||||
const initializedPacket = RewardPacket.init(true, 1);
|
||||
initializedPacket.update(boughtItem);
|
||||
|
||||
const packet = initializedPacket.get();
|
||||
player.map.registerIndividualNetworkPacket(player.id, packet);
|
||||
};
|
||||
|
||||
await redis.del(`o_${playerID}_${paymentPacketID}`);
|
||||
await execute("UPDATE economy SET corsaireCoins = corsaireCoins + ? WHERE playerID = ?", [amount, playerID]);
|
||||
};
|
||||
|
||||
res.cork(() => res.writeStatus("200").end());
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
const Game = require("../../../Game/Game");
|
||||
|
||||
const isAuthenticated = require("../../../Utility/isAuthenticated");
|
||||
|
||||
module.exports = async (res, req) => {
|
||||
res.onAborted(() => res.isAborted = true);
|
||||
|
||||
const playerID = req.getQuery("playerId");
|
||||
|
||||
const hasPermission = await isAuthenticated(res, req);
|
||||
if (!hasPermission) return;
|
||||
|
||||
if (!playerID) return res.cork(() => res.writeStatus("400").end("Missing player id"));
|
||||
|
||||
const player = Game.players[playerID];
|
||||
if (!player) return res.cork(() => res.writeStatus("400").end("Unknown player"));
|
||||
|
||||
player.disconnect();
|
||||
|
||||
res.cork(() => res
|
||||
.writeStatus("200")
|
||||
.end("Player has been disconnected"));
|
||||
};
|
||||
@@ -0,0 +1,65 @@
|
||||
// const Game = require("../../../Game/Game");
|
||||
|
||||
// const isAuthenticated = require("../../../Utility/isAuthenticated");
|
||||
|
||||
const ShopEntryPacket = require("../../../Packets/ShopEntry");
|
||||
|
||||
const BASE_SHOP = require("../../../Data/shop");
|
||||
|
||||
const { SHOP } = BASE_SHOP;
|
||||
|
||||
module.exports = (res, req) => {
|
||||
res.onAborted(() => res.isAborted = true);
|
||||
console.info("hit")
|
||||
let buffer = Buffer.alloc(0);
|
||||
|
||||
res.onData(async (chunk, isLast) => {
|
||||
buffer = Buffer.concat([buffer, Buffer.from(chunk)]);
|
||||
|
||||
if (isLast) {
|
||||
const body = buffer.toString("utf8");
|
||||
|
||||
let data = {};
|
||||
|
||||
try {
|
||||
data = JSON.parse(body);
|
||||
} catch {
|
||||
console.info("error")
|
||||
res
|
||||
.writeStatus("400 Bad Request")
|
||||
.end();
|
||||
};
|
||||
|
||||
console.info(data);
|
||||
|
||||
const shopEntryAddQuery = await execute("INSERT INTO shop (itemID, categoryID, currencyType, currencyAmount, unit) VALUES (?, ?, ?, ?, ?)", [data.itemID, data.categoryID, data.currencyType, data.currencyAmount, data.unit]);
|
||||
if (!shopEntryAddQuery.data) {
|
||||
return res
|
||||
.writeStatus("400 Bad Request")
|
||||
.end();
|
||||
};
|
||||
|
||||
const shopEntry = {
|
||||
id: shopEntryAddQuery.data.insertId,
|
||||
itemID: parseInt(data.itemID),
|
||||
categoryID: parseInt(data.categoryID),
|
||||
currencyType: parseInt(data.currencyType),
|
||||
currencyAmount: parseInt(data.currencyAmount),
|
||||
unit: parseInt(data.unit),
|
||||
shopCategoryID: 2,
|
||||
isAvailable: () => true
|
||||
};
|
||||
|
||||
console.info(shopEntryAddQuery.data);
|
||||
|
||||
SHOP.push(shopEntry);
|
||||
|
||||
const shopEntryPacket = ShopEntryPacket(shopEntry);
|
||||
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20].forEach(mapID => app.publish(mapID.toString(), shopEntryPacket, true, true));
|
||||
|
||||
res.cork(() => res
|
||||
.writeStatus("200")
|
||||
.end("OK"));
|
||||
};
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,38 @@
|
||||
// const Game = require("../../../Game/Game");
|
||||
|
||||
// const isAuthenticated = require("../../../Utility/isAuthenticated");
|
||||
|
||||
const ShopEntryRemovedPacket = require("../../../Packets/ShopEntryRemoved");
|
||||
|
||||
const BASE_SHOP = require("../../../Data/shop");
|
||||
|
||||
const { SHOP } = BASE_SHOP;
|
||||
|
||||
module.exports = async (res, req) => {
|
||||
res.onAborted(() => res.isAborted = true);
|
||||
|
||||
const shopEntryID = req.getParameter(0);
|
||||
if (!shopEntryID) return;
|
||||
|
||||
const shopEntryRemoveQuery = await execute("DELETE FROM shop WHERE id = ?", [shopEntryID]);
|
||||
if (!shopEntryRemoveQuery.data) return;
|
||||
|
||||
const shopEntryIndex = SHOP.findIndex(entry => entry.id === parseInt(shopEntryID));
|
||||
if (shopEntryIndex === -1) return;
|
||||
|
||||
const { shopCategoryID, itemID } = SHOP[shopEntryIndex];
|
||||
|
||||
SHOP.splice(shopEntryIndex, 1);
|
||||
|
||||
const shopEntryRemoved = {
|
||||
shopCategoryID: 4,
|
||||
id: itemID
|
||||
};
|
||||
|
||||
const shopEntryRemovedPacket = ShopEntryRemovedPacket(shopEntryRemoved);
|
||||
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20].forEach(mapID => app.publish(mapID.toString(), shopEntryRemovedPacket, true, true));
|
||||
|
||||
res.cork(() => res
|
||||
.writeStatus("200")
|
||||
.end("OK"));
|
||||
};
|
||||
Reference in New Issue
Block a user