wowok_agent 0.1.11 → 0.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,217 @@
1
+ import * as fs from 'fs';
2
+ import * as path from 'path';
3
+ import * as os from 'os';
4
+ import { BuyRequiredEnum } from 'wowok';
5
+
6
+ const Private_FileName = 'wowok.sel.dat';
7
+ const Private_Key = 'wowok-sel-v1';
8
+
9
+ export interface PrivateInfo_Data {
10
+ name: string;
11
+ default?: boolean;
12
+ info: Map<BuyRequiredEnum | string, string>;
13
+ }
14
+
15
+ export class PrivateInfo {
16
+ constructor(storage: 'File' | 'Explorer' = 'File') {
17
+ this.storage = storage;
18
+ }
19
+ static _instance: any;
20
+
21
+ static Instance() : PrivateInfo {
22
+ if (!PrivateInfo._instance) {
23
+ PrivateInfo._instance = new PrivateInfo();
24
+ }; return PrivateInfo._instance
25
+ }
26
+
27
+ private storage: 'File' | 'Explorer' = 'File';
28
+
29
+ private _add(buffer:string | null | undefined, name:string, info:Map<BuyRequiredEnum | string, string>, bDefault?: boolean) : PrivateInfo_Data[] | undefined{
30
+ var data : PrivateInfo_Data[] | undefined;
31
+
32
+ try {
33
+ if (buffer) {
34
+ data = JSON.parse(buffer) as PrivateInfo_Data[];
35
+ if (data) {
36
+ const f = data.find(v => v.name === name);
37
+ if (f) {
38
+ f.default = bDefault;
39
+ f.info = info;
40
+ } else {
41
+ if (bDefault) {
42
+ data.forEach(v => v.default = false)
43
+ }
44
+ data.push({name:name, info:info, default:bDefault})
45
+ }
46
+ } else {
47
+ data = [{name:name, info:info, default:bDefault}];
48
+ }
49
+ return data
50
+ }
51
+ } catch(e) { console.log(e) }
52
+ }
53
+
54
+ private _remove(buffer:string | null | undefined, name:string) : PrivateInfo_Data[] | undefined{
55
+ var data : PrivateInfo_Data[] | undefined;
56
+
57
+ try {
58
+ if (buffer) {
59
+ data = JSON.parse(buffer) as PrivateInfo_Data[];
60
+ if (data) {
61
+ return data.filter(v => v.name !== name);
62
+ }
63
+ }
64
+ } catch(e) { console.log(e) }
65
+ }
66
+
67
+ private _default(buffer:string | null | undefined) : PrivateInfo_Data | undefined {
68
+ var data : PrivateInfo_Data[] | undefined;
69
+ try {
70
+ if (buffer) {
71
+ data = JSON.parse(buffer) as PrivateInfo_Data[];
72
+ if (data) {
73
+ const f = data.find(v => v.default);
74
+ if (f) {
75
+ return f
76
+ }
77
+ }
78
+ }
79
+ } catch(e) { console.log(e) }
80
+ }
81
+ private _get(buffer:string | null | undefined, name?:string, bNotFoundReturnDefault?:boolean) : PrivateInfo_Data | undefined {
82
+ var data : PrivateInfo_Data[] | undefined;
83
+ try {
84
+ if (buffer) {
85
+ data = JSON.parse(buffer) as PrivateInfo_Data[];
86
+ if (data) {
87
+ const f = data.find(v => v.name === name);
88
+ if (f) {
89
+ return f
90
+ }
91
+ if (bNotFoundReturnDefault) {
92
+ return data.find(v => v.default)
93
+ }
94
+ }
95
+ }
96
+ } catch(e) { console.log(e) }
97
+ }
98
+
99
+ private _rename(buffer:string | null | undefined, oldName:string, newName:string, bSwapIfExisted:boolean=true) : PrivateInfo_Data[] | undefined {
100
+ var data : PrivateInfo_Data[] | undefined;
101
+ try {
102
+ if (buffer) {
103
+ data = JSON.parse(buffer) as PrivateInfo_Data[];
104
+
105
+ if (data) {
106
+ const f1 = data.find(v => v.name === oldName);
107
+ if (!f1) return undefined;
108
+
109
+ const f2 = data.find(v => v.name === newName);
110
+ if (f2) {
111
+ if (bSwapIfExisted) {
112
+ f1.name = newName;
113
+ f2.name = oldName;
114
+ return data
115
+ }
116
+ } else {
117
+ f1.name = newName;
118
+ return data;
119
+ }
120
+ }
121
+ }
122
+ } catch(e) { console.log(e) }
123
+ }
124
+
125
+ set_storage(storage: 'File' | 'Explorer' = 'File') {
126
+ this.storage = storage
127
+ }
128
+
129
+ default() : PrivateInfo_Data | undefined {
130
+ try {
131
+ if (this.storage === 'File') {
132
+ const filePath = path.join(os.homedir(), Private_FileName);
133
+ return this._default(fs.readFileSync(filePath, 'utf-8'));
134
+ } else if (this.storage === 'Explorer') {
135
+ return this._default(localStorage.getItem(Private_Key));
136
+ }
137
+ } catch (e) { console.log(e) }
138
+ }
139
+ get(name?: string, bNotFoundReturnDefault:boolean=true) : PrivateInfo_Data | undefined {
140
+ try {
141
+ if (this.storage === 'File') {
142
+ const filePath = path.join(os.homedir(), Private_FileName);
143
+ return this._get(fs.readFileSync(filePath, 'utf-8'), name, bNotFoundReturnDefault);
144
+ } else if (this.storage === 'Explorer') {
145
+ return this._get(localStorage.getItem(Private_Key), name, bNotFoundReturnDefault);
146
+ }
147
+ } catch (e) { console.log(e) }
148
+ }
149
+ rename(oldName:string, newName:string, bSwapIfExisted:boolean=true) : boolean {
150
+ var res : PrivateInfo_Data[] | undefined;
151
+ try {
152
+ if (this.storage === 'File') {
153
+ const filePath = path.join(os.homedir(), Private_FileName);
154
+ res = this._rename(fs.readFileSync(filePath, 'utf-8'), oldName, newName, bSwapIfExisted);
155
+ if (res) {fs.writeFileSync(filePath, JSON.stringify(res), 'utf-8') }
156
+ } else if (this.storage === 'Explorer') {
157
+ res = this._rename(localStorage.getItem(Private_Key), oldName, newName, bSwapIfExisted);
158
+ if (res) localStorage.setItem(Private_Key, JSON.stringify(res));
159
+ }
160
+ } catch (e) { console.log(e) }
161
+ return res ? true : false
162
+ }
163
+
164
+ list() : PrivateInfo_Data[] {
165
+ try {
166
+ if (this.storage === 'File') {
167
+ const filePath = path.join(os.homedir(), Private_FileName);
168
+ return JSON.parse(fs.readFileSync(filePath, 'utf-8')) as PrivateInfo_Data[];
169
+ } else if (this.storage === 'Explorer') {
170
+ return JSON.parse(localStorage.getItem(Private_Key) ?? '') as PrivateInfo_Data[];
171
+ }
172
+ } catch (e) { console.log(e) }
173
+ return []
174
+ }
175
+
176
+ add(name:string, info:Map<BuyRequiredEnum | string, string>, bDefault?: boolean) {
177
+ try {
178
+ if (this.storage === 'File') {
179
+ const filePath = path.join(os.homedir(), Private_FileName);
180
+ fs.readFile(filePath, 'utf-8', (err, d) => {
181
+ const data = this._add(d, name, info, bDefault);
182
+ fs.writeFileSync(filePath, JSON.stringify(data), 'utf-8')
183
+ });
184
+ } else if (this.storage === 'Explorer') {
185
+ const data = this._add(localStorage.getItem(Private_Key), name, info, bDefault);
186
+ localStorage.setItem(Private_Key, JSON.stringify(data))
187
+ }
188
+ } catch (e) { console.log(e) }
189
+ }
190
+
191
+ remove(name:string) {
192
+ try {
193
+ if (this.storage === 'File') {
194
+ const filePath = path.join(os.homedir(), Private_FileName);
195
+ fs.readFile(filePath, 'utf-8', (err, d) => {
196
+ const data = this._remove(d, name);
197
+ fs.writeFileSync(filePath, JSON.stringify(data), 'utf-8')
198
+ });
199
+ } else if (this.storage === 'Explorer') {
200
+ const data = this._remove(localStorage.getItem(Private_Key), name);
201
+ localStorage.setItem(Private_Key, JSON.stringify(data))
202
+ }
203
+ } catch (e) { console.log(e) }
204
+ }
205
+
206
+ removeall() {
207
+ try {
208
+ if (this.storage === 'File') {
209
+ const filePath = path.join(os.homedir(), Private_FileName);
210
+ fs.unlink(filePath, (err) => {console.log(err)});
211
+ } else if (this.storage === 'Explorer') {
212
+ localStorage.removeItem(Private_Key)
213
+ }
214
+ } catch (e) { console.log(e) }
215
+ }
216
+ }
217
+