-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
147 lines (114 loc) · 2.8 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'use strict'
const fs = require('fs')
const rating = require('./rating')
function initCategories() {
let obj = {}
let categories = fs.readdirSync('categories')
for (let category of categories) {
obj[category] = {}
// items of category
let items = fs.readdirSync('categories/' + category)
let id = 0
for (let item of items) {
if (item == 'config.json')
continue
obj[category][item] = []
// files of item
let files = fs.readdirSync('categories/' + category + '/' + item)
for (let file of files) {
obj[category][item].push({id: id, name: file})
id++
}
}
}
global.categories = obj
}
function initConfigs() {
global.configs = {}
let categories = fs.readdirSync('categories')
for (let category of categories) {
let items = fs.readdirSync('categories/' + category)
if (items.indexOf('config.json') != -1)
configs[category] = require('./categories/' + category + '/config')
else
throw new Error("category '" + category + "' doesn't have the config.json-file!")
}
}
function getRandomCollection(category, category_name) {
let collection = []
let items = Object.keys(category)
let items_stored = items.slice()
// remove empty items
for (let item of items_stored) {
if (!category[item].length)
items.remove(item)
}
while (collection.length < 6) {
let item = items[Math.randomInt(items.length - 1)]
// first item
if (!collection.length) {
let files = category[item]
let file = files[Math.randomInt(files.length - 1)]
let url = 'categories/%s/%s/%s'.format(category_name, item, file.name)
collection.push(file.id, url, item)
// restore
items = items_stored.slice()
} else
collection.push(item)
// remove selected
items.remove(item)
}
return collection
}
function getRating(score) {
let ranks = Object.keys(rating)
for (let rank of ranks) {
if (score <= rating[rank])
return rank
}
return ranks[ranks.length - 1]
}
function getCountFilesInCategory(category) {
let count = 0
let items = fs.readdirSync('categories/' + category)
for (let item of items) {
let files = fs.readdirSync('categories/' + category + '/' + item)
count += files.length
}
return count
}
function excludeShownImages(items, shown_images) {
for (let i in shown_images) {
for (let j in items) {
let files = items[j]
let removed = false
for (let l in files) {
let file = files[l]
if (file.id == shown_images[i]) {
items[j].splice(l, 1)
removed = true
break
}
}
if (removed)
break
}
}
}
function isEmptyCategory(category) {
let items = Object.keys(category)
for (let item of items) {
if (category[item].length)
return false
}
return true
}
module.exports = {
initCategories,
initConfigs,
getRandomCollection,
getRating,
getCountFilesInCategory,
excludeShownImages,
isEmptyCategory
}