Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,122 changes: 1,122 additions & 0 deletions generated/schema.graphql

Large diffs are not rendered by default.

1,369 changes: 1,335 additions & 34 deletions generated/schema.ts

Large diffs are not rendered by default.

62,195 changes: 32,102 additions & 30,093 deletions generated/types.ts

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion hasura/functions/steam-account/claim_free_steam_account.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ declare
begin
-- Prefer the account this node last logged in with so the
-- per-account Steam cache stays warm.
-- role = 'gpu' only: 'friends' accounts are reserved for the presence bot and
-- must never be claimed for GPU work (a Steam account can't log in twice).
if p_node_id is not null then
select id
into v_id
from steam_accounts
where last_node_id = p_node_id
and role = 'gpu'
and id not in (select * from busy_steam_account_ids())
order by id
for update skip locked
Expand All @@ -22,7 +25,8 @@ begin
select id
into v_id
from steam_accounts
where id not in (select * from busy_steam_account_ids())
where role = 'gpu'
and id not in (select * from busy_steam_account_ids())
order by id
for update skip locked
limit 1;
Expand Down
64 changes: 64 additions & 0 deletions hasura/metadata/actions.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1446,3 +1446,67 @@ type Mutation {
): SuccessOutput
}


type Mutation {
assignSteamPresenceBot: SteamPresenceBotAssignment
}

type Query {
steamPresenceAdminStatus: SteamPresenceAdminStatusOutput!
}

type Mutation {
addSteamPresenceBotAccount(
username: String!
bot_secret: String!
friend_capacity: Int
): SuccessOutput
}

type Mutation {
submitSteamPresenceSteamGuard(
account_id: String!
code: String!
): SuccessOutput
}

type Mutation {
removeSteamPresenceBotAccount(
account_id: String!
): SuccessOutput
}

type SteamPresenceBotAssignment {
enabled: Boolean!
steamId: String
addUrl: String
status: String
}

type SteamPresencePool {
bots: Int!
online: Int!
watching: Int!
pending: Int!
capacity: Int!
}

type SteamPresenceBot {
id: String!
username: String!
steamId: String
steamLevel: Int
online: Boolean!
needs2fa: Boolean!
guardType: String
guardLastWrong: Boolean!
watching: Int!
assigned: Int!
capacity: Int!
}

type SteamPresenceAdminStatusOutput {
enabled: Boolean!
pool: SteamPresencePool!
bots: [SteamPresenceBot!]!
}
44 changes: 44 additions & 0 deletions hasura/metadata/actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,46 @@ actions:
permissions:
- role: administrator
comment: Write content to file on game server
- name: assignSteamPresenceBot
definition:
kind: synchronous
handler: '{{HASURA_GRAPHQL_ACTIONS_HOOK}}'
forward_client_headers: true
permissions:
- role: user
comment: Assign the presence bot a user should add as a friend
- name: steamPresenceAdminStatus
definition:
kind: ""
handler: '{{HASURA_GRAPHQL_ACTIONS_HOOK}}'
forward_client_headers: true
permissions:
- role: administrator
comment: Steam presence bot admin dashboard status
- name: addSteamPresenceBotAccount
definition:
kind: synchronous
handler: '{{HASURA_GRAPHQL_ACTIONS_HOOK}}'
forward_client_headers: true
permissions:
- role: administrator
comment: Add a friends-role presence bot account to the pool
- name: submitSteamPresenceSteamGuard
definition:
kind: synchronous
handler: '{{HASURA_GRAPHQL_ACTIONS_HOOK}}'
forward_client_headers: true
permissions:
- role: administrator
comment: Submit a Steam Guard code for a presence bot account
- name: removeSteamPresenceBotAccount
definition:
kind: synchronous
handler: '{{HASURA_GRAPHQL_ACTIONS_HOOK}}'
forward_client_headers: true
permissions:
- role: administrator
comment: Remove a friends-role presence bot account
custom_types:
enums: []
input_objects:
Expand All @@ -1106,6 +1146,10 @@ custom_types:
- name: ClipOutputInput
- name: ClipSpecInput
objects:
- name: SteamPresenceBotAssignment
- name: SteamPresencePool
- name: SteamPresenceBot
- name: SteamPresenceAdminStatusOutput
- name: SteamMatchHistoryLinkOutput
- name: SteamMatchHistoryPollOutput
- name: PendingMatchImportActionOutput
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
table:
name: player_steam_bot_friend
schema: public
object_relationships:
- name: player
using:
foreign_key_constraint_on: steam_id
select_permissions:
- role: user
permission:
columns:
- steam_id
- bot_steamid64
- status
- last_presence_state
- friended_at
- created_at
- updated_at
filter:
steam_id:
_eq: X-Hasura-User-Id
comment: ""
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ select_permissions:
- role
- status
- steam_id
- last_presence_state
- presence_updated_at
filter:
friend_steam_id:
_eq: X-Hasura-User-Id
Expand Down
1 change: 1 addition & 0 deletions hasura/metadata/databases/default/tables/tables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
- "!include public_player_premier_rank_history.yaml"
- "!include public_player_sanctions.yaml"
- "!include public_player_stats.yaml"
- "!include public_player_steam_bot_friend.yaml"
- "!include public_player_steam_match_auth.yaml"
- "!include public_player_unused_utility.yaml"
- "!include public_player_utility.yaml"
Expand Down
10 changes: 10 additions & 0 deletions hasura/migrations/default/1865000000000_steam_presence/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DELETE FROM public.e_notification_types WHERE "value" = 'MatchImported';

