combined gambling and lobby and added authenticateToken on every action.
This commit is contained in:
@@ -3,18 +3,17 @@
|
|||||||
"username": "jerome",
|
"username": "jerome",
|
||||||
"password": "Sigma",
|
"password": "Sigma",
|
||||||
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb3dlcl91c2VybmFtZSI6Implcm9tZSIsImlhdCI6MTc0NDg4OTY3MSwiZXhwIjoxNzQ0ODkxNDcxfQ.zYg3uxh_iNZ2xdEma8p3p-URT3Xa7wnYiQyOx0-QTac",
|
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb3dlcl91c2VybmFtZSI6Implcm9tZSIsImlhdCI6MTc0NDg4OTY3MSwiZXhwIjoxNzQ0ODkxNDcxfQ.zYg3uxh_iNZ2xdEma8p3p-URT3Xa7wnYiQyOx0-QTac",
|
||||||
"money": 200
|
"money": 400
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"username": "Gianimon",
|
"username": "Gianimon",
|
||||||
"password": "gianimon",
|
"password": "gianimon",
|
||||||
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IkdpYW5pbW9uIiwiaWF0IjoxNzQ0ODc0NTczLCJleHAiOjE3NDQ4NzYzNzN9.dGp5WTKIDVhomKZ8J7nevz8O7PqwRhR9wEgeyDtL_XE",
|
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IkdpYW5pbW9uIiwiaWF0IjoxNzQ0ODc0NTczLCJleHAiOjE3NDQ4NzYzNzN9.dGp5WTKIDVhomKZ8J7nevz8O7PqwRhR9wEgeyDtL_XE",
|
||||||
"money": 200
|
"money": 300
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"username": "any",
|
"username": "any",
|
||||||
"password": "any",
|
"password": "any",
|
||||||
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFueSIsImlhdCI6MTc0NDg4OTk1OSwiZXhwIjoxNzQ0ODkxNzU5fQ.TA6BA09DQlmUqEzQxD_wF0r-VyqCMccoxq8SxPnB9-4",
|
"money": 300
|
||||||
"money": 200
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
+75
@@ -14,6 +14,10 @@ app.use(express.json());
|
|||||||
app.use("/swagger-ui", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
|
app.use("/swagger-ui", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
|
||||||
app.use(cookieParser());
|
app.use(cookieParser());
|
||||||
|
|
||||||
|
/*
|
||||||
|
Here are Helper function -----------------------------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
function get_data() {
|
function get_data() {
|
||||||
const dataPath = path.join(__dirname, "data.json");
|
const dataPath = path.join(__dirname, "data.json");
|
||||||
if (!fs.existsSync(dataPath)) {
|
if (!fs.existsSync(dataPath)) {
|
||||||
@@ -61,6 +65,10 @@ function authenticateToken(request, response, next) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Here is the the Login logic -----------------------------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
app.post("/register", (request, response) => {
|
app.post("/register", (request, response) => {
|
||||||
let {username, password} = request.body;
|
let {username, password} = request.body;
|
||||||
let lower_username = username.toLowerCase();
|
let lower_username = username.toLowerCase();
|
||||||
@@ -155,6 +163,73 @@ app.delete("/logout", authenticateToken, (request, response) => {
|
|||||||
response.json({message: "Logged out successfully."});
|
response.json({message: "Logged out successfully."});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
Here will be the Game logic -----------------------------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
const playerMap = {};
|
||||||
|
|
||||||
|
async function createLobby() {
|
||||||
|
let response = await fetch("https://www.deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1")
|
||||||
|
let data = await response.json();
|
||||||
|
let lobbyId = data.deck_id
|
||||||
|
await fetch(`https://www.deckofcardsapi.com/api/deck/${lobbyId}/pile/players/add/?cards=`)
|
||||||
|
console.log(lobbyId);
|
||||||
|
return lobbyId;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function joinLobby(lobbyId, player_name) {
|
||||||
|
|
||||||
|
let response = await fetch(`https://www.deckofcardsapi.com/api/deck/${lobbyId}/pile/${player_name}/add/?cards=`)
|
||||||
|
console.log(response);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
app.get("/lobby/create", authenticateToken, async (req, res) => {
|
||||||
|
const username = req.query.username;
|
||||||
|
|
||||||
|
if (!username) {
|
||||||
|
return res.status(400).send("Username is required");
|
||||||
|
}
|
||||||
|
|
||||||
|
let lobbyId = await createLobby();
|
||||||
|
|
||||||
|
if (!playerMap[lobbyId]) {
|
||||||
|
playerMap[lobbyId] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!playerMap[lobbyId].includes(username)) {
|
||||||
|
playerMap[lobbyId].push(username);
|
||||||
|
}
|
||||||
|
res.redirect(`/lobby/join?lobbyId=${lobbyId}&username=${username}`);
|
||||||
|
})
|
||||||
|
|
||||||
|
app.get("/lobby/join", authenticateToken, async (req, res) => {
|
||||||
|
let lobbyId = req.query.lobbyId;
|
||||||
|
let username = req.query.username;
|
||||||
|
|
||||||
|
const users = get_data();
|
||||||
|
const user = users.find(u => u.username === username);
|
||||||
|
if (!user) {
|
||||||
|
return res.status(401).json({error: "User doesn't exist"});
|
||||||
|
}
|
||||||
|
console.log(playerMap);
|
||||||
|
if (lobbyId in playerMap) {
|
||||||
|
await joinLobby(lobbyId, user.username);
|
||||||
|
|
||||||
|
res.json({lobbyId, username});
|
||||||
|
} else console.log("Lobby with " + lobbyId + " not found.");
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get("/lobby/players", authenticateToken, async (req, res) => {
|
||||||
|
let lobbyId = req.query.lobbyId;
|
||||||
|
let players = playerMap[lobbyId] || [];
|
||||||
|
res.json({players});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log("Server is running on port " + port);
|
console.log("Server is running on port " + port);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,88 +0,0 @@
|
|||||||
const express = require("express");
|
|
||||||
const path = require("path");
|
|
||||||
const fs = require("fs");
|
|
||||||
const app = express();
|
|
||||||
const port = 3000;
|
|
||||||
|
|
||||||
const playerMap = {};
|
|
||||||
|
|
||||||
async function createLobby() {
|
|
||||||
let response = await fetch("https://www.deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1")
|
|
||||||
let data = await response.json();
|
|
||||||
let lobbyId = data.deck_id
|
|
||||||
await fetch(`https://www.deckofcardsapi.com/api/deck/${lobbyId}/pile/players/add/?cards=`)
|
|
||||||
console.log(lobbyId);
|
|
||||||
return lobbyId;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function joinLobby(lobbyId, player_name) {
|
|
||||||
|
|
||||||
let response = await fetch(`https://www.deckofcardsapi.com/api/deck/${lobbyId}/pile/${player_name}/add/?cards=`)
|
|
||||||
console.log(response);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
app.get("/lobby/create", async (req, res) => {
|
|
||||||
const username = req.query.username;
|
|
||||||
|
|
||||||
if (!username) {
|
|
||||||
return res.status(400).send("Username is required");
|
|
||||||
}
|
|
||||||
|
|
||||||
let lobbyId = await createLobby();
|
|
||||||
|
|
||||||
if (!playerMap[lobbyId]) {
|
|
||||||
playerMap[lobbyId] = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!playerMap[lobbyId].includes(username)) {
|
|
||||||
playerMap[lobbyId].push(username);
|
|
||||||
}
|
|
||||||
res.redirect(`/lobby/join?lobbyId=${lobbyId}&username=${username}`);
|
|
||||||
})
|
|
||||||
|
|
||||||
app.get("/lobby/join", async (req, res) => {
|
|
||||||
let lobbyId = req.query.lobbyId;
|
|
||||||
let username = req.query.username;
|
|
||||||
|
|
||||||
const users = get_data();
|
|
||||||
const user = users.find(u => u.username === username);
|
|
||||||
if (!user) {
|
|
||||||
return res.status(401).json({error: "User doesn't exist"});
|
|
||||||
}
|
|
||||||
console.log(playerMap);
|
|
||||||
if (lobbyId in playerMap) {
|
|
||||||
await joinLobby(lobbyId, user.username);
|
|
||||||
|
|
||||||
res.json({lobbyId, username});
|
|
||||||
} else console.log("Lobby with " + lobbyId + " not found.");
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get("/lobby/players", async (req, res) => {
|
|
||||||
let lobbyId = req.query.lobbyId;
|
|
||||||
let players = playerMap[lobbyId] || [];
|
|
||||||
res.json({players});
|
|
||||||
});
|
|
||||||
|
|
||||||
function get_data() {
|
|
||||||
const dataPath = path.join(__dirname, "data.json");
|
|
||||||
if (!fs.existsSync(dataPath)) {
|
|
||||||
fs.writeFileSync(dataPath, JSON.stringify([]));
|
|
||||||
}
|
|
||||||
const data = fs.readFileSync(dataPath);
|
|
||||||
return JSON.parse(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
function save_data(data) {
|
|
||||||
fs.writeFileSync(path.join(__dirname, "data.json"), JSON.stringify(data, null, 2));
|
|
||||||
}
|
|
||||||
|
|
||||||
function saveUser(user) {
|
|
||||||
const users = get_data();
|
|
||||||
users.push(user);
|
|
||||||
save_data(users);
|
|
||||||
}
|
|
||||||
|
|
||||||
app.listen(port, () => {
|
|
||||||
console.log("Server is running on port " + port);
|
|
||||||
});
|
|
||||||
+1
-1
@@ -17,7 +17,7 @@ const doc = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const outputFile = './swagger.json';
|
const outputFile = './swagger.json';
|
||||||
const routes = ['./gambling.js', './lobby.js'];
|
const routes = ['./gambling.js'];
|
||||||
|
|
||||||
/* NOTE: If you are using the express Router, you must pass in the 'routes' only the
|
/* NOTE: If you are using the express Router, you must pass in the 'routes' only the
|
||||||
root file where the route starts, such as index.js, app.js, routes.js, etc ... */
|
root file where the route starts, such as index.js, app.js, routes.js, etc ... */
|
||||||
|
|||||||
Reference in New Issue
Block a user