feat: use wx.createUserInfoButton to get weixin's avarta
This commit is contained in:
+37
-20
@@ -229,6 +229,7 @@ class PlayerInfo {
|
||||
this.ws = ws;
|
||||
this.playerId = playerId;
|
||||
this.nickname = '';
|
||||
this.avatarUrl = '';
|
||||
this.roomId = null;
|
||||
this.teamId = null;
|
||||
this.isAlive = true;
|
||||
@@ -248,7 +249,7 @@ class TeamRoom {
|
||||
* @param {string} [battleMode='3v3'] - Battle mode ('1v1' or '3v3')
|
||||
* @param {string} [leaderNickname=''] - Display nickname of the leader
|
||||
*/
|
||||
constructor(id, leaderWs, leaderId, battleMode = '3v3', leaderNickname = '') {
|
||||
constructor(id, leaderWs, leaderId, battleMode = '3v3', leaderNickname = '', leaderAvatarUrl = '') {
|
||||
this.id = id;
|
||||
this.state = 'forming'; // forming | matching | playing | finished
|
||||
this.createdAt = Date.now();
|
||||
@@ -259,8 +260,8 @@ class TeamRoom {
|
||||
this.teamSize = config.teamSize;
|
||||
this.fillWithBotsEnabled = config.fillWithBots;
|
||||
|
||||
// Team A members: { ws, playerId, nickname, ready, isBot, disconnectedAt }
|
||||
this.teamA = [{ ws: leaderWs, playerId: leaderId, nickname: leaderNickname || '', ready: true, isBot: false, disconnectedAt: null }];
|
||||
// Team A members: { ws, playerId, nickname, avatarUrl, ready, isBot, disconnectedAt }
|
||||
this.teamA = [{ ws: leaderWs, playerId: leaderId, nickname: leaderNickname || '', avatarUrl: leaderAvatarUrl || '', ready: true, isBot: false, disconnectedAt: null }];
|
||||
// Team B members
|
||||
this.teamB = [];
|
||||
this.leaderId = leaderId;
|
||||
@@ -328,19 +329,17 @@ class TeamRoom {
|
||||
}
|
||||
|
||||
/** Add a player to team A */
|
||||
addToTeamA(ws, playerId, nickname = '') {
|
||||
if (this.isTeamAFull()) return false;
|
||||
this.teamA.push({ ws, playerId, nickname, ready: false, isBot: false, disconnectedAt: null });
|
||||
addToTeamA(ws, playerId, nickname = '', avatarUrl = '') {
|
||||
if (this.teamA.length >= this.teamSize) return false;
|
||||
this.teamA.push({ ws, playerId, nickname, avatarUrl, ready: false, isBot: false, disconnectedAt: null });
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Add a player to team B */
|
||||
addToTeamB(ws, playerId, nickname = '') {
|
||||
addToTeamB(ws, playerId, nickname = '', avatarUrl = '') {
|
||||
if (this.teamB.length >= this.teamSize) return false;
|
||||
this.teamB.push({ ws, playerId, nickname, ready: false, isBot: false, disconnectedAt: null });
|
||||
this.teamB.push({ ws, playerId, nickname, avatarUrl, ready: false, isBot: false, disconnectedAt: null });
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Remove a player from the team room */
|
||||
removePlayer(playerId) {
|
||||
this.teamA = this.teamA.filter(m => m.playerId !== playerId);
|
||||
@@ -356,6 +355,7 @@ class TeamRoom {
|
||||
ws: null,
|
||||
playerId: `bot_a_${botCounter}_${this.id}`,
|
||||
nickname: '',
|
||||
avatarUrl: '',
|
||||
ready: true,
|
||||
isBot: true,
|
||||
disconnectedAt: null,
|
||||
@@ -367,6 +367,7 @@ class TeamRoom {
|
||||
ws: null,
|
||||
playerId: `bot_b_${botCounter}_${this.id}`,
|
||||
nickname: '',
|
||||
avatarUrl: '',
|
||||
ready: true,
|
||||
isBot: true,
|
||||
disconnectedAt: null,
|
||||
@@ -433,6 +434,7 @@ class TeamRoom {
|
||||
teamA: this.teamA.map(m => ({
|
||||
playerId: m.playerId,
|
||||
nickname: m.nickname || '',
|
||||
avatarUrl: m.avatarUrl || '',
|
||||
ready: m.ready,
|
||||
isBot: m.isBot,
|
||||
isLeader: m.playerId === this.leaderId,
|
||||
@@ -441,6 +443,7 @@ class TeamRoom {
|
||||
teamB: this.teamB.map(m => ({
|
||||
playerId: m.playerId,
|
||||
nickname: m.nickname || '',
|
||||
avatarUrl: m.avatarUrl || '',
|
||||
ready: m.ready,
|
||||
isBot: m.isBot,
|
||||
connected: m.isBot || (m.ws && m.ws.readyState === 1),
|
||||
@@ -548,7 +551,7 @@ function handleCreateRoom(ws, data) {
|
||||
const roomCode = generateRoomCode();
|
||||
|
||||
// Create a TeamRoom in 1v1 mode instead of a legacy Room
|
||||
const teamRoom = new TeamRoom(roomCode, ws, playerInfo.playerId, '1v1', playerInfo.nickname || '');
|
||||
const teamRoom = new TeamRoom(roomCode, ws, playerInfo.playerId, '1v1', playerInfo.nickname || '', playerInfo.avatarUrl || '');
|
||||
teamRooms.set(roomCode, teamRoom);
|
||||
playerInfo.teamId = roomCode;
|
||||
|
||||
@@ -597,7 +600,7 @@ function handleJoinRoom(ws, data) {
|
||||
}
|
||||
|
||||
// Join as team B
|
||||
teamRoom.addToTeamB(ws, playerInfo.playerId, playerInfo.nickname || '');
|
||||
teamRoom.addToTeamB(ws, playerInfo.playerId, playerInfo.nickname || '', playerInfo.avatarUrl || '');
|
||||
playerInfo.teamId = roomId;
|
||||
|
||||
console.log(`[Server] Player ${playerInfo.playerId} joined 1v1 room ${roomId} (TeamRoom)`);
|
||||
@@ -667,7 +670,7 @@ function handleCreateTeam(ws, data) {
|
||||
}
|
||||
|
||||
const teamId = generateTeamId();
|
||||
const teamRoom = new TeamRoom(teamId, ws, playerInfo.playerId, '3v3', playerInfo.nickname || '');
|
||||
const teamRoom = new TeamRoom(teamId, ws, playerInfo.playerId, '3v3', playerInfo.nickname || '', playerInfo.avatarUrl || '');
|
||||
teamRooms.set(teamId, teamRoom);
|
||||
playerInfo.teamId = teamId;
|
||||
|
||||
@@ -691,7 +694,7 @@ function handleJoinTeam(ws, data) {
|
||||
// Team was cleaned up (e.g. leader disconnected during dev-tool reload).
|
||||
// Auto-create a new room with the same ID so the invite link still works.
|
||||
console.log(`[Server] Team ${teamId} not found, auto-creating for ${playerInfo.playerId}`);
|
||||
teamRoom = new TeamRoom(teamId, ws, playerInfo.playerId, '3v3', playerInfo.nickname || '');
|
||||
teamRoom = new TeamRoom(teamId, ws, playerInfo.playerId, '3v3', playerInfo.nickname || '', playerInfo.avatarUrl || '');
|
||||
teamRooms.set(teamId, teamRoom);
|
||||
playerInfo.teamId = teamId;
|
||||
sendMessage(ws, NET_MSG.TEAM_STATE, teamRoom.getTeamState());
|
||||
@@ -713,7 +716,7 @@ function handleJoinTeam(ws, data) {
|
||||
handleLeaveTeam(ws, {});
|
||||
}
|
||||
|
||||
teamRoom.addToTeamA(ws, playerInfo.playerId, playerInfo.nickname || '');
|
||||
teamRoom.addToTeamA(ws, playerInfo.playerId, playerInfo.nickname || '', playerInfo.avatarUrl || '');
|
||||
playerInfo.teamId = teamId;
|
||||
|
||||
console.log(`[Server] Player ${playerInfo.playerId} joined team ${teamId}`);
|
||||
@@ -914,7 +917,7 @@ function handleSoloMatch(ws, data) {
|
||||
|
||||
// Create a solo team room for this player
|
||||
const teamId = generateTeamId();
|
||||
const teamRoom = new TeamRoom(teamId, ws, playerInfo.playerId, '3v3', playerInfo.nickname || '');
|
||||
const teamRoom = new TeamRoom(teamId, ws, playerInfo.playerId, '3v3', playerInfo.nickname || '', playerInfo.avatarUrl || '');
|
||||
teamRoom.state = 'matching';
|
||||
teamRoom.matchStartTime = Date.now();
|
||||
teamRooms.set(teamId, teamRoom);
|
||||
@@ -987,7 +990,7 @@ function tryMatchTeams() {
|
||||
|
||||
// Merge team B members into team A room as opponents
|
||||
for (const member of teamB_room.teamA) {
|
||||
teamA_room.addToTeamB(member.ws, member.playerId, member.nickname || '');
|
||||
teamA_room.addToTeamB(member.ws, member.playerId, member.nickname || '', member.avatarUrl || '');
|
||||
if (member.ws) {
|
||||
const info = players.get(member.ws);
|
||||
if (info) info.teamId = teamA_room.id;
|
||||
@@ -1052,9 +1055,9 @@ function tryMatchTeams() {
|
||||
|
||||
// Alternate: odd index -> team A, even index -> team B
|
||||
if (i % 2 === 1 && !gameRoom.isTeamAFull()) {
|
||||
gameRoom.addToTeamA(ws, info.playerId, info.nickname || '');
|
||||
gameRoom.addToTeamA(ws, info.playerId, info.nickname || '', info.avatarUrl || '');
|
||||
} else {
|
||||
gameRoom.addToTeamB(ws, info.playerId, info.nickname || '');
|
||||
gameRoom.addToTeamB(ws, info.playerId, info.nickname || '', info.avatarUrl || '');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1405,7 +1408,7 @@ function handleMessage(ws, rawData) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { type, data, playerId, nickname } = msg;
|
||||
const { type, data, playerId, nickname, avatarUrl } = msg;
|
||||
|
||||
// Update player info
|
||||
const playerInfo = players.get(ws);
|
||||
@@ -1434,6 +1437,20 @@ function handleMessage(ws, rawData) {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Refresh avatarUrl on every message (it may be granted mid-session).
|
||||
if (typeof avatarUrl === 'string' && avatarUrl && playerInfo.avatarUrl !== avatarUrl) {
|
||||
playerInfo.avatarUrl = avatarUrl;
|
||||
if (playerInfo.teamId) {
|
||||
const tr = teamRooms.get(playerInfo.teamId);
|
||||
if (tr) {
|
||||
const member = tr.getMemberByWs(ws);
|
||||
if (member && member.avatarUrl !== avatarUrl) {
|
||||
member.avatarUrl = avatarUrl;
|
||||
tr.broadcast(NET_MSG.TEAM_STATE, tr.getTeamState());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
|
||||
Reference in New Issue
Block a user