Example #1
0
		public static IUser GetUserAsNPCFromList(List<ObjectId> id) {
			if (id.Count > 0) {
				IUser result = new Sockets.User();
				result.Player = GetAnNPCByID(id[0]);
				result.CurrentState = UserState.TALKING;
				return result;
			}

			return null;
		}
Example #2
0
		//this shouldn't be a static class eventually.
		public static void Main(string[] args) {
			Console.Title = "Novus MUD Server 0.7";
			string databasePath;
            string ipAddress;
            string port;
			//start the Mongodatabase if the server is running
			try {
				ParseConfigFile(out databasePath, out ipAddress, out port);
			}
			catch (FileNotFoundException) {
				Console.WriteLine("Settings.cfg file not found in current directory.  Server shutting down.");
				return;
			}

			System.Diagnostics.Process.Start(databasePath);

			Console.WriteLine(">>> MongoDB initialized <<<");
			Console.Write("Registering classes in Database...");
			MongoUtils.ClassMapper.RegisterMappings();
			Console.WriteLine("Done");

			MessageBuffer messageHandler = new MessageBuffer("Server");

			Sockets.Server server = Sockets.Server.GetServer(ipAddress, int.Parse(port));

            //get script singletons
            Scripts.Login loginScript = Scripts.Login.GetScript();
			Scripts.CreateCharacter CreationScript = Scripts.CreateCharacter.GetScript();
			Scripts.LevelUpScript levelUpScript = Scripts.LevelUpScript.GetScript();

			Commands.CommandParser.LoadUpCommandDictionary();
			Character.NPCUtils npcUtils = Character.NPCUtils.GetInstance();
			//npcUtils.LoadNPCs();

			MudTimer.StartUpTimers();

			try {
				server.StartServer();

				Console.WriteLine(">>> Listening at IP: " + server.IPAddress + " <<<");
				Console.WriteLine(">>> Port: " + server.Port + " <<<");

				StringBuilder sb = new StringBuilder();
				double speed = 0.0D;

				while (true) {
					if (Console.KeyAvailable) {
						ConsoleKeyInfo key = Console.ReadKey();

						if (key.Key == ConsoleKey.Backspace && sb.Length > 0) {
							sb.Remove(sb.Length - 1, 1);
						}
						else if (key.Key != ConsoleKey.Enter) {
							sb.Append(key.KeyChar);
						}

						if (key.Key == ConsoleKey.Enter) {
							Console.WriteLine(">>> " + sb.ToString());
							if (sb.ToString().Contains("-exit")) {
								break;
							}
							else {
								server.SendToAllClients(sb.ToString());
							}
							sb.Clear();
						}
					}
					try {
						//run NPC AI on separate thread, yes even if no players are playing.
						npcUtils.ProcessAIForNPCs();
						
						if (Sockets.Server.GetCurrentUserList().Count > 0) {
							int index = 0;
							System.Diagnostics.Stopwatch stopWatch = System.Diagnostics.Stopwatch.StartNew();

                            //Players actively playing
							foreach (IUser user in Sockets.Server.GetCurrentUserList()) {
								if (user.CurrentState == UserState.TALKING || user.CurrentState == UserState.LIMBO) {
									CommandParser.ParseCommands(user);
								}

                                //Players who just connected
								else if (user.CurrentState == UserState.JUST_CONNECTED) {
									//just connected let's make them login
									loginScript.AddUserToScript(Sockets.Server.GetCurrentUserList().ElementAt(index));
									user.CurrentState = UserState.LOGGING_IN;
								}

								 //the player should not receive any messages while in the level up script
								else if (user.CurrentState == UserState.LEVEL_UP) {
									if (user.CurrentState == UserState.LEVEL_UP) {
										levelUpScript.AddUserToScript(Sockets.Server.GetCurrentUserList().ElementAt(index));
									}

									string temp = levelUpScript.ExecuteScript(user.UserID);

									user.MessageHandler(temp);


									if (user.InBufferReady && user.CurrentState != UserState.TALKING) {
										user.CurrentState = levelUpScript.InsertResponse(user.InBuffer, user.UserID);
									}									
								}

                                //Players singing in to their character
								else if (user.CurrentState == UserState.LOGGING_IN) {
									//they are in the middle of the login process
									string temp = loginScript.ExecuteScript(user.UserID);
									if (!string.IsNullOrEmpty(temp)) {
										if (temp.Contains("Welcome"))
											temp += "\n\n\r";
										user.MessageHandler(temp);
									}

									if (user.InBufferReady && user.CurrentState != UserState.TALKING) {
										user.CurrentState = loginScript.InsertResponse(user.InBuffer, user.UserID);
									}

									if (user.CurrentState == UserState.CREATING_CHARACTER) {
										CreationScript.AddUserToScript(Sockets.Server.GetCurrentUserList().ElementAt(index));
									}
								}

                                //Players creating a new character
								else if (user.CurrentState == UserState.CREATING_CHARACTER) {
									string temp = CreationScript.ExecuteScript(user.UserID);
									if (!string.IsNullOrEmpty(temp)) {
										if (temp.Contains("Welcome"))
											temp += "\n\n\r";
										user.MessageHandler(temp);
									}

									if (user.InBufferReady && user.CurrentState != UserState.TALKING) {
										user.CurrentState = CreationScript.InsertResponse(user.InBuffer, user.UserID);
									}
								}
								//on this method not sure if I want to display one line per each call of this or display everything to the user 
								//until their outbuffer is empty.  It works this way as of now.
								server.SendToAllClients();
								index++;
								stopWatch.Stop();
								if ((double)stopWatch.Elapsed.TotalSeconds > speed) {
									speed = (double)stopWatch.Elapsed.TotalSeconds;
									Console.WriteLine(String.Format("Slowest: {0}", stopWatch.Elapsed));
								}
							}
						}
					}
					catch (Exception ex) {
						IUser temp = new Sockets.User(true);
						temp.UserID = new MongoDB.Bson.ObjectId("Internal");
						// CommandParser.ReportBug(temp, new List<string>(new string[] { "Bug Internal Error: " + ex.Message + "\n" + ex.StackTrace }));
					}
				}

				Console.WriteLine(">>> Server shutting down <<<");
				server.ShutdownServer();
			}
			catch (SocketException se) {
				Console.WriteLine(se.Message);
			}
			catch (Exception ex) {
				Console.WriteLine("Whoa! : " + ex.Message + "\n\n" + ex.StackTrace);
			}
			Console.ReadLine();
		}
