Skip to content

Commit

Permalink
Guild rewrite, fixes slothpixel#690
Browse files Browse the repository at this point in the history
  • Loading branch information
builder-247 authored and ChristianDobbie committed Jan 10, 2022
1 parent da5b6fe commit 63faa45
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 123 deletions.
18 changes: 12 additions & 6 deletions routes/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const getUUID = require('../store/getUUID');
const buildBans = require('../store/buildBans');
const buildBoosters = require('../store/buildBoosters');
const buildCounts = require('../store/buildCounts');
const buildGuild = require('../store/buildGuild');
const { queryAuctionId } = require('../store/queryAuctions');
const { getGuildFromPlayer, getGuildFromName, getGuildFromID } = require('../store/buildGuild');
const { buildProfileList, buildProfile } = require('../store/buildSkyBlockProfiles');
const { buildSkyblockCalendar, buildSkyblockEvents } = require('../store/buildSkyblockCalendar');
const { playerObject } = require('./objects');
Expand Down Expand Up @@ -871,8 +871,14 @@ Consider supporting The Slothpixel Project on Patreon to help cover the hosting
},
route: () => '/guilds/:player',
func: async (request, response, callback) => {
let id;
try {
const guild = await getGuildFromPlayer(request.params.player, { shouldPopulatePlayers: request.query.populatePlayers });
id = await getUUID(request.params.player);
} catch {
return response.status(404).json({ error: 'Invalid username' });
}
try {
const guild = await buildGuild('player', id, { shouldPopulatePlayers: request.query.populatePlayers });
if (guild.guild === null) {
return response.status(404).json(guild);
}
Expand Down Expand Up @@ -1028,7 +1034,7 @@ Consider supporting The Slothpixel Project on Patreon to help cover the hosting
route: () => '/guilds/name/:name',
func: async (request, response, callback) => {
try {
const guild = await getGuildFromName(request.params.name, { shouldPopulatePlayers: request.query.populatePlayers });
const guild = await buildGuild('name', request.params.name, { shouldPopulatePlayers: request.query.populatePlayers });
if (guild.guild === null) {
return response.status(404).json(guild);
}
Expand All @@ -1041,8 +1047,8 @@ Consider supporting The Slothpixel Project on Patreon to help cover the hosting
},
'/guilds/id/{guildID}': {
get: {
summary: 'Get guild stats by the name of the guild',
description: 'Look up a guild from the its name',
summary: 'Get guild stats by the internal ID of the guild',
description: 'Look up a guild from the its ID',
operationId: 'guild',
tags: [
'guild',
Expand Down Expand Up @@ -1184,7 +1190,7 @@ Consider supporting The Slothpixel Project on Patreon to help cover the hosting
route: () => '/guilds/id/:id',
func: async (request, response, callback) => {
try {
const guild = await getGuildFromID(request.params.id, { shouldPopulatePlayers: request.query.populatePlayers });
const guild = await buildGuild('id', request.params.id, { shouldPopulatePlayers: request.query.populatePlayers });
if (guild.guild === null) {
return response.status(404).json(guild);
}
Expand Down
139 changes: 30 additions & 109 deletions store/buildGuild.js
Original file line number Diff line number Diff line change
@@ -1,133 +1,54 @@
/* eslint-disable consistent-return */
const config = require('../config');
const processGuildData = require('../processors/processGuildData');
const getUUID = require('./getUUID');
const { generateJob, getData } = require('../util/utility');
const {
generateJob,
getData,
} = require('../util/utility');
const redis = require('./redis');
const cachedFunction = require('./cachedFunction');
const { populatePlayers } = require('./buildPlayer');

/*
* Functions to build/cache guild object
* Currently doesn't support search by name
*/
async function getGuildData(id) {
const body = await getData(redis, generateJob('guild', {
id,
}).url);
if (body.guild === null) {
// removeGuild(id);
function getGuildData({ guild }) {
if (guild === null) {
return null;
}
const guild = processGuildData(body.guild);
return guild;
}

async function createGuildCache(uuid) {
const guildData = await getData(redis, generateJob('findguild', {
id: uuid,
}).url);

if (guildData.guild === null) {
return null;
}

return guildData.guild;
}

async function createGuildCacheFromName(name) {
const guildData = await getData(redis, generateJob('findguildByName', {
id: name,
}).url);

if (guildData.guild === null) {
return null;
}

return guildData.guild;
}

async function getGuildID(uuid) {
return createGuildCache(uuid);
/*
try {
const guild = await getGuildByPlayer(uuid);
if (guild !== null) {
logger.debug(`Found cached guild for ${uuid}: ${guild.name}`);
return guild.id;
}
return createGuildCache(uuid);
} catch {
return createGuildCache(uuid);
}
*/
}

async function getGuildIDFromName(name) {
return createGuildCacheFromName(name);
}

async function buildGuild(uuid) {
const id = await getGuildID(uuid);
if (id == null) {
return { guild: null };
}
return cachedFunction(`guild:${id}`, async () => {
const guild = await getGuildData(id);
if (!guild) {
return { guild: null };
return processGuildData(guild);
}

async function buildGuild(type, id, { shouldPopulatePlayers = false } = {}) {
const key = `guild:${type}:${id}`;
const guild = await cachedFunction(key, async () => {
let body = {};
switch (type) {
case 'player':
body = await getData(redis, generateJob('guildByPlayer', { id }).url);
break;
case 'name':
body = await getData(redis, generateJob('guildByName', { id }).url);
break;
default:
case 'id':
body = await getData(redis, generateJob('guildById', { id }).url);
}

if (config.ENABLE_DB_CACHE) {
// insertGuild(id, guild);
}

return guild;
}, { cacheDuration: config.GUILD_CACHE_SECONDS, shouldCache: config.ENABLE_GUILD_CACHE });
}

async function buildGuildFromName(name) {
const id = await getGuildIDFromName(name);
if (id == null) {
return { guild: null };
}
return cachedFunction(`guild:${id}`, async () => {
const guild = await getGuildData(id);
const guild = getGuildData(body);
if (!guild) {
return { guild: null };
}

return guild;
}, { cacheDuration: config.GUILD_CACHE_SECONDS, shouldCache: config.ENABLE_GUILD_CACHE });
}

async function getGuildFromPlayer(playerName, { shouldPopulatePlayers = false } = {}) {
const guild = await buildGuild(await getUUID(playerName));
if (shouldPopulatePlayers) {
const players = await populatePlayers(guild.members);
guild.members = players;
}
return guild;
}

async function getGuildFromName(guildName, { shouldPopulatePlayers = false } = {}) {
const guild = await buildGuildFromName(guildName);
if (shouldPopulatePlayers) {
const players = await populatePlayers(guild.members);
guild.members = players;
}
return guild;
}
}, {
cacheDuration: config.GUILD_CACHE_SECONDS,
shouldCache: config.ENABLE_GUILD_CACHE,
});

async function getGuildFromID(guildID, { shouldPopulatePlayers = false } = {}) {
const guild = await getGuildData(guildID);
if (shouldPopulatePlayers) {
const players = await populatePlayers(guild.members);
guild.members = players;
guild.members = await populatePlayers(guild.members);
}
return guild;
}

module.exports = {
getGuildFromPlayer, getGuildFromName, getGuildData, getGuildFromID,
};
module.exports = buildGuild;
16 changes: 8 additions & 8 deletions util/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,24 +241,24 @@ function generateJob(type, payload) {
url: `${apiUrl}/counts?key=${apiKey}`,
};
},
findguild() {
guildByPlayer() {
return {
url: `${apiUrl}/findguild?key=${apiKey}&byUuid=${payload.id}`,
url: `${apiUrl}/guild?key=${apiKey}&player=${payload.id}`,
};
},
findguildByName() {
guildByName() {
return {
url: `${apiUrl}/findguild?key=${apiKey}&byName=${payload.id}`,
url: `${apiUrl}/guild?key=${apiKey}&name=${payload.id}`,
};
},
friends() {
guildById() {
return {
url: `${apiUrl}/friends?key=${apiKey}&uuid=${payload.id}`,
url: `${apiUrl}/guild?key=${apiKey}&id=${payload.id}`,
};
},
guild() {
friends() {
return {
url: `${apiUrl}/guild?key=${apiKey}&id=${payload.id}`,
url: `${apiUrl}/friends?key=${apiKey}&uuid=${payload.id}`,
};
},
gamecounts() {
Expand Down

0 comments on commit 63faa45

Please sign in to comment.