-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryState.js
179 lines (167 loc) · 4.2 KB
/
MemoryState.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//Game possible States:
// oneFlipped
// matched
// notMatched
// noFlipped
// winnder
var BaseState = Backbone.Model.extend({
initialize: function(owner) {
this.owner = owner;
},
enter: function() {
// Implement me in your state objects
},
execute: function() {
// Implement me in your state objects
},
oneFlip:function(card){
console.log('changing to the oneflipped state...');
this.owner.changeState(this.owner.states.oneFlipped,card);
},
match:function(){
console.log('changing to the matched state...');
this.owner.changeState(this.owner.states.matched);
},
notMatched:function(){
console.log('changing to the notmatched state...');
this.owner.changeState(this.owner.states.notMatched);
},
noFlip:function(){
console.log('changing to the noFlipped state...');
this.owner.changeState(this.owner.states.noFlipped);
},
win:function(){
console.log('changing to the winner state...');
this.owner.changeState(this.owner.states.winner);
},
exit:function(){
// Implement me in your state objects
}
})
var oneFlippedState = BaseState.extend({
card:undefined,
enter:function(card){
if (card)
this.card = card;
console.log('enter in oneFlippedState');
console.log('you flipped ' + this.card)
},
execute:function(){
if (this.card)
console.log('executing oneFlippedState');
},
exit:function(){
if (this.card)
//this.card.render();
console.log('exit oneFlippedState')
},
oneFlip:function(card){
console.log('already in state oneflip... Go to match or not matched here?');
}
});
var twoFlippedState = BaseState.extend({
card:undefined,
enter:function(card){
if (card)
this.card = card;
console.log('enter in 2FlippedState');
console.log('you flipped ' + this.card)
},
execute:function(){
if (this.card)
console.log('executing 2FlippedState');
},
exit:function(){
if (this.card)
console.log('exit 2FlippedState')
},
twoflip:function(card){
console.log('already in state tw0flip... Go to match or not matched here?');
}
});
//This Model depends on global var pairsOfCards
var matchedState = BaseState.extend({
matches:[],
enter:function(){
console.log('enter in matchedState');
},
execute:function(card, callback){
console.log('executing matchedState: set the matched card...');
this.matches.push(card);
//if matched length is 6, winner. otherwise, noflip.
if(this.matches.length===pairsOfCards){
this.win();
if (callback) {
callback({win: true});
}
}
else
this.noFlip();
//this.exit(); exiting should be done from the state. All evalutation code should go here.
},
exit:function(){
console.log('exit matched')
},
match:function(){
console.log('already in state matched...');
}
});
var notMatchedState = BaseState.extend({
enter:function(){
console.log('enter in notMatchedState');
},
execute:function(){
console.log('executing notMatchedState');
console.log('go to noFlipState')
this.noFlip()
},
exit:function(){
console.log('exit notMatchedState')
},
notMatched:function(){
console.log('already in state notMatched... ');
}
});
var noFlipState = BaseState.extend({
enter:function(){
console.log('enter in noFlipState');
},
execute:function(){
//console.log('executing noFlipState');
},
exit:function(){
console.log('exit noFlipState')
},
noFlip:function(){
console.log('already in state oneflip... Go to match or not matched here?');
}
});
var winnerState = BaseState.extend({
enter:function(){
console.log('enter in winnerState');
},
execute:function(){
//console.log('executing winnerState');
//Send Data to Keen
var cookieData = $.cookie("Lol-Cat_Memory_cookie");
var now =new Date().getTime()/1000;
var length = now - cookieData["session_start_time"];
console.log("Length is "+length)
if(isNaN(length) || length < 0)
length = -1;
var winstate = {
winner: "true",
game_length: length,
session_id: $.cookie("Lol-Cat_Memory_cookie")["id"],
permanent_id: $.cookie("Lol-Cat_Memory__permanent_cookie")["id"]
}
console.log(winstate)
// Keen.addEvent('Lolcat-MemoryWinner', winstate)
},
exit:function(){
console.log('exit winnerState')
},
win:function(){
console.log('already in state win');
}
});