Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: is verified player flag #335

Merged
merged 1 commit into from
Feb 16, 2024
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
10 changes: 5 additions & 5 deletions src/server/api/match-maker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ const makeMatch = () => {
let botsInMatch = getRandomInt({ max: MAX_BOTS_PER_MATCH, min: 1 });
const humansInMatch = env.PLAYERS_PER_MATCH - botsInMatch;

const playerIds = lobbyQueue.pickPlayers(humansInMatch);
const humans = lobbyQueue.pickPlayers(humansInMatch);

if (playerIds.length < humansInMatch) {
botsInMatch = env.PLAYERS_PER_MATCH - playerIds.length;
if (humans.length < humansInMatch) {
botsInMatch = env.PLAYERS_PER_MATCH - humans.length;
}

const totalPlayers = playerIds.length + botsInMatch;
const totalPlayers = humans.length + botsInMatch;

if (
botsInMatch <= MAX_BOTS_PER_MATCH &&
totalPlayers === env.PLAYERS_PER_MATCH
) {
const match = new Match(playerIds, botsInMatch);
const match = new Match(humans, botsInMatch);

matches.set(match.id, match);
}
Expand Down
9 changes: 7 additions & 2 deletions src/server/api/routers/lobby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { observable } from "@trpc/server/observable";

import type { QueueUpdatePayload, ReadyToPlayPayload } from "~/types/index.js";
import { lobbyQueue } from "~/server/service/index.js";
import { selectUserById } from "~/server/db/user.js";

import { createTRPCRouter, protectedProcedure } from "../trpc.js";
import { ee, getOngoingMatchByUserId } from "../match-maker.js";
Expand Down Expand Up @@ -34,9 +35,13 @@ export const lobbyRouter = createTRPCRouter({
};
});
}),
join: protectedProcedure.mutation(({ ctx }) => {
join: protectedProcedure.mutation(async ({ ctx }) => {
const { id } = ctx.session.user;
const joinCount = lobbyQueue.join(id);
const user = await selectUserById(id);

if (!user) throw new TRPCError({ code: "FORBIDDEN" });

const joinCount = lobbyQueue.join(user);
const userIsPlaying = !!getOngoingMatchByUserId(id);

if (joinCount > 1 || userIsPlaying) {
Expand Down
4 changes: 2 additions & 2 deletions src/server/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
uuid,
varchar,
} from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod";
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import { type z } from "zod";

import { PUBLIC_KEY_LENGTH } from "~/constants/index.js";
Expand All @@ -35,7 +35,7 @@ export const usersRelations = relations(users, ({ one }) => ({
}),
}));

export const userSchema = createInsertSchema(users);
export const userSchema = createSelectSchema(users);
export type User = z.infer<typeof userSchema>;

export const userAchievements = bbPgTable("user_achievement", {
Expand Down
1 change: 1 addition & 0 deletions src/server/service/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export class Agent {
votes: [],
achievements: [],
isOnline: true,
isVerified: true,
};
}

Expand Down
22 changes: 12 additions & 10 deletions src/server/service/lobby-queue.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { type User } from "~/server/db/schema.js";

class LobbyQueue {
private _listeners = new Map<string, number>();
private _queue: string[] = [];
private _queue: User[] = [];

get queue() {
return this._queue;
}

join(userId: string) {
const count = this._listeners.get(userId);
join(user: User) {
const count = this._listeners.get(user.id);
const newCount = count ? count + 1 : 1;

this._listeners.set(userId, newCount);
this._listeners.set(user.id, newCount);

if (newCount === 1) {
this._queue.push(userId);
this._queue.push(user);
}

return newCount;
Expand All @@ -27,23 +29,23 @@ class LobbyQueue {
if (count <= 1) {
this._listeners.delete(userId);

const index = this._queue.indexOf(userId);
const index = this._queue.findIndex((u) => u.id === userId);
this._queue.splice(index, 1);
} else {
this._listeners.set(userId, count - 1);
}
}

pickPlayers(humansInMatch: number) {
const ids = this._queue.splice(0, humansInMatch);
const users = this._queue.splice(0, humansInMatch);

ids.forEach((id) => this._listeners.delete(id));
users.forEach((user) => this._listeners.delete(user.id));

return ids;
return users;
}

getPlayerPosition(userId: string) {
return this._queue.indexOf(userId) + 1;
return this._queue.findIndex((u) => u.id === userId) + 1;
}

has(userId: string) {
Expand Down
29 changes: 10 additions & 19 deletions src/server/service/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
userAchievements,
type UserAchievements,
users,
type User,
} from "~/server/db/schema.js";
import {
selectMatchPlayedByUser,
Expand Down Expand Up @@ -91,7 +92,7 @@ export class Match {
return this._agents;
}

constructor(playerIds: string[], botsInMatch: number) {
constructor(users: User[], botsInMatch: number) {
this._id = uuid();

this._agents = lodash.range(0, botsInMatch).map(() => {
Expand All @@ -101,16 +102,18 @@ export class Match {

const botPlayers = this.agents.map((agent) => agent.toPlayer());

const humanPlayers = playerIds.map((id) => {
const humanPlayers = users.map((user) => {
const characterId = this.popCharacterId();
return this.generatePlayer(id, characterId);
return this.generatePlayer(user, characterId);
});

this._players = lodash.shuffle([...botPlayers, ...humanPlayers]);

this.addPrompt();

this.initMatch(playerIds).catch((err) =>
const userIds = users.map((u) => u.id);

this.initMatch(userIds).catch((err) =>
console.error("Error initializing match: ", err),
);

Expand All @@ -122,7 +125,6 @@ export class Match {

private async initMatch(playerIds: string[]) {
await this.getPlayerStats();
await this.checkVerifiedPlayers();

ee.emit("readyToPlay", {
roomId: this._id,
Expand Down Expand Up @@ -172,9 +174,9 @@ export class Match {
return characterId;
}

private generatePlayer(userId: string, characterId: CharacterId): PlayerType {
private generatePlayer(user: User, characterId: CharacterId): PlayerType {
return {
userId,
userId: user.id,
characterId,
score: 0,
isBot: false,
Expand All @@ -189,6 +191,7 @@ export class Match {
correctGuesses: 0,
achievements: [],
isOnline: true,
isVerified: !!(user.username && user.address),
};
}

Expand Down Expand Up @@ -242,18 +245,6 @@ export class Match {
);
}

private async checkVerifiedPlayers() {
const promises = this.players
.filter((player) => !player.isBot)
.map(async (player) => {
if (player.isVerified !== undefined) return;

const checkPlayer = await selectUserById(player.userId);
player.isVerified = !!(checkPlayer?.username && checkPlayer?.address);
});
await Promise.allSettled(promises);
}

// TODO: make a proper DB relation with user and matches instead of doing this
private async getPlayerStats() {
const promises = this.players
Expand Down
2 changes: 1 addition & 1 deletion src/types/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const playerSchema = z.object({
humansFooledScore: z.number(),
correctGuesses: z.number(),
votes: z.array(z.string().uuid()).optional(), // array of voted ids
isVerified: z.boolean().optional(),
isVerified: z.boolean(),
achievements: z.array(achievementIdSchema), // array of achievement ids
isOnline: z.boolean(),
});
Expand Down