DROP INDEX IF EXISTS public.idx_steam_accounts_role;
ALTER TABLE "public"."steam_accounts"
DROP COLUMN IF EXISTS "friend_capacity",
DROP COLUMN IF EXISTS "steamid64",
DROP COLUMN IF EXISTS "role";

DROP INDEX IF EXISTS public.idx_player_steam_bot_friend_account;
DROP TABLE IF EXISTS "public"."player_steam_bot_friend";
39 changes: 39 additions & 0 deletions hasura/migrations/default/1865000000000_steam_presence/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
-- Tracks which players have added a 5stack presence bot as a Steam friend, so
-- the bot can watch their live match state and trigger near-real-time imports.
CREATE TABLE IF NOT EXISTS "public"."player_steam_bot_friend" (
"steam_id" bigint NOT NULL,
-- Which pool account they friended (nullable in the single-bot phase).
"bot_steam_account_id" uuid,
"bot_steamid64" bigint,
-- 'pending' (invite shown, not yet accepted) | 'friends'
"status" text NOT NULL DEFAULT 'pending',
"last_presence_state" jsonb,
"friended_at" timestamptz,
"created_at" timestamptz NOT NULL DEFAULT now(),
"updated_at" timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY ("steam_id"),
FOREIGN KEY ("steam_id") REFERENCES "public"."players"("steam_id") ON UPDATE cascade ON DELETE cascade,
FOREIGN KEY ("bot_steam_account_id") REFERENCES "public"."steam_accounts"("id") ON UPDATE cascade ON DELETE set null
);

-- Find all friends assigned to a given bot account (capacity / sharding).
CREATE INDEX IF NOT EXISTS idx_player_steam_bot_friend_account
ON public.player_steam_bot_friend (bot_steam_account_id);

-- Partition the shared pool: an account is used EITHER for GPU work (logging in
-- on a game node to watch demos/render) OR as a presence "friends" bot — never
-- both, since a Steam account can only be logged in one place at a time. The
-- GPU claimer (claim_free_steam_account) filters role='gpu', so friends accounts
-- are never handed to a GPU job.
ALTER TABLE "public"."steam_accounts"
ADD COLUMN IF NOT EXISTS "role" text NOT NULL DEFAULT 'gpu',
ADD COLUMN IF NOT EXISTS "steamid64" bigint,
ADD COLUMN IF NOT EXISTS "friend_capacity" integer NOT NULL DEFAULT 250;

CREATE INDEX IF NOT EXISTS idx_steam_accounts_role
ON public.steam_accounts (role);

-- Notification fired when a player's Valve match is auto-imported.
INSERT INTO public.e_notification_types ("value", "description") VALUES
('MatchImported', 'A Valve match you played was imported to 5stack')
ON CONFLICT (value) DO UPDATE SET "description" = EXCLUDED."description";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE "public"."steam_accounts" DROP COLUMN IF EXISTS "steam_level";
DROP INDEX IF EXISTS public.idx_steam_presence_events_created_at;
DROP TABLE IF EXISTS "public"."steam_presence_events";
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- Real-time activity feed for the presence bot admin dashboard, subscribed to
-- over websockets via Hasura (replaces the Redis ring buffer).
CREATE TABLE IF NOT EXISTS "public"."steam_presence_events" (
"id" uuid NOT NULL DEFAULT gen_random_uuid(),
"type" text NOT NULL,
"message" text NOT NULL,
"created_at" timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY ("id")
);

CREATE INDEX IF NOT EXISTS idx_steam_presence_events_created_at
ON public.steam_presence_events (created_at DESC);

-- Bot's Steam level — friend capacity is derived from it (Steam allows
-- 250 + 5*level friends, capped at 2000). Detected on login.
ALTER TABLE "public"."steam_accounts"
ADD COLUMN IF NOT EXISTS "steam_level" integer;
8 changes: 6 additions & 2 deletions hasura/views/v_my_friends.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ SELECT DISTINCT ON (p.steam_id, fr.friend_steam_id)
fr.status,
fr.friend_steam_id,
fr.invited_by_steam_id,
get_player_elo(p) AS elo
get_player_elo(p) AS elo,
pbf.last_presence_state,
pbf.updated_at AS presence_updated_at
FROM friend_relationships fr
JOIN players p ON p.steam_id = fr.player_steam_id;
JOIN players p ON p.steam_id = fr.player_steam_id
LEFT JOIN player_steam_bot_friend pbf
ON pbf.steam_id = p.steam_id AND pbf.status = 'friends';

