Skip to content

Commit

Permalink
feat: leaderboard bug bash fixes, updated response classes (#966)
Browse files Browse the repository at this point in the history
  • Loading branch information
anitarua authored Oct 12, 2023
1 parent cd51a5e commit 746444a
Show file tree
Hide file tree
Showing 9 changed files with 431 additions and 485 deletions.
162 changes: 61 additions & 101 deletions packages/client-sdk-nodejs/src/internal/leaderboard-data-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
Interceptor,
Metadata,
ServiceError,
status,
} from '@grpc/grpc-js';
import {version} from '../../package.json';
import {ILeaderboardDataClient} from '@gomomento/sdk-core/dist/src/internal/clients/leaderboard/ILeaderboardDataClient';
Expand Down Expand Up @@ -120,43 +119,51 @@ export class LeaderboardDataClient implements ILeaderboardDataClient {
return metadata;
}

private convertMapToElementsList(elements: Map<number, number>): _Element[] {
private convertMapOrRecordToElementsList(
elements: Record<number, number> | Map<number, number>
): _Element[] {
const convertedElements: _Element[] = [];
elements.forEach((score, id) =>
convertedElements.push(new _Element({id: id, score: score}))
);
if (elements instanceof Map) {
elements.forEach((score, id) =>
convertedElements.push(new _Element({id: id, score: score}))
);
} else {
Object.entries(elements).forEach(element =>
convertedElements.push(
new _Element({id: Number(element[0]), score: element[1]})
)
);
}
return convertedElements;
}

public async leaderboardUpsert(
public async upsert(
cacheName: string,
leaderboardName: string,
elements: Map<number, number>
elements: Record<number, number> | Map<number, number>
): Promise<LeaderboardUpsert.Response> {
const size =
elements instanceof Map ? elements.size : Object.keys(elements).length;
try {
validateLeaderboardNumberOfElements(elements.size);
validateLeaderboardNumberOfElements(size);
} catch (err) {
return new LeaderboardUpsert.Error(normalizeSdkError(err as Error));
}
this.logger.trace(
`Issuing 'upsert' request; cache: ${cacheName}, leaderboard: ${leaderboardName}, number of elements: ${elements.size}`
);
return await this.sendLeaderboardUpsert(
cacheName,
leaderboardName,
elements
`Issuing 'upsert' request; cache: ${cacheName}, leaderboard: ${leaderboardName}, number of elements: ${size}`
);
return await this.sendUpsert(cacheName, leaderboardName, elements);
}

private async sendLeaderboardUpsert(
private async sendUpsert(
cacheName: string,
leaderboardName: string,
elements: Map<number, number>
elements: Record<number, number> | Map<number, number>
): Promise<LeaderboardUpsert.Response> {
const request = new leaderboard._UpsertElementsRequest({
cache_name: cacheName,
leaderboard: leaderboardName,
elements: this.convertMapToElementsList(elements),
elements: this.convertMapOrRecordToElementsList(elements),
});
const metadata = this.createMetadata(cacheName);
return await new Promise(resolve => {
Expand All @@ -177,7 +184,7 @@ export class LeaderboardDataClient implements ILeaderboardDataClient {
});
}

public async leaderboardFetchByScore(
public async fetchByScore(
cacheName: string,
leaderboardName: string,
minScore?: number,
Expand All @@ -197,13 +204,13 @@ export class LeaderboardDataClient implements ILeaderboardDataClient {
return new LeaderboardFetch.Error(normalizeSdkError(err as Error));
}
this.logger.trace(
`Issuing 'leaderboardFetchByScore' request; cache: ${cacheName}, leaderboard: ${leaderboardName}, order: ${orderValue.toString()}, minScore: ${
`Issuing 'fetchByScore' request; cache: ${cacheName}, leaderboard: ${leaderboardName}, order: ${orderValue.toString()}, minScore: ${
minScore ?? 'null'
}, maxScore: ${
maxScore?.toString() ?? 'null'
}, offset: ${offsetValue.toString()}, count: ${countValue.toString()}`
);
return await this.sendLeaderboardFetchByScore(
return await this.sendFetchByScore(
cacheName,
leaderboardName,
orderValue,
Expand All @@ -214,7 +221,7 @@ export class LeaderboardDataClient implements ILeaderboardDataClient {
);
}

private async sendLeaderboardFetchByScore(
private async sendFetchByScore(
cacheName: string,
leaderboardName: string,
order: LeaderboardOrder,
Expand Down Expand Up @@ -260,55 +267,41 @@ export class LeaderboardDataClient implements ILeaderboardDataClient {
if (resp) {
const foundElements = (resp as leaderboard._GetByScoreResponse)
.elements;
if (foundElements.length) {
resolve(new LeaderboardFetch.Found(foundElements));
} else {
// Empty list means requested elements were not found
resolve(new LeaderboardFetch.NotFound());
}
resolve(new LeaderboardFetch.Success(foundElements));
} else {
if (err?.code === status.NOT_FOUND) {
resolve(new LeaderboardFetch.NotFound());
} else {
resolve(new LeaderboardFetch.Error(cacheServiceErrorMapper(err)));
}
resolve(new LeaderboardFetch.Error(cacheServiceErrorMapper(err)));
}
}
);
});
}

public async leaderboardFetchByRank(
public async fetchByRank(
cacheName: string,
leaderboardName: string,
startRank?: number,
endRank?: number,
startRank: number,
endRank: number,
order?: LeaderboardOrder
): Promise<LeaderboardFetch.Response> {
const rankOrder = order ?? LeaderboardOrder.Ascending;
const startRankValue = startRank === undefined ? 0 : startRank;
const endRankValue =
endRank === undefined ? startRankValue + 8192 : endRank;
try {
validateLeaderboardRanks(startRankValue, endRankValue);
validateLeaderboardRanks(startRank, endRank);
} catch (err) {
return new LeaderboardFetch.Error(normalizeSdkError(err as Error));
}
this.logger.trace(
`Issuing 'leaderboardFetchByRank' request; cache: ${cacheName}, leaderboard: ${leaderboardName}, order: ${rankOrder.toString()}, startRank: ${
startRank ?? 'null'
}, endRank: ${endRank?.toString() ?? 'null'}`
`Issuing 'fetchByRank' request; cache: ${cacheName}, leaderboard: ${leaderboardName}, order: ${rankOrder.toString()}, startRank: ${startRank}, endRank: ${endRank}`
);
return await this.sendLeaderboardFetchByRank(
return await this.sendFetchByRank(
cacheName,
leaderboardName,
startRankValue,
endRankValue,
startRank,
endRank,
rankOrder
);
}

private async sendLeaderboardFetchByRank(
private async sendFetchByRank(
cacheName: string,
leaderboardName: string,
startRank: number,
Expand Down Expand Up @@ -343,45 +336,31 @@ export class LeaderboardDataClient implements ILeaderboardDataClient {
if (resp) {
const foundElements = (resp as leaderboard._GetByRankResponse)
.elements;
if (foundElements.length) {
resolve(new LeaderboardFetch.Found(foundElements));
} else {
// Empty list means requested elements were not found
resolve(new LeaderboardFetch.NotFound());
}
resolve(new LeaderboardFetch.Success(foundElements));
} else {
if (err?.code === status.NOT_FOUND) {
resolve(new LeaderboardFetch.NotFound());
} else {
resolve(new LeaderboardFetch.Error(cacheServiceErrorMapper(err)));
}
resolve(new LeaderboardFetch.Error(cacheServiceErrorMapper(err)));
}
}
);
});
}

public async leaderboardGetRank(
public async getRank(
cacheName: string,
leaderboardName: string,
ids: Array<number>,
order?: LeaderboardOrder
): Promise<LeaderboardFetch.Response> {
const orderValue = order ?? LeaderboardOrder.Ascending;
this.logger.trace(
`Issuing 'leaderboardGetRank' request; cache: ${cacheName}, leaderboard: ${leaderboardName}, order: ${orderValue.toString()}, number of ids: ${
`Issuing 'getRank' request; cache: ${cacheName}, leaderboard: ${leaderboardName}, order: ${orderValue.toString()}, number of ids: ${
ids.length
}`
);
return await this.sendLeaderboardGetRank(
cacheName,
leaderboardName,
ids,
orderValue
);
return await this.sendGetRank(cacheName, leaderboardName, ids, orderValue);
}

private async sendLeaderboardGetRank(
private async sendGetRank(
cacheName: string,
leaderboardName: string,
ids: Array<number>,
Expand Down Expand Up @@ -410,35 +389,26 @@ export class LeaderboardDataClient implements ILeaderboardDataClient {
if (resp) {
const foundElements = (resp as leaderboard._GetRankResponse)
.elements;
if (foundElements.length) {
resolve(new LeaderboardFetch.Found(foundElements));
} else {
// Empty list means requested elements were not found
resolve(new LeaderboardFetch.NotFound());
}
resolve(new LeaderboardFetch.Success(foundElements));
} else {
if (err?.code === status.NOT_FOUND) {
resolve(new LeaderboardFetch.NotFound());
} else {
resolve(new LeaderboardFetch.Error(cacheServiceErrorMapper(err)));
}
resolve(new LeaderboardFetch.Error(cacheServiceErrorMapper(err)));
}
}
);
});
}

public async leaderboardLength(
public async length(
cacheName: string,
leaderboardName: string
): Promise<LeaderboardLength.Response> {
this.logger.trace(
`Issuing 'leaderboardLength' request; cache: ${cacheName}, leaderboard: ${leaderboardName}`
`Issuing 'length' request; cache: ${cacheName}, leaderboard: ${leaderboardName}`
);
return await this.sendLeaderboardLength(cacheName, leaderboardName);
return await this.sendLength(cacheName, leaderboardName);
}

private async sendLeaderboardLength(
private async sendLength(
cacheName: string,
leaderboardName: string
): Promise<LeaderboardLength.Response> {
Expand All @@ -458,22 +428,16 @@ export class LeaderboardDataClient implements ILeaderboardDataClient {
if (resp) {
const length = (resp as leaderboard._GetLeaderboardLengthResponse)
.count;
resolve(new LeaderboardLength.Found(length));
resolve(new LeaderboardLength.Success(length));
} else {
if (err?.code === status.NOT_FOUND) {
resolve(new LeaderboardLength.NotFound());
} else {
resolve(
new LeaderboardLength.Error(cacheServiceErrorMapper(err))
);
}
resolve(new LeaderboardLength.Error(cacheServiceErrorMapper(err)));
}
}
);
});
}

public async leaderboardRemoveElements(
public async removeElements(
cacheName: string,
leaderboardName: string,
ids: Array<number>
Expand All @@ -486,16 +450,12 @@ export class LeaderboardDataClient implements ILeaderboardDataClient {
);
}
this.logger.trace(
`Issuing 'leaderboardRemoveElements' request; cache: ${cacheName}, leaderboard: ${leaderboardName}, number of elements: ${ids.length.toString()}`
);
return await this.sendLeaderboardRemoveElements(
cacheName,
leaderboardName,
ids
`Issuing 'removeElements' request; cache: ${cacheName}, leaderboard: ${leaderboardName}, number of elements: ${ids.length.toString()}`
);
return await this.sendRemoveElements(cacheName, leaderboardName, ids);
}

private async sendLeaderboardRemoveElements(
private async sendRemoveElements(
cacheName: string,
leaderboardName: string,
ids: Array<number>
Expand Down Expand Up @@ -526,17 +486,17 @@ export class LeaderboardDataClient implements ILeaderboardDataClient {
});
}

public async leaderboardDelete(
public async delete(
cacheName: string,
leaderboardName: string
): Promise<LeaderboardDelete.Response> {
this.logger.trace(
`Issuing 'leaderboardDelete' request; cache: ${cacheName}, leaderboard: ${leaderboardName}`
`Issuing 'delete' request; cache: ${cacheName}, leaderboard: ${leaderboardName}`
);
return await this.sendLeaderboardDelete(cacheName, leaderboardName);
return await this.sendDelete(cacheName, leaderboardName);
}

private async sendLeaderboardDelete(
private async sendDelete(
cacheName: string,
leaderboardName: string
): Promise<LeaderboardDelete.Response> {
Expand Down
Loading

0 comments on commit 746444a

Please sign in to comment.