forked from preboot/angularjs-webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
127 lines (107 loc) · 3.36 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import angular from 'angular';
import uiBootstrap from 'angular-ui-bootstrap';
import firebase from 'firebase';
import '../style/app.css';
// Set the firebase configuration for your app
const config = {
apiKey: "AIzaSyAOnQKBQ1O8MredgNX5rBO9tqyzkGcL8ik",
authDomain: "wordgameangularjs.firebaseapp.com",
databaseURL: "https://wordgameangularjs.firebaseio.com/",
storageBucket: "gs://wordgameangularjs.appspot.com"
};
firebase.initializeApp(config);
// Get a reference to the database service
const database = firebase.database();
let app = () => {
return {
template: require('./app.html'),
controller: 'AppCtrl',
controllerAs: 'appCtrl'
}
};
class AppCtrl {
constructor($scope, $interval, $q) {
this.$scope = $scope;
this.$interval = $interval;
this.$q = $q;
this.wordIncrement = 0;
this.mistakes = 0;
$scope.score = 0;
$scope.verifyWord = this.onVerifyWord.bind(this);
$scope.verifyIfDelete = this.onVerifyIfDelete.bind(this);
$scope.time = 40;
$scope.startGame = this.onStartGame.bind(this);
$scope.showLoadingScreen = true;
this.getData();
}
getData() {
const wordsPromise = database.ref('/words/').once('value').then((snapshot) => snapshot.val());
const usersPromise = database.ref('/players/').once('value').then((snapshot) => snapshot.val());
this.$q.all([wordsPromise, usersPromise]).then(values => {
this.$scope.showLoadingScreen = false;
this.wordList = this.shuffle(values[0]);
this.$scope.players = values[1];
this.randomScrambledWord = this.getRandomScrambledWord();
this.randomSolvedWord = this.getRandomSolvedWord();
});
}
onStartGame() {
if (!this.interval) {
this.interval = this.$interval(() => {
if (this.$scope.time > 0) {
this.$scope.time--;
} else {
clearInterval(this.interval);
console.log('game over');
}
}, 1000);
}
}
onVerifyIfDelete($event) {
if (
($event.keyCode === 8 || $event.keyCode === 46) &&
this.$scope.score > 0 &&
this.$scope.word !== ''
) {
this.$scope.score--;
}
}
onVerifyWord() {
if (this.$scope.word.toLowerCase() === this.randomSolvedWord) {
this.$scope.correct = true;
this.$scope.wrong = false;
this.$scope.score += this.calculateScore(this.randomSolvedWord, this.mistakes);
this.mistakes = 0;
this.wordIncrement++;
this.randomScrambledWord = this.getRandomScrambledWord();
this.randomSolvedWord = this.getRandomSolvedWord();
this.$scope.word = '';
} else {
this.$scope.correct = false;
this.$scope.wrong = true;
}
}
shuffle(array) {
for (let i = array.length; i; i--) {
let j = Math.floor(Math.random() * i);
[array[i - 1], array[j]] = [array[j], array[i - 1]];
}
return array;
}
getRandomSolvedWord() {
return this.wordList[this.wordIncrement];
}
getRandomScrambledWord() {
return this.shuffle(this.getRandomSolvedWord().split('')).join('')
}
calculateScore(word, mistakesCount) {
const maxScore = Math.floor(Math.pow(1.95, parseInt(word.length / 3)));
const finalScore = maxScore - mistakesCount;
return finalScore < 0 ? 0 : finalScore;
}
}
const MODULE_NAME = 'app';
angular.module(MODULE_NAME, [uiBootstrap])
.directive('app', app)
.controller('AppCtrl', AppCtrl);
export default MODULE_NAME;