This repository has been archived by the owner on Jun 8, 2019. It is now read-only.
forked from xTCry/VCoin
-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathhelpers.js
167 lines (139 loc) · 4.09 KB
/
helpers.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const fs = require('fs'),
colors = require('colors/safe'),
ReadLine = require('readline')
const pJson = require('./package.json')
// TODO: Переписать данный файл, например в части работы с async функциями (existsAsync, writeFileAsync, ...)
let offColors = false
let rl = ReadLine.createInterface(process.stdin, process.stdout)
rl.setPrompt('VCoinX > ')
rl.prompt()
rl.isQst = false
rl.questionAsync = (question) => {
return new Promise((resolve) => {
rl.isQst = true
rl.question(question, _ => {
rl.isQst = false
resolve(_)
})
})
}
function formatScore (e) {
return (arguments.length > 1 && void 0 !== arguments[1] && arguments[1])
? (function (e, t, n, a) {
var r, o, c, s, i
r = parseInt(e = (+e || 0).toFixed(t), 10) + '';
(o = r.length) > 3 ? o %= 3 : o = 0
i = o ? (r.substr(0, o) + a) : ''
c = r.substr(o).replace(/(\d{3})(?=\d)/g, '$1' + a)
s = t ? n + Math.abs(e - r).toFixed(t).replace(/-/, 0).slice(2) : ''
return i + c + s
}(e / 1e3, 3, ',', ' '))
: (e / 1e3).toFixed(3).toString().replace('.', ',')
}
colors.setTheme({
dateBG: 'white',
dataC: 'yellow',
warnBG: 'bgBlack',
warn: 'yellow',
errorBG: 'bgBlack',
error: 'red',
})
function con (message, color, colorBG) {
if (message === undefined) return console.log('\n')
let temp = (!offColors ? colors.dateBG('[' + dateF() + ']') : dateF()) + ': ' + ccon(message, color, colorBG, 1)
console.log(temp)
}
function ccon (message, color, colorBG, ret) {
let temp = ''
if (message === undefined) {
console.log('\n')
return
}
if (color === true) {
color = 'white'
colorBG = 'Red'
temp = !offColors ? colors.yellow.bgRed('[ОШИБКА]: ') : '[ОШИБКА]: '
}
colorBG = 'bg' + ((typeof colorBG === 'string') ? colorBG : 'Black')
color = (typeof color === 'string') ? color : 'green'
temp += !offColors ? colors[colorBG](colors[color](message)) : message
!ret && console.log(temp)
return temp
}
function setColorsM (e) {
offColors = !!e
}
function dateF (date) {
if (!isNaN(date) && date < 9900000000) date *= 1000
date = date !== undefined ? new Date(date) : new Date()
var dYear = date.getFullYear(),
dMonth = (date.getMonth() + 1).toString().padStart(2, 0),
dDay = date.getDate().toString().padStart(2, 0),
dHour = date.getHours().toString().padStart(2, 0),
dMinutes = date.getMinutes().toString().padStart(2, 0),
dSeconds = date.getSeconds().toString().padStart(2, 0),
date_format = dDay + '.' + dMonth + '.' + dYear + ' ' + dHour + ':' + dMinutes + ':' + dSeconds
return date_format
}
function rand (min, max) {
if (max === undefined) max = min
min = 0
return Math.floor(min + Math.random() * (max + 1 - min))
}
let cFile = './main.log'
async function infLog (data) {
data = '\n[' + dateF() + '] \t' + data
let exists = await existsAsync(cFile)
if (!exists) {
let errWrite = await writeFileAsync(cFile, 'Log.' + data)
if (errWrite) throw errWrite
}
else await appendFileAsync(cFile, data)
}
function existsFile (f) {
return fs.existsSync(f)
}
function existsAsync (path) {
return new Promise(resolve => fs.existsSync(path, exists => resolve(exists)))
}
function writeFileAsync (path, data) {
return new Promise(resolve => fs.writeFile(path, data, err => resolve(err)))
}
function appendFileAsync (path, data) {
return new Promise(resolve => fs.appendFile(path, data, err => resolve(err)))
}
function getVersion () {
return pJson.version
}
function setTerminalTitle (title) {
process.stdout.write(
String.fromCharCode(27) + ']0;' + title + String.fromCharCode(7)
)
}
function beep () {
process.stdout.write('\x07')
}
function removeLetters (e) {
return parseInt(e.replace(/\D+/g, ''))
}
function mathPrice (price, count) {
return count <= 1 ? price : Math.ceil(1.3 * mathPrice(price, count - 1))
}
module.exports = {
rl,
con,
ccon,
setColorsM,
offColors,
formatScore,
existsFile,
existsAsync,
writeFileAsync,
appendFileAsync,
getVersion,
setTerminalTitle,
infLog,
rand,
beep,
mathPrice,
}