Files
chocomae/src/game/typing/lib/typing_text_manager.js
T

239 lines
6.5 KiB
JavaScript

function TypingTextManager() {
this.init();
}
TypingTextManager.prototype.init = function() {
this.contents = [];
}
TypingTextManager.prototype.getContents = function() {
return this.contents;
}
TypingTextManager.prototype.add = function(arr) {
this.contents = this.contents.concat(arr);
}
TypingTextManager.prototype.slice = function(start, end) {
this.contents = this.contents.slice(start, end);
}
TypingTextManager.prototype.getShuffledArray = function(arr) {
return Phaser.ArrayUtils.shuffle(arr);
}
TypingTextManager.prototype.fillLeftRightBasicKey = function() {
var length = this.contents.length;
// console.log(length);
for(var i = length - 1; i > -1; i--) {
var char = this.contents[i];
// console.log(char);
if(this.isKoreanConsonant(char)) {
this.contents.splice(i + 1, 0, "ㅓ");
} else if(this.isKoreanVowel(char)) {
this.contents.splice(i, 0, "ㄹ");
} else if(this.isEnglishLeftHand(char)) {
this.contents.splice(i + 1, 0, "j");
} else if(this.isEnglishRightHand(char)) {
this.contents.splice(i, 0, "f");
}
}
}
TypingTextManager.prototype.isKoreanConsonant = function(char) {
if(char.length != 1)
return false;
if(TypingTextManager.KOREAN_LETTER_CHO.indexOf(char) > -1)
return true;
else if(TypingTextManager.KOREAN_LETTER_JONG.indexOf(char) > -1)
return;
return false;
}
TypingTextManager.prototype.isKoreanVowel = function(char) {
if(char.length != 1)
return false;
if(TypingTextManager.KOREAN_LETTER_JUNG.indexOf(char) > -1)
return true;
return false;
}
TypingTextManager.prototype.isEnglishLeftHand = function(char) {
if(char.length != 1)
return false;
if(TypingTextManager.ENGLISH_LETTER_LEFT.indexOf(char) > -1)
return true;
return false;
}
TypingTextManager.prototype.isEnglishRightHand = function(char) {
if(char.length != 1)
return false;
if(TypingTextManager.ENGLISH_LETTER_RIGHT.indexOf(char) > -1)
return true;
return false;
}
TypingTextManager.prototype.makePracticeContents = function(arr, repeatCount) {
this.init();
this.add(arr);
for(var i = 0; i < repeatCount; i++) {
this.add(this.getShuffledArray(arr));
}
this.fillLeftRightBasicKey();
}
TypingTextManager.prototype.makeTestContents = function(arr) {
this.init();
this.add(this.getShuffledArray(arr));
}
TypingTextManager.prototype.makeExaminationContents = function(arr) {
this.init();
// getExaminationArray : slice too long sentence by proper words and last space
// this.add(arr);
for(var i = 0; i < arr.length; i++) {
var sentence = arr[i];
if(sentence.length == 0)
continue;
if(sentence.length > TypingTextManager.SENTENCE_MAX_CHARACTER)
this.add(this.getSplitedSentences(sentence));
else
this.add(sentence);
}
}
TypingTextManager.prototype.isWordTooLong = function(word) {
var uniCharCount = StringUtil.getUnicodeAlphabetCount(word);
// console.log("word : " + word + "*");
// console.log("uniCharCount : " + uniCharCount);
if(uniCharCount > TypingTextManager.SENTENCE_MAX_CHARACTER)
return true;
return false;
}
TypingTextManager.prototype.isNotEmptySentence = function(sentence) {
if(sentence.legnth > 0)
return true;
return false;
}
/*
TypingTextManager.prototype.getSplitedWord = function(word, splitIndex) {
var startIndex = splitIndex * TypingTextManager.SENTENCE_MAX_CHARACTER;
var endIndex = (splitIndex + 1) * TypingTextManager.SENTENCE_MAX_CHARACTER;
return word.substring(startIndex, endIndex);
}
*/
TypingTextManager.prototype.getSplitedSentences = function(sentence) {
var words = sentence.split(" ");
var wordCount = words.length;
var splitedSentences = [];
var sentenceIndex = 0;
splitedSentences[sentenceIndex] = "";
// console.log(sentence);
// console.log(words);
for(var i = 0; i < wordCount; i++) {
// console.log(words[i]);
// console.log(splitedSentences[sentenceIndex].length);
var wordUniCharCount = StringUtil.getUnicodeAlphabetCount(words[i]);
// if a word is longer than SENTENCE_MAX_CHARACTER
// then split the word with SENTENCE_MAX_CHARACTER length
if(this.isWordTooLong(words[i])) {
// console.log(words[i]);
if(this.isNotEmptySentence(splitedSentences[sentenceIndex]))
sentenceIndex++; // start with new empty sentence
var tooLongWord = words[i];
while(typeof tooLongWord != "undefined" && tooLongWord.length > 0) {
// console.log("tooLongWord before : " + tooLongWord);
var multibyteIndex = StringUtil.getMultibyteIndex(tooLongWord, TypingTextManager.SENTENCE_MAX_CHARACTER);
// console.log("multibyteIndex : " + multibyteIndex);
var splitedSentence = tooLongWord.substring(0, multibyteIndex);
// console.log("splitedSentence : " + splitedSentence);
tooLongWord = tooLongWord.substring(multibyteIndex);
// console.log("tooLongWord : after " + tooLongWord);
splitedSentences[sentenceIndex] = splitedSentence;
sentenceIndex++;
}
continue;
}
// console.log("splitedSentences[" + sentenceIndex + "] : " + splitedSentences[sentenceIndex]);
var shortSentenceUniCharCount = StringUtil.getUnicodeAlphabetCount(splitedSentences[sentenceIndex]);
if(shortSentenceUniCharCount + wordUniCharCount < TypingTextManager.SENTENCE_MAX_CHARACTER)
if(i < wordCount - 1)
splitedSentences[sentenceIndex] = splitedSentences[sentenceIndex].concat(words[i], " ");
else // last word
splitedSentences[sentenceIndex] = splitedSentences[sentenceIndex].concat(words[i]);
else {
sentenceIndex++;
if(i < wordCount - 1)
splitedSentences[sentenceIndex] = words[i] + " ";
else // last word
splitedSentences[sentenceIndex] = words[i];
}
}
return splitedSentences;
}
TypingTextManager.shuffledArray = function(arr) {
return Phaser.ArrayUtils.shuffle(arr);
}
TypingTextManager.KOREAN_LETTER_CHO = [
"ㄱ", "ㄲ", "ㄴ", "ㄷ", "ㄸ", "ㄹ", "ㅁ", "ㅂ", "ㅃ", "ㅅ",
"ㅆ", "ㅇ", "ㅈ", "ㅉ", "ㅊ", "ㅋ", "ㅌ", "ㅍ", "ㅎ"
];
TypingTextManager.KOREAN_LETTER_JUNG = [
"ㅏ", "ㅐ", "ㅑ", "ㅒ", "ㅓ", "ㅔ", "ㅕ", "ㅖ", "ㅗ", "ㅘ",
"ㅙ", "ㅚ", "ㅛ", "ㅜ", "ㅝ", "ㅞ", "ㅟ", "ㅠ", "ㅡ", "ㅢ", "ㅣ"
];
TypingTextManager.KOREAN_LETTER_JONG = [
"", "ㄱ", "ㄲ", "ㄳ", "ㄴ", "ㄵ", "ㄶ", "ㄷ", "ㄹ", "ㄺ",
"ㄻ", "ㄼ", "ㄽ", "ㄾ", "ㄿ", "ㅀ", "ㅁ", "ㅂ", "ㅄ", "ㅅ",
"ㅆ", "ㅇ", "ㅈ", "ㅊ", "ㅋ", "ㅌ", "ㅍ", "ㅎ"
];
TypingTextManager.ENGLISH_LETTER_LEFT = [
"q", "w", "e", "r", "t",
"a", "s", "d", "f", "g",
"z", "x", "c", "v", "b"
];
TypingTextManager.ENGLISH_LETTER_RIGHT = [
"y", "u", "i", "o", "p",
"h", "j", "k", "l",
"n", "m"
];
TypingTextManager.SENTENCE_MAX_CHARACTER = 48;