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 change: 1 addition & 0 deletions drizzle/0008_sticky_harrier.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "puzzle_completions" ADD COLUMN "country" text DEFAULT 'XX' NOT NULL;
108 changes: 108 additions & 0 deletions drizzle/meta/0008_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"id": "575abe9f-65f9-42e8-b6f7-9e715c4b0ad7",
"prevId": "81ed1aad-013c-4677-a86b-123dc69df522",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.puzzle_completions": {
"name": "puzzle_completions",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"user_id": {
"name": "user_id",
"type": "text",
"primaryKey": false,
"notNull": false
},
"puzzle_id": {
"name": "puzzle_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"num_moves": {
"name": "num_moves",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"country": {
"name": "country",
"type": "text",
"primaryKey": false,
"notNull": true,
"default": "'XX'"
},
"attempts": {
"name": "attempts",
"type": "integer",
"primaryKey": false,
"notNull": true,
"default": 1
},
"timestamp": {
"name": "timestamp",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {
"puzzle_completion_puzzle_id_index": {
"name": "puzzle_completion_puzzle_id_index",
"columns": [
{
"expression": "puzzle_id",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
},
"puzzle_completion_id_and_user_id_index": {
"name": "puzzle_completion_id_and_user_id_index",
"columns": [
{
"expression": "puzzle_id",
"isExpression": false,
"asc": true,
"nulls": "last"
},
{
"expression": "user_id",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
7 changes: 7 additions & 0 deletions drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@
"when": 1735947892500,
"tag": "0007_abnormal_magus",
"breakpoints": true
},
{
"idx": 8,
"version": "7",
"when": 1770888296796,
"tag": "0008_sticky_harrier",
"breakpoints": true
}
]
}
12 changes: 6 additions & 6 deletions package-lock.json

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

1 change: 1 addition & 0 deletions src/lib/server/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const puzzleCompletionTable = pgTable(
userId: text('user_id'),
puzzleId: text('puzzle_id').notNull(),
numMoves: integer('num_moves').notNull(),
country: text('country').notNull().default('XX'),
attempts: integer('attempts').notNull().default(1),
timestamp: timestamp('timestamp', { withTimezone: true }).notNull().defaultNow()
},
Expand Down
5 changes: 5 additions & 0 deletions src/lib/server/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { RequestEvent } from '@sveltejs/kit';

export const getRequestCountry = (e: RequestEvent) => {
return e.request.headers.get('CF-IPCountry') || 'XX';
};
15 changes: 7 additions & 8 deletions src/routes/api/stats/+server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { db } from '$lib/server/db';
import { getPuzzleStatistics } from '$lib/server/db/handlers/stats';
import { puzzleCompletionTable } from '$lib/server/db/schema';
import { getRequestCountry } from '$lib/server/helpers';
import { cached } from '$lib/server/kv';
import { getPlatform } from '$lib/server/miniflare';
import { Dir } from '$lib/stores/grid';
Expand Down Expand Up @@ -63,18 +64,15 @@ export const POST = endpoint({
eq(puzzleCompletionTable.userId, uid)
);

console.log(verified);

// TODO: maybe use transaction?
const [existing] = await db.select().from(puzzleCompletionTable).where(puzzleIdAndUserId);

console.log({ existing });

if (!existing) {
await db.insert(puzzleCompletionTable).values({
puzzleId: puzzle.id,
numMoves: verified.moves,
userId: uid
userId: uid,
country: getRequestCountry(e)
});

console.log('inserted new', {
Expand All @@ -99,15 +97,15 @@ export const POST = endpoint({
console.log('inserted better', {
numMoves: verified.moves,
timestamp: new Date(),
attempts: existing.attempts + 1 // increase attempts
attempts: existing.attempts + 1
});

return;
}

await db
.update(puzzleCompletionTable)
.set({ attempts: existing.attempts + 1 }) // increase attempts always
.set({ attempts: existing.attempts + 1 }) // increase attempts even if result is worse
.where(puzzleIdAndUserId);

console.log('increased attempts');
Expand All @@ -117,7 +115,8 @@ export const POST = endpoint({
await db.insert(puzzleCompletionTable).values({
puzzleId: puzzle.id,
numMoves: verified.moves,
userId: null
userId: null,
country: getRequestCountry(e)
});
}
}),
Expand Down