All files / web/src/db/schema players.ts

95.17% Statements 138/145
100% Branches 4/4
50% Functions 1/2
95.17% Lines 138/145

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1462x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 62x 62x 62x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                
import { createId } from '@paralleldrive/cuid2'
import { index, integer, sqliteTable, text } from 'drizzle-orm/sqlite-core'
import { users } from './users'
 
/**
 * Help mode for practice sessions
 * - 'auto': Help automatically appears after timeout
 * - 'manual': Help only appears when student clicks for it
 * - 'teacher-approved': Student can request help, but teacher must approve
 */
export type HelpMode = 'auto' | 'manual' | 'teacher-approved'
 
/**
 * Settings that control help behavior during practice sessions
 *
 * Note: Help is now boolean (used or not used). BKT uses 0.5x evidence weight
 * for problems where help was used.
 */
export interface StudentHelpSettings {
  /** How help is triggered */
  helpMode: HelpMode
 
  /** For beginners: help doesn't count against mastery */
  beginnerFreeHelp: boolean
 
  /** For advanced students: help requires teacher approval */
  advancedRequiresApproval: boolean
}
 
/**
 * Default help settings for new students
 */
export const DEFAULT_HELP_SETTINGS: StudentHelpSettings = {
  helpMode: 'auto',
  beginnerFreeHelp: true,
  advancedRequiresApproval: false,
}
 
/**
 * Players table - user-created player profiles for games
 *
 * Each user can have multiple players (for multi-player modes).
 * Players are scoped to a user and deleted when user is deleted.
 */
export const players = sqliteTable(
  'players',
  {
    id: text('id')
      .primaryKey()
      .$defaultFn(() => createId()),
 
    /** Foreign key to users table - cascades on delete */
    userId: text('user_id')
      .notNull()
      .references(() => users.id, { onDelete: 'cascade' }),
 
    /** Player display name */
    name: text('name').notNull(),
 
    /** Player emoji avatar */
    emoji: text('emoji').notNull(),
 
    /** Player color (hex) for UI theming */
    color: text('color').notNull(),
 
    /** Whether this player is currently active in games */
    isActive: integer('is_active', { mode: 'boolean' }).notNull().default(false),
 
    /** When this player was created */
    createdAt: integer('created_at', { mode: 'timestamp' })
      .notNull()
      .$defaultFn(() => new Date()),
 
    /**
     * Help settings for practice sessions
     * Controls how help is triggered and escalated
     */
    helpSettings: text('help_settings', {
      mode: 'json',
    }).$type<StudentHelpSettings>(),
 
    /**
     * Teacher notes about this student
     * Free-form text for observations, reminders, etc.
     */
    notes: text('notes'),
 
    /**
     * Whether this student is archived (hidden from default view)
     * Archived students are not deleted but don't appear in normal lists
     */
    isArchived: integer('is_archived', { mode: 'boolean' }).notNull().default(false),
 
    /**
     * Whether this player is a practice student (appears in practice system).
     * Arcade-only players have this set to false — they don't count toward
     * the practice student limit and don't appear in the practice UI.
     */
    isPracticeStudent: integer('is_practice_student', { mode: 'boolean' }).notNull().default(true),
 
    /**
     * Child's birthday (YYYY-MM-DD, optional, set by parent/teacher)
     * Used to compute age for adaptive experiences.
     */
    birthday: text('birthday'),
 
    /**
     * Whether this player can be bulk-deleted by the debug cleanup tool.
     * Set to true for debug players, seed players, and e2e test players.
     */
    isExpungeable: integer('is_expungeable', { mode: 'boolean' }).notNull().default(false),
 
    /**
     * Family code for sharing access to this player with other parents
     * Format: FAM-XXXXXX (6 alphanumeric chars)
     */
    familyCode: text('family_code').unique(),
 
    /**
     * When the current family code was generated.
     * Codes expire after 7 days — expired codes are rejected at link time.
     */
    familyCodeGeneratedAt: integer('family_code_generated_at', { mode: 'timestamp' }),
  },
  (table) => ({
    /** Index for fast lookups by userId */
    userIdIdx: index('players_user_id_idx').on(table.userId),
  })
)
 
export type Player = typeof players.$inferSelect
export type NewPlayer = typeof players.$inferInsert
 
/**
 * Generate a unique family code for sharing player access with other parents
 * Format: FAM-XXXXXX (6 alphanumeric characters, no confusing chars like 0/O, 1/I)
 */
export function generateFamilyCode(): string {
  const chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'
  let code = 'FAM-'
  for (let i = 0; i < 6; i++) {
    code += chars.charAt(Math.floor(Math.random() * chars.length))
  }
  return code
}