186 lines
5.4 KiB
JavaScript
186 lines
5.4 KiB
JavaScript
|
|
import https from 'https';
|
|
import request from 'request';
|
|
import WebSocket from 'ws';
|
|
import AwsSign from 'aws-sign';
|
|
import { parseString } from 'xml2js';
|
|
|
|
const HOST = 'api-quiz.hype.space';
|
|
const headers = {
|
|
'x-hq-client': 'Android/1.12.2',
|
|
'content-type': 'application/json; charset=UTF-8',
|
|
'user-agent': 'okhttp/3.8.0',
|
|
};
|
|
|
|
export default class Account {
|
|
constructor({
|
|
accessToken,
|
|
admin,
|
|
authToken,
|
|
avatarUrl,
|
|
guest,
|
|
loginToken,
|
|
userId,
|
|
username,
|
|
tester,
|
|
}) {
|
|
this.accessToken = accessToken;
|
|
this.admin = admin;
|
|
this.authToken = authToken;
|
|
this.avatarUrl = avatarUrl;
|
|
this.guest = guest;
|
|
this.loginToken = loginToken;
|
|
this.userId = userId;
|
|
this.username = username;
|
|
this.tester = tester;
|
|
this.broadcastId = null;
|
|
this.socket = null;
|
|
this.lastAnswerId = null;
|
|
}
|
|
|
|
activate(broadcastId, socket) {
|
|
this.broadcastId = broadcastId;
|
|
this.socket = socket;
|
|
}
|
|
|
|
sendAnswer(questionId, answerId) {
|
|
const answerPackage = {
|
|
type: 'answer',
|
|
broadcastId: this.broadcastId,
|
|
questionId,
|
|
answerId,
|
|
};
|
|
this.lastAnswerId = answerId;
|
|
try {
|
|
this.socket.send(JSON.stringify(answerPackage));
|
|
} catch (error) {
|
|
console.log('\x1b[1m\x1b[31m%s\x1b[0m', `WebSocket error: ${this.username} can\'t send answer.`);
|
|
this.lastAnswerId = null;
|
|
}
|
|
}
|
|
|
|
cashout(email) {
|
|
const options = {
|
|
host: HOST,
|
|
path: '/users/me/payouts',
|
|
method: 'POST',
|
|
headers: {
|
|
...headers,
|
|
'authorization': `Bearer ${this.accessToken}`,
|
|
},
|
|
};
|
|
|
|
const req = https.request(options, (res, err) => {
|
|
if (err) {
|
|
console.log(`Error occurred cashing out for ${this.username} with email ${email}:`, err);
|
|
return;
|
|
}
|
|
console.log('Sent request successfully? Waiting for data.')
|
|
|
|
res.on('data', (data) => {
|
|
console.log('RESULT', data.toString('utf8'));
|
|
});
|
|
});
|
|
|
|
req.write(JSON.stringify({email}));
|
|
req.end();
|
|
};
|
|
|
|
changeAvatar() {
|
|
let awsCredentials;
|
|
let error;
|
|
|
|
const options = {
|
|
host: HOST,
|
|
path: '/credentials/s3',
|
|
method: 'GET',
|
|
headers: {
|
|
...headers,
|
|
'authorization': `Bearer ${this.accessToken}`,
|
|
},
|
|
};
|
|
|
|
const req = https.request(options, (res, err) => {
|
|
if (err) {
|
|
console.log(`Error occurred requesting AWS credentials for ${this.username}:`, err);
|
|
return;
|
|
}
|
|
|
|
res.on('data', (data) => {
|
|
awsCredentials = JSON.parse(data);
|
|
|
|
if (!awsCredentials.accessKeyId) {
|
|
if (awsCredentials.error) {
|
|
error = awsCredentials.error;
|
|
} else {
|
|
error = data;
|
|
}
|
|
console.log(`Error occurred parsing AWS credentials for ${this.username}:`, error);
|
|
return;
|
|
} else {
|
|
const filename = `${this.username}.gif`;
|
|
console.log('AWS Credentials', awsCredentials);
|
|
const signer = new AwsSign({
|
|
accessKeyId: awsCredentials.accessKeyId,
|
|
secretAccessKey: awsCredentials.secretKey,
|
|
securityToken: awsCredentials.sessionToken,
|
|
expiration: awsCredentials.expiration,
|
|
});
|
|
let options = {
|
|
method: 'PUT',
|
|
host: 'hypespace-quiz.s3.amazonaws.com',
|
|
path: '/avatars/' + filename,
|
|
headers: {
|
|
'content-type': 'image/jpeg',
|
|
},
|
|
};
|
|
signer.sign(options);
|
|
const image = request.get('https://picsum.photos/200?random', (imageErr, imageRes, imageBody) => {
|
|
const bytes = new Buffer(imageBody);
|
|
const avatarReq = https.request(options, (res, err) => {
|
|
if (err) {
|
|
console.log(`Error uploading avatar for player ${this.username}`, err);
|
|
return;
|
|
}
|
|
|
|
res.on('data', (data) => {
|
|
console.log(`Uploaded avatar for player ${this.username}`);
|
|
parseString(data, (xmlErr, xmlResult) => { console.log('xmlResult', xmlResult); });
|
|
const avatarOptions = {
|
|
method: 'PUT',
|
|
host: HOST,
|
|
path: '/users/me/avatarUrl',
|
|
headers: {
|
|
'content-type': 'application/json',
|
|
Authorization: `Bearer ${this.accessToken}`,
|
|
},
|
|
};
|
|
const changeAvatarReq = https.request(avatarOptions, (res, err) => {
|
|
res.on('data', (data) => {
|
|
const avatar = JSON.parse(data);
|
|
if (avatar.avatarUrl) {
|
|
console.log(`Updated avatar for player ${this.username} to ${avatar.avatarUrl}.`);
|
|
} else {
|
|
if (avatar.error) {
|
|
console.log(`Error updating avatar for player ${this.username}`, avatar.error, avatar);
|
|
} else {
|
|
console.log(`Unknown error updating avatar for player ${this.username}`, avatar);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
changeAvatarReq.write(JSON.stringify({avatarUrl: filename}));
|
|
changeAvatarReq.end();
|
|
});
|
|
});
|
|
avatarReq.write(bytes);
|
|
avatarReq.end();
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
req.end();
|
|
}
|
|
}
|