feat: clan name discriminators #1147
@ -46,7 +46,7 @@ export const addToGuildController: RequestHandler = async (req, res) => {
 | 
				
			|||||||
            arg: [
 | 
					            arg: [
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    Key: "clan",
 | 
					                    Key: "clan",
 | 
				
			||||||
                    Tag: guild.Name + "#000"
 | 
					                    Tag: guild.Name
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            ],
 | 
					            ],
 | 
				
			||||||
            sub: "/Lotus/Language/Menu/Mailbox_ClanInvite_Title",
 | 
					            sub: "/Lotus/Language/Menu/Mailbox_ClanInvite_Title",
 | 
				
			||||||
 | 
				
			|||||||
@ -2,7 +2,11 @@ import { RequestHandler } from "express";
 | 
				
			|||||||
import { getAccountIdForRequest } from "@/src/services/loginService";
 | 
					import { getAccountIdForRequest } from "@/src/services/loginService";
 | 
				
			||||||
import { getJSONfromString } from "@/src/helpers/stringHelpers";
 | 
					import { getJSONfromString } from "@/src/helpers/stringHelpers";
 | 
				
			||||||
import { Guild, GuildMember } from "@/src/models/guildModel";
 | 
					import { Guild, GuildMember } from "@/src/models/guildModel";
 | 
				
			||||||
import { updateInventoryForConfirmedGuildJoin } from "@/src/services/guildService";
 | 
					import {
 | 
				
			||||||
 | 
					    createUniqueClanName,
 | 
				
			||||||
 | 
					    getGuildClient,
 | 
				
			||||||
 | 
					    updateInventoryForConfirmedGuildJoin
 | 
				
			||||||
 | 
					} from "@/src/services/guildService";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const createGuildController: RequestHandler = async (req, res) => {
 | 
					export const createGuildController: RequestHandler = async (req, res) => {
 | 
				
			||||||
    const accountId = await getAccountIdForRequest(req);
 | 
					    const accountId = await getAccountIdForRequest(req);
 | 
				
			||||||
@ -10,7 +14,7 @@ export const createGuildController: RequestHandler = async (req, res) => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    // Create guild on database
 | 
					    // Create guild on database
 | 
				
			||||||
    const guild = new Guild({
 | 
					    const guild = new Guild({
 | 
				
			||||||
        Name: payload.guildName
 | 
					        Name: await createUniqueClanName(payload.guildName)
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
    await guild.save();
 | 
					    await guild.save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -24,7 +28,7 @@ export const createGuildController: RequestHandler = async (req, res) => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    await updateInventoryForConfirmedGuildJoin(accountId, guild._id);
 | 
					    await updateInventoryForConfirmedGuildJoin(accountId, guild._id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    res.json(guild);
 | 
					    res.json(await getGuildClient(guild, accountId));
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
interface ICreateGuildRequest {
 | 
					interface ICreateGuildRequest {
 | 
				
			||||||
 | 
				
			|||||||
@ -3,7 +3,7 @@ import { Guild } from "@/src/models/guildModel";
 | 
				
			|||||||
import { getAccountIdForRequest } from "@/src/services/loginService";
 | 
					import { getAccountIdForRequest } from "@/src/services/loginService";
 | 
				
			||||||
import { logger } from "@/src/utils/logger";
 | 
					import { logger } from "@/src/utils/logger";
 | 
				
			||||||
import { getInventory } from "@/src/services/inventoryService";
 | 
					import { getInventory } from "@/src/services/inventoryService";
 | 
				
			||||||
import { getGuildClient } from "@/src/services/guildService";
 | 
					import { createUniqueClanName, getGuildClient } from "@/src/services/guildService";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const getGuildController: RequestHandler = async (req, res) => {
 | 
					const getGuildController: RequestHandler = async (req, res) => {
 | 
				
			||||||
    const accountId = await getAccountIdForRequest(req);
 | 
					    const accountId = await getAccountIdForRequest(req);
 | 
				
			||||||
@ -11,6 +11,12 @@ const getGuildController: RequestHandler = async (req, res) => {
 | 
				
			|||||||
    if (inventory.GuildId) {
 | 
					    if (inventory.GuildId) {
 | 
				
			||||||
        const guild = await Guild.findOne({ _id: inventory.GuildId });
 | 
					        const guild = await Guild.findOne({ _id: inventory.GuildId });
 | 
				
			||||||
        if (guild) {
 | 
					        if (guild) {
 | 
				
			||||||
 | 
					            // Handle guilds created before we added discriminators
 | 
				
			||||||
 | 
					            if (guild.Name.indexOf("#") == -1) {
 | 
				
			||||||
 | 
					                guild.Name = await createUniqueClanName(guild.Name);
 | 
				
			||||||
 | 
					                await guild.save();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (guild.CeremonyResetDate && Date.now() >= guild.CeremonyResetDate.getTime()) {
 | 
					            if (guild.CeremonyResetDate && Date.now() >= guild.CeremonyResetDate.getTime()) {
 | 
				
			||||||
                logger.debug(`ascension ceremony is over`);
 | 
					                logger.debug(`ascension ceremony is over`);
 | 
				
			||||||
                guild.CeremonyEndo = undefined;
 | 
					                guild.CeremonyEndo = undefined;
 | 
				
			||||||
 | 
				
			|||||||
@ -18,6 +18,7 @@ import { ExportDojoRecipes, IDojoBuild } from "warframe-public-export-plus";
 | 
				
			|||||||
import { logger } from "../utils/logger";
 | 
					import { logger } from "../utils/logger";
 | 
				
			||||||
import { config } from "./configService";
 | 
					import { config } from "./configService";
 | 
				
			||||||
import { Account } from "../models/loginModel";
 | 
					import { Account } from "../models/loginModel";
 | 
				
			||||||
 | 
					import { getRandomInt } from "./rngService";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const getGuildForRequest = async (req: Request): Promise<TGuildDatabaseDocument> => {
 | 
					export const getGuildForRequest = async (req: Request): Promise<TGuildDatabaseDocument> => {
 | 
				
			||||||
    const accountId = await getAccountIdForRequest(req);
 | 
					    const accountId = await getAccountIdForRequest(req);
 | 
				
			||||||
@ -315,3 +316,16 @@ export const updateInventoryForConfirmedGuildJoin = async (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    await inventory.save();
 | 
					    await inventory.save();
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const createUniqueClanName = async (name: string): Promise<string> => {
 | 
				
			||||||
 | 
					    const initialDiscriminator = getRandomInt(0, 999);
 | 
				
			||||||
 | 
					    let discriminator = initialDiscriminator;
 | 
				
			||||||
 | 
					    do {
 | 
				
			||||||
 | 
					        const fullName = name + "#" + discriminator.toString().padStart(3, "0");
 | 
				
			||||||
 | 
					        if (!(await Guild.exists({ Name: fullName }))) {
 | 
				
			||||||
 | 
					            return fullName;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        discriminator = (discriminator + 1) % 1000;
 | 
				
			||||||
 | 
					    } while (discriminator != initialDiscriminator);
 | 
				
			||||||
 | 
					    throw new Error(`clan name is so unoriginal it's already been done 1000 times: ${name}`);
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user