-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathsession-controller.js
More file actions
101 lines (98 loc) · 3.74 KB
/
session-controller.js
File metadata and controls
101 lines (98 loc) · 3.74 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
var module = angular.module('scorekeep');
module.controller('SessionController', Session);
function Session($scope, $http, $location, $interval, $routeParams, SessionService, RulesService, GameCollection, UserService, GameService, api) {
$scope.games = GameService.query({ sessionid: $routeParams.sessionid });
$scope.session = new SessionService;
$scope.user = UserService.get({ id: $routeParams.userid });
$scope.allrules = RulesService.query();
$scope.loadSession = function() {
GetSession = $scope.games.$promise.then(function(result) {
return $scope.session.$get({ id: $routeParams.sessionid });
});
GetSession.then(function() {
// identify new games
gameids = []
if ( $scope.games == null ) {
$scope.games = [];
}
for (var i = 0; i < $scope.games.length; i++) {
// if the game has been removed from the session
if ( !$scope.session.games.includes($scope.games[i].id) ) {
$scope.games.splice(i, 1);
} else {
gameids.push($scope.games[i].id);
}
}
if ( $scope.session.games == null ) {
$scope.session.games = [];
}
for (var i = 0; i < $scope.session.games.length; i++) {
if ( !gameids.includes($scope.session.games[i]) ) {
console.log("new game id: " + $scope.session.games[i]);
game = new GameService;
game.$get({ id: $scope.session.games[i], sessionid: $routeParams.sessionid });
$scope.games.push(game);
}
}
})
}
$scope.loadSession();
$scope.interval = $interval(function(){
$scope.loadSession();
}, 5000);
$scope.createGame = function (gamename, gamerules) {
var sessionid = $routeParams.sessionid;
CreateGame = GameCollection.createGame(sessionid, gamename, gamerules);
CreateGame.then(function(game) {
$scope.games.push(game);
});
};
$scope.setGameRules = function(gameid, rulesid){
return GameCollection.setField($scope.session.id, gameid, "rules", rulesid);
}
$scope.startGame = function(index){
// refresh session before setting users?
users = $scope.games[index].users = $scope.session.users;
gameid = $scope.games[index].id;
rulesid = $scope.games[index].rules;
sessionid = $scope.session.id;
SetUsers = GameCollection.setUsers(sessionid, gameid, users);
/* PUT /user cannot handle fast requests
for (user in users) {
GameCollection.setField($scope.session.id, $scope.games[index].id, "user", users[user]);
}*/
SetRules = SetUsers.then(function(setUsersResult) {
return $scope.setGameRules(gameid, rulesid);
});
SetStartTime = SetRules.then(function(setRulesResult) {
time = $scope.games[index].startTime = Date.now();
return GameCollection.setField($scope.session.id, $scope.games[index].id, "starttime", time);
});
SetStartTime.then(function(setStartTimeResult) {
$location.path('/game/' + $scope.session.id + '/' + $scope.games[index].id + '/' + $scope.user.id);
});
}
$scope.endGame = function(index){
time = $scope.games[index].endTime = Date.now();
GameCollection.setField($scope.session.id, $scope.games[index].id, "endtime", time);
}
$scope.deleteGame = function (index) {
var sessionid = $routeParams.sessionid;
var gameid = $scope.games[index].id;
DeleteGame = GameCollection.deleteGame(sessionid, gameid);
DeleteGame.then(function () {
$scope.games.splice(index, 1);
});
};
$scope.deleteSession = function (index) {
var sessionId = $scope.session.id;
DeleteSession = SessionCollection.deleteSession(sessionid);
DeleteSession.then(function() {
$location.path('/');
});
};
$scope.$on('$destroy',function(){
if($scope.interval)
$interval.cancel($scope.interval);
});
}