/// <summary> /// Get the account reference /// </summary> /// <param name="username">The username to check</param> /// <param name="password">The password to check</param> /// <returns></returns> public string GetAccountReference(string username, string password) { // Load the char names list into memory string path = FilePathSystem.GetFilePath("Characters", "CharNamesList"); List <SMAccount> allCharacters = new List <SMAccount>(); if (File.Exists(path)) { using (StreamReader r = new StreamReader(path)) { string json = r.ReadToEnd(); allCharacters = JsonConvert.DeserializeObject <List <SMAccount> >(json); } } // Select the name from the list SMAccount smcn = allCharacters.FirstOrDefault(c => ((c.UserName == username) && (Utility.Crypto.DecryptStringAES(c.HashedPassword) == password))); // if the username / password is correct if (smcn != null) { return(smcn.AccountReference); // .. return the account reference. } else { return(""); // .. return nothing } }
/// <summary> /// Checks that a username / password combination is valid /// </summary> /// <param name="username">The username being checked</param> /// <param name="password">The password being checked</param> /// <returns></returns> public bool CheckUserName(string username) { // Load the char names list into memory string path = FilePathSystem.GetFilePath("Characters", "CharNamesList"); List <SMAccount> allCharacters = new List <SMAccount>(); if (File.Exists(path)) { using (StreamReader r = new StreamReader(path)) { string json = r.ReadToEnd(); allCharacters = JsonConvert.DeserializeObject <List <SMAccount> >(json); } } // Select the name from the list SMAccount smcn = allCharacters.FirstOrDefault(c => (c.UserName == username)); // if the name doesn't exist... if (smcn == null) { return(true); // .. the name can be used } else { return(false); // .. the name can't be used } }
public static SMNPC GetNewNPC(string NPCType, bool unique = false) { SMNPC newNPC = new SMNPC(); // ... if they're not, spawn them into the room. string newNPCPath = FilePathSystem.GetFilePath("NPCs", NPCType); // If that NPC Exits if (File.Exists(newNPCPath)) { // Read the data in using (StreamReader r = new StreamReader(newNPCPath)) { // Read the data in string json = r.ReadToEnd(); // Deserialise the JSON to an object and add it to the list of NPCS in memory newNPC = JsonConvert.DeserializeObject <SMNPC>(json); if (!unique) { newNPC.UserID = Guid.NewGuid().ToString(); } } } return(newNPC); }
/// <summary> /// Saves the room to file. /// </summary> public void SaveToFile() { string path = FilePathSystem.GetFilePath("Location", "Loc" + this.RoomID); string roomJSON = JsonConvert.SerializeObject(this, Formatting.Indented); using (StreamWriter w = new StreamWriter(path)) { w.WriteLine(roomJSON); } }
public void RebuildCharacterJSONFile() { string commandsFolderPath = FilePathSystem.GetFilePathFromFolder("Characters"); DirectoryInfo dirInfo = new DirectoryInfo(commandsFolderPath); FileInfo[] CommandFiles = dirInfo.GetFiles("*.json"); List <SMCharacter> smcl = new List <SMCharacter>(); foreach (FileInfo file in CommandFiles) { if (file.Name != "CharNamesList.json") { string path = FilePathSystem.GetFilePath("Characters", file.Name, ""); // Use a stream reader to read the file in (based on the path) using (StreamReader r = new StreamReader(path)) { // Create a new JSON string to be used... string json = r.ReadToEnd(); // Get all the commands from the commands file try { SMCharacter nsmc = JsonConvert.DeserializeObject <SMCharacter>(json); smcl.Add(nsmc); } catch { Utils.LogError("Failed to write char record in CharNamesList.json - record: " + file.Name); } } } } List <SMAccount> smal = new List <SMAccount>(); foreach (SMCharacter smc in smcl) { SMAccount sma = new SMAccount(); sma.AccountReference = smc.UserID; sma.CharacterName = smc.GetFullName(); sma.UserName = smc.Username; sma.HashedPassword = smc.Password; smal.Add(sma); } string listJSON = JsonConvert.SerializeObject(smal, Formatting.Indented); string path2 = FilePathSystem.GetFilePath("Characters", "CharNamesList"); using (StreamWriter w = new StreamWriter(path2)) { w.WriteLine(listJSON); } }
public string LogIn(string usernameIn, string passwordIn, string connectionAddress) { // Load the char names list into memory string path = FilePathSystem.GetFilePath("Characters", "CharNamesList"); List <SMAccount> allCharacters = new List <SMAccount>(); if (File.Exists(path)) { using (StreamReader r = new StreamReader(path)) { string json = r.ReadToEnd(); allCharacters = JsonConvert.DeserializeObject <List <SMAccount> >(json); } } // Find the relevant one SMAccount smcn = allCharacters.FirstOrDefault(c => ((c.EmailAddress == usernameIn) && (c.HashedPassword == Utility.Crypto.DecryptStringAES(passwordIn)))); // Check if we've found an account if (smcn != null) { // Check if the current connection address is the account reference, // if not add it to the list so we can check when connecting so we // can load the correct account in without the username / password // being needed each time. if (connectionAddress != smcn.AccountReference) { // remove the current char reference from the file allCharacters.Remove(smcn); // create a new char reference element. SMAccountOtherReference smaor = new SMAccountOtherReference(); smaor.OtherReference = smcn.AccountReference; // Add the character back to the list with the new reference allCharacters.Add(smcn); // Save the file back to the disk string listJSON = JsonConvert.SerializeObject(allCharacters, Formatting.Indented); using (StreamWriter w = new StreamWriter(path)) { w.WriteLine(listJSON); } } // return the main account reference return(smcn.AccountReference); } else { // return nothing return(""); } }
/// <summary> /// Adds the name to the CharNamesList /// </summary> /// <param name="fullCharName">The full name of the character being added</param> /// <returns></returns> public void AddNameToList(string fullCharName, string accountReference, SMCharacter smc = null) { // Load the char names list into memory string path = FilePathSystem.GetFilePath("Characters", "CharNamesList"); List <SMAccount> allCharacters = new List <SMAccount>(); // Check if the file exists at the path. if (File.Exists(path)) { using (StreamReader r = new StreamReader(path)) { string json = r.ReadToEnd(); allCharacters = JsonConvert.DeserializeObject <List <SMAccount> >(json); } } // Create a new character reference SMAccount smcn = new SMAccount(); smcn.CharacterName = fullCharName; smcn.AccountReference = accountReference; // If the character is passed in... if (smc != null) { // ... check if there is a username and password associated if ((smc.Username != null) && (smc.Password != null)) { // Assigned the username to the email address smcn.UserName = smc.Username; smcn.HashedPassword = smc.Password; } } // Check if the account reference is already in the list SMAccount sma = allCharacters.FirstOrDefault(c => c.AccountReference == accountReference); // If it does exist... if (sma != null) { // ... remove the old version allCharacters.Remove(sma); } // Add the character to the list allCharacters.Add(smcn); // Save the list to disk. string listJSON = JsonConvert.SerializeObject(allCharacters, Formatting.Indented); using (StreamWriter w = new StreamWriter(path)) { w.WriteLine(listJSON); } }
/// <summary> /// Checks that a character name can be used (i.e. hasn't already been used). /// </summary> /// <param name="fullCharName">The full name of the character being checked</param> /// <returns></returns> public bool CheckCharNameCanBeUsed(string fullCharName) { // Load the char names list into memory string path = FilePathSystem.GetFilePath("Characters", "CharNamesList"); List <SMAccount> allCharacters = new List <SMAccount>(); if (File.Exists(path)) { using (StreamReader r = new StreamReader(path)) { string json = r.ReadToEnd(); allCharacters = JsonConvert.DeserializeObject <List <SMAccount> >(json); } } // Select the name from the list SMAccount smcn = allCharacters.FirstOrDefault(c => c.CharacterName.ToLower() == fullCharName.ToLower()); // if the name doesn't exist... if (smcn == null) { // Load the char names list into memory path = FilePathSystem.GetFilePath("NPCs", "NPCNamesList"); List <SMAccount> npcCharacters = new List <SMAccount>(); if (File.Exists(path)) { using (StreamReader r = new StreamReader(path)) { string json = r.ReadToEnd(); npcCharacters = JsonConvert.DeserializeObject <List <SMAccount> >(json); } } // Select the name from the list SMAccount npccn = npcCharacters.FirstOrDefault(c => c.CharacterName.ToLower() == fullCharName.ToLower()); if (npccn == null) { return(true); // .. the name can be used } else { return(false); // can't use the name as an NPC has it already. } } else { return(false); // .. the name can't be used } }
/// <summary> /// Gets a character object, and loads it into memory. /// </summary> /// <param name="userID">userID is based on the id from the slack channel</param> /// <param name="newCharacter">newCharacter to change the output of the text based on whether the character is new or not</param> /// <returns>String message for usage</returns> public string GetCharacterOLD(string userID, bool newCharacter = false) { List <SMCharacter> smcs = (List <SMCharacter>)HttpContext.Current.Application["SMCharacters"]; SMCharacter character = smcs.FirstOrDefault(smc => smc.UserID == userID); if ((character != null) && (!newCharacter)) { return("You're already logged in!"); } else { string path = FilePathSystem.GetFilePath("Characters", "Char" + userID); if (File.Exists(path)) { SMCharacter smc = new SMCharacter(); using (StreamReader r = new StreamReader(path)) { string json = r.ReadToEnd(); smc = JsonConvert.DeserializeObject <SMCharacter>(json); } SMRoom room = smc.GetRoom(); if (room != null) { //TODO room.Announce() someone has entered the room or new player ect... } smcs.Add(smc); HttpContext.Current.Application["SMCharacters"] = smcs; if (!newCharacter) { return("Welcome back " + smc.FirstName); } string returnString = "Welcome to SlackMud!\n"; returnString += "We've created your character in the magical world of Arrelvia!"; // TODO, use a welcome script! // TODO, get room details return(returnString); } else { // If the UserID doesn't have a character already, inform them that they need to create one. return("You do not have a character yet, you need to create one..."); } } }
private SMCurrency CreateCurrencyFromJSON(string filename) { string path = FilePathSystem.GetFilePath("Misc", filename); SMCurrency currency = new SMCurrency(); if (File.Exists(path)) { using (StreamReader r = new StreamReader(path)) { string json = r.ReadToEnd(); currency = JsonConvert.DeserializeObject <SMCurrency>(json); } } return(currency); }
/// <summary> /// Gets a string representing the json spec for a quest. /// </summary> /// <param name="questName">The quest name.</param> /// <returns>Json spec for the quest or an empty string.</returns> private static string GetQuestSpecJson(string questName) { string filename = questName; string filePath = FilePathSystem.GetFilePath("Quests", filename); string objectSpecJson = ""; if (File.Exists(filePath)) { using (StreamReader reader = new StreamReader(filePath)) { objectSpecJson = reader.ReadToEnd(); } } return(objectSpecJson); }
private List <SMBodyPart> CreateBodyPartsFromJSON(string filename) { string path = FilePathSystem.GetFilePath("Misc", filename); List <SMBodyPart> parts = new List <SMBodyPart>(); if (File.Exists(path)) { using (StreamReader r = new StreamReader(path)) { string json = r.ReadToEnd(); parts = JsonConvert.DeserializeObject <List <SMBodyPart> >(json); } } return(parts); }
/// <summary> /// Gets a string representing the json spec for an item. /// </summary> /// <param name="itemType">The items type.</param> /// <param name="itemName">THe items name.</param> /// <returns>Json spec for the item or an empty string.</returns> private static string GetItemSpecJson(string itemType, string itemName) { string filename = $"{itemType}.{itemName}"; string filePath = FilePathSystem.GetFilePath("Objects", filename); string objectSpecJson = ""; if (File.Exists(filePath)) { using (StreamReader reader = new StreamReader(filePath)) { objectSpecJson = reader.ReadToEnd(); } } return(objectSpecJson); }
/// <summary> /// Internal Method to get base attributes from a file /// </summary> /// <returns>An Attribues list</returns> /// <param name="fileName">File name of the objects json file.</param> private SMAttributes CreateBaseAttributesFromJson(string fileName) { string path = FilePathSystem.GetFilePath("Misc", fileName); SMAttributes attrs = new SMAttributes(); if (File.Exists(path)) { using (StreamReader r = new StreamReader(path)) { string json = r.ReadToEnd(); attrs = JsonConvert.DeserializeObject <SMAttributes>(json); } } return(attrs); }
/// <summary> /// Gets a character and also loads the character to memory if it isn't already there. /// </summary> /// <param name="userID">The id of the character you want to load</param> /// <returns>A character</returns> public SMCharacter GetCharacter(string userID, string userName = null, string password = null) { // Get the character file if it exists List <SMCharacter> smcs = (List <SMCharacter>)HttpContext.Current.Application["SMCharacters"]; SMCharacter charInMem = smcs.FirstOrDefault(smc => smc.UserID == userID); if (charInMem == null) { // Get the right path, and work out if the file exists. string path = FilePathSystem.GetFilePath("Characters", "Char" + userID); // Check if the character exists.. if (File.Exists(path)) { using (StreamReader r = new StreamReader(path)) { // Get the character from the json string string json = r.ReadToEnd(); charInMem = JsonConvert.DeserializeObject <SMCharacter>(json); bool canLogin = true; if (password != null) { canLogin = (Crypto.DecryptStringAES(charInMem.Password) == password); } if ((canLogin) && (password != null)) { canLogin = (Crypto.DecryptStringAES(charInMem.Password) == password); } if (canLogin) { // Add the character to the application memory smcs.Add(charInMem); HttpContext.Current.Application["SMCharacters"] = smcs; // Send a message so everyone knows someone has logged in. SendGlobalMessage(charInMem, "[i][b]Global Message:[/b] " + charInMem.GetFullName() + " has logged into the game[/i]"); } } } } return(charInMem); }
/// <summary> /// Gets a room and also loads the room to memory if it isn't already there. /// </summary> /// <param name="roomID">The id of the location you want to load</param> /// <returns>A room</returns> public SMRoom GetRoom(string roomID) { // Get the room file if it exists List <SMRoom> smrs = (List <SMRoom>)HttpContext.Current.Application["SMRooms"]; SMRoom roomInMem = smrs.FirstOrDefault(smr => smr.RoomID == roomID); // If the room is not already in memory load the room if (roomInMem == null) { // Get the right path, and work out if the file exists. string path = FilePathSystem.GetFilePath("Locations", "Loc" + roomID); // Check if the character exists.. if (File.Exists(path)) { // Use a stream reader to read the file in (based on the path) using (StreamReader r = new StreamReader(path)) { // Create a new JSON string to be used... string json = r.ReadToEnd(); // ... get the information from the the room information. roomInMem = JsonConvert.DeserializeObject <SMRoom>(json); // Check whether the room is instanced or not if (roomInMem.Instanced) { // First change the name of the room. roomInMem.RoomID = roomInMem.RoomID + "||" + Guid.NewGuid(); } // Add the room to the application memory smrs.Add(roomInMem); HttpContext.Current.Application["SMRooms"] = smrs; // Try to spawn some creatures into the room on first load roomInMem.Spawn(); } } } return(roomInMem); }
public SMItem GetProducedItem() { SMItem smi = null; // Get the right path, and work out if the file exists. string path = FilePathSystem.GetFilePath("Objects", this.GetProducedItemName()); // Check if the character exists.. if (File.Exists(path)) { using (StreamReader r = new StreamReader(path)) { // Get the character from the json string string json = r.ReadToEnd(); smi = JsonConvert.DeserializeObject <SMItem>(json); } } return(smi); }
/// <summary> /// Gets a character and also loads the character to memory if it isn't already there. /// </summary> /// <param name="userID">The id of the character you want to load</param> /// <returns>A character</returns> public SMCharacter GetCharacter(string userID, string userName = null, string password = null) { // Get the room file if it exists List <SMCharacter> smcs = (List <SMCharacter>)HttpContext.Current.Application["SMCharacters"]; SMCharacter charInMem = smcs.FirstOrDefault(smc => smc.UserID == userID); if (charInMem == null) { // Get the right path, and work out if the file exists. string path = FilePathSystem.GetFilePath("Characters", "Char" + userID); // Check if the character exists.. if (File.Exists(path)) { using (StreamReader r = new StreamReader(path)) { // Get the character from the json string string json = r.ReadToEnd(); charInMem = JsonConvert.DeserializeObject <SMCharacter>(json); bool canLogin = true; if (password != null) { canLogin = (Crypto.DecryptStringAES(charInMem.Password, "ProvinceMUD") == password); } if ((canLogin) && (password != null)) { canLogin = (Crypto.DecryptStringAES(charInMem.Password, "ProvinceMUD") == password); } if (canLogin) { // Add the character to the application memory smcs.Add(charInMem); HttpContext.Current.Application["SMCharacters"] = smcs; } } } } return(charInMem); }
public string GetFlushLocation() { string defaultRoomPath = FilePathSystem.GetFilePath("Scripts", "Flush-Location"); if (File.Exists(defaultRoomPath)) { // Use a stream reader to read the file in (based on the path) using (StreamReader r = new StreamReader(defaultRoomPath)) { // Create a new JSON string to be used... string json = r.ReadToEnd(); // ... get the information from the the start location token.. SMStartLocation sl = JsonConvert.DeserializeObject <SMStartLocation>(json); // Set the start location. return(sl.StartLocation); } } return("1"); }
/// <summary> /// Update the user details of the character to the character names file /// This provides easy reference to the username / password when logging in /// on other platforms. /// </summary> /// <param name="smc">The character being used</param> public void UpdateUserDetails(SMCharacter smc) { // Load the char names list into memory string path = FilePathSystem.GetFilePath("Characters", "CharNamesList"); List <SMAccount> allCharacters = new List <SMAccount>(); if (File.Exists(path)) { using (StreamReader r = new StreamReader(path)) { string json = r.ReadToEnd(); allCharacters = JsonConvert.DeserializeObject <List <SMAccount> >(json); } } // Find the relevant one SMAccount smcn = allCharacters.FirstOrDefault(c => c.CharacterName.ToLower() == smc.GetFullName().ToLower()); // remove the existing one from the memory allCharacters.Remove(smcn); // Check if we found something, if we didn't we might have an issue.. if (smcn != null) { smcn.EmailAddress = smc.Username; smcn.HashedPassword = smc.Password; } // Now save that back out. allCharacters.Add(smcn); string listJSON = JsonConvert.SerializeObject(allCharacters, Formatting.Indented); using (StreamWriter w = new StreamWriter(path)) { w.WriteLine(listJSON); } }
protected void Application_Start(object sender, EventArgs e) { // Rebuild the char names list json new SMAccountHelper().RebuildCharacterJSONFile(); // Websocket WebSocketCollection wsClients = new WebSocketCollection(); Application["WSClients"] = wsClients; // Botclients //List<BotClient> botClients = new List<BotClient>(); //Application["BotClients"] = botClients; // Character List List <SMCharacter> smc = new List <SMCharacter>(); Application["SMCharacters"] = smc; // Room List List <SMRoom> smr = new List <SMRoom>(); Application["SMRooms"] = smr; // Load the Commands into memory for usage later // Used for both parsing commands sent in and also for help output List <SMCommand> smcl = new List <SMCommand>(); // Get all files from the Commands path string commandsFolderPath = FilePathSystem.GetFilePathFromFolder("Commands"); DirectoryInfo dirInfo = new DirectoryInfo(commandsFolderPath); FileInfo[] CommandFiles = dirInfo.GetFiles("Commands.*.json"); foreach (FileInfo file in CommandFiles) { string path = FilePathSystem.GetFilePath("Commands", file.Name, ""); // Use a stream reader to read the file in (based on the path) using (StreamReader r = new StreamReader(path)) { // Create a new JSON string to be used... string json = r.ReadToEnd(); // Get all the commands from the commands file smcl.AddRange(JsonConvert.DeserializeObject <List <SMCommand> >(json)); } } Application["SMCommands"] = smcl; // Load class builder specs into memory for usage later // Used for creating objects that are used to call user commands on ClassBuilderSpecs cbs = new ClassBuilderSpecs(); string specsPath = FilePathSystem.GetFilePath("Misc", "ClassBuilder"); if (File.Exists(specsPath)) { using (StreamReader r = new StreamReader(specsPath)) { string json = r.ReadToEnd(); cbs = JsonConvert.DeserializeObject <ClassBuilderSpecs>(json); } } Application["ClassBuilderSpecs"] = cbs; // Load the Skills into memory for usage later // Used for both parsing commands sent in and also for help output List <SMSkill> lsk = new List <SMSkill>(); // Get all filenames from path string skillFolderFilePath = FilePathSystem.GetFilePathFromFolder("Skills"); DirectoryInfo d = new DirectoryInfo(skillFolderFilePath); //Assuming Test is your Folder FileInfo[] Files = d.GetFiles(); foreach (FileInfo file in Files) { string skillFilePath = FilePathSystem.GetFilePath("Skills", file.Name, ""); // Use a stream reader to read the file in (based on the path) using (StreamReader r = new StreamReader(skillFilePath)) { // Create a new JSON string to be used... string json = r.ReadToEnd(); // ... get the information from the help file lsk.Add(JsonConvert.DeserializeObject <SMSkill>(json)); } } Application["SMSkills"] = lsk; // Load the Receipes into memory for usage later // Used for both parsing commands sent in and also for help output List <SMReceipe> smrl = new List <SMReceipe>(); // Get all filenames from path string receipeFolderFilePath = FilePathSystem.GetFilePathFromFolder("Receipe"); d = new DirectoryInfo(receipeFolderFilePath); //Assuming Test is your Folder Files = d.GetFiles(); foreach (FileInfo file in Files) { string receipeFilePath = FilePathSystem.GetFilePath("Receipe", file.Name, ""); // Use a stream reader to read the file in (based on the path) using (StreamReader r = new StreamReader(receipeFilePath)) { // Create a new JSON string to be used... string json = r.ReadToEnd(); // ... get the information from the help file smrl.Add(JsonConvert.DeserializeObject <SMReceipe>(json)); } } Application["SMReceipes"] = smrl; // Load the Skills into memory for usage later // Used for both parsing commands sent in and also for help output List <SMNPC> lnpcs = new List <SMNPC>(); //Get all filenames from path string NPCsFolderFilePath = FilePathSystem.GetFilePathFromFolder("NPCs"); d = new DirectoryInfo(NPCsFolderFilePath); //Assuming Test is your Folder Files = d.GetFiles(); foreach (FileInfo file in Files) { if (file.Name != "NPCNamesList.json") { string NPCFilePath = FilePathSystem.GetFilePath("NPCs", file.Name, ""); // Use a stream reader to read the file in (based on the path) using (StreamReader r = new StreamReader(NPCFilePath)) { // Create a new JSON string to be used... string json = r.ReadToEnd(); // ... get the information from the help file lnpcs.Add(JsonConvert.DeserializeObject <SMNPC>(json)); } } } Application["SMNPCs"] = lnpcs; // load and fetch NPCRaces, using a dictionary for speedy lookups later. Dictionary <string, NPCRace> dnpcRaces = new Dictionary <string, NPCRace>(); string NPCRacesFolderFilePath = FilePathSystem.GetFilePathFromFolder("NPCRaces"); d = new DirectoryInfo(NPCRacesFolderFilePath); Files = d.GetFiles(); foreach (FileInfo file in Files) { string RaceFilePath = FilePathSystem.GetFilePath("NPCRaces", file.Name, ""); using (StreamReader r = new StreamReader(RaceFilePath)) { string json = r.ReadToEnd(); NPCRace raceFromJSON = JsonConvert.DeserializeObject <NPCRace>(json); dnpcRaces[raceFromJSON.Name.ToLower()] = raceFromJSON; } } Application["NPCRaces"] = dnpcRaces; Application["Parties"] = new List <SMParty>(); #region "The Pulse" // Set the current context to pass into the thread HttpContext ctx = HttpContext.Current; // Create a new thread Thread pulse = new Thread(new ThreadStart(() => { HttpContext.Current = ctx; new SMPulse().Initiate(); // this is the item that is going to initiate })); // Start the thread pulse.Start(); #endregion // Register the global configuration items for the bot helper //GlobalConfiguration.Configure(WebAPIConfig.Register); }
/// <summary> /// Create new character method /// </summary> /// <param name="userID">UserID - from the Slack UserID</param> /// <param name="firstName">The first name of the character</param> /// <param name="lastName">The last name of the character</param> /// <param name="age">The age of the character</param> /// <param name="sexIn">M or F for the male / Female character</param> /// <param name="characterType">M or F for the male / Female character</param> /// <returns>A string with the character information</returns> public string CreateCharacter(string userID, string firstName, string lastName, string sexIn, string age, string characterType = "BaseCharacter", string responseURL = null, string userName = null, string password = null, bool autoLogin = true) { // Get the path for the character string path = FilePathSystem.GetFilePath("Characters", "Char" + userID); // If the file doesn't exist i.e. the character doesn't exist if (!File.Exists(path)) { // Create the character options SMCharacter SMChar = new SMCharacter(); SMChar.UserID = userID; SMChar.FirstName = firstName; SMChar.LastName = lastName; SMChar.LastLogindate = DateTime.Now; SMChar.LastInteractionDate = DateTime.Now; SMChar.PKFlag = false; SMChar.Sex = char.Parse(sexIn); SMChar.Age = int.Parse(age); if (userName != null) { SMChar.Username = userName; } if (password != null) { SMChar.Password = Utility.Crypto.EncryptStringAES(password); } // Add default attributes to the character SMChar.Attributes = CreateBaseAttributesFromJson("Attribute." + characterType); // Set default character slots before adding items to them SMChar.Slots = CreateSlotsFromJSON("Slots." + characterType); // Add default items to the character SMSlot back = SMChar.GetSlotByName("Back"); back.EquippedItem = SMItemFactory.Get("Container", "SmallBackpack"); // Add default body parts to the new character SMChar.BodyParts = CreateBodyPartsFromJSON("BodyParts." + characterType); // Add the base currency SMChar.Currency = CreateCurrencyFromJSON("Currency.BaseCharacter"); // Set the start location SMChar.RoomID = "1"; string defaultRoomPath = FilePathSystem.GetFilePath("Scripts", "EnterWorldProcess-FirstLocation"); if (File.Exists(defaultRoomPath)) { // Use a stream reader to read the file in (based on the path) using (StreamReader r = new StreamReader(defaultRoomPath)) { // Create a new JSON string to be used... string json = r.ReadToEnd(); // ... get the information from the the start location token.. SMStartLocation sl = JsonConvert.DeserializeObject <SMStartLocation>(json); // Set the start location. SMChar.RoomID = sl.StartLocation; // TODO Add room to memory if not already there. } } // Write the character to the stream SMChar.SaveToFile(); // Write an account reference to the CharNamesList new SMAccountHelper().AddNameToList(SMChar.GetFullName(), SMChar.UserID, SMChar); // Check if there is a response URL if ((responseURL != null) && (autoLogin)) { // Log the newly created character into the game if in something like Slack Login(userID, true, responseURL); return(""); } else { // Return the userid ready for logging in return(userID); } } else { // If they already have a character tell them they do and that they need to login. // log the newly created character into the game // Login(userID, true, responseURL); // Get all current characters List <SMCharacter> smcs = (List <SMCharacter>)HttpContext.Current.Application["SMCharacters"]; SMCharacter character = smcs.FirstOrDefault(smc => smc.UserID == userID); return(ResponseFormatterFactory.Get().General("You already have a character, you cannot create another.")); } }
/// <summary> /// Logs someone in with the Slack UserID /// </summary> /// <param name="userID">Slack UserID</param> /// <returns>A string response</returns> public string Login(string userID, bool newCharacter = false, string responseURL = null, string connectionService = "slack", string password = null) { // Variables for the return string string returnString = ""; // Get all current characters List <SMCharacter> smcs = (List <SMCharacter>)HttpContext.Current.Application["SMCharacters"]; SMCharacter character = smcs.FirstOrDefault(smc => smc.UserID == userID); // Get the right path, and work out if the file exists. string path = FilePathSystem.GetFilePath("Characters", "Char" + userID); // Check if the character exists.. if (!File.Exists(path)) { // If they don't exist inform the person as to how to create a new user returnString = ResponseFormatterFactory.Get().General("You must create a character, to do so, use the command /sm CreateCharacter FIRSTNAME,LASTNAME,SEX,AGE"); returnString += ResponseFormatterFactory.Get().General("i.e. /sm CreateCharacter Paul,Hutson,m,34"); // return information [need to return this without a player!] return(returnString); } else { if ((character != null) && (!newCharacter)) { returnString = ResponseFormatterFactory.Get().General("You're already logged in!"); return(returnString); } else { // Get the character character = GetCharacter(userID, password); // Reset the character activity, just in case! character.CurrentActivity = null; // Set the response URL of the character if (responseURL != null) { character.ResponseURL = responseURL; } // Set the connection service character.ConnectionService = connectionService; // Set the last login datetime character.LastLogindate = DateTime.Now; // Check that the currency is OK. if (character.Currency == null) { character.Currency = new SMCurrency(); character.Currency.AmountOfCurrency = 50; } if (!newCharacter) { returnString = ResponseFormatterFactory.Get().Bold("Welcome back " + character.FirstName + " " + character.LastName + " (you are level " + character.CalculateLevel() + ")", 1); } else { returnString = ResponseFormatterFactory.Get().Bold("Welcome to SlackMud!"); returnString += ResponseFormatterFactory.Get().General("We've created your character in the magical world of Arrelvia!"); // TODO, use a welcome script! } // Check if the player was last in an instanced location. if (character.RoomID.Contains("||")) { // Find the details of the original room type SMRoom originalRoom = GetRoom(character.RoomID.Substring(0, character.RoomID.IndexOf("||"))); character.RoomID = originalRoom.InstanceReloadLocation; } // Clear out any old party references character.PartyReference = null; // Get the location details returnString += GetLocationDetails(character.RoomID, character); // Clear old responses an quests from the character character.ClearQuests(); if (character.NPCsWaitingForResponses != null) { character.NPCsWaitingForResponses.RemoveAll(a => a.NPCID != ""); } // Return the text output character.sendMessageToPlayer(returnString); // Walk the character in SMRoom room = character.GetRoom(); if (room != null) { // Announce someone has walked into the room. room.Announce(ResponseFormatterFactory.Get().Italic(character.GetFullName() + " walks in.")); room.ProcessNPCReactions("PlayerCharacter.Enter", character); } return(""); } } }