Skip to content

Commit

Permalink
Return when no data
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterbrandsen committed May 26, 2024
1 parent 3de7017 commit 7bc74d4
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 70 deletions.
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ DEBUG=FALSE
SCREEPS_TOKEN=""
CHECK_FOR_NEW_ACTIONS=TRUE
API_SETTINGS='{}'
ROOM_NAME='E0N0'

# Postgres
POSTGRES_ENABLED='TRUE'
Expand Down
48 changes: 48 additions & 0 deletions src/adminUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { ScreepsAPI } from "screeps-api"
import axios from "axios"
import { CronJob } from "cron";
import { UploadAdminUtils } from "./data/upload.js";

async function adminUtilsHandler() {
try {
const url = `${process.env.PRIVATE_SERVER_PROTOCOL}://${process.env.PRIVATE_SERVER_HOST}:${process.env.PRIVATE_SERVER_PORT}`
const { data } = await axios.get(`${url}/stats`);

const users = {}
data.users.forEach(user => {
users[user.username] = user;
});
data.users = users;

UploadAdminUtils(data);
} catch (error) {

}
}

if (process.env.PRIVATE_SERVER_USERNAME) {
const api = new ScreepsAPI({
token: process.env.SCREEPS_TOKEN,
protocol: process.env.PRIVATE_SERVER_PROTOCOL || "https",
hostname: process.env.PRIVATE_SERVER_HOST || "screeps.com",
port: process.env.PRIVATE_SERVER_PORT || 443,
});
await api.auth(
process.env.PRIVATE_SERVER_USERNAME,
process.env.PRIVATE_SERVER_PASSWORD
);

api.socket.connect()
api.socket.on('connected', () => {
console.log('Connected to socket')
})

new CronJob(
"* * * * *",
adminUtilsHandler,
null,
true,
"Europe/Amsterdam",
);
adminUtilsHandler()
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "dotenv/config";
import Requests from "./requests/index.js";
import "./socket/console.js";
import "./adminUtils.js";

Requests.executeCycle();
51 changes: 25 additions & 26 deletions src/process/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function shouldFail(opts) {
const data = validData[key]
if (!data) return false;

if (data.tick + 500 > opts.tick) return false
if (data.tick + 2500 > opts.tick) return false
delete validData[key]
return true;
}
Expand All @@ -32,35 +32,34 @@ export default async function processData(opts, proxyIndex) {
}

const dataResult = await GetRoomHistory(proxy, opts.shard, opts.room, opts.tick);
if (dataResult.status === "Success") {
const { data } = dataResult;
if (opts.username) {
opts.timestamp = data.timestamp;
opts.data = await ProcessDataBroker.single({ roomData: data, opts });
if (!opts.data) {
return {
status: "No user",
switch (dataResult.status) {
case "Success": {
const { data } = dataResult;
if (opts.username) {
opts.timestamp = data.timestamp;
opts.data = await ProcessDataBroker.single({ roomData: data, opts });
if (!opts.data) {
return {
status: "No user",
}
}
validData[`${opts.shard}-${opts.room}`] = opts;
}
validData[`${opts.shard}-${opts.room}`] = opts;
}
return {
status: "Success",
}
}
if (opts.failed) {
if (shouldFail(opts)) {
return {
status: "Failed",
};
status: "Success",
}
}
default: {
if (shouldFail(opts)) {
return {
status: "Failed",
};
}

opts.data = validData[`${opts.shard}-${opts.room}`];
return {
status: "Success",
};
opts.data = validData[`${opts.shard}-${opts.room}`];
return {
status: "Success"
}
}
}

opts.failed = true;
return processData(opts, proxyIndex);
}
42 changes: 0 additions & 42 deletions src/socket/console.js

This file was deleted.

0 comments on commit 7bc74d4

Please sign in to comment.