From 38dba09bb3496a96ab46be5ed8fce03c985db26d Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Noblot Date: Thu, 5 Oct 2017 11:51:15 +0200 Subject: [PATCH] improvement --- package.json | 2 ++ src/pages/easy/easy.ts | 41 ++++++++++++------------ src/pages/hard/hard.ts | 57 +++++++++++++++++----------------- src/pages/nav/nav.ts | 26 ++++++++-------- src/pages/normal/normal.ts | 48 +++++++++++++++------------- src/providers/share-service.ts | 4 +-- 6 files changed, 94 insertions(+), 84 deletions(-) diff --git a/package.json b/package.json index 366c210..cebd5da 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,8 @@ "devDependencies": { "@ionic/app-scripts": "2.1.3", "@ionic/cli-plugin-ionic-angular": "1.4.1", + "@types/angular": "^1.6.32", + "@types/ionic": "0.0.35", "ionic": "3.10.3", "typescript": "2.3.4" }, diff --git a/src/pages/easy/easy.ts b/src/pages/easy/easy.ts index 4d2e6f1..581a1f8 100644 --- a/src/pages/easy/easy.ts +++ b/src/pages/easy/easy.ts @@ -12,7 +12,7 @@ export class EasyPage { constructor(public navCtrl: NavController, public navParams: NavParams, private shareService: ShareService, private storage: Storage) { } - possible: Array = [ + possible: Array = [ "AliceBlue","Aqua","Aquamarine","Azure","Beige","Black","Blue","BlueViolet","Brown","BurlyWood","CadetBlue","Chartreuse","Chocolate","Coral","CornflowerBlue","Crimson","Cyan","DarkBlue","DarkCyan","DarkGoldenRod","DarkGrey","DarkGreen","DarkKhaki","DarkMagenta","DarkOliveGreen","DarkOrange","DarkOrchid","DarkRed","DarkSalmon","DarkSeaGreen","DarkSlateBlue","DarkSlateGrey","DarkTurquoise","DarkViolet","DeepPink","DeepSkyBlue","DimGrey","DodgerBlue","FireBrick","FloralWhite","ForestGreen","Fuchsia","Gainsboro","GhostWhite","Gold","GoldenRod","Grey","Green","GreenYellow","HotPink","IndianRed","Indigo","Ivory","Khaki","Lavender","LavenderBlush","LawnGreen","LemonChiffon","LightBlue","LightCoral","LightCyan","LightGrey","LightGreen","LightPink","LightSalmon","LightSeaGreen","LightSkyBlue","LightSlateGrey","LightSteelBlue","LightYellow","LimeGreen","Magenta","Maroon","MidnightBlue","Olive","Orange","OrangeRed","Orchid","PaleGreen","PaleTurquoise","PaleVioletRed","Pink","Purple","Red","RoyalBlue","SaddleBrown","Salmon","SeaGreen","Silver","SkyBlue","Snow","SpringGreen","SteelBlue","Teal","Tomato","Turquoise","Violet","White","WhiteSmoke","Yellow","YellowGreen" ]; point: number = 0; @@ -20,9 +20,9 @@ export class EasyPage { nbColors: number[] = [2, 4, 6, 8]; difficulty: number = 0; hexa: string = "#"; - goodColor: string =""; + goodColor: string = ""; color: string; - colors: Array = []; + colors: Array = []; fail: number = 0; testcolor: boolean; record: number; @@ -30,31 +30,34 @@ export class EasyPage { i: number = 0; k: number = 1; - getRandomInt(min, max) { + getRandomInt(min: number, max: number): number { return Math.floor(Math.random() * (max - min + 1)) + min; } - generateColorCode(){ + generateColorCode(): string { this.color = this.possible[this.getRandomInt(0, this.possible.length - 1)]; return this.color; } - setDifficulty(){ - if (this.point == 0 && this.point < 5) return this.difficulty = 0; + setDifficulty(): number { + if (this.point === 0 && this.point < 5) return this.difficulty = 0; if (this.point >= 5 && this.point < 10) return this.difficulty = 1; if (this.point >= 10 && this.point < 15) return this.difficulty = 2; if (this.point >= 15) return this.difficulty = 3; } - checkResponse(userChoice){ - if(userChoice != this.goodColor) return this.wrongResponse(); + checkResponse(userChoice: string): void { + if(userChoice != this.goodColor){ + this.wrongResponse(); + return; + } this.point++; this.ngOnInit(); } - wrongResponse(){ + wrongResponse(): void { this.fail = 1; this.defineBestScore(); } - checkColorArray(obj){ + checkColorArray(obj: string): void { for(let i = 0; i<=this.colors.length; i++) { if (this.colors[i] == obj){ this.color = this.generateColorCode(); @@ -62,7 +65,7 @@ export class EasyPage { } } } - defineBestScore(){ + defineBestScore(): void { this.storage.get('bestScoreEasy').then((val) => { if (val == null || this.point > val) { this.storage.set('bestScoreEasy', this.point); @@ -71,21 +74,21 @@ export class EasyPage { this.record = val; }); } - reset(){ + reset(): void { this.point = 0; this.fail = 0; this.bestPoint = false; } - shareScore(){ + shareScore(): void { this.shareService.shareScreenshot(); } - ngOnInit(){ - if(this.fail == 1) this.reset(); + ngOnInit(): void { + if(this.fail === 1) this.reset(); this.colors = []; this.setDifficulty(); - for(this.k=1 ; this.k <= this.nbColors[this.difficulty] ; this.k++){ + for(this.k = 1 ; this.k <= this.nbColors[this.difficulty] ; this.k++){ this.color = this.generateColorCode(); this.checkColorArray(this.color); @@ -96,10 +99,10 @@ export class EasyPage { } this.goodColor = this.colors[Math.floor(Math.random() * this.colors.length)]; } - goToNavPage() { + goToNavPage(): void { this.navCtrl.pop() } - ionViewWillLeave() { + ionViewWillLeave(): void { this.navParams.get("parentPage").ngOnInit(); } } diff --git a/src/pages/hard/hard.ts b/src/pages/hard/hard.ts index 0280ab5..3ab5176 100644 --- a/src/pages/hard/hard.ts +++ b/src/pages/hard/hard.ts @@ -16,45 +16,45 @@ export class HardPage { nbColors: number[] = [2, 4, 6]; difficulty: number = 0; hexa: string = "#"; - goodColor: string =""; - color: string; - colors: Array = []; + goodColor: string = ""; + colors: Array = []; fail: number = 0; record: number; - - i: number = 0; - k: number = 1; - - getRandomInt(min, max) { + getRandomInt(min: number, max: number): number { return Math.floor(Math.random() * (max - min + 1)) + min; } - generateColorCode(){ - this.i = 0; - this.hexa = "#"; - for( this.i=1 ; this.i <= 6 ; this.i++){ - var randomInt = this.getRandomInt(0, this.possible.length - 1); - this.hexa += this.possible[randomInt]; + generateColorCode(): string { + let hexa: string = "#"; + let len: number = this.possible.length - 1; + + for(let i: number = 1 ; i <= 6 ; i++){ + let randomInt: number = this.getRandomInt(0, len); + hexa += this.possible[randomInt]; } - return this.hexa; + return hexa; } - setDifficulty(){ - if (this.point == 0 && this.point < 5) return this.difficulty = 0; + setDifficulty(): number { + if (this.point === 0 && this.point < 5) return this.difficulty = 0; if (this.point >= 5 && this.point < 10) return this.difficulty = 1; if (this.point >= 10) return this.difficulty = 2; } - checkResponse(userChoice){ - if(userChoice != this.goodColor) return this.wrongResponse(); + checkResponse(userChoice: string): void{ + if(userChoice != this.goodColor) + { + this.wrongResponse(); + return; + } this.point++; this.ngOnInit(); } - wrongResponse(){ + wrongResponse(): void { this.fail = 1; this.defineBestScore(); } - defineBestScore(){ + defineBestScore(): void { this.storage.get('bestScoreHard').then((val) => { if (val == null || this.point > val) { this.storage.set('bestScoreHard', this.point); @@ -63,23 +63,24 @@ export class HardPage { this.record = val; }); } - reset(){ + reset(): void { this.point = 0; this.fail = 0; this.bestPoint = false; } - shareScore(){ + shareScore(): void { this.shareService.shareScreenshot(); } - ngOnInit(){ - if(this.fail == 1) this.reset(); + ngOnInit(): void { + if(this.fail === 1) this.reset(); this.colors = []; this.setDifficulty(); - for(this.k=1 ; this.k <= this.nbColors[this.difficulty] ; this.k++){ - this.color = this.generateColorCode(); - this.colors.push(this.color); + let color:string = ""; + for(let k:number = 1 ; k <= this.nbColors[this.difficulty] ; k++){ + color = this.generateColorCode(); + this.colors.push(color); } this.goodColor = this.colors[Math.floor(Math.random() * this.colors.length)]; } diff --git a/src/pages/nav/nav.ts b/src/pages/nav/nav.ts index a5d41c1..dbc0dd8 100644 --- a/src/pages/nav/nav.ts +++ b/src/pages/nav/nav.ts @@ -27,35 +27,35 @@ export class NavPage { recordNormal: number; recordHard: number; - toggleHelp(difficulty){ + toggleHelp(difficulty: string){ switch(difficulty) { case "easy": { - this.helpEasy = (this.helpEasy == false) ? true : false; + this.helpEasy = !this.helpEasy; break; } case "normal": { - this.helpNormal = (this.helpNormal == false) ? true : false; + this.helpNormal = !this.helpNormal; break; } case "hard": { - this.helpHard = (this.helpHard == false) ? true : false; + this.helpHard = !this.helpHard; break; } case "doom": { - this.helpDoom = (this.helpDoom == false) ? true : false; + this.helpDoom = !this.helpDoom; break; } } } - getBestScore() { - this.storage.get('bestScoreEasy').then((val) => { + getBestScore(): void { + this.storage.get('bestScoreEasy').then((val: number) => { this.recordEasy = val; }); - this.storage.get('bestScoreNormal').then((val) => { + this.storage.get('bestScoreNormal').then((val: number) => { this.recordNormal = val; }); - this.storage.get('bestScoreHard').then((val) => { + this.storage.get('bestScoreHard').then((val: number) => { this.recordHard = val; }); } @@ -65,17 +65,17 @@ export class NavPage { this.slides.slideTo(2, 500); } - goToEasyPage() { + goToEasyPage(): void { this.navCtrl.push(EasyPage, { "parentPage": this }); } - goToNormalPage() { + goToNormalPage(): void { this.navCtrl.push(NormalPage, { "parentPage": this }); } - goToHardPage() { + goToHardPage(): void { this.navCtrl.push(HardPage, { "parentPage": this }); } - ngOnInit(){ + ngOnInit(): void { this.getBestScore(); } } diff --git a/src/pages/normal/normal.ts b/src/pages/normal/normal.ts index 128218c..e1f7987 100644 --- a/src/pages/normal/normal.ts +++ b/src/pages/normal/normal.ts @@ -12,7 +12,7 @@ export class NormalPage { constructor(public navCtrl: NavController, public navParams: NavParams, private shareService: ShareService, private storage: Storage) { } - possible: Array = [ + possible: Array = [ "AliceBlue","Aqua","Aquamarine","Azure","Beige","Black","Blue","BlueViolet","Brown","BurlyWood","CadetBlue","Chartreuse","Chocolate","Coral","CornflowerBlue","Crimson","Cyan","DarkBlue","DarkCyan","DarkGoldenRod","DarkGrey","DarkGreen","DarkKhaki","DarkMagenta","DarkOliveGreen","DarkOrange","DarkOrchid","DarkRed","DarkSalmon","DarkSeaGreen","DarkSlateBlue","DarkSlateGrey","DarkTurquoise","DarkViolet","DeepPink","DeepSkyBlue","DimGrey","DodgerBlue","FireBrick","FloralWhite","ForestGreen","Fuchsia","Gainsboro","GhostWhite","Gold","GoldenRod","Grey","Green","GreenYellow","HotPink","IndianRed","Indigo","Ivory","Khaki","Lavender","LavenderBlush","LawnGreen","LemonChiffon","LightBlue","LightCoral","LightCyan","LightGrey","LightGreen","LightPink","LightSalmon","LightSeaGreen","LightSkyBlue","LightSlateGrey","LightSteelBlue","LightYellow","LimeGreen","Magenta","Maroon","MidnightBlue","Olive","Orange","OrangeRed","Orchid","PaleGreen","PaleTurquoise","PaleVioletRed","Pink","Purple","Red","RoyalBlue","SaddleBrown","Salmon","SeaGreen","Silver","SkyBlue","Snow","SpringGreen","SteelBlue","Teal","Tomato","Turquoise","Violet","White","WhiteSmoke","Yellow","YellowGreen" ]; point: number = 0; @@ -20,9 +20,9 @@ export class NormalPage { nbColors: number[] = [2, 4, 6, 8]; difficulty: number = 0; hexa: string = "#"; - goodColor: string =""; + goodColor: string = ""; color: string; - colors: Array = []; + colors: Array = []; fail: number = 0; testcolor: boolean; record: number; @@ -32,40 +32,44 @@ export class NormalPage { i: number = 0; k: number = 1; - getRandomInt(min, max) { + getRandomInt(min: number, max: number): number { return Math.floor(Math.random() * (max - min + 1)) + min; } - generateColorCode(){ + generateColorCode(): string { this.color = this.possible[this.getRandomInt(0, this.possible.length - 1)]; return this.color; } - setDifficulty(){ - if (this.point == 0 && this.point < 5) return this.difficulty = 0; + setDifficulty(): number { + if (this.point === 0 && this.point < 5) return this.difficulty = 0; if (this.point >= 5 && this.point < 10) return this.difficulty = 1; if (this.point >= 10 && this.point < 15) return this.difficulty = 2; if (this.point >= 15) return this.difficulty = 3; } - checkResponse(userChoice){ + checkResponse(userChoice: string): void { this.activeTimer = false; - if(userChoice != this.goodColor) return this.wrongResponse(); + if(userChoice != this.goodColor) + { + this.wrongResponse(); + return; + } this.point++; setTimeout(() => this.ngOnInit(), 10); } - wrongResponse(){ + wrongResponse(): void { this.fail = 1; this.defineBestScore(); } - checkColorArray(obj){ - for(let i = 0; i<=this.colors.length; i++) { + checkColorArray(obj: string): void { + for(let i = 0; i <= this.colors.length; i++) { if (this.colors[i] == obj){ this.color = this.generateColorCode(); this.checkColorArray(this.color); } } } - defineBestScore(){ + defineBestScore(): void { this.storage.get('bestScoreNormal').then((val) => { if (val == null || this.point > val) { this.storage.set('bestScoreNormal', this.point); @@ -74,36 +78,36 @@ export class NormalPage { this.record = val; }); } - reset(){ + reset(): void { this.point = 0; this.fail = 0; this.bestPoint = false; this.activeTimer = true; } - shareScore(){ + shareScore(): void { this.shareService.shareScreenshot(); } - resetTimer(){ + resetTimer(): void { var page = this; clearTimeout(this.timer); this.timer = setTimeout(function(){ page.wrongResponse(); },3010); } - resetAnimTimer(){ + resetAnimTimer(): void { this.activeTimer = false; setTimeout(() => this.ngOnInit(), 10); } - ngOnInit(){ + ngOnInit(): void { this.activeTimer = true; this.resetTimer(); - if(this.fail == 1) this.reset(); + if(this.fail === 1) this.reset(); this.colors = []; this.setDifficulty(); - for(this.k=1 ; this.k <= this.nbColors[this.difficulty] ; this.k++){ + for(this.k = 1 ; this.k <= this.nbColors[this.difficulty] ; this.k++){ this.color = this.generateColorCode(); this.checkColorArray(this.color); if(this.testcolor){ @@ -113,11 +117,11 @@ export class NormalPage { } this.goodColor = this.colors[Math.floor(Math.random() * this.colors.length)]; } - ionViewWillLeave() { + ionViewWillLeave(): void { this.navParams.get("parentPage").ngOnInit(); clearTimeout(this.timer); } - goToNavPage() { + goToNavPage(): void { this.navCtrl.pop() } } diff --git a/src/providers/share-service.ts b/src/providers/share-service.ts index 1cf3d62..2747f68 100644 --- a/src/providers/share-service.ts +++ b/src/providers/share-service.ts @@ -6,7 +6,7 @@ import { Injectable } from "@angular/core"; export class ShareService { constructor(private socialSharing: SocialSharing, private screenshot: Screenshot) {} - shareScreenshot() { + shareScreenshot(): void { this.screenshot.URI(80) .then((res) => { this.sharePicture(res.URI) @@ -16,7 +16,7 @@ export class ShareService { }); } - sharePicture(picture) { + sharePicture(picture): void { this.socialSharing.share('', '', picture).then(() => {}, () => { alert('SocialSharing failed');