Example #3
0
		 public string ExecuteScript(string userId) {
			 string message = null;
			 UserScript specificUser = usersLoggingIn.Where(u => u.user.UserID == userId).SingleOrDefault();
			 if (specificUser != null && specificUser.lastStep != specificUser.currentStep) {
				 switch (specificUser.currentStep) {
                     case Steps.SPLASH:
                         message = SplashScreen();
                         usersLoggingIn.Where(u => u.user.UserID == userId).SingleOrDefault().currentStep = Steps.NAME;
						 usersLoggingIn.Where(u => u.user.UserID == userId).SingleOrDefault().lastStep = Steps.NONE;
                         break;
					 case Steps.NAME:
						 message = AskForFirstName();
						 usersLoggingIn.Where(u => u.user.UserID == userId).SingleOrDefault().currentStep = Steps.AWAITINGRESPONSE;
						 usersLoggingIn.Where(u => u.user.UserID == userId).SingleOrDefault().lastStep = Steps.NAME;
						 break;
					 case Steps.PASSWORD:
						 message = AskForPassword();
						 usersLoggingIn.Where(u => u.user.UserID == userId).SingleOrDefault().currentStep = Steps.AWAITINGRESPONSE;
						 usersLoggingIn.Where(u => u.user.UserID == userId).SingleOrDefault().lastStep = Steps.PASSWORD;
						 break;
					 case Steps.SUCCEEDED:
						 //let's see if they are connecting back to a limbo character first and set things right
						 IUser user = new Sockets.User();
						 user = Sockets.Server.GetAUserPlusState(specificUser.user.UserID, UserState.LIMBO);
						 if (user != null) {
							 Sockets.Server.UpdateUserSocket(specificUser.user.UserID);
							 specificUser.user = user; 
						 }
						 else {
                             specificUser.user.Player.Load(specificUser.user.Player.ID);
						 }
						 message = "Welcome " + specificUser.user.Player.FirstName + " " + specificUser.user.Player.LastName + "!";
						 specificUser.user.CurrentState = UserState.TALKING;
						 specificUser.user.InBuffer = "look\r\n";
						 usersLoggingIn.Remove(specificUser);
						 break;
					 case Steps.AWAITINGRESPONSE:
					 default:
						 break;
				 }
			 }
			 else {
				 if (specificUser != null && specificUser.currentStep == Steps.NAME) {
					 message = "No character with that name exists!\n\rDo you want to create a new character? (Y/N)";
					 specificUser.currentStep = Steps.AWAITINGRESPONSE;
                     specificUser.lastStep = Steps.CREATECHAR;
				 }
				 else if (specificUser != null && specificUser.currentStep == Steps.PASSWORD) {
					 message = "Incorrect Password!";
                     specificUser.lastStep = Steps.NONE;
				 }
			 }

			 return message;
		 }