Commit 0474e72a authored by 神楽坂玲奈's avatar 神楽坂玲奈

fix

parent d097bd50
/** /**
* Created by weijian on 2016/10/26. * Created by weijian on 2016/10/26.
*/ */
import {EventEmitter, Injectable, NgZone} from '@angular/core'; import { EventEmitter, Injectable, NgZone } from '@angular/core';
import { HttpClient } from '@angular/common/http';
// import {error} from 'util';
// import Timer = NodeJS.Timer;
// const Logger = {
// 'error': (message: string) => {
// console.error('DownloadService: ', message);
// }
// };
import Aria2 from 'aria2'; import Aria2 from 'aria2';
const MAX_LIST_NUM = 1000; const MAX_LIST_NUM = 1000;
...@@ -18,33 +10,48 @@ const ARIA2_INTERVAL = 500; ...@@ -18,33 +10,48 @@ const ARIA2_INTERVAL = 500;
export class DownloadStatus { export class DownloadStatus {
completedLength: number; completedLength: number;
downloadSpeed: number; downloadSpeed: number;
gid: string;
status: string;
totalLength: number;
totalLengthText: string;
errorCode: string;
errorMessage: string;
constructor(item?: any) {
if (item) {
this.completedLength = parseInt(item.completedLength) || 0;
this.downloadSpeed = parseInt(item.downloadSpeed) || 0;
this.totalLength = parseInt(item.totalLength) || 0;
this.gid = item.gid;
this.status = item.status;
this.errorCode = item.errorCode;
this.errorMessage = item.errorMessage;
} else {
this.completedLength = 0;
this.downloadSpeed = 0;
this.totalLength = 0;
}
}
get downloadSpeedText (): string { get downloadSpeedText(): string {
if (!isNaN(this.downloadSpeed) && this.downloadSpeed !== 0) { if (!isNaN(this.downloadSpeed) && this.downloadSpeed !== 0) {
const speedUnit = ['Byte/s', 'KB/s', 'MB/s', 'GB/s', 'TB/s']; const speedUnit = ['Byte/s', 'KB/s', 'MB/s', 'GB/s', 'TB/s'];
let currentUnit = Math.floor(Math.log(this.downloadSpeed) / Math.log(1024)); let currentUnit = Math.floor(Math.log(this.downloadSpeed) / Math.log(1024));
return (this.downloadSpeed / 1024 ** currentUnit).toFixed(1) + ' ' + speedUnit[currentUnit]; return (this.downloadSpeed / 1024 ** currentUnit).toFixed(1) + ' ' + speedUnit[currentUnit];
} }
return ''; return '';
}; }
gid: string;
status: string;
totalLength: number;
totalLengthText: string;
errorCode: string;
errorMessage: string;
combine (...others: DownloadStatus[]): DownloadStatus { combine(...others: DownloadStatus[]): DownloadStatus {
const priority = { const priority = {
undefined: -1, undefined: -1,
'': -1, '': -1,
'active': 0, active: 0,
'complete': 0, complete: 0,
'paused': 1, paused: 1,
'waiting': 1, waiting: 1,
'removed': 2, removed: 2,
'error': 3 error: 3,
}; };
let status = Object.assign(new DownloadStatus(), this); let status = Object.assign(new DownloadStatus(), this);
for (let o of others) { for (let o of others) {
...@@ -58,57 +65,49 @@ export class DownloadStatus { ...@@ -58,57 +65,49 @@ export class DownloadStatus {
status.totalLength += o.totalLength; status.totalLength += o.totalLength;
status.completedLength += o.completedLength; status.completedLength += o.completedLength;
} }
} }
return status; return status;
} }
// 0相等. 1不想等 // 0相等. 1不想等
compareTo (other: DownloadStatus): number { compareTo(other: DownloadStatus): number {
if (this.status !== other.status || if (
this.status !== other.status ||
this.downloadSpeed !== other.downloadSpeed || this.downloadSpeed !== other.downloadSpeed ||
this.completedLength !== other.completedLength || this.completedLength !== other.completedLength ||
this.totalLength !== other.totalLength) { this.totalLength !== other.totalLength
) {
return 1; return 1;
} else { } else {
return 0; return 0;
} }
} }
constructor (item ?: any) {
if (item) {
this.completedLength = parseInt(item.completedLength) || 0;
this.downloadSpeed = parseInt(item.downloadSpeed) || 0;
this.totalLength = parseInt(item.totalLength) || 0;
this.gid = item.gid;
this.status = item.status;
this.errorCode = item.errorCode;
this.errorMessage = item.errorMessage;
} else {
this.completedLength = 0;
this.downloadSpeed = 0;
this.totalLength = 0;
}
}
} }
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root',
}) })
export class DownloadService { export class DownloadService {
// 强制指定IPv4,接到过一个反馈无法监听v6的。默认的host值是localhost,会连v6。 // 强制指定IPv4,接到过一个反馈无法监听v6的。默认的host值是localhost,会连v6。
aria2 = new Aria2({host: '127.0.0.1'}); aria2 = new Aria2({ host: '127.0.0.1' });
open = this.aria2.open(); open = this.aria2.open();
updateEmitter = new EventEmitter<void>(); updateEmitter = new EventEmitter<void>();
downloadList: Map<string, DownloadStatus> = new Map(); downloadList: Map<string, DownloadStatus> = new Map();
taskMap: Map<string, string[]> = new Map(); taskMap: Map<string, string[]> = new Map();
async refreshDownloadList () { constructor(private ngZone: NgZone) {
let activeList = await this.aria2.tellActive(); ngZone.runOutsideAngular(async () => {
let waitList = await this.aria2.tellWaiting(0, MAX_LIST_NUM); await this.open;
let stoppedList = await this.aria2.tellStopped(0, MAX_LIST_NUM); setInterval(async () => {
await this.refreshDownloadList();
}, ARIA2_INTERVAL);
});
}
async refreshDownloadList() {
let activeList = await this.aria2.call('tellActive');
let waitList = await this.aria2.call('tellWaiting', 0, MAX_LIST_NUM);
let stoppedList = await this.aria2.call('tellStopped', 0, MAX_LIST_NUM);
this.downloadList.clear(); this.downloadList.clear();
for (let item of activeList) { for (let item of activeList) {
this.downloadList.set(item.gid, new DownloadStatus(item)); this.downloadList.set(item.gid, new DownloadStatus(item));
...@@ -122,47 +121,26 @@ export class DownloadService { ...@@ -122,47 +121,26 @@ export class DownloadService {
this.updateEmitter.emit(); this.updateEmitter.emit();
} }
constructor (private ngZone: NgZone) { async progress(id: string, callback: (downloadStatus: DownloadStatus) => void) {
ngZone.runOutsideAngular(async () => {
await this.open;
setInterval(async () => {
await this.refreshDownloadList();
}, ARIA2_INTERVAL);
});
}
private createId (): string {
function s4 () {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
async progress (id: string, callback: (downloadStatus: DownloadStatus) => void) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let gids = this.taskMap.get(id); let gids = this.taskMap.get(id);
if (gids) { if (gids) {
let allStatus: DownloadStatus; let allStatus: DownloadStatus;
let subscription = this.updateEmitter.subscribe(() => { let subscription = this.updateEmitter.subscribe(() => {
try { try {
let status: DownloadStatus = new DownloadStatus(); let status: DownloadStatus = new DownloadStatus();
// 合并每个状态信息 // 合并每个状态信息
status = status = gids!
gids!.map((value, index, array) => { .map((value, index, array) => {
let s = this.downloadList.get(value); let s = this.downloadList.get(value);
if (!s) { if (!s) {
throw 'Gid not exists'; throw 'Gid not exists';
} }
return s; return s;
}) })
.reduce((previousValue, currentValue, currentIndex, array) => { .reduce((previousValue, currentValue, currentIndex, array) => {
return previousValue.combine(currentValue); return previousValue.combine(currentValue);
}, status); }, status);
if (!allStatus) { if (!allStatus) {
allStatus = status; allStatus = status;
} else { } else {
...@@ -178,7 +156,6 @@ export class DownloadService { ...@@ -178,7 +156,6 @@ export class DownloadService {
} else { } else {
callback(allStatus); callback(allStatus);
} }
} catch (e) { } catch (e) {
reject(e); reject(e);
subscription.unsubscribe(); subscription.unsubscribe();
...@@ -190,19 +167,19 @@ export class DownloadService { ...@@ -190,19 +167,19 @@ export class DownloadService {
}); });
} }
async getFiles (id: string): Promise<string[]> { async getFiles(id: string): Promise<string[]> {
let gids = this.taskMap.get(id)!; let gids = this.taskMap.get(id)!;
let files: string[] = []; let files: string[] = [];
for (let gid of gids) { for (let gid of gids) {
let file = await this.aria2.getFiles(gid); let file = await this.aria2.call('getFiles', gid);
files.push(file[0].path); files.push(file[0].path);
} }
return files; return files;
} }
async addMetalink (metalink: string, library: string): Promise<string> { async addMetalink(metalink: string, library: string): Promise<string> {
let encodedMeta4 = Buffer.from((metalink)).toString('base64'); let encodedMeta4 = Buffer.from(metalink).toString('base64');
let gidList = await this.aria2.addMetalink(encodedMeta4, {dir: library}); let gidList = await this.aria2.call('addMetalink', encodedMeta4, { dir: library });
let taskId = this.createId(); let taskId = this.createId();
this.taskMap.set(taskId, gidList); this.taskMap.set(taskId, gidList);
// 每次添加任务,刷新一下本地任务列表 // 每次添加任务,刷新一下本地任务列表
...@@ -210,21 +187,13 @@ export class DownloadService { ...@@ -210,21 +187,13 @@ export class DownloadService {
return taskId; return taskId;
} }
async addUri (url: string, destination: string): Promise<string> { private createId(): string {
await this.open; function s4() {
let taskId = this.createId(); return Math.floor((1 + Math.random()) * 0x10000)
let gid = await this.aria2.addUri([url], {dir: destination}); .toString(16)
this.taskMap.set(taskId, [gid]); .substring(1);
return taskId;
}
async pause (id: string): Promise<void> {
await this.open;
try {
await this.aria2.pause(id);
} catch (e) {
} }
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}
} }
...@@ -26,9 +26,9 @@ import { SettingsService } from '../settings.service'; ...@@ -26,9 +26,9 @@ import { SettingsService } from '../settings.service';
import _ from 'lodash-es'; import _ from 'lodash-es';
import fg from 'fast-glob'; import fg from 'fast-glob';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { lastValueFrom, Subscription } from 'rxjs';
import WillNavigateEvent = Electron.WillNavigateEvent; import WillNavigateEvent = Electron.WillNavigateEvent;
import Timer = NodeJS.Timer; import Timer = NodeJS.Timer;
import { lastValueFrom, Subscription } from 'rxjs';
interface SystemConf { interface SystemConf {
use_d3d: string; use_d3d: string;
...@@ -300,6 +300,7 @@ export class YGOProComponent implements OnInit, OnDestroy { ...@@ -300,6 +300,7 @@ export class YGOProComponent implements OnInit, OnDestroy {
if (_.isArray(objValue)) { if (_.isArray(objValue)) {
return objValue.concat(srcValue); return objValue.concat(srcValue);
} }
return;
}); });
} }
} }
...@@ -602,7 +603,12 @@ export class YGOProComponent implements OnInit, OnDestroy { ...@@ -602,7 +603,12 @@ export class YGOProComponent implements OnInit, OnDestroy {
system_conf.nickname = this.loginService.user.username; system_conf.nickname = this.loginService.user.username;
await this.save_system_conf(system_conf);*/ 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 }); return this.start_game('main', {
server,
password: name,
username: this.loginService.user.username,
deck: this.current_deck,
});
} }
async edit_deck(deck: string) { async edit_deck(deck: string) {
...@@ -709,7 +715,7 @@ export class YGOProComponent implements OnInit, OnDestroy { ...@@ -709,7 +715,7 @@ export class YGOProComponent implements OnInit, OnDestroy {
data.exp_rank_ex = exp_rank_ex; data.exp_rank_ex = exp_rank_ex;
data.arena_rank_ex = arena_rank_ex; data.arena_rank_ex = arena_rank_ex;
// if (start_time !== data.start_time) { // if (start_time !== data.start_time) {
this.appsService.showResult('projects/ygopro-result/end_YGOPro_single.html', data, 202, 222); this.appsService.showResult('projects/ygopro-result/end_YGOPro_single.html', data, 202, 222);
// } // }
}); });
} catch (error) { } catch (error) {
......
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