public static void loadAllRooms() // load ALL the rooms!! { try { foreach (string fileName in Directory.GetFiles("rooms", "*.room")) // Every file in the room/ directory that has the .room extension is a room. I'm thinking that rooms should be separated into subfolders based on arrangement/relevant story/quests. That would require some adjustments to this method. { // I'm thinking about making a .map file for each room group. The map file would define story goals, mobs confined to the story area, exits to other maps, etc. room tempRoom = new room(); // In fact, should there be an overmap? The overmap would contain the non-story rooms you inhabit when travelling between maps... XmlTextReader reader = new XmlTextReader(fileName); while (reader.Read()) { switch (reader.Name) { case "RoomData": if (reader.HasAttributes) { tempRoom.roomName = reader.GetAttribute("name"); } break; case "Description": if (reader.GetAttribute("name") == "RoomDescription") { tempRoom.description = reader.ReadString(); } else { tempRoom.altDescription = reader.ReadString(); } break; case "EXIT": EXIT tempExit = new EXIT(); tempExit.exitName = reader.GetAttribute("name"); tempExit.exitDestination = reader.GetAttribute("leadsto"); tempExit.exitDescription = reader.GetAttribute("description"); tempRoom.exits.Add(tempExit); break; default: break; } } NetworkServer.roomArray.Add(tempRoom); } } catch (Exception e) { Console.WriteLine(e); LogHandler.LogAServerEvent(e.ToString()); } }
public void onRoomChange(Client changingClient, room newRoom) { newRoom.occupants.Add(changingClient); if (changingClient.currentRoom != null) { changingClient.currentRoom.occupants.Remove(changingClient); } changingClient.currentRoom = newRoom; changingClient.outgoing.WriteLine(changingClient.currentRoom.description); foreach (EXIT exit in changingClient.currentRoom.exits) { changingClient.outgoing.WriteLine(exit.exitDescription); } foreach (Client occupant in changingClient.currentRoom.occupants) { changingClient.outgoing.WriteLine(occupant.clientUsername); } }
public Client(TcpClient connectedClient) { //*************************************************** // This is here to account for the extra bytes that fat ftp clients (putty, etc) will send upon an initial connection. // Eventually a proper telnet handler will exist and this won't be necessary. //byte[] bytesFromClient = new byte[32]; // Set the buffer size. //var testReader = connectedClient.GetStream().BeginRead(bytesFromClient, 0, bytesFromClient.Length, null, null); // Read all incoming bytes. //WaitHandle waiter = testReader.AsyncWaitHandle; //bool finishedRead = waiter.WaitOne(250, false); // If there are no bytes to read, we'll wait 1/4 of a second before continuing. //if (finishedRead) //{ // connectedClient.GetStream().EndRead(testReader); // foreach (int i in bytesFromClient) // Console.WriteLine("Read a byte:" + i); // This is for debugging. //} //else //{ // connectedClient.GetStream().EndRead(testReader); //} //*************************************************** // Need to add a unique ID, possibly based on yearmonthdayhourminutesecondincrement pattern. This will be a session-unique ID, // but we also need an account-specific identifier. this.isAdmin = false; this.connectedOn = DateTime.Now; // When the client connected. this.disconnectFlag = false; this.loggedInFlag = false; this.clientUsername = "******"; this.connectedClient = connectedClient; this.clientIP = IPAddress.Parse(((IPEndPoint)connectedClient.Client.RemoteEndPoint).Address.ToString()); this.clientEndPoint = connectedClient.Client.RemoteEndPoint; NetworkServer.clientsToLogIn.Add(this); Console.WriteLine("Client connected from: " + clientIP); this.daStream = connectedClient.GetStream(); this.incoming = new StreamReader(daStream); this.outgoing = new StreamWriter(daStream); this.outgoing.AutoFlush = true; // Why? //this.daStream.ReadTimeout = 250; try { this.outgoing.Write("Press any key..."); this.incoming.ReadLine(); } catch (Exception e) { Console.WriteLine(e); } //this.daStream.ReadTimeout = -1; InputHandler.DisplayTextFile(this, "WelcomeScreen.txt"); // Display the welcome screen when the client connects. // Also need a mechanic for creating accounts while (!GateKeeper(this)) { } this.loggedInFlag = true; Console.WriteLine(this.clientUsername + " logged in."); LogHandler.LogAClientEvent("connected", this); // Is it better to put this here or within the GateKeeper? this.outgoing.WriteLine("Welcome " + clientUsername); ChatServer.globalChannel.Add(this); using (SqlConnection dbConnection = new SqlConnection(NetworkServer.connectionString)) // Move this to a method? { dbConnection.Open(); SqlDataReader dbReader = null; SqlCommand dbCommand = new SqlCommand("select lastRoom from users where username like '" + clientUsername + "'", dbConnection); dbReader = dbCommand.ExecuteReader(); dbReader.Read(); string roomToFindInArrayList = dbReader["lastRoom"].ToString(); lastRoom = RoomHandler.findRoom(roomToFindInArrayList); if (lastRoom == null) { lastRoom = RoomHandler.findRoom("TestRoom1"); } } onRoomChange(this, lastRoom); // Make the client appear in the room there were in when they logged off. this.outgoing.Write("\nInput Please > "); }
/* **********DEPRECATED********** * public static bool roomExists(string roomToCheck) // This might be redundant. Mayhap we can use the findRoom method for this purpose. * { * foreach (room queryRoom in NetworkServer.roomArray) * { * if (queryRoom.roomName == roomToCheck) * return true; * } * return false; * } */ public static void changeRoom(Client roomChangeClient, room newRoom) // Here we will update chat channels, room occupant arrays, etc. { roomChangeClient.onRoomChange(roomChangeClient, newRoom); }