File size: 5,105 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
 * Community server federation — connect, sync, and manage servers.
 *
 * @module lib/gamification/servers
 */

import crypto from "crypto";

export interface ServerConnection {
  id: string;
  name: string;
  url: string;
  status: "connected" | "disconnected" | "error";
  lastSyncAt: string | null;
  errorMessage: string | null;
}

/**
 * Connect to a community server.
 */
export async function connectServer(
  name: string,
  url: string,
  apiKey: string
): Promise<ServerConnection> {
  const id = crypto.randomUUID();
  const apiKeyHash = crypto
    .pbkdf2Sync(apiKey, "omniroute-federation-salt", 120000, 32, "sha256")
    .toString("hex");

  const { connectServer: dbConnect } = await import("../db/gamification");
  dbConnect(id, name, url, apiKeyHash);

  return { id, name, url, status: "connected", lastSyncAt: null, errorMessage: null };
}

/**
 * Disconnect from a community server.
 */
export async function disconnectServer(serverId: string): Promise<void> {
  const { disconnectServer: dbDisconnect } = await import("../db/gamification");
  dbDisconnect(serverId);
}

/**
 * List all connected servers.
 */
export async function listServers(): Promise<ServerConnection[]> {
  const { listServers: dbList } = await import("../db/gamification");
  return dbList() as ServerConnection[];
}

/**
 * Sync leaderboard with a community server.
 * Fetches remote scores and merges into local leaderboard.
 */
export async function syncLeaderboard(
  serverId: string
): Promise<{ synced: number; errors: string[] }> {
  const db = (await import("../db/core")).getDbInstance();

  const server = db
    .prepare(
      "SELECT url, api_key_hash FROM community_servers WHERE id = ? AND status = 'connected'"
    )
    .get(serverId) as { url: string; api_key_hash: string } | undefined;

  if (!server) {
    return { synced: 0, errors: ["Server not found or not connected"] };
  }

  try {
    // Fetch remote leaderboard
    const response = await fetch(`${server.url}/api/gamification/federation/leaderboard`, {
      headers: { Authorization: `Bearer ${server.api_key_hash}` },
      signal: AbortSignal.timeout(10000),
    });

    if (!response.ok) {
      throw new Error(`HTTP ${response.status}`);
    }

    const data = (await response.json()) as {
      entries: Array<{ apiKeyId: string; score: number }>;
    };

    // Overwrite local scores with remote scores (not additive)
    const db2 = (await import("../db/core")).getDbInstance();
    for (const entry of data.entries) {
      db2
        .prepare(
          `INSERT INTO leaderboard (api_key_id, scope, score, updated_at)
         VALUES (?, 'global', ?, datetime('now'))
         ON CONFLICT(api_key_id, scope) DO UPDATE SET score = excluded.score, updated_at = excluded.updated_at`
        )
        .run(entry.apiKeyId, entry.score);
    }

    // Update last sync time
    db.prepare(
      "UPDATE community_servers SET last_sync_at = datetime('now'), error_message = NULL WHERE id = ?"
    ).run(serverId);

    return { synced: data.entries.length, errors: [] };
  } catch (err: any) {
    db.prepare("UPDATE community_servers SET status = 'error', error_message = ? WHERE id = ?").run(
      err.message,
      serverId
    );

    return { synced: 0, errors: [err.message] };
  }
}

/**
 * Push local scores to a community server.
 */
export async function pushScore(
  serverId: string,
  apiKeyId: string,
  score: number
): Promise<{ success: boolean; error?: string }> {
  const db = (await import("../db/core")).getDbInstance();

  const server = db
    .prepare(
      "SELECT url, api_key_hash FROM community_servers WHERE id = ? AND status = 'connected'"
    )
    .get(serverId) as { url: string; api_key_hash: string } | undefined;

  if (!server) {
    return { success: false, error: "Server not found or not connected" };
  }

  try {
    const response = await fetch(`${server.url}/api/gamification/federation/score`, {
      method: "POST",
      headers: {
        Authorization: `Bearer ${server.api_key_hash}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ apiKeyId, score }),
      signal: AbortSignal.timeout(10000),
    });

    if (!response.ok) {
      throw new Error(`HTTP ${response.status}`);
    }

    return { success: true };
  } catch (err: any) {
    return { success: false, error: err.message };
  }
}

/**
 * Health check a server connection.
 */
export async function healthCheck(
  serverId: string
): Promise<{ healthy: boolean; latencyMs: number }> {
  const db = (await import("../db/core")).getDbInstance();

  const server = db.prepare("SELECT url FROM community_servers WHERE id = ?").get(serverId) as
    | { url: string }
    | undefined;

  if (!server) return { healthy: false, latencyMs: 0 };

  const start = Date.now();
  try {
    const response = await fetch(`${server.url}/api/gamification/federation/leaderboard`, {
      signal: AbortSignal.timeout(5000),
    });
    return { healthy: response.ok, latencyMs: Date.now() - start };
  } catch {
    return { healthy: false, latencyMs: Date.now() - start };
  }
}