-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdaemon.js
189 lines (160 loc) · 5.42 KB
/
daemon.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
189
var Faye = require('faye');
var GameLoopDispatch = require('game-loop-dispatch');
var uuid = require('node-uuid');
var _ = require('lodash');
var OSRM = require('./OSRM.RoutingGeometry.js');
var request = require('superagent');
var xml = require('node-xml');
var hubUrl = 'http://localhost:5000';
var bayeuxUrl = hubUrl + '/bayeux';
var avatars = [
'elvis',
'emo',
'escafandra',
'estilista',
'extraterrestre',
'fisicoculturista',
'funky',
'futbolista_brasilero',
'gay',
'geisha',
'ghostbuster',
'glamrock_singer',
'guerrero_chino',
'hiphopper',
'hombre_hippie'
];
var randomAvatar = function(){
return avatars[Math.floor(Math.random()*avatars.length)];
};
var teams = ['ESC', 'LL+GW', 'ORWELL', 'FH-J', 'CY', undefined];
var randomTeam = function(){
return teams[Math.floor(Math.random()*teams.length)];
};
var Position = function(position){
max = 50; mul = 0.0025;
this.lat = position[0] - (max/2)*mul + Math.floor(Math.random()*max)*mul;
this.lng = position[1] - (max/2)*mul + Math.floor(Math.random()*max)*mul;
this.toString = function() {
return [this.lat, this.lng].join(",");
}
};
var Ghost = function(hub, position){
this.hub = hub;
this.position = position;
this.uuid = uuid();
this.avatar = randomAvatar() ;
this.nickname = this.avatar;
this.team = randomTeam();
this.color = '#' + Math.floor(Math.random()*16777215).toString(16);
this.track = {
channel: {
url: bayeuxUrl,
path: '/' + this.uuid + '/track',
protocol: 'bayeux'
},
feed: {
url: hubUrl,
path: '/' + this.uuid + '/track',
}
};
this.waypoints = null;
this.api_url = 'http://router.project-osrm.org/viaroute';
var from = new Position(position);
var mid = new Position(position);
var to = new Position(position);
this.route_endpoints = [from.toString(), mid.toString(), to.toString(), from.toString()];
request.get(this.api_url)
.query({ z: 14,
output: "json", instructions: true,
jsonp: "OSRM.JSONP.callbacks.redraw" })
.query("loc=" + this.route_endpoints.join("&loc="))
.set("Accept", "application/json")
.set("User-Agent", "dspace-daemon-ai-player/0.0.1")
.end(function(res) {
var data = JSON.parse(res.text.match(/.*?\((.*?)\)/)[1]);
this.waypoints = OSRM.RoutingGeometry._decode(data["route_geometry"], OSRM.CONSTANTS.PRECISION);
this.speed = Math.random()*0.001;
this.waypoints = _.flatten(_.map(this.waypoints, function(waypoint, index){
if(waypoint === this.waypoints[this.waypoints.length - 1]){
return [];
}
var interpolations = [];
var next = this.waypoints[index + 1];
var distance = {
x: next[0] - waypoint[0],
y: next[1] - waypoint[1]
};
var steps = Math.sqrt(Math.pow(distance.x, 2) + Math.pow(distance.y, 2)) / this.speed;
var offset = {
x: 0,
y: 0
};
for(i=0; i < steps; i++){
interpolations.push([waypoint[0] + offset.x, waypoint[1] + offset.y]);
offset.x += distance.x / steps;
offset.y += distance.y / steps;
}
return interpolations;
}.bind(this)), true); // shallow
}.bind(this));
this.counter = 0;
this.toJSON = function(){
return {
uuid: this.uuid,
nickname: this.nickname,
avatar: this.avatar,
team: this.team,
color: this.color,
track: this.track,
};
}.bind(this);
this.publish = function(){
if(!this.waypoints) return;
var nextWaypoint = this.waypoints[this.counter];
console.log(this.nickname + " " + nextWaypoint);
this.hub.publish(this.track.channel.path,
{ coords:
{ latitude: nextWaypoint[0],
longitude: nextWaypoint[1],
accuracy: 20 },
player: { uuid: this.uuid } }
);
//this.avatar = randomAvatar();
this.hub.publish('/dev', this.toJSON() );
this.counter++;
if(this.counter >= this.waypoints.length) this.counter = 0;
}.bind(this);
};
var pubsub = new Faye.Client(bayeuxUrl);
var loop = new GameLoopDispatch({ interval: 500 });
var getStartPosition = function(city_name, startCb) {
if(!city_name) city_name = "Graz"
//var overpass_url = 'http://overpass-api.de/api/interpreter';
var overpass_url = 'http://overpass.osm.rambler.ru/cgi/interpreter';
var query = '<?xml version="1.0" encoding="UTF-8"?><osm-script><query type="node"><has-kv k="name" v="' + city_name + '"/><has-kv k="place" v="city"/></query><print/></osm-script>';
var parser = new xml.SaxParser(function(p) {
p.onStartElementNS(function(elem, attrs, prefix, uri, namespaces) {
if(elem == 'node') {
attrs = attrs.reduce(function(a, kv) { a[kv[0]] = kv[1]; return a; }, {});
startCb([Number(attrs.lat), Number(attrs.lon)]);
}
}.bind(this));
});
request
.get(overpass_url)
.query({"data": query})
.end(function(res) {
var buf = new Buffer([]);
res.on('data', function(chunk) { buf = Buffer.concat([buf, chunk]); });
res.on('end', function() {
parser.parseString(buf.toString());
});
});
}
getStartPosition(process.argv[2], function(start_position) {
var ghosts = [];
_.times(5, function(){ ghosts.push(new Ghost(pubsub, start_position)); }.bind(this));
loop.tick = function(){ _.each(ghosts, function(ghost){ ghost.publish(); }); };
loop.start();
});