///////////////////////////// // TypingTest class TypingTest { create() { self = this; this.initTypingData(); sessionStorageManager.isNewBestRecrd = false; game.stage.backgroundColor = '#4d4d4d'; // top let backButton = new BackButton( () => { sessionStorageManager.resetPlayingAppData(); location.href = '../../web/client/menu_typing_test.html'; }); this.averageTypingSpeedText = new AverageTypingSpeed(); this.contentProgressText = new ContentProgress(); let fullscreenButton = new FullscreenButton(game); // typing content var typingAreaPositionY = 260; var bar = game.add.graphics(); bar.beginFill(0x000000, 0.2); bar.drawRect(0, typingAreaPositionY, GAME_SCREEN_SIZE.x, 120); if(isTypingWordStage()) textStyleBasic.font = "bold 84px Arial"; else // Sentence stage textStyleBasic.font = "bold 48px Arial"; this.textTypingContent = game.add.text(0, 2, "test", textStyleBasic) .setTextBounds(0, typingAreaPositionY, GAME_SCREEN_SIZE.x, 120) .setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2) .addColor(TypingTest.COLOR_CONTENT_WRONG, 0); textStyleBasic.font = "32px Arial"; var textDoneColor = [ '#99994d', '#77774d', '#66664d' ]; this.textTypingContentsDone = []; this.textTypingRecordsDone = []; for(var i = 0; i < TypingTest.TYPING_CONTENT_DONE_COUNT; i++) { this.textTypingContentsDone[i] = game.add.text(0, 2, "test", textStyleBasic) .setTextBounds(0, typingAreaPositionY - 50 - i * 50, GAME_SCREEN_SIZE.x, 40) .setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2) .addColor(textDoneColor[i], 0); var scorePositionX = 780; if(isTypingWordStage()) scorePositionX = 600; this.textTypingRecordsDone[i] = game.add.text(0, 2, "", textStyleBasic) .setTextBounds(scorePositionX, typingAreaPositionY - 50 - i * 50, 200, 40) .setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2) .addColor(textDoneColor[i], 0); } var textPreviewColor = [ '#999999', '#888888', '#777777' ]; this.textTypingContentPreview = []; for(var i = 0; i < TypingTest.TYPING_CONTENT_PREVIEW_COUNT; i++) { this.textTypingContentPreview[i] = game.add.text(0, 2, "test", textStyleBasic) .setTextBounds(0, typingAreaPositionY + 130 + i * 50, GAME_SCREEN_SIZE.x, 40) .setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2) .addColor(textPreviewColor[i], 0); } // input text this.inputTextContent = new InputTypeText(game.world.centerX, typingAreaPositionY + 360); this.inputTextContent.anchor.set(0.5); this.inputTextContent.canvasInput.value(''); this.inputTextContent.canvasInput.focus(); // inputTextContent.canvasInput._onkeydown = this.checkInputText; // inputTextContent.canvasInput._onkeyup = function() { this.inputTextContent.canvasInput._onkeyup = function() { self.checkTypingContents(event); } // bottom let screenBottom = new ScreenBottom(); screenBottom.makeBottomLine(); screenBottom.printBottomLeftTextWithBestRecord(sessionStorageManager.bestRecord); screenBottom.printBottomCenterText(sessionStorageManager.playingAppKoreanName); screenBottom.printBottomRightText(sessionStorageManager.playerName); this.startGame(); // this.countDown(); } /* countDown() { const style = { font: "bold 200px Arial", fill: "#fff", boundsAlignH: "center", boundsAlignV: "middle" }; this.countDownText = game.add.text(0, 0, "", style); this.countDownText.setTextBounds(0, 0, game.world.width, game.world.height); this.countDownText.stroke = "#333"; this.countDownText.strokeThickness = 50; this.countDownNumber = 3; if(isDebugMode()) this.countDownNumber = 1; this.tweenCountDown(); } tweenCountDown() { if(this.countDownNumber === 0) { this.startGame(); return; } this.countDownText.text = this.countDownNumber.toString(); this.countDownText.alpha = 1; let countDownTween = game.add.tween(this.countDownText); countDownTween.to( { alpha: 0 }, GAME_SCREEN_SIZE.x, Phaser.Easing.Linear.None, true); countDownTween.onComplete.add(this.tweenCountDown, this); this.countDownNumber--; } */ startGame() { this.averageTypingSpeedText.print(0); this.showTypingTestContents(); this.showPlayingWordNumber(); } gameOver() { let gameOverText = new GameOverText(); game.time.events.add(Phaser.Timer.SECOND * 2, this.goResult, this); } goResult() { sessionStorageManager.record = this.typingRecordTotal; if(sessionStorageManager.record > sessionStorageManager.bestRecord) sessionStorageManager.bestRecord = sessionStorageManager.record; location.href = '../../web/client/result.html'; } initTypingData() { this.typingContents = this.getTypingTestContents(); var randomContents = Phaser.ArrayUtils.shuffle(this.typingContents); this.wordCountForStage = TypingTest.WORD_COUNT_FOR_STAGE; if(isDebugMode()) this.wordCountForStage = 3; this.typingRandomContents = randomContents.slice(1, this.wordCountForStage + 1); // start Num, end Num (not index) this.typingContentLength = this.typingContents.length; this.typingIndex = 0; this.isTyping = false; this.timeTypingStart = 0; this.timeTypingEnd = 0; this.typingLetterCountTotal = 0; this.typingRecordTotal = 0; } getTypingTestContents() { var typingTestContents = this.loadTestContent(); // console.log('playingStageData.language : ' + playingStageData.language); // console.log('playingStageData.level : ' + playingStageData.level); // console.log('typingTestContents : ' + typingTestContents); this.typingRecordForLines = []; this.typingElapsedTime = []; for(var i = 0; i < typingTestContents.length; i++) { this.typingRecordForLines[i] = 0; this.typingElapsedTime[i] = 0; } return typingTestContents; } loadTestContent() { var testContent = []; switch(sessionStorageManager.playingAppName) { case "test_korean_word": case "practice_korean_word": testContent = koreanWordList; break; case "test_korean_sentence": case "practice_korean_sentence": testContent = koreanSentenceList; break; case "test_english_word": case "practice_english_word": testContent = englishWordList; break; case "test_english_sentence": case "practice_english_sentence": testContent = englishSentenceList; break; } return testContent; } showTypingTestContents() { for(var i = 0; i < TypingTest.TYPING_CONTENT_DONE_COUNT; i++) { var doneIndex = this.typingIndex - i - 1; if(doneIndex < 0) { this.textTypingContentsDone[i].text = ""; this.textTypingRecordsDone[i].text = ""; } else { this.textTypingContentsDone[i].text = this.typingRandomContents[doneIndex]; this.textTypingRecordsDone[i].text = Math.floor(this.typingRecordForLines[doneIndex]); } } if(this.typingIndex == this.wordCountForStage) { this.textTypingContent.text = ""; return; } this.textTypingContent.text = this.typingRandomContents[this.typingIndex]; for(var i = 0; i < TypingTest.TYPING_CONTENT_PREVIEW_COUNT; i++) { var previewIndex = this.typingIndex + i + 1; if(previewIndex < this.typingRandomContents.length) this.textTypingContentPreview[i].text = this.typingRandomContents[previewIndex]; else this.textTypingContentPreview[i].text = ""; } } resetTypingContent() { this.textTypingContent.clearColors(); this.textTypingContent.addColor(TypingTest.COLOR_CONTENT_WRONG, 0); this.inputTextContent.canvasInput.value(''); } checkTypingContents(evnet) { if(event.keyCode == Phaser.Keyboard.ENTER) { // console.log("### enter ###"); var inputContent = this.inputTextContent.canvasInput.value(); var typingContent = this.typingRandomContents[this.typingIndex]; if(inputContent == typingContent) { this.calculateTypingRecord(typingContent); this.playNextContent(); if(this.typingIndex == this.wordCountForStage) this.goResult(); // this.state.start('TypingTestResult'); } } else { // console.log(this.inputTextContent.canvasInput.value()); if(this.isTyping == false) { // console.log(event); this.setTimeTypingStart(); } this.showTypingContentHighlight(); } } showTypingContentHighlight() { var typingContent = this.typingRandomContents[this.typingIndex]; var inputContent = this.inputTextContent.canvasInput.value(); this.textTypingContent.clearColors(); this.textTypingContent.addColor(TypingTest.COLOR_CONTENT_RIGHT, 0); if(typingContent == null) return; var typingContentLength = typingContent.length; for(var i = 0; i < typingContentLength; i++) { // console.log("typingContent.charAt(i) : " + typingContent.charAt(i)); // console.log("inputContent.charAt(i) : " + inputContent.charAt(i)); if(typingContent.charAt(i) != inputContent.charAt(i)) { this.textTypingContent.addColor(TypingTest.COLOR_CONTENT_WRONG, i); return; } } } calculateTypingRecord(typingContent) { // console.log('입력 단어 : ' + this.typingRandomContents[this.typingIndex]); this.isTyping = false; this.timeTypingEnd = game.time.elapsedSince(0); // console.log('this.timeTypingEnd : ' + this.timeTypingEnd); var elapsedTimeMS = this.timeTypingEnd - this.timeTypingStart; // console.log('elapsedTimeMS : ' + elapsedTimeMS); var typingContentLength = StringUtil.getUnicodeAlphabetCount(typingContent); // console.log('typingContentLength : ' + typingContentLength); var typingContentPlusEnterCount = typingContentLength + TypingTest.TYPING_COUNT_PLUS_ENTER; var typingRecord = (typingContentPlusEnterCount + TypingTest.TYPING_COUNT_PLUS_ENTER) * 60000 / elapsedTimeMS; this.typingElapsedTime[this.typingIndex] = elapsedTimeMS; this.typingLetterCountTotal += typingContentPlusEnterCount; this.typingRecordForLines[this.typingIndex] = typingRecord; // console.log(this.typingRecordForLines[this.typingIndex]); var typingElapsedTimeTotal = 0; for(let i = 0; i <= this.typingIndex; i++) { // console.log('i : ' + i + ' - time : ' + this.typingElapsedTime[i]); typingElapsedTimeTotal += this.typingElapsedTime[i]; } this.typingRecordTotal = this.typingLetterCountTotal * 60000 / typingElapsedTimeTotal; // console.log(this.typingLetterCountTotal); // console.log(typingElapsedTimeTotal); // console.log(typingRecordTotal); // this.textTypingRecordTotal.text = Math.floor(typingRecordTotal); this.averageTypingSpeedText.print(Math.floor(this.typingRecordTotal)); // console.log('-------------- total --------------'); // console.log('typingLetterCountTotal : ' + this.typingLetterCountTotal); // console.log('typingElapsedTimeTotal : ' + typingElapsedTimeTotal); // console.log('typingRecordTotal : ' + typingRecordTotal); // console.log('-----------------------------------'); } playNextContent() { this.typingIndex++; this.showTypingTestContents(); this.showPlayingWordNumber(); this.resetTypingContent(); } showPlayingWordNumber() { this.contentProgressText.print(this.typingIndex + 1, this.wordCountForStage); } setTimeTypingStart() { this.timeTypingStart = game.time.elapsedSince(0); var timeGap = this.timeTypingStart - this.timeTypingEnd; if(timeGap < 50) return; this.isTyping = true; // console.log('last End : ' + this.timeTypingEnd); // console.log('this.timeTypingStart : ' + this.timeTypingStart); // console.log('time gap : ' + (this.timeTypingStart - this.timeTypingEnd)); } } TypingTest.TYPING_CONTENT_PREVIEW_COUNT = 3; TypingTest.TYPING_CONTENT_DONE_COUNT = 3; TypingTest.TYPING_COUNT_PLUS_ENTER = 1; TypingTest.OFFSET_RIGHT_ALIGN = 10; TypingTest.WORD_COUNT_FOR_STAGE = 10; // if(isDebugMode()) // WORD_COUNT_FOR_STAGE = 3; TypingTest.COLOR_CONTENT_WRONG = "#aaaaaa"; TypingTest.COLOR_CONTENT_RIGHT = "#ffff4d";