using SPTarkov.DI.Annotations; using SPTarkov.Server.Core.Models.Common; using SPTarkov.Server.Core.Models.Eft.Launcher; using SPTarkov.Server.Core.Models.Eft.Profile; using SPTarkov.Server.Core.Models.Spt.Config; using SPTarkov.Server.Core.Models.Spt.Mod; using SPTarkov.Server.Core.Models.Utils; using SPTarkov.Server.Core.Servers; using SPTarkov.Server.Core.Services; using SPTarkov.Server.Core.Utils; using Info = SPTarkov.Server.Core.Models.Eft.Profile.Info; namespace SPTarkov.Server.Core.Controllers; [Injectable] public class LauncherV2Controller( ISptLogger logger, IReadOnlyList loadedMods, HashUtil hashUtil, SaveServer saveServer, DatabaseService databaseService, ServerLocalisationService serverLocalisationService, ConfigServer configServer, Watermark watermark ) { protected readonly CoreConfig _coreConfig = configServer.GetConfig(); /// /// Returns a simple string of Pong! /// /// public string Ping() { return "Pong!"; } /// /// Returns all available profile types and descriptions for creation. /// - This is also localised. /// /// dict of profile names + description public Dictionary Types() { var result = new Dictionary(); var dbProfiles = databaseService.GetProfileTemplates(); foreach (var (templateName, template) in dbProfiles) { result.TryAdd( templateName, serverLocalisationService.GetText(template.DescriptionLocaleKey) ); } return result; } /// /// Checks if login details were correct. /// /// /// public bool Login(LoginRequestData info) { var sessionId = GetSessionId(info); return !sessionId.IsEmpty(); } /// /// Register a new profile. /// /// /// public async Task Register(RegisterData info) { foreach (var (_, profile) in saveServer.GetProfiles()) { if (info.Username == profile.ProfileInfo!.Username) { return false; } } await CreateAccount(info); return true; } /// /// Make a password change. /// /// /// public async Task PasswordChange(ChangeRequestData info) { var sessionId = GetSessionId(info); if (sessionId.IsEmpty()) { return false; } if (!Login(info)) { return false; } saveServer.GetProfile(sessionId).ProfileInfo!.Password = info.Change; await saveServer.SaveProfileAsync(sessionId); return true; } /// /// Remove profile from server. /// /// /// public bool Remove(LoginRequestData info) { var sessionId = GetSessionId(info); return !sessionId.IsEmpty() && saveServer.RemoveProfile(sessionId); } /// /// Gets the Servers SPT Version. /// - "4.0.0" /// /// public string SptVersion() { return watermark.GetVersionTag(); } /// /// Gets the compatible EFT Version. /// - "0.14.9.31124" /// /// public string EftVersion() { return _coreConfig.CompatibleTarkovVersion; } /// /// Gets the Servers loaded mods. /// /// public Dictionary LoadedMods() { return loadedMods.ToDictionary( sptMod => sptMod.ModMetadata.Name, sptMod => sptMod.ModMetadata ); } /// /// Creates the account from provided details. /// /// /// protected async Task CreateAccount(RegisterData info) { var profileId = new MongoId(); var scavId = new MongoId(); var newProfileDetails = new Info { ProfileId = profileId, ScavengerId = scavId, Aid = hashUtil.GenerateAccountId(), Username = info.Username, Password = info.Password, IsWiped = true, Edition = info.Edition, }; saveServer.CreateProfile(newProfileDetails); await saveServer.LoadProfileAsync(profileId); await saveServer.SaveProfileAsync(profileId); return profileId; } protected MongoId GetSessionId(LoginRequestData info) { foreach (var (sessionId, profile) in saveServer.GetProfiles()) { if ( info.Username == profile.ProfileInfo!.Username && info.Password == profile.ProfileInfo.Password ) { return sessionId; } } return MongoId.Empty(); } public SptProfile GetProfile(MongoId sessionId) { return saveServer.GetProfile(sessionId); } }