1+ var module = angular . module ( 'scorekeep' ) ;
2+
3+ module . controller ( 'DemoController' , DemoController ) ;
4+ function DemoController ( $scope , $http , $location , SessionCollection , UserCollection , GameCollection , GameHistoryModel , api , $sce ) {
5+ var ddbOutput = "Click the button above to generate traces to AWS DynamoDB." ;
6+ var rdsDefaultOutput = "Click the button above to populate the table and generate traces to AWS RDS." ;
7+ var rdsRunningOutput = "Populating table..." ;
8+ var ddbRunning = false ;
9+ var rdsRunning = false ;
10+ var shouldRunDdbDemo = false ;
11+ var shouldRunRdsDemo = false ;
12+ $scope . gameHistory = [ ] ;
13+
14+ $scope . isRdsConfigured = false ;
15+ var isRdsConfigured = function ( ) {
16+ GameHistoryModel . get ( ) . then (
17+ function ( success ) {
18+ $scope . isRdsConfigured = true ;
19+ } ,
20+ function ( error ) {
21+ $scope . isRdsConfigured = false ;
22+ }
23+ ) ;
24+ } ;
25+ isRdsConfigured ( ) ;
26+
27+ var runDdbDemo = function ( ) {
28+ ddbRunning = true ;
29+ var user1 , user2 , session , game ;
30+ ddbOutput = "Creating users...<br/>" ;
31+
32+ // Sick chaining
33+ UserCollection . createUser ( "random" , null )
34+ . then ( function ( result ) {
35+ console . log ( result ) ;
36+ user1 = result ;
37+ ddbOutput += "Created user " + user1 . name + ".<br/>" ;
38+ return UserCollection . createUser ( "random" , null ) ;
39+ } )
40+ . then ( function ( result ) {
41+ console . log ( result ) ;
42+ user2 = result ;
43+ ddbOutput += "Created user " + user2 . name + ".<br/>" ;
44+ ddbOutput += "Initializing session...<br/>" ;
45+ return SessionCollection . createSession ( null , null ) ;
46+ } )
47+ . then ( function ( result ) {
48+ console . log ( result ) ;
49+ session = result ;
50+ ddbOutput += "Creating tic-tac-toe game...<br/>" ;
51+ return GameCollection . createGame ( session . id , "tic-tac-toe" , "TICTACTOE" ) ;
52+ } )
53+ . then ( function ( result ) {
54+ console . log ( result ) ;
55+ game = result ;
56+ ddbOutput += "Game is about to begin...<br/>" ;
57+ return GameCollection . setUsers ( session . id , game . id , [ user1 . id , user2 . id ] ) ;
58+ } )
59+ . then ( function ( result ) {
60+ console . log ( result ) ;
61+ // Avoid NPE in service
62+ return GameCollection . setField ( session . id , game . id , "rules" , "TICTACTOE" ) ;
63+ } )
64+ . then ( function ( result ) {
65+ console . log ( result ) ;
66+ ddbOutput += "Playing game<br/>" ;
67+ ddbOutput += user1 . name + " made move X1<br/>" ;
68+ return GameCollection . move ( session . id , game . id , user1 . id , "X1" ) ;
69+ } )
70+ . then ( function ( result ) {
71+ console . log ( result ) ;
72+ ddbOutput += user2 . name + " made move O2<br/>" ;
73+ return GameCollection . move ( session . id , game . id , user2 . id , "O2" ) ;
74+ } )
75+ . then ( function ( result ) {
76+ console . log ( result ) ;
77+ ddbOutput += user1 . name + " made move X3<br/>" ;
78+ return GameCollection . move ( session . id , game . id , user1 . id , "X3" ) ;
79+ } )
80+ . then ( function ( result ) {
81+ console . log ( result ) ;
82+ ddbOutput += user2 . name + " made move O4<br/>" ;
83+ return GameCollection . move ( session . id , game . id , user2 . id , "O4" ) ;
84+ } )
85+ . then ( function ( result ) {
86+ console . log ( result ) ;
87+ ddbOutput += user1 . name + " made move X5<br/>" ;
88+ return GameCollection . move ( session . id , game . id , user1 . id , "X5" ) ;
89+ } )
90+ . then ( function ( result ) {
91+ console . log ( result ) ;
92+ ddbOutput += user2 . name + " made move O6<br/>" ;
93+ return GameCollection . move ( session . id , game . id , user2 . id , "O6" ) ;
94+ } )
95+ . then ( function ( result ) {
96+ console . log ( result ) ;
97+ ddbOutput += user1 . name + " made move X7<br/>" ;
98+ return GameCollection . move ( session . id , game . id , user1 . id , "X7" ) ;
99+ } )
100+ . then ( function ( result ) {
101+ console . log ( result ) ;
102+ ddbOutput += user2 . name + " made move O8<br/>" ;
103+ return GameCollection . move ( session . id , game . id , user2 . id , "O8" ) ;
104+ } )
105+ . then ( function ( result ) {
106+ console . log ( result ) ;
107+ ddbOutput += user1 . name + " made move X9<br/>" ;
108+ return GameCollection . move ( session . id , game . id , user1 . id , "X9" ) ;
109+ } )
110+ . then ( function ( result ) {
111+ ddbOutput += "Game Over!<br/>" ;
112+ // Keep repeating
113+ if ( shouldRunDdbDemo ) {
114+ runDdbDemo ( ) ;
115+ } else {
116+ ddbRunning = false ;
117+ }
118+ } ) ;
119+ } ;
120+
121+ var runRdsDemo = function ( ) {
122+ rdsRunning = true ;
123+ GameHistoryModel . create ( )
124+ . then ( function ( result ) {
125+ console . log ( result ) ;
126+ return GameHistoryModel . get ( ) ;
127+ } )
128+ . then ( function ( result ) {
129+ console . log ( result ) ;
130+ $scope . gameHistory = result ;
131+ if ( shouldRunRdsDemo ) {
132+ runRdsDemo ( ) ;
133+ } else {
134+ rdsRunning = false ;
135+ }
136+ } ) ;
137+ }
138+
139+ $scope . getDdbOutput = function ( ) {
140+ return $sce . trustAsHtml ( ddbOutput ) ;
141+ } ;
142+
143+ $scope . getRdsOutput = function ( ) {
144+ var output = rdsRunning ? rdsRunningOutput : rdsDefaultOutput ;
145+ return $sce . trustAsHtml ( output ) ;
146+ } ;
147+
148+ $scope . getDdbDemoPrompt = function ( ) {
149+ if ( shouldRunDdbDemo ) {
150+ return "Stop AWS DynamoDB Demo" ;
151+ } else if ( ddbRunning ) {
152+ return "Finishing AWS DynamoDB Demo" ;
153+ } else {
154+ return "Start AWS DynamoDB Demo" ;
155+ }
156+ } ;
157+
158+ $scope . getRdsDemoPrompt = function ( ) {
159+ if ( shouldRunRdsDemo ) {
160+ return "Stop AWS RDS Demo" ;
161+ } else if ( rdsRunning ) {
162+ return "Finishing AWS RDS Demo" ;
163+ } else {
164+ return "Start AWS RDS Demo" ;
165+ }
166+ } ;
167+
168+ $scope . toggleDdbDemo = function ( ) {
169+ shouldRunDdbDemo = ! shouldRunDdbDemo ;
170+ if ( shouldRunDdbDemo && ! ddbRunning ) {
171+ runDdbDemo ( ) ;
172+ }
173+ } ;
174+
175+ $scope . toggleRdsDemo = function ( ) {
176+ shouldRunRdsDemo = ! shouldRunRdsDemo ;
177+ if ( shouldRunRdsDemo && ! rdsRunning ) {
178+ runRdsDemo ( ) ;
179+ }
180+ } ;
181+ }
0 commit comments