Permalink
Browse files

server

  • Loading branch information...
1 parent 87936d7 commit 498f89d950358453360228728f94957fd6ca1b74 @lericson lericson committed Mar 8, 2012
Showing with 86 additions and 3 deletions.
  1. +7 −0 luddenotes.md
  2. +32 −3 server/server.coffee
  3. +47 −0 server/waitlist.coffee
View
@@ -51,6 +51,13 @@ def match(user, waitlist):
3. and the rest is history
+
+```
+C> type: 'hello', profile: …, location: …
+S> ok: true
+```
+
+
## i framtiden
- notifications
View
@@ -3,6 +3,7 @@ sio = require 'socket.io'
logger = require 'winston'
static = require 'node-static'
http = require 'http'
+waitlist = require './waitlist.coffee'
# static server
fileServer = new static.Server './../client'
@@ -14,7 +15,35 @@ server = http.createServer (request, response) ->
sio = sio.listen server
server.listen 8080
-sio.sockets.on 'connection', (socket) ->
- # make it go
+wait = new waitlist.Waitlist
+
+[
+ {uid: "516251607", point: [59.33630, 18.02872]},
+ {uid: "123123", point: [59.31090, 18.08367]},
+ {uid: "123124", point: [59.312569, 18.09267]},
+ {uid: "123125", point: [65.640457, 22.012873]}]
+
+clients = {}
+
+new_client = (socket) ->
+ socket.on 'hello', (info) ->
+ user = uid: info.user.id, point: [info.location.coords.latitude, info.location.coords.longitude]
+ clients[user.uid] = client = {user: user, socket: socket}
+ wait.search user, (mate) ->
+ console.log('blah')
+ if not mate?
+ console.log('wait add')
+ wait.add user
+ else
+ console.log('do it')
+ console.log user, mate
+ client.mate = mate
+ mate.mate = mate
+ client.socket.emit 'knock'
+ clients[mate.uid].socket.emit 'knock'
+
socket.on 'message', (args...) ->
- console.log 'client said', args.join ', '
+ #console.log 'client said', args.join ', '
+ #user = uid: "666", point: [55, 18]
+
+sio.sockets.on 'connection', new_client
View
@@ -0,0 +1,47 @@
+async = require 'async'
+
+RADIUS = 6378135
+
+_deg2rad = (v) -> (v / 180.0) * Math.PI
+
+distance = (p1, p2) ->
+ # Great circle distance between two points (law of cosines).
+ # The 2D great-circle distance between the two given points, in meters.
+ [p1lat, p1lon] = _deg2rad p1[0], _deg2rad p1[1]
+ [p2lat, p2lon] = _deg2rad p2[0], _deg2rad p2[1]
+ angle = (Math.sin(p1lat) * Math.sin(p2lat) +
+ Math.cos(p1lat) * Math.cos(p2lat) *
+ Math.cos(p2lon - p1lon))
+ return RADIUS * Math.acos(Math.min(Math.max(angle, -1.0), 1.0))
+
+class __Search
+ threshold: base: 100, max: 500
+
+ constructor: (@user, @waitlist) ->
+
+ _threshold: (tolerance) ->
+ delta = @threshold.max - @threshold.base
+ return @threshold.base + tolerance * delta
+
+ first: (callback, tolerance = 0.0) ->
+ threshold = @_threshold(tolerance)
+ async.sortBy waitlist, @score, (err, results) -> callback(err, results[0])
+
+class Waitlist
+ constructor: (users = []) ->
+ @_users = []
+ for user in users
+ @_users[user.uid] = user
+
+ add: (user) ->
+ @_users[user.uid] = user
+
+ search: (user, callback) ->
+ score = (other, callback) -> callback null, distance(user.point, other.point)
+ async.sortBy (@_users[u] for u of @_users), score, (err, results) =>
+ for user in results
+ delete @_users[user.uid]
+ return callback user
+ return callback null
+
+exports.Waitlist = Waitlist

0 comments on commit 498f89d

Please sign in to comment.