File size: 5,610 Bytes
2ce42d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// populate_interface.js

class GridPopulator {
    constructor(gridContainerId, initialUsers = 10) {
        this.gridContainer = document.getElementById(gridContainerId);
        this.users = new Map();
        this.nextUserId = 1;
        this.updateInterval = 2000;
        this.maxUsers = 15;
        this.minUsers = 5;
        
        // Set up grid layout
        this.gridContainer.className = "grid gap-4 p-4";
        
        // Initialize with default users
        for (let i = 0; i < initialUsers; i++) {
            this.addUser();
        }

        this.updateGridLayout();
        this.startUpdates();
    }

    generateUserName() {
        return `${this.nextUserId}`;
    }

    updateGridLayout() {
        const cols = Math.ceil(Math.sqrt(this.users.size));
        this.gridContainer.style.gridTemplateColumns = `repeat(${cols}, minmax(0, 1fr))`;
    }

    addUser() {
        const personality = window.personalitySystem.getRandomPersonality();
        const square = window.personalitySystem.createPlayerSquare(personality);
        const userId = this.nextUserId++;
        
        // Update the square's text to show student instead of "You"
        const nameSpan = square.querySelector('.text-xs');
        nameSpan.textContent = this.generateUserName();

        this.users.set(userId, {
            element: square,
            personality: personality
        });

        this.gridContainer.appendChild(square);
        this.updateGridLayout();

        // Add to chat content
        /* const chatContent = document.getElementById('chat-content');
        if (chatContent) {
            const config = window.personalitySystem.config[personality];
            const joinMessage = document.createElement('div');
            joinMessage.className = 'text-gray-700 flex items-center gap-2';
            joinMessage.innerHTML = `
                <span class="font-bold">${this.users.size}.</span>
                <span>Student ${userId} joined as ${config.name}</span>
            `;
            chatContent.appendChild(joinMessage);
            chatContent.scrollTop = chatContent.scrollHeight;
        } */

        return userId;
    }

    removeUser() {
        if (this.users.size <= this.minUsers) return;

        const userIds = Array.from(this.users.keys());
        const randomId = userIds[Math.floor(Math.random() * userIds.length)];
        const user = this.users.get(randomId);

        user.element.classList.add('pop-out');

        // Add to chat content
        /* const chatContent = document.getElementById('chat-content');
        if (chatContent) {
            const config = window.personalitySystem.config[user.personality];
            const leaveMessage = document.createElement('div');
            leaveMessage.className = 'text-gray-700 flex items-center gap-2';
            leaveMessage.innerHTML = `
                <span class="font-bold">${this.users.size}.</span>
                <span>Student ${randomId} (${config.name}) left</span>
            `;
            chatContent.appendChild(leaveMessage);
            chatContent.scrollTop = chatContent.scrollHeight;
        } */

        setTimeout(() => {
            user.element.remove();
            this.users.delete(randomId);
            this.updateGridLayout();
        }, 300);
    }

    startUpdates() {
        setInterval(() => {
            const shouldAdd = Math.random() > 0.5;
            
            if (shouldAdd && this.users.size < this.maxUsers) {
                this.addUser();
            } else if (this.users.size > this.minUsers) {
                this.removeUser();
            }

            // Randomly update some existing users' personalities
            this.users.forEach((user, userId) => {
                if (Math.random() < 0.1) {
                    const newPersonality = window.personalitySystem.getRandomPersonality();
                    if (newPersonality !== user.personality) {
                        const event = new CustomEvent('personalityUpdated', {
                            detail: { personality: newPersonality }
                        });
                        user.element.dispatchEvent(event);
                        user.personality = newPersonality;
                        
                        // Update chat with personality change
                        /* const chatContent = document.getElementById('chat-content');
                        if (chatContent) {
                            const config = window.personalitySystem.config[newPersonality];
                            const changeMessage = document.createElement('div');
                            changeMessage.className = 'text-gray-700 flex items-center gap-2';
                            changeMessage.innerHTML = `
                                <span class="font-bold">${this.users.size}.</span>
                                <span>Student ${userId} changed to ${config.name}</span>
                            `;
                            chatContent.appendChild(changeMessage);
                            chatContent.scrollTop = chatContent.scrollHeight;
                        } */
                    }
                }
            });
        }, this.updateInterval);
    }
}

// Initialize when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
    // Make sure personality system is initialized first
    if (window.personalitySystem) {
        window.gridPopulator = new GridPopulator('grid-container');
    } else {
        console.error('Personality system must be initialized before grid populator');
    }
});