-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwai.js
165 lines (147 loc) · 4.69 KB
/
wai.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
'use strict'
var WebSocket = require("ws");
var request = require("request");
var PlayerData = require("./PlayerData.js");
var cortex = require("./cortex.js");
var ok = require("okay");
module.exports = function(config){
this.context = {};
this.config = config;
}
module.exports.prototype.prepare = function(username, twitterId, tokens) {
var self = this;
this.username = username;
this.twitterId = twitterId;
this.cortex = new cortex(this);
// console.log("Cortext", this.cortex)
var msg = {
"Command": "login",
"Username": username,
"TwitterId": twitterId,
"AccessToken": tokens.oauthAccessToken,
"AccessTokenSecret": tokens.oauthAccessTokenSecret
};
this.getEmperor(ok(console.error.bind(console), function(data) {
var emperor = JSON.parse(data)[0];
// console.log(self.cortex)
self.cortex.rememberEmperor(emperor);
}));
this.getLeaderboardRaces(ok(console.error.bind(console), function(data) {
var races = JSON.parse(data);
}));
// console.log("config", this.config)
this.ws = new WebSocket(this.config.socketUrl);
this.ws.on("open", function() {
console.log("websocket open")
self.ws.send(JSON.stringify(msg));
});
this.ws.on("message", function(message) {
// console.log("websocket message", message)
self.parseMessage(message);
});
this.ws.on("error", function(err){
console.error(arguments)
})
}
module.exports.prototype.parseMessage = function(command) {
var data = JSON.parse(command);
if (data.Command) {
this.context.currentTime = data.Timestamp;
switch (data.Command) {
case "login_success":
console.log("Wai is logged in-wooohooooo :D")
var pd = new PlayerData();
pd.Username = this.username;
pd.TwitterID = this.twitterId;
pd.Position = data.Position;
pd.Race = data.RaceID;
pd.HomePlanet = data.HomePlanet;
this.cortex.setPlayerData(pd)
this.scopeOfView(0, 0, 145907, 145907)
// some fuzzy module decision making passing playerData
break;
case "scope_of_view_result":
console.log("scope_of_view_result")
this.cortex.parseView(data)
// some fuzzy module decision making
break;
case "state_change":
console.log("state_change")
// some fuzzy module decision making
break;
case "request_setup_params":
console.log("request_setup_params")
// decide
this.setupParameters(5, 0);
break;
case "send_missions":
console.log("send_missions")
// console.log("send_missions:", data)
// some fuzzy module decision making
break;
case "send_mission_failed":
console.log("send_mission_failed")
// some fuzzy module decision making
break;
case "server_params":
console.log("Wai should understand server_params")
this.context.serverParams = {
HomeSPM: data.HomeSPM,
PlanetsSPM: data.PlanetsSPM,
Races: data.Races,
ShipsDeathModifier: data.ShipsDeathModifier
}
break;
case "error":
console.error(data.Message)
break;
case "owner_change":
console.log("Wai has lost a planet ;C")
break;
default:
console.log("default", data);
}
}
}
module.exports.prototype.scopeOfView = function(x, y, width, height) {
var self = this;
// console.log("scopeOfView", x, y, width, height)
this.ws.send('{' +
'"Command": "scope_of_view",' +
'"Position": {"x": '+x+', "y": '+y+'},' +
'"Resolution": ['+width+', '+height+']' +
'}');
setTimeout(function () {
self.scopeOfView(x, y, width, height);
}, 7000);
}
module.exports.prototype.sendMission = function(type, source, target, ships, waypoints) {
// console.log("sendMission:", type, source, target, ships, waypoints)
console.log("sendMission", target)
var waypointsPath = waypoints ? JSON.stringify(waypoints) : "[]";
this.ws.send('{' +
'"Command": "start_mission",' +
'"Type": "'+type+'",' +
'"StartPlanets": ["'+source.join('","')+'"],' +
'"EndPlanet": "'+target+'",' +
'"Fleet": '+ ships + ',' +
'"Path": '+ waypointsPath +
'}');
}
module.exports.prototype.setupParameters = function(race, sun) {
this.ws.send('{' +
'"Command": "setup_parameters",' +
'"Race": '+race+',' +
'"SunTextureId": '+ sun +
'}')
}
module.exports.prototype.getLeaderboardRaces = function (done) {
request.get(this.config.ajaxUrl + "/races/", ok(done, function(response, body) {
done(null, body)
}))
}
module.exports.prototype.getEmperor = function(done) {
request.get(this.config.ajaxUrl + "/players/?page=1", ok(done, function(response, body) {
done(null, body)
}))
}