Skip to content

Commit

Permalink
Add new codes
Browse files Browse the repository at this point in the history
  • Loading branch information
alvachien committed Apr 7, 2024
1 parent 2090787 commit b76d7d7
Show file tree
Hide file tree
Showing 26 changed files with 133 additions and 6 deletions.
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"bootstrap": "^5.3.0",
"echarts": "^5.4.1",
"echarts-wordcloud": "^2.0.0",
"howler": "^2.2.4",
"html2canvas": "^1.4.1",
"jspdf": "^2.5.1",
"katex": "^0.16.0",
Expand All @@ -66,6 +67,7 @@
"@angular/cli": "^16.1.4",
"@angular/compiler-cli": "^16.1.4",
"@types/echarts": "^4.9.16",
"@types/howler": "^2.2.11",
"@types/jasmine": "^4.3.0",
"@types/marked": "^4.0.8",
"@types/node": "^18.14.0",
Expand Down
3 changes: 2 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { NgxEchartsModule } from 'ngx-echarts';
import * as echarts from 'echarts';
import { NuMonacoEditorModule } from '@ng-util/monaco-editor';

import { ODataService, QuizService, UIUtilityService, AuthGuardService, EnglishLearningService } from './services';
import { ODataService, QuizService, UIUtilityService, AuthGuardService, EnglishLearningService, AudioService } from './services';
import { environment } from 'src/environments/environment';
import { NavItemFilterPipe } from './pipes';
import { QuizFailureDailogComponent } from './pages/quiz-failure-dailog';
Expand Down Expand Up @@ -59,6 +59,7 @@ import { AuthConfigModule } from './auth/auth-config.module';
UIUtilityService,
QuizService,
EnglishLearningService,
AudioService,
AuthGuardService,
{
provide: TRANSLOCO_CONFIG,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<div class="type-game">
<!-- <canvas id="type-game-canvas"></canvas> -->
<div class="typing-container">
<span *ngFor="let echar of chararray">
{{echar.visible ? echar.letter : '_'}}
</span>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.typing-container {
font-size: 72px;
display: flex;
justify-content: center;
height: 250px;
align-items: center;
}
61 changes: 58 additions & 3 deletions src/app/pages/puzzle-game/typing-game/typing-game.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,82 @@ import {
Output,
ViewChild,
} from '@angular/core';
import { AudioService } from 'src/app/services';

export interface typingCompare {
expected: string;
inputted: string;
}

interface typingWord {
visible: boolean;
letter: string;
}

@Component({
selector: 'khb-typing-game',
templateUrl: './typing-game.component.html',
styleUrls: ['./typing-game.component.scss'],
})
export class TypingGameComponent implements OnInit, AfterViewInit {
// @ViewChild('type-game-canvas', { static: true }) canvasTypeGame!: ElementRef;
srcstring = 'fighter';
private _arwords: typingWord[] = [];
private _keyidx = -1;

get chararray(): typingWord[] {
return this._arwords;
}

constructor() {
@HostListener('document:keyup', ['$event'])
handleKeyboardEvent(event: KeyboardEvent) {
// if (event.key == 'Tab' && this.lastField == 'subject') {
// var el: any = document.querySelectorAll('.ql-editor')

// if (el && el.length == 1) {
// el[0].focus();
// this.lastField = 'body';
// }
// }
console.log(event.key);
if (this._keyidx >= 0 && this._keyidx < this._arwords.length) {
if (event.key === 'Backspace') {
this._keyidx--;
if (this._keyidx >= 0) {
this._arwords[this._keyidx].visible = false;
} else {
this._keyidx = 0;
}
} else {
if (event.key === this._arwords[this._keyidx].letter) {
this._arwords[this._keyidx].visible = true;
this._keyidx++;
} else {
// Sending the error indicator.
this.audiosrv.playSound('beep.wav');
}
}
}
}

constructor(private audiosrv: AudioService) {
// Constructor
}

ngOnInit(): void {
// On Init.
const archars = this.srcstring.split('');
archars.forEach((val) => {
this._arwords.push({
visible: false,
letter: val,
});
});
}

ngAfterViewInit(): void {
// After view init.
if (this._arwords.length > 0) {
this._keyidx = 0;
}
}

}
16 changes: 16 additions & 0 deletions src/app/services/audio-service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { of } from 'rxjs';

import { AudioService } from './audio-service';

describe('AudioService', () => {
let service: AudioService;

beforeEach(() => {
service = TestBed.inject(AudioService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
19 changes: 19 additions & 0 deletions src/app/services/audio-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Injectable } from '@angular/core';
import { Howl, Howler } from 'howler';

const TYPING_SOUND_URL = 'assets/typing/sounds/';

@Injectable({
providedIn: 'root',
})
export class AudioService {
playSound(filename: string): void {
const path = TYPING_SOUND_URL + filename;
const sound = new Howl({
src: path,
format: ['wav'],
});
Howler.volume(1);
sound.play();
}
}
1 change: 1 addition & 0 deletions src/app/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './uiutility.service';
export * from './auth-guard.service';
export * from './auth.service';
export * from './english-learning.service';
export * from './audio-service';
Binary file added src/assets/typing/sounds/beep.wav
Binary file not shown.
Binary file added src/assets/typing/sounds/click.wav
Binary file not shown.
Binary file added src/assets/typing/sounds/correct.wav
Binary file not shown.
Binary file added src/assets/typing/sounds/key-sound/Alpacas.mp3
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added src/assets/typing/sounds/key-sound/Default.wav
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added src/assets/typing/sounds/key-sound/Topre.mp3
Binary file not shown.
Binary file not shown.

0 comments on commit b76d7d7

Please sign in to comment.