-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathdemo-controller.js
More file actions
181 lines (170 loc) · 5.93 KB
/
demo-controller.js
File metadata and controls
181 lines (170 loc) · 5.93 KB
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
180
181
var module = angular.module('scorekeep');
module.controller('DemoController', DemoController);
function DemoController($scope, $http, $location, SessionCollection, UserCollection, GameCollection, GameHistoryModel, api, $sce) {
var ddbOutput = "Click the button above to generate traces to AWS DynamoDB.";
var rdsDefaultOutput = "Click the button above to populate the table and generate traces to AWS RDS.";
var rdsRunningOutput = "Populating table...";
var ddbRunning = false;
var rdsRunning = false;
var shouldRunDdbDemo = false;
var shouldRunRdsDemo = false;
$scope.gameHistory = [];
$scope.isRdsConfigured = false;
var isRdsConfigured = function() {
GameHistoryModel.get().then(
function(success) {
$scope.isRdsConfigured = true;
},
function(error) {
$scope.isRdsConfigured = false;
}
);
};
isRdsConfigured();
var runDdbDemo = function() {
ddbRunning = true;
var user1, user2, session, game;
ddbOutput = "Creating users...<br/>";
// Sick chaining
UserCollection.createUser("random", null)
.then(function(result) {
console.log(result);
user1 = result;
ddbOutput += "Created user " + user1.name + ".<br/>";
return UserCollection.createUser("random", null);
})
.then(function(result) {
console.log(result);
user2 = result;
ddbOutput += "Created user " + user2.name + ".<br/>";
ddbOutput += "Initializing session...<br/>";
return SessionCollection.createSession(null, null);
})
.then(function(result) {
console.log(result);
session = result;
ddbOutput += "Creating tic-tac-toe game...<br/>";
return GameCollection.createGame(session.id, "tic-tac-toe", "TicTacToe");
})
.then(function(result) {
console.log(result);
game = result;
ddbOutput += "Game is about to begin...<br/>";
return GameCollection.setUsers(session.id, game.id, [user1.id, user2.id]);
})
.then(function(result) {
console.log(result);
// Avoid NPE in service
return GameCollection.setField(session.id, game.id, "rules", "TicTacToe");
})
.then(function(result) {
console.log(result);
ddbOutput += "Playing game<br/>";
ddbOutput += user1.name + " made move X1<br/>";
return GameCollection.move(session.id, game.id, user1.id, "X1");
})
.then(function(result) {
console.log(result);
ddbOutput += user2.name + " made move O2<br/>";
return GameCollection.move(session.id, game.id, user2.id, "O2");
})
.then(function(result) {
console.log(result);
ddbOutput += user1.name + " made move X3<br/>";
return GameCollection.move(session.id, game.id, user1.id, "X3");
})
.then(function(result) {
console.log(result);
ddbOutput += user2.name + " made move O4<br/>";
return GameCollection.move(session.id, game.id, user2.id, "O4");
})
.then(function(result) {
console.log(result);
ddbOutput += user1.name + " made move X5<br/>";
return GameCollection.move(session.id, game.id, user1.id, "X5");
})
.then(function(result) {
console.log(result);
ddbOutput += user2.name + " made move O6<br/>";
return GameCollection.move(session.id, game.id, user2.id, "O6");
})
.then(function(result) {
console.log(result);
ddbOutput += user1.name + " made move X7<br/>";
return GameCollection.move(session.id, game.id, user1.id, "X7");
})
.then(function(result) {
console.log(result);
ddbOutput += user2.name + " made move O8<br/>";
return GameCollection.move(session.id, game.id, user2.id, "O8");
})
.then(function(result) {
console.log(result);
ddbOutput += user1.name + " made move X9<br/>";
return GameCollection.move(session.id, game.id, user1.id, "X9");
})
.then(function(result) {
ddbOutput += "Game Over!<br/>";
// Keep repeating
if (shouldRunDdbDemo) {
runDdbDemo();
} else {
ddbRunning = false;
}
});
};
var runRdsDemo = function() {
rdsRunning = true;
GameHistoryModel.create()
.then(function(result) {
console.log(result);
return GameHistoryModel.get();
})
.then(function(result) {
console.log(result);
$scope.gameHistory = result;
if (shouldRunRdsDemo) {
runRdsDemo();
} else {
rdsRunning = false;
}
});
}
$scope.getDdbOutput = function() {
return $sce.trustAsHtml(ddbOutput);
};
$scope.getRdsOutput = function() {
var output = rdsRunning ? rdsRunningOutput : rdsDefaultOutput;
return $sce.trustAsHtml(output);
};
$scope.getDdbDemoPrompt = function() {
if (shouldRunDdbDemo) {
return "Stop AWS DynamoDB Demo";
} else if (ddbRunning) {
return "Finishing AWS DynamoDB Demo";
} else {
return "Start AWS DynamoDB Demo";
}
};
$scope.getRdsDemoPrompt = function() {
if (shouldRunRdsDemo) {
return "Stop AWS RDS Demo";
} else if (rdsRunning) {
return "Finishing AWS RDS Demo";
} else {
return "Start AWS RDS Demo";
}
};
$scope.toggleDdbDemo = function() {
shouldRunDdbDemo = !shouldRunDdbDemo;
if (shouldRunDdbDemo && !ddbRunning) {
runDdbDemo();
}
};
$scope.toggleRdsDemo = function() {
shouldRunRdsDemo = !shouldRunRdsDemo;
if (shouldRunRdsDemo && !rdsRunning) {
runRdsDemo();
}
};
}