CREATE OR REPLACE FUNCTION public.ti_v_my_friends() RETURNS trigger
LANGUAGE plpgsql
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { FixturesModule } from "./fixtures/fixtures.module";
import { TournamentsModule } from "./tournaments/tournaments.module";
import { FaceitModule } from "./faceit/faceit.module";
import { SteamMatchHistoryModule } from "./steam-match-history/steam-match-history.module";
import { SteamPresenceModule } from "./steam-presence/steam-presence.module";
import { NewsModule } from "./news/news.module";
import { ScrimsModule } from "./scrims/scrims.module";

Expand Down Expand Up @@ -144,6 +145,7 @@ import { ScrimsModule } from "./scrims/scrims.module";
TournamentsModule,
FaceitModule,
SteamMatchHistoryModule,
SteamPresenceModule,
NewsModule,
ScrimsModule,
],
Expand Down
6 changes: 6 additions & 0 deletions src/hasura/hasura.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ export class HasuraService {
"insert into settings (name, value) values ('relay_domain', $1) on conflict (name) do update set value = $1",
[this.appConfig.relayDomain],
);

// Steam presence bot is on by default; seed the row so the admin toggle
// reflects it. `do nothing` preserves an admin's explicit off.
await this.postgresService.query(
"insert into settings (name, value) values ('public.steam_presence_enabled', 'true') on conflict (name) do nothing",
);
}

private async applyMigrations(path: string): Promise<number> {
Expand Down
6 changes: 5 additions & 1 deletion src/postgres/jobs/ReindexTables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ export class ReindexTables extends WorkerHost {
const start = Date.now();

await this.postgres.query(`REINDEX TABLE CONCURRENTLY ${job.data.table}`);
await this.postgres.query(`VACUUM (ANALYZE) ${job.data.table}`);

this.logger.log(`Reindexed ${job.data.table}`, `${Date.now() - start}ms`);
this.logger.log(
`Reindexed + vacuumed ${job.data.table}`,
`${Date.now() - start}ms`,
);
return;
}

Expand Down
36 changes: 36 additions & 0 deletions src/steam-match-history/match-import.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { DemoMetadataService } from "../demos/demo-metadata.service";
import { ParsedDemo, ParsedPlayer } from "../demos/demo-parser.service";
import { S3Service } from "../s3/s3.service";
import { FaceitService } from "../faceit/faceit.service";
import { SteamPresenceService } from "../steam-presence/steam-presence.service";
import { e_match_types_enum } from "../../generated";

type MatchType = e_match_types_enum;
Expand All @@ -36,6 +37,7 @@ export class MatchImportService {
private readonly s3: S3Service,
@Inject(forwardRef(() => FaceitService))
private readonly faceit: FaceitService,
private readonly steamPresence: SteamPresenceService,
@InjectQueue(SteamMatchHistoryQueues.CheckSteamBansForMatch)
private readonly steamBansQueue: Queue,
) {
Expand Down Expand Up @@ -248,9 +250,43 @@ export class MatchImportService {
),
);

void this.steamPresence
.notifyMatchImported({
matchId,
matchType,
mapName: parsed.map_name ?? null,
players: MatchImportService.computeImportedPlayers(parsed, players),
})
.catch((error) =>
this.logger.warn(
`match-imported notify failed for match ${matchId}: ${(error as Error)?.message ?? String(error)}`,
),
);

return { matchId };
}

private static computeImportedPlayers(
parsed: ParsedDemo,
players: ParsedPlayer[],
): Array<{ steamId: string; kills: number; deaths: number }> {
const kills = new Map<string, number>();
const deaths = new Map<string, number>();
for (const kill of parsed.kills ?? []) {
if (kill.killer) {
kills.set(kill.killer, (kills.get(kill.killer) ?? 0) + 1);
}
if (kill.victim) {
deaths.set(kill.victim, (deaths.get(kill.victim) ?? 0) + 1);
}
}
return players.map((player) => ({
steamId: player.steam_id,
kills: kills.get(player.steam_id) ?? 0,
deaths: deaths.get(player.steam_id) ?? 0,
}));
}

// On import AND re-import: (1) refresh every participant's CURRENT faceit elo
// onto their players row — exactly what the player page does — then (2)
// snapshot this match's elo into the rank history.
Expand Down
2 changes: 2 additions & 0 deletions src/steam-match-history/steam-match-history.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { PostgresModule } from "../postgres/postgres.module";
import { loggerFactory } from "../utilities/LoggerFactory";
import { getQueuesProcessors } from "../utilities/QueueProcessors";
import { FaceitModule } from "../faceit/faceit.module";
import { SteamPresenceModule } from "../steam-presence/steam-presence.module";
import { SteamMatchHistoryController } from "./steam-match-history.controller";
import { SteamMatchHistoryService } from "./steam-match-history.service";
import { SteamBansService } from "./steam-bans.service";
Expand Down Expand Up @@ -86,6 +87,7 @@ import { ProcessUploadedDemo } from "./jobs/ProcessUploadedDemo";
ClipsModule,
PostgresModule,
forwardRef(() => FaceitModule),
SteamPresenceModule,
],
providers: [
SteamMatchHistoryService,
Expand Down
Loading