-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse-objects.js
More file actions
157 lines (145 loc) · 3.37 KB
/
Copy pathparse-objects.js
File metadata and controls
157 lines (145 loc) · 3.37 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
//Initialize
Parse.initialize("UH0vJUKo17FcCgOJPX6KFAmK0DxnQwCKtV4yQwva", "HwdLU8EQA6IIXP10n9ovZgnAJAwk1P5qzwlK5Clu");
/* Game object
Data:
gameId: a unique id set when creating a new game (string)
title: title of the game (string)
timeLimit: limitation of the game time (number)
radius: limitation on game area (number)
numOfPlayers: number of players in this game (number)
*/
//return a collection of users in a game
function getAllUsers(gameId) {
var query = new Parse.Query(User);
query.equalTo("gameId",gameId);
var collection = query.collection();
collection.fetch({
success: function (collection) {
collection.each(function (object) {
//do something
});
},
error:function(collection, error) {
console.warn("Collection error");
}
});
}
var Game = Parse.Object.extend("Game", {
//to prevent front end typo. Wrap up all gets and sets
getGameId: function() {
return this.get("gameId");
},
getTitle: function() {
return this.get("title");
},
getNumOfPlayers: function() {
return this.get("numOfPlayers");
},
getTimeLimit: function() {
return this.get("timeLimit");
},
getRadius: function() {
return this.get("radius");
},
setTitle: function(title) {
this.set("title",title);
},
setNumOfPlayers: function(numOfPlayers) {
this.set("numOfPlayers", numOfPlayers);
},
setTimeLimit: function(timeLimit) {
this.set("timeLimit",timeLimit);
},
setRadius: function(radius) {
this.set("radius",radius);
}
},
{
createNewGame: function(gameId) {
//buggy needs callbacks
var query = new Parse.Query(Game);
var idUnique = true;
query.equalTo("gameId", gameId);
query.find({
success:function(results) {
if (results.length == 0)
this.createTheGame(gameId);
else
gameIdAlreadyExists();
},
error:function(){
alert("create new game fails");
}
});
}
});
/* User object
Data:
userID: a unique id set when creating user (string)
isSeeker (number 0 means false 1 means true)
gameID: game the user is in. set when creating user
lat: latitude
long: longitude
*/
var User = Parse.Object.extend("User", {
//to prevent front end typo. Wrap up all gets and sets
getUserId: function() {
return this.get("userId");
},
getIsSeeker: function() {
return this.get("isSeeker");
},
getGameId: function() {
return this.get("gameId");
},
getLat: function() {
return this.get("lat");
},
getLong: function() {
return this.get("long");
},
setisSeeker: function(isSeeker) {
this.set("isSeeker", isSeeker);
},
setlat: function(lat) {
this.set("lat", lat);
},
setTimeLimit: function(long) {
this.set("long",long);
}
},
{
//class methods
//buggy needs callbacks
createNewUser: function(gameId, userId) {
var gameQuery = new Parse.Query(Game);
gameQuery.equalTo("gameId", gameId);
gameQuery.find({
success:function(results) {
if (results.length > 0)
this.checkUserUniqueness(gameId,userId);
else
gameIdNotExist();
},
error: function() {
alert("createNewUser fails");
}
});
},
checkUserUniqueness: function (gameId, userId) {
var userQuery = new Parse.Query(User);
userQuery.equalTo("gameId",gameId);
userQuery.equalTo("userId",userId);
gameQuery.find({
success:function(results) {
if (results.length == 0)
this.createTheUser();
else
userIdAlreadyExists();
},
error: function(){
alert("checkUserUniqueness fails");
}
});
}
});