Skip to content
This repository has been archived by the owner on Jul 5, 2021. It is now read-only.

Commit

Permalink
Merge pull request #32 from TheSnowfield/test
Browse files Browse the repository at this point in the history
Fix aggregate and best30
  • Loading branch information
TheSnowfield authored May 15, 2021
2 parents eb9d0df + 2634e73 commit d480b5a
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 43 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ services:
BOTARCAPI_CONFIG: |
{
"SERVER_PORT": 80
"BOTARCAPI_VERSTR": "v0.3.1",
"BOTARCAPI_VERSTR": "v0.3.2",
"ARCAPI_VERSION": 14,
"ARCAPI_APPVERSION": "3.6.0c",
"ARCAPI_USERAGENT": "Grievous Lady (Linux; U; Android 2.3.3; BotArcAPI)",
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "botarcapi",
"version": "0.3.1",
"version": "0.3.2",
"description": "A fast and convenient Arcaea API for your bot.",
"dependencies": {
"abab": "^2.0.3",
Expand Down
4 changes: 3 additions & 1 deletion source/@declares/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ declare var BOTARCAPI_FORWARD_TIMESEC_DEFAULT: number;
declare var BOTARCAPI_FORWARD_FEED_MAX: number;
declare var BOTARCAPI_USERBEST_HISTORY_MAX: number;
declare var BOTARCAPI_USERBEST_HISTORY_DEFAULT: number;
declare var BOTARCAPI_AGGREGATE_LIMITATION: number;
declare var BOTARCAPI_AGGREGATE_ENABLED: boolean;
declare var BOTARCAPI_AGGREGATE_CONCURRENT: boolean;

declare var ARCAPI_RETRY: number;
declare var ARCAPI_VERSION: number;
declare var ARCAPI_APPVERSION: string;
declare var ARCAPI_USERAGENT: string;
declare var ARCAPI_AGGREGATE_LIMITATION: number;
declare var ARCAPI_URL: string;

declare var DATABASE_PATH: string;
Expand Down
17 changes: 14 additions & 3 deletions source/corefunc/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const _default_config: any = {
// botarcapi version
'BOTARCAPI_MAJOR': 0,
'BOTARCAPI_MINOR': 3,
'BOTARCAPI_VERSION': 1,
'BOTARCAPI_VERSTR': 'BotArcAPI v0.3.1',
'BOTARCAPI_VERSION': 2,
'BOTARCAPI_VERSTR': 'BotArcAPI v0.3.2',

// useragent whitelist
// if set '[]' will accept all requests
Expand Down Expand Up @@ -42,12 +42,23 @@ const _default_config: any = {
'BOTARCAPI_USERBEST_HISTORY_MAX': 20,
'BOTARCAPI_USERBEST_HISTORY_DEFAULT': 7,

// aggregate limit
'BOTARCAPI_AGGREGATE_LIMITATION': 6,

// sending only one 'compose/aggregate' request instead
// of send a huge of requests to arcapi.
// this feature is for 3.6.0 arcapi
'BOTARCAPI_AGGREGATE_ENABLED': false,

// send a huge of requests concurrently
// only valid on 'BOTARCAPI_AGGREGATE_ENABLED' set to false
'BOTARCAPI_AGGREGATE_CONCURRENT': false,

// arcaea api config
'ARCAPI_RETRY': 3,
'ARCAPI_VERSION': 14,
'ARCAPI_APPVERSION': '3.6.0c',
'ARCAPI_USERAGENT': 'Grievous Lady (Linux; U; Android 2.3.3; BotArcAPI)',
'ARCAPI_AGGREGATE_LIMITATION': 6,
'ARCAPI_URL': 'https://arcapi.lowiro.com/blockchain',

// path to database folder
Expand Down
107 changes: 74 additions & 33 deletions source/modules/arcfetch/arcapi.aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,95 @@ const TAG: string = 'arcapi.aggregate.ts';
import syslog from '../syslog/syslog';
import arcfetch, { ArcFetchRequest, ArcFetchMethod } from './arcfetch';
import IArcAccount from './interfaces/IArcAccount';
import arcapi_any from './arcapi.any';

export default (account: IArcAccount, endpoints: Array<string>) => {
return new Promise((resolve, reject) => {

return new Promise(async (resolve, reject) => {

// account will be BANNED from server if exceed
if (endpoints.length > ARCAPI_AGGREGATE_LIMITATION)
if (endpoints.length > BOTARCAPI_AGGREGATE_LIMITATION)
return reject(new Error('Endpoints limit exceeded'));

// construct endpoint object
const _endpoints: Array<any> = [];
endpoints.forEach((element, index) => {
_endpoints.push({ endpoint: element, id: index });
});

// construct remote request
const _remote_request =
new ArcFetchRequest(ArcFetchMethod.GET, 'compose/aggregate', {
deviceId: account.device,
userToken: account.token,
submitData: new URLSearchParams({ 'calls': JSON.stringify(_endpoints) })
if (BOTARCAPI_AGGREGATE_ENABLED) {

// construct endpoint object
const _endpoints: Array<any> = [];
endpoints.forEach((element, index) => {
_endpoints.push({ endpoint: element, id: index });
});

// send request
arcfetch(_remote_request)
.then((root: any) => {
// construct remote request
const _remote_request =
new ArcFetchRequest(ArcFetchMethod.GET, 'compose/aggregate', {
deviceId: account.device,
userToken: account.token,
submitData: new URLSearchParams({ 'calls': JSON.stringify(_endpoints) })
});

// send request
arcfetch(_remote_request)
.then((root: any) => {

// teardown the object and pack data into array
const _data: Array<any> = [];
root.value.forEach((element: any) => {
_data[element.id] = element.value[0];
})

// teardown the object and pack data into array
const _data: Array<any> = [];
root.value.forEach((element: any) => {
_data[element.id] = element.value[0];
resolve(_data);
})
.catch((e: string) => {

// if token is invalid
// just erase the token and wait for
// auto login in next time allocating
if (e == 'UnauthorizedError') {
account.token = '';
syslog.w(TAG, `Invalid token => ${account.name} ${account.token}`);
}

reject(e);

});

} else {
if (BOTARCAPI_AGGREGATE_CONCURRENT) {

resolve(_data);
})
.catch((e: string) => {
// construct tasks
const _tasks: Array<Promise<any>> = [];
endpoints.forEach((element, index) => {
_tasks.push(new Promise(async (resolve, reject) => {
resolve(await arcapi_any(account, ArcFetchMethod.GET, element, ""));
}));
});

// if token is invalid
// just erase the token and wait for
// auto login in next time allocating
if (e == 'UnauthorizedError') {
account.token = '';
syslog.w(TAG, `Invalid token => ${account.name} ${account.token}`);
// wait for data coming in
Promise.all(_tasks)
.then(data => {
let _results: any[] = [];

data.forEach((element, index) =>
_results.push(element.value[0]));

resolve(_results);

}).catch((e: string) => reject(e));

} else {

let _results: any[] = [];

// request one by one
for (let i = 0; i < endpoints.length; ++i) {
const _data: any = await arcapi_any(account, ArcFetchMethod.GET, endpoints[i], "");
_results.push(_data.value[0]);
}

reject(e);
resolve(_results);
}

});
}

});

Expand Down
2 changes: 1 addition & 1 deletion source/publicapi/v4/user/best.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export default (argument: any): Promise<any> => {

// get rank result
try {
_arc_ranklist = await arcapi_rank_friend(_arc_account, _arc_songid, _arc_difficulty, 0, 1);
_arc_ranklist = await arcapi_rank_friend(_arc_account, _arc_songid, _arc_difficulty);
} catch (e) { syslog.e(TAG, e.stack); throw new APIError(-18, 'internal error occurred'); }

if (!_arc_ranklist.length)
Expand Down
4 changes: 2 additions & 2 deletions source/publicapi/v4/user/best30.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ const do_fetch_userbest30 =
const _endpoints: Array<string> = [];
for (let j = 0; j < 6; ++j) {
const v = _chartheap[i * 6 + j];
_endpoints.push(`/score/song/friend?song_id=${v.sid}&difficulty=${v.rating_class}&start=0&limit=1`);
_endpoints.push(`score/song/friend?song_id=${v.sid}&difficulty=${v.rating_class}&start=0&limit=11`);
}

// send request
Expand Down Expand Up @@ -247,7 +247,7 @@ const do_fetch_userbest30 =

// fill the endpoints and chartheap
const v: any = _arc_chartlist.shift();
_endpoints.push(`/score/song/friend?song_id=${v.sid}&difficulty=${v.rating_class}&start=0&limit=1`);
_endpoints.push(`score/song/friend?song_id=${v.sid}&difficulty=${v.rating_class}&start=0&limit=11`);
_chartheap.push(v);

} else break;
Expand Down

0 comments on commit d480b5a

Please sign in to comment.