-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryGame.js
188 lines (165 loc) · 4.49 KB
/
MemoryGame.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
180
181
182
183
184
185
186
187
188
//MemoryGame.js
var Db = require('mongodb').Db;
var Server = require('mongodb').Server;
var ObjectID = require('mongodb').ObjectID;
var util = require('util');
//authenticate instagram api
var instagram = require('instagram').createClient(process.env['INSTAGRAM_CLIENT_ID'] ,process.env['INSTAGRAM_CLIENT_SECRET'] );
MemoryGame = function(dbconfig){
//connecting to mongo db
this.db= new Db(dbconfig.database, new Server(dbconfig.hostname, dbconfig.port, {auto_reconnect: true}));
//authenticate the server
if(dbconfig.auth) {
this.db.open(function(err, data) {
if(err){
console.log("error opening: "+err);
//callback(err);
}else {
data.authenticate(dbconfig.username, dbconfig.password, function(err2, data2) {
if(err2){
console.log("error authenticating: "+err2);
//callback(err2)
}else{
console.log("database opened.");
//callback(null,data2);
}
});
}
});
}else{
this.db.open(function() {});
}
this.cards = [];
};
MemoryGame.prototype.getCollection = function(collectionName, callback) {
this.db.collection(collectionName, function(error, coll) {
if(error) callback(error)
else callback(null, coll)
});
}
MemoryGame.prototype.saveTagToDB = function(tag, callback) {
this.getCollection('tags', function(error, tags) {
if(error) callback(error)
else {
//will add a new tag to the db and increment an existing tag's total and updated_at
tags.update(
{ 'tagName': tag },
{ $set: { 'updated_at': new Date() }, $inc: { 'total': 1 } },
{ upsert: true },
function(error, obj){
if(error){
console.log('error: '+error.message)
callback(error);
}else{
callback(null, tag);
}
}
);
}
});
}
MemoryGame.prototype.getPopularTags = function(callback) {
this.getCollection('tags', function(error, tags){
if(error) callback(error);
else {
tags.find( {}, { 'limit': 50, 'sort': [[ 'total', 'desc' ], ['updated_at', 'desc']] } ).toArray(function(error, popTags){
if(error) callback(error);
else {
callback(null, popTags);
}
});
}
});
}
MemoryGame.prototype.newGame = function(config, callback) {
this.numMatches = config.matches;
this.tag = config.tag;
this.getCollection('games', function(error, games) {
if(error) callback(error)
else {
callback(null, games);
}
});
}
MemoryGame.prototype.getPhotos = function(callback) {
if(this.tag) {
this.saveTagToDB(this.tag, function(error, searchTag){
if(error) callback(error);
else {
instagram.tags.media(searchTag, function (images, error) {
if(error) console.log("instagram error: "+error);
else {
callback(null, images);
}
});
}
});
}else{
instagram.media.popular(function(images, error){
if(error) console.log("instagram error: "+error);
else {
callback(null, images);
}
});
}
}
MemoryGame.prototype.setupGame = function(config, callback) {
this.game_id = "g"+makeid();
var card = {};
var i = 0;
//insert cards into a game
for(var x in config.photos) {
if(i < this.numMatches) {
for(var j = 0; j < 2; j++) {
card = {
'game_id' : this.game_id,
'card_id' : makeid(),
'source' : config.photos[x].images.thumbnail.url,
'pic_id' : config.photos[x].id,
'created_at' : new Date()
};
if(this.tag != null)
card.tag = this.tag;
config.games.insert(card);
}
i++;
}else break;
}
callback(null, this.game_id);
}
MemoryGame.prototype.dealGame = function(game_id, callback) {
var gameTag = this.tag;
this.getCollection('games', function(error, games, gameTag) {
if(error) callback(error)
else {
var thisGameTag = gameTag;
games.find({'game_id':game_id}, {'card_id': 1, 'tag': 1}).sort({'card_id':1}).toArray(function(error, cards, thisGameTag) {
if(error) callback(error)
else {
var delt = [{"game_id": game_id, "cards": cards}];
callback(null, delt);
}
});
}
});
}
MemoryGame.prototype.flipCard = function(game_id, card_id, callback) {
this.getCollection('games', function(error, games) {
if(error) callback(error)
else {
games.findOne({'game_id':game_id, 'card_id': card_id}, function(error, pic) {
if(error) callback(error)
else callback(null, pic);
});
}
});
}
function makeid()
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
exports.MemoryGame = MemoryGame;