Skip to content

Commit

Permalink
feat(game/server): number format config
Browse files Browse the repository at this point in the history
  • Loading branch information
itschip committed Jan 19, 2024
1 parent 9265115 commit ff41f46
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
30 changes: 29 additions & 1 deletion apps/game/server/misc/generateUniquePhoneNumber.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { config } from '../server';
import { DbInterface } from '@npwd/database';
import { playerLogger } from '../players/player.utils';
import {mainLogger} from "../sv_logger";

const exp = global.exports;

Expand All @@ -21,10 +22,36 @@ const genNumber = (length: number): string => {
return strNumber.substr(addAmount);
};

/*
const generateUsNumber = (): string => {
const rawNumber = genNumber(10);
return rawNumber.replace(/(\d{3})(\d{3})(\d{4})/, '$1-$2-$3');
};
*/

function generatePhoneNumber(pattern: string): string {
// Extract digit groups from the pattern
const digitGroups = pattern.match(/\d+/g);

if (!digitGroups) {
mainLogger.error('Invalid phone number format');
throw new Error('Invalid phone number format');
}

// Generate random numbers for each group
const randomNumberParts = digitGroups.map(group => {
const length = parseInt(group);
let randomNumber = '';
for (let i = 0; i < length; i++) {
randomNumber += Math.floor(Math.random() * 10).toString();
}
return randomNumber;
});

// Join the parts with a dash if more than one group, otherwise return as is
return randomNumberParts.join(randomNumberParts.length > 1 ? '-' : '');
}

/**/
export async function generateUniquePhoneNumber(): Promise<string> {
Expand All @@ -39,7 +66,8 @@ export async function generateUniquePhoneNumber(): Promise<string> {
}

const query = `SELECT EXISTS(SELECT * FROM ${config.database.playerTable} WHERE ${config.database.phoneNumberColumn} = ?)`;
const dashNumber = generateUsNumber();

const dashNumber = generatePhoneNumber(config.general.phoneNumberFormat);

const [results] = await DbInterface._rawExec(query, [dashNumber]);

Expand Down
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"toggleKey": "f1",
"toggleCommand": "phone",
"defaultLanguage": "en",
"showId": false
"showId": false,
"phoneNumberFormat": "/(\\d{3})(\\d{3})(\\d{4})/"
},
"contacts": {
"frameworkPay": false,
Expand Down
2 changes: 1 addition & 1 deletion typings/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ interface Debug {
}

interface General {
useDashNumber: boolean;
useResourceIntegration: boolean;
toggleKey: string;
toggleCommand: string;
defaultLanguage: string;
showId: boolean;
phoneNumberFormat: string;
}

interface Contacts {
Expand Down

0 comments on commit ff41f46

Please sign in to comment.