Skip to content

Commit

Permalink
Adds more logging/catches more errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachrip committed Mar 15, 2023
1 parent cce1af6 commit b19e99d
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 78 deletions.
4 changes: 3 additions & 1 deletion app/utils.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,14 @@ export async function getUser() {
if (isAxiosError(e)) {
switch (e.response?.status) {
case 404: {
console.log('404, continuing');
console.log('tried', region, shard, 'got 404, continuing');
break;
}
default: {
console.warn(
'Caught error trying to get user for region/shard detection',
region,
shard,
e
);
break;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "valpal",
"version": "0.0.2",
"version": "0.0.3",
"private": true,
"sideEffects": false,
"scripts": {
Expand Down
178 changes: 102 additions & 76 deletions server/appman.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,108 +113,134 @@ export class AppManager {
};

async connect() {
console.log('Attempting to connect to websocket');
try {
console.log('Attempting to connect to websocket');

const lockfile = await getLockfile();
const lockfile = await getLockfile();

if (!lockfile) {
console.log('Lockfile not found');
setTimeout(() => {
this.connect();
}, 5000);
return;
}
if (!lockfile) {
console.log('Lockfile not found');
setTimeout(() => {
this.connect();
}, 5000);
return;
}

const { port, password } = lockfile;
const { port, password } = lockfile;

const ws = new WebSocket(`wss://riot:${password}@localhost:${port}`, {
rejectUnauthorized: false,
});
const ws = new WebSocket(`wss://riot:${password}@localhost:${port}`, {
rejectUnauthorized: false,
});

let user: User;
let user: User;

const matchCharacterSelectionStates = new Map<string, string>();
const matchCharacterSelectionStates = new Map<string, string>();

ws.on('open', async () => {
console.log('Connected to websocket');

user = (await getUser())!;
ws.on('open', async () => {
try {
console.log('Connected to websocket');

ws.send(JSON.stringify([5, 'OnJsonApiEvent']));
});
user = (await getUser())!;

let abortController: AbortController = new AbortController();
ws.send(JSON.stringify([5, 'OnJsonApiEvent']));
} catch (e) {
console.warn('Caught error in websocket open handler', e);
}
});

ws.on('message', async (data) => {
const parsed = tryParseJson<[number, string, object]>(data.toString());
let abortController: AbortController = new AbortController();

if (!parsed) {
return;
}
ws.on('message', async (data) => {
try {
if (!user) {
return;
}

const [type, event, payload] = parsed;
const parsed = tryParseJson<[number, string, object]>(
data.toString()
);

if (type !== 8 || event !== 'OnJsonApiEvent') {
return;
}
if (!parsed) {
return;
}

const { uri, eventType } = payload as {
uri: string;
eventType: string;
data: object;
};
const [type, event, payload] = parsed;

const uriMatch = uri.match(/\/pregame\/v1\/matches\/(.*)/);
if (eventType === 'Create' && uriMatch && uriMatch[1]) {
const matchId = uriMatch[1];
console.log('Match found:', matchId);
if (type !== 8 || event !== 'OnJsonApiEvent') {
return;
}

const existingState = matchCharacterSelectionStates.get(matchId);
const { uri, eventType } = payload as {
uri: string;
eventType: string;
data: object;
};

if (existingState === 'locked') {
console.log('Match already locked, no need to process');
return;
}
const uriMatch = uri.match(/\/pregame\/v1\/matches\/(.*)/);
if (eventType === 'Create' && uriMatch && uriMatch[1]) {
const matchId = uriMatch[1];
console.log('Match found:', matchId);

const match = await user.getPregame();
const existingState = matchCharacterSelectionStates.get(matchId);

const player = match.AllyTeam.Players.find(
(player) => player.Subject === user.userId
);
if (existingState === 'locked') {
console.log('Match already locked, no need to process');
return;
}

const newState = player?.CharacterSelectionState;
const match = await user.getPregame();

if (
newState &&
newState !== matchCharacterSelectionStates.get(matchId)
) {
if (newState === 'locked' && this.isAutoShuffleEnabled) {
this.equip(
user,
this.isAgentDetectionEnabled ? player.CharacterID : undefined
const player = match.AllyTeam.Players.find(
(player) => player.Subject === user.userId
);
}

matchCharacterSelectionStates.set(matchId, newState);
const newState = player?.CharacterSelectionState;

if (
newState &&
newState !== matchCharacterSelectionStates.get(matchId)
) {
if (newState === 'locked' && this.isAutoShuffleEnabled) {
this.equip(
user,
this.isAgentDetectionEnabled ? player.CharacterID : undefined
);
}

matchCharacterSelectionStates.set(matchId, newState);
}
}
} catch (e) {
console.warn('Caught error in websocket message handler:', e);
}
}
});
});

ws.on('close', () => {
abortController?.abort();
console.log('Disconnected from websocket');
setTimeout(() => {
this.connect();
}, 5000);
});
ws.on('close', () => {
try {
abortController?.abort();
console.log('Disconnected from websocket');
setTimeout(() => {
this.connect();
}, 5000);
} catch (e) {
console.warn('Caught error in websocket close handler:', e);
}
});

ws.on('error', (err) => {
abortController?.abort();
console.log('Error:', err);
setTimeout(() => {
this.connect();
}, 5000);
});
ws.on('error', (err) => {
try {
abortController?.abort();
console.log('Error:', err);
setTimeout(() => {
this.connect();
}, 5000);
} catch (e) {
console.warn('Caught error in websocket error handler:', e);
}
});
} catch (e) {
console.warn('Caught error in connect method:', e);
}
}

equip = async (user: User, agentId?: string) => {
Expand Down

0 comments on commit b19e99d

Please sign in to comment.