Commit 3744f833 authored by Zak Stephens's avatar Zak Stephens

Get snapshot route pretty functional.

parent 1f9a5bc9
......@@ -33,6 +33,7 @@ const app = express();
app.use('/api/v1/microtransactions', require('./routes/microtransactions'));
app.use('/api/v1/messages', require('./routes/messages')(gameState));
app.use('/api/v1/players', require('./routes/players')(gameState, playerRepository));
app.use('/api/v1/snapshot', require('./routes/snapshot')(gameState, playerRepository));
/* Speaking of serving: server! */
......
......@@ -43,12 +43,12 @@ module.exports = async db => {
assert(username.length > 0);
assert(password != null);
const passwordHash = (await dbGetAsync('SELECT password_hash FROM Players WHERE username = ?;', username)).password_hash;
if (passwordHash == null) {
const row = await dbGetAsync('SELECT password_hash FROM Players WHERE username = ?;', username);
if (row == null) {
return false; // The user doesn't exist.
}
return argon2.verify(passwordHash, password);
return argon2.verify(row.password_hash, password);
},
playerWithUsernameExists: async username =>
......@@ -61,7 +61,7 @@ module.exports = async db => {
return row.id;
},
getPlayerPosition: async id => {
getPlayerLocation: async id => {
const row = await dbGetAsync('SELECT x, y, map FROM PlayerLocations WHERE player_id = ?;', id);
assert(row);
......
......@@ -24,7 +24,7 @@ module.exports = (gameState, playerRepository) => {
/* Move player. */
router.post('/:username/movements',
(req, res, next) => playerUtil.basicAuthExpectPlayer(req.params.username, playerRepository)(req, res, next),
async (req, res, next) => (await playerUtil.basicAuthExpectPlayer(req.params.username, playerRepository))(req, res, next),
express.json(),
async (req, res) => {
const id = await playerRepository.getPlayerIdFromUsername(req.params.username);
......
const express = require('express');
const playerUtil = require('../player-util');
module.exports = (gameState, playerRepository) => {
const router = express.Router();
router.get('/:playerUsername',
async (req, res, next) => (await playerUtil.basicAuthExpectPlayer(req.params.playerUsername, playerRepository))(req, res, next),
async (req, res) => {
const currentPlayerId = await playerRepository.getPlayerIdFromUsername(req.params.playerUsername);
const currentPlayerState = await playerUtil.getPlayerState(currentPlayerId, gameState, playerRepository);
const snapshot = {
map: {
collision: gameState.maps[currentPlayerState.location.map].collision,
tiles: gameState.maps[currentPlayerState.location.map].tiles
},
players: {},
messages: gameState.messages
};
/* Players in same map. */
for ([id, playerState] of Object.entries(gameState.players)) {
if (playerState.location.map === currentPlayerState.location.map) {
snapshot.players[id] = playerState;
}
}
res.json(snapshot);
}
);
return router;
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment