Initial clean state

This commit is contained in:
RandyJC
2025-12-09 08:55:01 +01:00
commit 497dd2ea4c
115 changed files with 12391 additions and 0 deletions

1
packages/common/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
node_modules

View File

@@ -0,0 +1,208 @@
import js from "@eslint/js"
import { defineConfig } from "eslint/config"
import globals from "globals"
import tseslint from "typescript-eslint"
export default defineConfig([
{
ignores: ["**/node_modules/**"],
},
{
files: ["**/*.ts"],
languageOptions: {
...js.configs.recommended.languageOptions,
parser: tseslint.parser,
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
globals: {
...globals.node,
},
},
plugins: {
"@typescript-eslint": tseslint.plugin,
},
rules: {
...js.configs.recommended.rules,
...tseslint.configs.recommendedTypeChecked[0].rules,
"array-callback-return": [
"error",
{ allowImplicit: false, checkForEach: true, allowVoid: true },
],
"no-await-in-loop": "error",
"no-constant-binary-expression": "error",
"no-constructor-return": "error",
"no-duplicate-imports": ["error", { includeExports: true }],
"no-new-native-nonconstructor": "error",
"no-promise-executor-return": ["error", { allowVoid: true }],
"no-self-compare": "error",
"no-template-curly-in-string": "error",
"no-unmodified-loop-condition": "error",
"no-unreachable-loop": "error",
"no-unused-private-class-members": "error",
"arrow-body-style": ["error", "as-needed"],
camelcase: [
"error",
{
properties: "always",
ignoreDestructuring: true,
ignoreImports: true,
ignoreGlobals: true,
},
],
"capitalized-comments": [
"error",
"always",
{ ignoreConsecutiveComments: true },
],
"class-methods-use-this": ["error", { enforceForClassFields: true }],
complexity: ["warn", 40],
"consistent-return": "error",
curly: ["error", "all"],
"default-param-last": "error",
"dot-notation": "error",
eqeqeq: ["error", "always"],
"func-name-matching": "error",
"func-names": "error",
"func-style": ["error", "declaration", { allowArrowFunctions: true }],
"grouped-accessor-pairs": ["error", "getBeforeSet"],
"guard-for-in": "error",
"init-declarations": ["error", "always"],
"logical-assignment-operators": [
"error",
"always",
{ enforceForIfStatements: true },
],
"max-classes-per-file": ["error", { ignoreExpressions: true }],
"max-depth": ["error", 3],
"max-lines": [
"error",
{ max: 500, skipBlankLines: true, skipComments: true },
],
"max-nested-callbacks": ["error", 3],
"max-params": ["error", 4],
"multiline-comment-style": ["error", "separate-lines"],
"no-alert": "error",
"no-bitwise": "error",
"no-caller": "error",
"no-else-return": "error",
"no-empty-function": "error",
"no-empty-static-block": "error",
"no-eq-null": "error",
"no-eval": "error",
"no-extend-native": "error",
"no-extra-label": "error",
"no-implicit-coercion": "error",
"no-implicit-globals": "error",
"no-implied-eval": "error",
"no-inline-comments": "error",
"no-invalid-this": "error",
"no-iterator": "error",
"no-labels": "error",
"no-lone-blocks": "error",
"no-lonely-if": "error",
"no-loop-func": "error",
"no-multi-assign": "error",
"no-multi-str": "error",
"no-nested-ternary": "error",
"no-new": "error",
"no-new-func": "error",
"no-new-wrappers": "error",
"no-object-constructor": "error",
"no-octal-escape": "error",
"no-param-reassign": "error",
"no-plusplus": "error",
"no-proto": "error",
"no-return-assign": ["error", "always"],
"no-script-url": "error",
"no-sequences": "error",
"no-throw-literal": "error",
"no-undef-init": "error",
"no-unneeded-ternary": ["error", { defaultAssignment: false }],
"no-unused-expressions": ["error", { enforceForJSX: true }],
"no-unused-vars": ["error", { argsIgnorePattern: "^_" }],
"no-useless-call": "error",
"no-useless-computed-key": ["error", { enforceForClassMembers: true }],
"no-useless-concat": "error",
"no-useless-constructor": "error",
"no-useless-rename": "error",
"no-useless-return": "error",
"no-var": "error",
"no-warning-comments": ["error", { terms: ["todo"] }],
"object-shorthand": ["error", "always"],
"one-var": ["error", "never"],
"operator-assignment": ["error", "always"],
"prefer-arrow-callback": "error",
"prefer-const": [
"error",
{ destructuring: "any", ignoreReadBeforeAssign: false },
],
"prefer-destructuring": "error",
"prefer-exponentiation-operator": "error",
"prefer-numeric-literals": "error",
"prefer-object-has-own": "error",
"prefer-object-spread": "error",
"prefer-promise-reject-errors": "error",
"prefer-regex-literals": ["error", { disallowRedundantWrapping: true }],
"prefer-rest-params": "error",
"prefer-spread": "error",
"prefer-template": "error",
radix: "error",
"require-await": "error",
"require-unicode-regexp": "error",
"symbol-description": "error",
yoda: "error",
"line-comment-position": ["error", { position: "above" }],
indent: "off",
"newline-before-return": "error",
"no-undef": "error",
"padded-blocks": ["error", "never"],
"padding-line-between-statements": [
"error",
{
blankLine: "always",
prev: "*",
next: [
"break",
"case",
"cjs-export",
"class",
"continue",
"do",
"if",
"switch",
"try",
"while",
"return",
],
},
{
blankLine: "always",
prev: [
"break",
"case",
"cjs-export",
"class",
"continue",
"do",
"if",
"switch",
"try",
"while",
"return",
],
next: "*",
},
],
quotes: [
"error",
"double",
{ avoidEscape: true, allowTemplateLiterals: true },
],
"space-before-blocks": "error",
semi: ["error", "never"],
},
},
])

View File

@@ -0,0 +1,20 @@
{
"name": "@rahoot/common",
"version": "1.0.0",
"type": "module",
"scripts": {
"lint": "eslint"
},
"dependencies": {
"socket.io": "^4.8.1",
"zod": "^3.25.76"
},
"devDependencies": {
"@eslint/js": "^9.38.0",
"@types/node": "^20.19.23",
"eslint": "^9.38.0",
"globals": "^16.4.0",
"typescript": "^5.9.3",
"typescript-eslint": "^8.46.2"
}
}

View File

@@ -0,0 +1,38 @@
export type Player = {
id: string
clientId: string
connected: boolean
username: string
points: number
}
export type Answer = {
playerId: string
answerId: number
points: number
}
export type Quizz = {
subject: string
questions: {
question: string
image?: string
media?: QuestionMedia
answers: string[]
solution: number
cooldown: number
time: number
}[]
}
export type QuestionMedia =
| { type: "image"; url: string; fileName?: string }
| { type: "audio"; url: string; fileName?: string }
| { type: "video"; url: string; fileName?: string }
export type QuizzWithId = Quizz & { id: string }
export type GameUpdateQuestion = {
current: number
total: number
}

View File

@@ -0,0 +1,99 @@
import { Server as ServerIO, Socket as SocketIO } from "socket.io"
import { GameUpdateQuestion, Player, Quizz, QuizzWithId } from "."
import { Status, StatusDataMap } from "./status"
export type Server = ServerIO<ClientToServerEvents, ServerToClientEvents>
export type Socket = SocketIO<ClientToServerEvents, ServerToClientEvents>
export type Message<K extends keyof StatusDataMap = keyof StatusDataMap> = {
gameId?: string
status: K
data: StatusDataMap[K]
}
export type MessageWithoutStatus<T = any> = {
gameId?: string
data: T
}
export type MessageGameId = {
gameId?: string
}
export interface ServerToClientEvents {
connect: () => void
// Game events
"game:status": (_data: { name: Status; data: StatusDataMap[Status] }) => void
"game:successRoom": (_data: string) => void
"game:successJoin": (_gameId: string) => void
"game:totalPlayers": (_count: number) => void
"game:errorMessage": (_message: string) => void
"game:startCooldown": () => void
"game:cooldown": (_count: number) => void
"game:cooldownPause": (_paused: boolean) => void
"game:reset": (_message: string) => void
"game:updateQuestion": (_data: { current: number; total: number }) => void
"game:playerAnswer": (_count: number) => void
// Player events
"player:successReconnect": (_data: {
gameId: string
status: { name: Status; data: StatusDataMap[Status] }
player: { username: string; points: number }
currentQuestion: GameUpdateQuestion
}) => void
"player:updateLeaderboard": (_data: { leaderboard: Player[] }) => void
// Manager events
"manager:successReconnect": (_data: {
gameId: string
status: { name: Status; data: StatusDataMap[Status] }
players: Player[]
currentQuestion: GameUpdateQuestion
}) => void
"manager:quizzList": (_quizzList: QuizzWithId[]) => void
"manager:gameCreated": (_data: { gameId: string; inviteCode: string }) => void
"manager:statusUpdate": (_data: {
status: Status
data: StatusDataMap[Status]
}) => void
"manager:newPlayer": (_player: Player) => void
"manager:removePlayer": (_playerId: string) => void
"manager:players": (_players: Player[]) => void
"manager:errorMessage": (_message: string) => void
"manager:playerKicked": (_playerId: string) => void
"manager:quizzLoaded": (_quizz: QuizzWithId) => void
"manager:quizzSaved": (_quizz: QuizzWithId) => void
"manager:quizzDeleted": (_id: string) => void
}
export interface ClientToServerEvents {
// Manager actions
"game:create": (_quizzId: string) => void
"manager:auth": (_password: string) => void
"manager:reconnect": (_message: { gameId: string }) => void
"manager:kickPlayer": (_message: { gameId: string; playerId: string }) => void
"manager:startGame": (_message: MessageGameId) => void
"manager:abortQuiz": (_message: MessageGameId) => void
"manager:pauseCooldown": (_message: MessageGameId) => void
"manager:resumeCooldown": (_message: MessageGameId) => void
"manager:endGame": (_message: MessageGameId) => void
"manager:skipQuestionIntro": (_message: MessageGameId) => void
"manager:nextQuestion": (_message: MessageGameId) => void
"manager:deleteQuizz": (_message: { id: string }) => void
"manager:showLeaderboard": (_message: MessageGameId) => void
"manager:getQuizz": (_quizzId: string) => void
"manager:saveQuizz": (_payload: { id: string | null; quizz: Quizz }) => void
// Player actions
"player:join": (_inviteCode: string) => void
"player:login": (_message: MessageWithoutStatus<{ username: string }>) => void
"player:reconnect": (_message: { gameId: string }) => void
"player:selectedAnswer": (
_message: MessageWithoutStatus<{ answerKey: number }>
) => void
// Common
disconnect: () => void
}

View File

@@ -0,0 +1,62 @@
import { Player, QuestionMedia } from "."
export const STATUS = {
SHOW_ROOM: "SHOW_ROOM",
SHOW_START: "SHOW_START",
SHOW_PREPARED: "SHOW_PREPARED",
SHOW_QUESTION: "SHOW_QUESTION",
SELECT_ANSWER: "SELECT_ANSWER",
SHOW_RESULT: "SHOW_RESULT",
SHOW_RESPONSES: "SHOW_RESPONSES",
SHOW_LEADERBOARD: "SHOW_LEADERBOARD",
FINISHED: "FINISHED",
WAIT: "WAIT",
} as const
export type Status = (typeof STATUS)[keyof typeof STATUS]
export type CommonStatusDataMap = {
SHOW_START: { time: number; subject: string }
SHOW_PREPARED: { totalAnswers: number; questionNumber: number }
SHOW_QUESTION: {
question: string
image?: string
media?: QuestionMedia
cooldown: number
}
SELECT_ANSWER: {
question: string
answers: string[]
image?: string
media?: QuestionMedia
time: number
totalPlayer: number
}
SHOW_RESULT: {
correct: boolean
message: string
points: number
myPoints: number
rank: number
aheadOfMe: string | null
}
WAIT: { text: string }
FINISHED: { subject: string; top: Player[] }
}
type ManagerExtraStatus = {
SHOW_ROOM: { text: string; inviteCode?: string }
SHOW_RESPONSES: {
question: string
responses: Record<number, number>
correct: number
answers: string[]
image?: string
media?: QuestionMedia
}
SHOW_LEADERBOARD: { oldLeaderboard: Player[]; leaderboard: Player[] }
}
export type PlayerStatusDataMap = CommonStatusDataMap
export type ManagerStatusDataMap = CommonStatusDataMap & ManagerExtraStatus
export type StatusDataMap = PlayerStatusDataMap & ManagerStatusDataMap

View File

@@ -0,0 +1,8 @@
import z from "zod"
export const usernameValidator = z
.string()
.min(4, "Username cannot be less than 4 characters")
.max(20, "Username cannot exceed 20 characters")
export const inviteCodeValidator = z.string().length(6, "Invalid invite code")

View File

@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"skipLibCheck": true,
"noEmit": true
}
}