Permalink
Browse files

all sorts

  • Loading branch information...
1 parent 7f88dee commit 087e3c1964580aa16fbc709a992639678e925b86 @edhiley edhiley committed Oct 27, 2013
View
@@ -1,12 +1,114 @@
var mongoose = require('mongoose'),
- Location = require('../models/location');
+ Location = require('../models/location'),
+ User = require('../models/user'),
+ Config = require('../../config/config'),
+ Game = require('../models/game'),
+ unirest = require('unirest');
+
+var sendSms = function(message, phoneNumber) {
+ console.log("Send Sms to user: " + message);
+
+ var phoneNumber = "07944166597";
+
+ phoneNumber = (phoneNumber.charAt(0) === "0") ?
+ phoneNumber.replace("0", "44") : phoneNumber;
+
+ var key = "343319f73c3abbbef5b7f1b8277d853a473b55a3";
+
+ url = "https://api.clockworksms.com/http/send.aspx?key=" + key + "&to=" + phoneNumber + "&content=" + encodeURIComponent(message);
+ console.log(url);
+ //var Request = unirest.get(url);
+ //console.log(Request);
+};
+
+if (typeof Number.prototype.toRad == 'undefined') {
+ Number.prototype.toRad = function() {
+ return this * Math.PI / 180;
+ }
+}
+
+var nearTo = function(lon1, lat1, lon2, lat2) {
+ var R = 6371; // km
+ var dLat = (lat2-lat1).toRad();
+ var dLon = (lon2-lon1).toRad();
+ var lat1 = lat1.toRad();
+ var lat2 = lat2.toRad();
+
+ var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
+ Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
+ var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
+ var d = R * c;
+ return d/1000; // distance in m
+};
+
+exports.track = function(req, res) {
+
+ var gameId = req.body.gameId;
+ var userId = req.body.userId;
+ var lng = req.body.lng;
+ var lat = req.body.lat;
+
+ User = mongoose.model('User');
-/*
-exports.create = function(req,res){
new Location({
- gameId: req.body.gameid,
- userId: req.body.userid,
- loc: [req.body.lng, req.body.lat]
- }).save();
- res.send({});
-};*/
+ gameId: gameId,
+ userId: userId,
+ loc: [lng, lat]
+ }).save(function(err, location){
+ if (err)
+ console.log(err);
+ // console.log(location);
+ });
+
+ Game.findById(gameId, function(err, game){
+
+ if (!game) {
+ throw new Error("no game found with id " + gameId);
+ }
+
+ var target = game.findVictim(userId);
+ if (!target)
+ throw new Error("No target found for user " + userId);
+ var targetId = target.victim;
+ console.log("targetId is " + targetId);
+
+ var assassin = game.findAssassin(userId);
+ if (!assassin)
+ throw new Error("No assassin found for user " + userId);
+ var assassinId = assassin.assassin;
+ console.log("assassinId is " + assassinId);
+
+ Location.findOne({
+ gameId: gameId,
+ userId: targetId
+ })
+ .sort({date: -1})
+ .exec(function(err, location){
+ console.log("Get location of target ..." + location);
+ if (location) {
+
+ if (nearTo(location.loc[0],
+ location.loc[1], lng, lat)) {
+
+ console.log("Promity alert fired for target " + targetId +
+ " assassin " + assassinId);
+
+ // !!! this needs to happen once in a period ...
+ User.findById(targetId, function(err, user) {
+ sendSms("Your assassin is within 100m of your location!!!", user.username);
+ });
+
+ User.findById(assassinId, function(err, user) {
+ sendSms("You are within 100m of your victim!!!", user.username);
+ });
+
+
+ }
+
+ res.json(location.loc);
+ }
+ res.end();
+ });
+
+ })
+};
@@ -76,7 +76,10 @@ exports.add = function(req, res){
exports.newuser = function(req, res){
- console.log("Adding new picture for new user...");
+ if(!req.body.username)
+ return;
+
+ console.log("Adding new picture for new user: " + req.body.username);
var query = User.find()
.where('username').eq(req.body.username);
View
@@ -82,6 +82,20 @@ GameSchema.methods.startIfFull = function() {
}
};
+GameSchema.methods.findVictim = function(userId) {
+ console.log("Find victim for user: " + userId);
+ return _.find(this.missions.reverse(), function(m) {
+ return m.assassin.equals(userId);
+ });
+};
+
+GameSchema.methods.findAssassin = function(userId) {
+ console.log("Find assassin for user: " + userId);
+ return _.find(this.missions.reverse(), function(m) {
+ return m.victim.equals(userId);
+ });
+};
+
GameSchema.methods.eliminate = function(user) {
console.log("eliminate " + user);
View
@@ -2,16 +2,10 @@ var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var LocationSchema = new Schema({
- gameId: {type:Schema.Types.ObjectId,pseudonym:String, index: {unique: true}},
- userId: {type:Schema.Types.ObjectId,pseudonym:String, index: {unique: true}},
+ gameId: {type:Schema.Types.ObjectId,pseudonym:String},
+ userId: {type:Schema.Types.ObjectId,pseudonym:String},
loc: {type: [Number],index:'2d'},
date: {type: Date, default: Date.now }
});
-/*
-TODO:
-userid, gameid, [lng, lat]
-return [lng, lat] of target (most recent);
-gameid, userid */
-
module.exports = mongoose.model('Location', LocationSchema);
View
@@ -1,9 +1,10 @@
var mongoose = require('mongoose'),
- Schema = mongoose.Schema;
+ Schema = mongoose.Schema;
var UserSchema = new Schema({
username: String,
defaultImage : String
});
+
mongoose.model('User', UserSchema);
@@ -1,9 +1,8 @@
h1
- pictures
+ Pictures
ul
each item in pictures
li
img(src="data:image/jpeg;base64," + item.encoded)
- div=item._id
- div=item.encoded
+ div=item._id
Oops, something went wrong.

0 comments on commit 087e3c1

Please sign in to comment.