Commit c818af1c authored by nanahira's avatar nanahira

use regex match for last deck

parent 44d0ba96
......@@ -93,7 +93,15 @@ export interface Points {
ratio: number;
}
interface YGOProDistroData {
deckPath: string;
replayPath: string;
systemConf?: string;
lastDeckFormat?: string;
}
interface YGOProData {
ygopro: YGOProDistroData;
servers: Server[];
}
......@@ -148,6 +156,8 @@ export class YGOProComponent implements OnInit, OnDestroy {
// tslint:disable-next-line:member-ordering
rooms_loading = true;
lastDeckFormat: RegExp;
default_options: Options = {
mode: 1,
rule: this.settingsService.getLocale().startsWith('zh') ? 0 : 1,
......@@ -269,7 +279,8 @@ export class YGOProComponent implements OnInit, OnDestroy {
}
async ngOnInit() {
this.servers = (<YGOProData>this.app.data).servers;
const ygoproData = <YGOProData>this.app.data;
this.servers = ygoproData.servers;
this.selectableServers = this.servers.filter(s => !s.hidden);
this.currentServer = this.selectableServers[0];
// this.reloadCurrentServer();
......@@ -281,6 +292,11 @@ export class YGOProComponent implements OnInit, OnDestroy {
locale = 'en-US';
}
if (ygoproData.ygopro.lastDeckFormat) {
//console.log(`Deck format pattern: ${ygoproData.ygopro.lastDeckFormat}`)
this.lastDeckFormat = new RegExp(ygoproData.ygopro.lastDeckFormat);
}
this.system_conf = this.app.systemConfPath;
await this.refresh(true);
......@@ -408,13 +424,30 @@ export class YGOProComponent implements OnInit, OnDestroy {
async refresh(init?: boolean) {
this.decks = await this.get_decks();
let system_conf = await this.load_system_conf();
if (this.lastDeckFormat) {
const systemConfString = await this.load_system_conf();
let lastDeck: string;
if (systemConfString) {
// console.log(`System conf string: ${systemConfString}`);
const lastDeckMatch = systemConfString.match(this.lastDeckFormat);
if (lastDeckMatch) {
lastDeck = lastDeckMatch[1];
// console.log(`Last deck ${lastDeck} read from ${this.system_conf}.`);
} else {
// console.error(`Deck pattern not found from pattern ${this.system_conf}: ${lastDeckMatch}`);
}
} else {
// console.error(`System conf ${this.system_conf} not found.`);
}
if (system_conf && this.decks.includes(system_conf.lastdeck)) {
this.current_deck = system_conf.lastdeck;
if (lastDeck && this.decks.includes(lastDeck)) {
// console.log(`Got last deck ${lastDeck}.`);
this.current_deck = lastDeck;
} else if (init) {
this.current_deck = this.decks[0];
}
}
this.replays = await this.get_replays();
......@@ -435,9 +468,33 @@ export class YGOProComponent implements OnInit, OnDestroy {
async get_decks(): Promise<string[]> {
try {
/*
const deckPath = this.app.ygoproDeckPath;
let result: string[] = [];
const files: string[] = await fs.readdir(deckPath);
for (const file of files) {
if (file.startsWith('.git')) { // fuck
continue;
}
if (path.extname(file) === '.ydk') {
result.push(path.basename(file, '.ydk'));
} else {
const fullPath = path.join(deckPath, file);
const stat = await fs.stat(fullPath);
if (stat.isDirectory()) {
const innerDecks = (await fs.readdir(fullPath))
.filter((iFile) => path.extname(iFile) === '.ydk')
.map((iFile) => path.join(file, path.basename(iFile, '.ydk')));
result = result.concat(innerDecks);
}
}
}
return result;
*/
let files: string[] = await fs.readdir(this.app.ygoproDeckPath);
return files.filter(file => path.extname(file) === '.ydk').map(file => path.basename(file, '.ydk'));
} catch (error) {
console.error(`Load deck fail: ${error.toString()}`);
return [];
}
}
......@@ -447,6 +504,7 @@ export class YGOProComponent implements OnInit, OnDestroy {
let files: string[] = await fs.readdir(this.app.ygoproReplayPath);
return files.filter(file => path.extname(file) === '.yrp').map(file => path.basename(file, '.yrp'));
} catch (error) {
console.error(`Load replay fail: ${error.toString()}`);
return [];
}
}
......@@ -486,13 +544,13 @@ export class YGOProComponent implements OnInit, OnDestroy {
}
};*/
async load_system_conf(): Promise<SystemConf> {
async load_system_conf(): Promise<string> {
if (!this.system_conf) {
return null;
}
try {
let data = await fs.readFile(this.system_conf, {encoding: 'utf-8'});
return <any>ini.parse(data);
return data;
} catch(e) {
return null;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment