BetterDiscordApp-v2/client/src/dev/serveremu.js

131 lines
4.4 KiB
JavaScript
Raw Normal View History

2018-12-04 08:22:04 +01:00
const dummyTags = ['dark', 'light', 'simple', 'minimal', 'extra', 'something', 'tag', 'whatever', 'another', 'transparent'];
export default class ServerEmu {
static async themes(args) {
if (!this._themes) this._themes = this.generateThemes();
await new Promise(r => setTimeout(r, Math.random() * 3000));
2018-12-04 09:48:25 +01:00
let docs = [];
2018-12-04 08:30:49 +01:00
if (args && args.sterm) {
const { sterm } = args;
const reg = new RegExp(sterm, 'gi');
2018-12-04 09:48:25 +01:00
docs = this._themes.filter(doc => doc.tags.includes(sterm) || reg.exec(doc.name) || reg.exec(doc.description));
} else {
docs = this._themes;
}
2018-12-04 23:40:57 +01:00
if (args.sort) {
switch (args.sort) {
case 'updated':
if (args.ascending) docs = docs.sort((docA, docB) => new Date(docA.updated).getTime() - new Date(docB.updated).getTime());
else docs = docs.sort((docA, docB) => new Date(docB.updated).getTime() - new Date(docA.updated).getTime());
break;
case 'installs':
if (args.ascending) docs = docs.sort((docA, docB) => docA.installs - docB.installs);
else docs = docs.sort((docA, docB) => docB.installs - docA.installs);
break;
case 'users':
if (args.ascending) docs = docs.sort((docA, docB) => docA.activeUsers - docB.activeUsers);
else docs = docs.sort((docA, docB) => docB.activeUsers - docA.activeUsers);
break;
2018-12-04 23:43:40 +01:00
case 'rating':
if (args.ascending) docs = docs.sort((docA, docB) => docA.rating - docB.rating);
else docs = docs.sort((docA, docB) => docB.rating - docA.rating);
break;
2018-12-04 23:40:57 +01:00
}
}
2018-12-04 09:48:25 +01:00
const total = docs.length;
const pages = Math.ceil(total / 9);
let page = 1;
if (args && args.page) {
page = args.page;
docs = docs.slice((page - 1) * 9, page * 9);
} else {
docs = docs.slice(0, 9);
2018-12-04 08:30:49 +01:00
}
2018-12-04 08:22:04 +01:00
return {
2018-12-04 08:30:49 +01:00
docs,
2018-12-04 23:40:57 +01:00
filters: {
sterm: args.sterm || '',
ascending: args.ascending || false,
sort: args.sort || 'name'
},
2018-12-04 08:22:04 +01:00
pagination: {
2018-12-04 09:48:25 +01:00
total,
pages,
2018-12-04 08:30:49 +01:00
limit: 9,
2018-12-04 09:48:25 +01:00
page
2018-12-04 08:22:04 +01:00
}
}
}
static generateThemes() {
const docs = [];
const count = Math.floor(Math.random() * 50 + 30);
for (let i = 0; i < count; i++) {
const id = `theme${i}-${this.randomId()}`;
const name = `Dummy Theme ${i}`;
const tags = dummyTags.sort(() => .5 - Math.random()).slice(0, 3);
docs.push({
id,
name,
tags,
installs: Math.floor(Math.random() * 5000) + 5000,
updated: this.randomTimestamp(),
rating: Math.floor(Math.random() * 500) + 500,
activeUsers: Math.floor(Math.random() * 1000) + 1000,
rated: Math.random() > .5,
version: this.randomVersion(),
repository: this.dummyRepo,
files: this.dummyFiles,
2018-12-04 08:30:49 +01:00
author: this.dummyAuthor,
description: ''
2018-12-04 08:22:04 +01:00
});
}
return docs;
}
static get dummyRepo() {
return {
name: 'ExampleRepository',
baseUri: 'https://github.com/Jiiks/ExampleRepository',
rawUri: 'https://github.com/Jiiks/ExampleRepository/raw/master'
}
}
static get dummyFiles() {
return {
readme: 'Example/readme.md',
previews: [{
large: 'Example/preview1-big.png',
thumb: 'Example/preview1-small.png'
}]
}
}
static get dummyAuthor() {
return 'Someone';
}
static randomId() {
return btoa(Math.random()).substring(3, 9);
}
static randomTimestamp() {
return `2018-${Math.floor((Math.random() * 12) + 1).toString().padStart(2, '0')}-${Math.floor((Math.random() * 30) + 1).toString().padStart(2, '0')}T14:51:32.057Z`;
}
static randomVersion() {
return `${Math.round(Math.random() * 3)}.${Math.round(Math.random() * 10)}.${Math.round(Math.random() * 10)}`;
}
}