Skip to content

Commit

Permalink
refactor: rename and relocate receivePersonaData function for better …
Browse files Browse the repository at this point in the history
…organization
  • Loading branch information
drazisil committed Jan 4, 2025
1 parent 970dd3f commit 67be790
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 66 deletions.
2 changes: 1 addition & 1 deletion packages/persona/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { getPersonasByPersonaId } from "./src/getPersonasByPersonaId.js";
export { receivePersonaData } from "./src/internal.js";
export { receivePersonaData } from "./src/receivePersonaData.js";
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

import { NPSMessage, ServerLogger } from "rusty-motors-shared";

/**
Expand All @@ -22,7 +21,11 @@ import { NPSMessage, ServerLogger } from "rusty-motors-shared";
* @param {ServerLogger} log
* @returns {Promise<NPSMessage>}
*/
export async function handleSelectGamePersona(requestPacket: NPSMessage, log: ServerLogger): Promise<NPSMessage> {

export async function handleSelectGamePersona(
requestPacket: NPSMessage,
log: ServerLogger,
): Promise<NPSMessage> {

Check warning on line 28 in packages/persona/src/handleSelectGamePersona.ts

View check run for this annotation

Codecov / codecov/patch

packages/persona/src/handleSelectGamePersona.ts#L25-L28

Added lines #L25 - L28 were not covered by tests
log.debug("_npsSelectGamePersona...");

log.debug(
Expand Down
63 changes: 0 additions & 63 deletions packages/persona/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { _gameLogout } from "./_gameLogout.js";
import { _getFirstBuddy } from "./_getFirstBuddy.js";
import { _selectGamePersona } from "./_selectGamePersona.js";
import { validatePersonaName } from "./handlers/validatePersonaName.js";
import type { BufferSerializer } from "rusty-motors-shared-packets";
import { getPersonaInfo } from "./handlers/getPersonaInfo.js";
import { getServerLogger } from "rusty-motors-shared";

Expand Down Expand Up @@ -257,68 +256,6 @@ async function getPersonaMaps({
}
}

/**
*
*
* @param {object} args
* @param {string} args.connectionId
* @param {SerializedBufferOld} args.message
* @param {ServerLogger} [args.log=getServerLogger({ name: "PersonaServer" })]
* @returns {Promise<{
* connectionId: string,
* messages: SerializedBufferOld[],
* }>}
* @throws {Error} Unknown code was received
*/
export async function receivePersonaData({
connectionId,
message,
log = getServerLogger("PersonaServer/receivePersonaData"),
}: {
connectionId: string;
message: BufferSerializer;
log?: ServerLogger;
}): Promise<{
connectionId: string;
messages: SerializedBufferOld[];
}> {
const data = message.serialize();
log.debug(`[${connectionId}] Entering receivePersonaData`);
log.debug(`[${connectionId}] Received persona data: ${data.toString("hex")}`);

// The packet needs to be an NPSMessage
const inboundMessage = new LegacyMessage();
inboundMessage._doDeserialize(message.serialize());

const supportedHandler = messageHandlers.find((h) => {
return h.opCode === inboundMessage._header.id;
});

if (typeof supportedHandler === "undefined") {
// We do not yet support this message code
throw Error(
`[connectionId}] UNSUPPORTED_MESSAGECODE: ${inboundMessage._header.id}`,
);
}

try {
const result = await supportedHandler.handler({
connectionId,
message: inboundMessage,
log,
});
log.debug(
`[${connectionId}] Returning with ${result.messages.length} messages`,
);
return result;
} catch (error) {
const err = Error(`[${connectionId}] Error handling persona data`, {
cause: error,
});
throw err;
}
}

export function personaToString(persona: Partial<PersonaRecord>): string {
return "".concat(
`PersonaRecord: customerId=${persona.customerId}, `,
Expand Down
71 changes: 71 additions & 0 deletions packages/persona/src/receivePersonaData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {
getServerLogger,
ServerLogger,
SerializedBufferOld,
LegacyMessage,
} from "rusty-motors-shared";
import type { BufferSerializer } from "rusty-motors-shared-packets";
import { messageHandlers } from "./internal";

/**
*
*
* @param {object} args
* @param {string} args.connectionId
* @param {SerializedBufferOld} args.message
* @param {ServerLogger} [args.log=getServerLogger({ name: "PersonaServer" })]
* @returns {Promise<{
* connectionId: string,
* messages: SerializedBufferOld[],
* }>}
* @throws {Error} Unknown code was received
*/

export async function receivePersonaData({
connectionId,
message,
log = getServerLogger("PersonaServer/receivePersonaData"),
}: {
connectionId: string;
message: BufferSerializer;
log?: ServerLogger;
}): Promise<{
connectionId: string;
messages: SerializedBufferOld[];
}> {
const data = message.serialize();
log.debug(`[${connectionId}] Entering receivePersonaData`);
log.debug(`[${connectionId}] Received persona data: ${data.toString("hex")}`);

// The packet needs to be an NPSMessage
const inboundMessage = new LegacyMessage();
inboundMessage._doDeserialize(message.serialize());

const supportedHandler = messageHandlers.find((h) => {
return h.opCode === inboundMessage._header.id;
});

if (typeof supportedHandler === "undefined") {
// We do not yet support this message code
throw Error(
`[connectionId}] UNSUPPORTED_MESSAGECODE: ${inboundMessage._header.id}`,
);
}

try {
const result = await supportedHandler.handler({
connectionId,
message: inboundMessage,
log,
});
log.debug(
`[${connectionId}] Returning with ${result.messages.length} messages`,
);
return result;
} catch (error) {
const err = Error(`[${connectionId}] Error handling persona data`, {
cause: error,
});
throw err;
}

Check warning on line 70 in packages/persona/src/receivePersonaData.ts

View check run for this annotation

Codecov / codecov/patch

packages/persona/src/receivePersonaData.ts#L55-L70

Added lines #L55 - L70 were not covered by tests
}

0 comments on commit 67be790

Please sign in to comment.