Skip to content

Commit 2b5ad8e

Browse files
authored
fix: always use username for temp hytale avatars (#131)
1 parent 6035ded commit 2b5ad8e

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/worker/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { get_rendered_image } from '../../pkg/mcavatar';
99
import type { CraftheadRequest } from './request';
1010

1111
interface DirectRenderService {
12-
renderAvatar(request: CraftheadRequest): Response;
12+
renderAvatar(incomingRequest: Request, request: CraftheadRequest): Promise<Response>;
1313
}
1414

1515
type GameService = typeof mojangService | typeof hytaleService;
@@ -111,7 +111,7 @@ async function processRequest(request: Request, interpreted: CraftheadRequest):
111111
case RequestedKind.Body:
112112
case RequestedKind.Bust: {
113113
if (hasRenderAvatar(service)) {
114-
return service.renderAvatar(interpreted);
114+
return service.renderAvatar(request, interpreted);
115115
}
116116
const skin = await service.retrieveSkin(request, interpreted);
117117
return renderImage(skin, interpreted);

src/worker/services/hytale/service.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,23 @@ interface NormalizedRequest {
134134

135135
/**
136136
* Normalizes the incoming request, such that we only work with UUIDs.
137-
* Also returns the profile if it was fetched during username lookup (to avoid double lookups).
137+
* Always fetches the profile to get the username (needed for text avatars and future skin support).
138138
*/
139139
async function normalizeRequest(incomingRequest: Request, request: CraftheadRequest): Promise<NormalizedRequest> {
140-
if (request.identityType === IdentityKind.Uuid || request.identityType === IdentityKind.TextureID) {
140+
if (request.identityType === IdentityKind.TextureID) {
141141
return { request };
142142
}
143143

144+
if (request.identityType === IdentityKind.Uuid) {
145+
// UUID provided - fetch profile to get username
146+
const lookup = await hytaleApi.fetchProfile(incomingRequest, request.identity);
147+
if (lookup.result) {
148+
return { request, profile: lookup.result };
149+
}
150+
return { request };
151+
}
152+
153+
// Username provided - look up to get UUID and profile
144154
const normalized: CraftheadRequest = { ...request, identityType: IdentityKind.Uuid };
145155

146156
const profile = await hytaleApi.lookupUsername(incomingRequest, request.identity);
@@ -223,8 +233,13 @@ async function retrieveTextureDirect(
223233
* TEMPORARY: Renders a text-based avatar with username initials.
224234
* Replace with real skin rendering once Hytale skin support is implemented.
225235
*/
226-
export function renderAvatar(request: CraftheadRequest): Response {
227-
const imageData = render_text_avatar(request.identity, request.size);
236+
export async function renderAvatar(incomingRequest: Request, request: CraftheadRequest): Promise<Response> {
237+
const { profile } = await normalizeRequest(incomingRequest, request);
238+
239+
// Use the username from profile if available, otherwise fall back to the original identity
240+
const username = profile?.name ?? request.identity;
241+
242+
const imageData = render_text_avatar(username, request.size);
228243
return new Response(imageData, {
229244
headers: {
230245
'Content-Type': 'image/png',
@@ -236,8 +251,8 @@ export function renderAvatar(request: CraftheadRequest): Response {
236251
/**
237252
* TEMPORARY: Returns a text avatar since real Hytale skins aren't implemented yet.
238253
*/
239-
export function retrieveSkin(_incomingRequest: Request, request: CraftheadRequest): Response {
240-
return renderAvatar(request);
254+
export async function retrieveSkin(incomingRequest: Request, request: CraftheadRequest): Promise<Response> {
255+
return renderAvatar(incomingRequest, request);
241256
}
242257

243258
/**

0 commit comments

Comments
 (0)