-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawesome.js
172 lines (135 loc) · 4.21 KB
/
awesome.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
/*jshint node:true */
"use strict";
var SERIALPORT = undefined;
//var SERIALPORT = '/dev/tty.usbmodemfd131';
//var SERIALPORT = '/dev/tty.usbserial-A6006klc';
// FIXME: this is in bad need of a rewrite
var util = require('util');
var events = require('eventemitter2');
var color = require('cli-color');
var os = require('os');
var fs = require('fs');
var exec = require('child_process').exec;
var sp;
var chat;
var HOST = process.argv[4] || "badger.encorelab.org";
var USER = process.argv[2];
var NICK = USER;
var PASSWORD = process.argv[3];
if (!PASSWORD) {
console.error("\nUsage: node awesome.js USERNAME PASSWORD [HOSTNAME]\n");
process.exit(1);
}
function RPA () {
events.EventEmitter2.call(this);
this.arduinoSaidHello = false;
this.winCount = 0;
this.loseCount = 0;
this.opponent = undefined;
this.yourWeapon = undefined;
this.theirWeapon = undefined;
}
util.inherits(RPA, events.EventEmitter2);
RPA.Arduino = require('./awesome.arduino').Arduino;
RPA.Groupchat = require('./awesome.xmpp').Groupchat;
RPA.prototype.connectToArduino = function () {
this.arduino = new RPA.Arduino();
var rpa = this;
function log(msg, data) {
if (data)
console.log(color.yellowBright("[Arduino] "+msg), util.inspect(data, true, null, true));
else
console.log(color.yellowBright("[Arduino] "+msg));
}
this.arduino
.on('online', function () {
log("Arduino is online");
})
.on('present', function () {
log("You are now present");
if (!rpa.chat) {
rpa.connectToGroupchat();
rpa.chat.once('connected', function () {
rpa.chat.enter();
});
} else {
rpa.chat.enter();
}
rpa.arduino.writeEvent('you_are_present');
})
.on('absent', function () {
log("You are now absent");
if (rpa.chat) {
rpa.chat.leave();
rpa.arduino.writeEvent('you_are_absent');
}
})
.on('you_choose', function (weapon) {
rpa.yourWeapon = weapon;
rpa.chat.sendEvent('choice', weapon);
rpa.arduino.writeEvent('you_choose_weapon', weapon[0]);
rpa.checkOutcome();
});
this.arduino.detectSerialport();
this.arduino.openSerialport();
};
RPA.prototype.connectToGroupchat = function () {
this.chat = new RPA.Groupchat(USER, PASSWORD, HOST, NICK);
var rpa = this;
function log(msg, data) {
if (data)
console.log(color.magentaBright("[XMPP] "+msg), util.inspect(data, true, null, true));
else
console.log(color.magentaBright("[XMPP] "+msg));
}
this.chat.log = log;
// this.chat.onAny(function (sev) {
// //rpa.arduino.emit(this.event, data);
// if (sev && sev.payload)
// rpa.arduino.writeEvent(this.event, sev.payload);
// else
// rpa.arduino.writeEvent(this.event);
// });
this.chat
.on('opponent_joined_groupchat', function (jid) {
rpa.opponent = jid;
rpa.arduino.writeEvent('opponent_is_present', jid.resource);
})
.on('opponent_left_groupchat', function (jid) {
rpa.opponent = jid;
rpa.arduino.writeEvent('opponent_is_absent', jid.resource);
})
.on('sail_event', function (sev) {
var eventType = sev.eventType;
var payload = sev.payload;
if (eventType == 'choice') {
log("Opponent chose ", payload);
rpa.theirWeapon = payload;
rpa.arduino.writeEvent('opponent_chooses_weapon', payload[0]);
rpa.checkOutcome();
}
});
this.chat.connect();
};
RPA.prototype.checkOutcome = function () {
if (this.yourWeapon && this.theirWeapon) {
if (this.yourWeapon === this.theirWeapon) {
this.arduino.writeEvent('tie');
} else if (this.yourWeapon === "Rock" && this.theirWeapon === "Scissors" ||
this.yourWeapon === "Paper" && this.theirWeapon === "Rock" ||
this.yourWeapon === "Scissors" && this.theirWeapon === "Paper") {
this.arduino.writeEvent('you_win');
this.winCount++;
} else {
this.arduino.writeEvent('you_lose');
this.loseCount++;
}
this.yourWeapon = undefined;
this.theirWeapon = undefined;
// TODO: should we really do this?
rpa.arduino.writeEvent('you_are_present');
rpa.arduino.writeEvent('opponent_is_present');
}
};
var rpa = new RPA();
rpa.connectToArduino();