Commit ed16c36b authored by nanahira's avatar nanahira

abstract ygopro

parent 9372c2cd
Pipeline #4967 failed with stages
in 3 minutes and 16 seconds
......@@ -35,7 +35,7 @@
</div>
<!--应用ready-->
<div *ngIf="currentApp.isReady() && (currentApp.id != 'ygopro')" class="i-b">
<div *ngIf="currentApp.isReady() && !currentApp.isYGOPro" class="i-b">
<button *ngIf="currentApp.runnable()" (click)="runApp(currentApp)" [disabled]="!appsService.allReady(currentApp)" type="button" class="btn btn-primary btn-sm">
<i class="fa fa-play" aria-hidden="true"></i> <span i18n>运行</span></button>
<button *ngIf="currentApp.actions.get('network')" [disabled]="!appsService.allReady(currentApp)" (click)="runApp(currentApp,'network')" type="button" class="btn btn-primary btn-sm">
......@@ -65,13 +65,13 @@
</div>
<!--<button (click)="log(appsService)">test</button>-->
<network *ngIf="(currentApp.id != 'ygopro') && currentApp && currentApp.network && currentApp.network.protocol == 'maotama'" [currentApp]="currentApp"></network>
<ygopro *ngIf="currentApp.isReady() && (currentApp.id == 'ygopro')" [app]="currentApp" [currentApp]="currentApp" (points)="onPoints($event)"></ygopro>
<network *ngIf="currentApp && !currentApp.isYGOPro && currentApp.network && currentApp.network.protocol == 'maotama'" [currentApp]="currentApp"></network>
<ygopro *ngIf="currentApp.isReady() && currentApp.isYGOPro" [app]="currentApp" [currentApp]="currentApp" (points)="onPoints($event)"></ygopro>
</div>
</div>
<div id="arena" class="panel panel-default" *ngIf="currentApp.id === 'ygopro' && points ">
<div id="arena" class="panel panel-default" *ngIf="currentApp.isYGOPro && points ">
<h2 i18n>排位成绩</h2>
<table class="table table-sm">
<tbody>
......
......@@ -3,6 +3,7 @@ import * as path from 'path';
import * as ini from 'ini';
import * as fs from 'fs';
import * as child_process from 'child_process';
import * as Mustache from 'mustache';
export enum Category {
game,
......@@ -62,6 +63,13 @@ export class AppStatus {
progressMessage: string;
}
export interface YGOProDistroData {
deckPath: string;
replayPath: string;
systemConf: string;
}
export class App {
id: string;
name: string; // i18n
......@@ -239,7 +247,7 @@ export class App {
return dependencies.every((dependency) => dependency.isReady());
}
async getSpawnAction(children: App[], action_name = 'main', referencedApp?: App, referencedAction?: Action, cwd?: string): Promise<SpawnAction> {
async getSpawnAction(children: App[], action_name = 'main', referencedApp?: App, referencedAction?: Action, cwd?: string, argsTemplate?: any): Promise<SpawnAction> {
const appCwd = (<AppLocal>this.local).path;
if(!cwd) {
cwd = appCwd;
......@@ -301,23 +309,31 @@ export class App {
if (action.open) {
const np2 = action.open;
const openAction = await np2.getSpawnAction([], 'main', this, action, cwd);
const openAction = await np2.getSpawnAction([], 'main', this, action, cwd, argsTemplate);
args = args.concat(openAction.args);
args.push(action.execute);
execute = openAction.execute;
cwd = openAction.cwd;
}
args = args.concat(action.args);
if (argsTemplate) {
for (let i = 0; i < args.length; ++i) {
if (typeof args[i] !== 'string') {
continue;
}
args[i] = Mustache.render(args[i], argsTemplate, null, { escape: (v) => v });
}
}
env = Object.assign(env, action.env);
return {
execute,
args,
env,
cwd
}
};
}
async spawnApp(children: App[], action_name = 'main') {
async spawnApp(children: App[], action_name = 'main', argsTemplate?: any) {
if (this.id === 'th123') {
let th105 = <App>this.references.get('th105');
......@@ -333,9 +349,44 @@ export class App {
}
const appCwd = (<AppLocal>this.local).path;
const {execute, args, env, cwd} = await this.getSpawnAction(children, action_name);
const {execute, args, env, cwd} = await this.getSpawnAction(children, action_name, null, null, null, argsTemplate);
console.log(execute, args, env, cwd, appCwd);
return child_process.spawn(execute, args, {env: env, cwd: cwd || appCwd});
}
get isYGOPro(): boolean {
return !!this.ygoproDistroData;
}
get ygoproDistroData(): YGOProDistroData {
if(!this.data) {
return null;
}
return this.data.ygopro || null;
}
get ygoproDeckPath(): string {
const distroData = this.ygoproDistroData;
if(!distroData) {
return null;
}
return path.join(this.local!.path, distroData.deckPath);
}
get ygoproReplayPath(): string {
const distroData = this.ygoproDistroData;
if(!distroData || !distroData.replayPath) {
return null;
}
return path.join(this.local!.path, distroData.replayPath);
}
get systemConfPath(): string {
const distroData = this.ygoproDistroData;
if(!distroData || !distroData.systemConf) {
return null;
}
return path.join(this.local!.path, distroData.systemConf);
}
}
......@@ -3,11 +3,9 @@
*/
import {ChangeDetectorRef, Component, ElementRef, EventEmitter, Input, OnDestroy, OnInit, Output, ViewChild} from '@angular/core';
import {Headers, Http} from '@angular/http';
import * as child_process from 'child_process';
import {clipboard, remote, shell} from 'electron';
import * as fs from 'fs-extra';
import * as ini from 'ini';
import {EncodeOptions} from 'ini';
import * as $ from 'jquery';
import * as path from 'path';
import 'rxjs/Rx';
......@@ -96,7 +94,7 @@ export interface Points {
}
interface YGOProData {
servers: Server[]
servers: Server[];
}
......@@ -140,7 +138,7 @@ export class YGOProComponent implements OnInit, OnDestroy {
selectableServers: Server[];
//selectingServerId: string;
// selectingServerId: string;
currentServer: Server;
/*reloadCurrentServer() {
......@@ -274,7 +272,7 @@ export class YGOProComponent implements OnInit, OnDestroy {
this.servers = (<YGOProData>this.app.data).servers;
this.selectableServers = this.servers.filter(s => !s.hidden);
this.currentServer = this.selectableServers[0];
//this.reloadCurrentServer();
// this.reloadCurrentServer();
let locale: string;
if (this.settingsService.getLocale().startsWith('zh')) {
......@@ -283,8 +281,8 @@ export class YGOProComponent implements OnInit, OnDestroy {
locale = 'en-US';
}
this.system_conf = path.join(this.app.local!.path, 'system.conf');
await this.refresh();
this.system_conf = this.app.systemConfPath;
await this.refresh(true);
let modal = $('#game-list-modal');
......@@ -387,7 +385,7 @@ export class YGOProComponent implements OnInit, OnDestroy {
let watchDropdownMenu = $('#watch-filter');
watchDropdownMenu.on("change", "input[type='checkbox']", (event) => {
//$(event.target).closest("label").toggleClass("active", (<HTMLInputElement> event.target).checked);
// $(event.target).closest("label").toggleClass("active", (<HTMLInputElement> event.target).checked);
this.refresh_replay_rooms();
});
......@@ -408,13 +406,13 @@ export class YGOProComponent implements OnInit, OnDestroy {
}
async refresh() {
async refresh(init?: boolean) {
this.decks = await this.get_decks();
let system_conf = await this.load_system_conf();
if (this.decks.includes(system_conf.lastdeck)) {
if (system_conf && this.decks.includes(system_conf.lastdeck)) {
this.current_deck = system_conf.lastdeck;
} else {
} else if (init) {
this.current_deck = this.decks[0];
}
......@@ -437,7 +435,7 @@ export class YGOProComponent implements OnInit, OnDestroy {
async get_decks(): Promise<string[]> {
try {
let files: string[] = await fs.readdir(path.join(this.app.local!.path, 'deck'));
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) {
return [];
......@@ -446,7 +444,7 @@ export class YGOProComponent implements OnInit, OnDestroy {
async get_replays(): Promise<string[]> {
try {
let files: string[] = await fs.readdir(path.join(this.app.local!.path, 'replay'));
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) {
return [];
......@@ -464,13 +462,14 @@ export class YGOProComponent implements OnInit, OnDestroy {
async delete_deck(deck: string) {
if (confirm('确认删除?')) {
try {
await fs.unlink(path.join(this.app.local!.path, 'deck', deck + '.ydk'));
await fs.unlink(path.join(this.app.ygoproDeckPath, deck + '.ydk'));
} catch (error) {
}
return this.refresh();
}
}
/*
async fix_fonts(data: SystemConf) {
if (!await this.get_font([data.numfont])) {
let font = await this.get_font(this.numfont);
......@@ -485,16 +484,20 @@ export class YGOProComponent implements OnInit, OnDestroy {
data['textfont'] = `${font} 14`;
}
}
};
};*/
async load_system_conf(): Promise<SystemConf> {
if (!this.system_conf) {
return null;
}
let data = await fs.readFile(this.system_conf, {encoding: 'utf-8'});
return <any>ini.parse(data);
};
/*
save_system_conf(data: SystemConf) {
return fs.writeFile(this.system_conf, ini.unsafe(ini.stringify(data, <EncodeOptions>{whitespace: true})));
};
};*/
async join(name: string, server: Server) {
/*let system_conf = await this.load_system_conf();
......@@ -506,7 +509,8 @@ export class YGOProComponent implements OnInit, OnDestroy {
system_conf.roompass = name;
system_conf.nickname = this.loginService.user.username;
await this.save_system_conf(system_conf);*/
return this.start_game(['-h', server.address, '-p', server.port.toString(), '-w', name, '-n', this.loginService.user.username, '-d', this.current_deck, '-j']);
// return this.start_game(['-h', server.address, '-p', server.port.toString(), '-w', name, '-n', this.loginService.user.username, '-d', this.current_deck, '-j']);
return this.start_game('main', {server, password: name, username: this.loginService.user.username, deck: this.current_deck});
};
async edit_deck(deck: string) {
......@@ -514,14 +518,16 @@ export class YGOProComponent implements OnInit, OnDestroy {
await this.fix_fonts(system_conf);
system_conf.lastdeck = deck;
await this.save_system_conf(system_conf);*/
return this.start_game(['-d', deck]);
// return this.start_game(['-d', deck]);
return this.start_game('deck', { deck })
}
async watch_replay(replay: string) {
/*let system_conf = await this.load_system_conf();
await this.fix_fonts(system_conf);
await this.save_system_conf(system_conf);*/
return this.start_game(['-r', path.join('replay', replay + '.yrp')]);
// return this.start_game(['-r', path.join('replay', replay + '.yrp')]);
return this.start_game('replay', { replay: path.join(this.app.ygoproReplayPath, `${replay}.yrp`) });
}
join_windbot(name?: string) {
......@@ -531,7 +537,7 @@ export class YGOProComponent implements OnInit, OnDestroy {
return this.join('AI#' + name, this.currentServer);
}
async start_game(args: string[]) {
async start_game(action: string, param: any) {
let data: any;
let start_time: string;
let exp_rank_ex: number;
......@@ -539,11 +545,8 @@ export class YGOProComponent implements OnInit, OnDestroy {
let win = remote.getCurrentWindow();
win.minimize();
await new Promise((resolve, reject) => {
let child = child_process.spawn(path.join(this.app.local!.path, this.app.actions.get('main')!.execute), args, {
cwd: this.app.local!.path,
stdio: 'inherit'
});
await new Promise(async (resolve, reject) => {
let child = await this.app.spawnApp([], action, param);
child.on('error', (error) => {
reject(error);
win.restore();
......
{
"name": "mycard",
"version": "3.0.48",
"version": "3.0.51",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"version": "3.0.48",
"version": "3.0.51",
"hasInstallScript": true,
"license": "UNLICENSED",
"dependencies": {
......@@ -35,6 +35,7 @@
"ini": "1.3.4",
"jquery": "2.2.4",
"marked": "0.3.6",
"mustache": "^4.2.0",
"raven-js": "3.16.1",
"raw-socket": "latest",
"reconnecting-websocket": "3.0.7",
......@@ -57,6 +58,7 @@
"@types/jquery": "^2.0.47",
"@types/lodash": "^4.14.172",
"@types/marked": "latest",
"@types/mustache": "^4.1.2",
"@types/node": "^14.0.1",
"@types/tether": "latest",
"@types/typeahead": "latest",
......@@ -564,6 +566,12 @@
"integrity": "sha512-Klz949h02Gz2uZCMGwDUSDS1YBlTdDDgbWHi+81l29tQALUtvz4rAYi5uoVhE5Lagoq6DeqAUlbrHvW/mXDgdQ==",
"dev": true
},
"node_modules/@types/mustache": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/@types/mustache/-/mustache-4.1.2.tgz",
"integrity": "sha512-c4OVMMcyodKQ9dpwBwh3ofK9P6U9ZktKU9S+p33UqwMNN1vlv2P0zJZUScTshnx7OEoIIRcCFNQ904sYxZz8kg==",
"dev": true
},
"node_modules/@types/node": {
"version": "14.0.1",
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.0.1.tgz",
......@@ -3539,6 +3547,14 @@
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
},
"node_modules/mustache": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/mustache/-/mustache-4.2.0.tgz",
"integrity": "sha512-71ippSywq5Yb7/tVYyGbkBggbU8H3u5Rz56fH60jGFgr8uHwxs+aSKeqmluIVzM0m0kB7xQjKS6qPfd0b2ZoqQ==",
"bin": {
"mustache": "bin/mustache"
}
},
"node_modules/nan": {
"version": "2.14.2",
"resolved": "https://registry.npmjs.org/nan/-/nan-2.14.2.tgz",
......@@ -5904,6 +5920,12 @@
"integrity": "sha512-Klz949h02Gz2uZCMGwDUSDS1YBlTdDDgbWHi+81l29tQALUtvz4rAYi5uoVhE5Lagoq6DeqAUlbrHvW/mXDgdQ==",
"dev": true
},
"@types/mustache": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/@types/mustache/-/mustache-4.1.2.tgz",
"integrity": "sha512-c4OVMMcyodKQ9dpwBwh3ofK9P6U9ZktKU9S+p33UqwMNN1vlv2P0zJZUScTshnx7OEoIIRcCFNQ904sYxZz8kg==",
"dev": true
},
"@types/node": {
"version": "14.0.1",
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.0.1.tgz",
......@@ -8269,6 +8291,11 @@
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
},
"mustache": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/mustache/-/mustache-4.2.0.tgz",
"integrity": "sha512-71ippSywq5Yb7/tVYyGbkBggbU8H3u5Rz56fH60jGFgr8uHwxs+aSKeqmluIVzM0m0kB7xQjKS6qPfd0b2ZoqQ=="
},
"nan": {
"version": "2.14.2",
"resolved": "https://registry.npmjs.org/nan/-/nan-2.14.2.tgz",
......
......@@ -70,6 +70,7 @@ System.config({
// other node.js libraries
"electron": "@node/electron",
"ini": "@node/ini",
"mustache": "@node/mustache",
"mkdirp": "@node/mkdirp",
"aria2": "@node/aria2",
"electron-sudo": "@node/electron-sudo",
......
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