public Server(Parser parser) { Log.Write("Binding events...", Log.ErrorLevel.DEBUG); Events.Ping += this.PingEvent; Events.EventNotFound += this.EventNotFound; Events.Quit += this.QuitEvent; Log.Write("Creating NetConnector...", Log.ErrorLevel.DEBUG); string addr = parser.Get ("Network.Address"); int port = int.Parse(parser.Get("Network.Port")); this.NetConnector = new NetConnector(addr, port); this.PingTimer = new Timer(30000); this.PingTimer.Elapsed += this.SendPingEvent; this.PingTimer.AutoReset = true; this.PingTimer.Enabled = true; this.PingTimer.Start(); }
//Constructor. Binds events and connects client socket to the head server. public MapServer(Parser parser) : base(parser) { Log.Write("Initializing Map Server specific features.", Log.ErrorLevel.DEBUG); string address = parser.Get("Network.HeadServer.Address"); int port = int.Parse(parser.Get("Network.HeadServer.Port")); string username = parser.Get("Network.HeadServer.Login"); string password = parser.Get("Network.HeadServer.Password"); this.challengesRequests = new Dictionary<Guid, Client>(); Log.Write("Binding Map server specific events", Log.ErrorLevel.DEBUG); Events.GetMap += this.GetMapEvent; Events.Auth += this.AuthEvent; Events.CharacterInfo += this.CharacterInfoEvent; Events.GetPosition += this.GetPositionEvent; Events.MoveUp += this.MoveUpEvent; Events.MoveDown += this.MoveDownEvent; Events.MoveLeft += this.MoveLeftEvent; Events.MoveRight += this.MoveRightEvent; this.PingTimer.Elapsed += this.SendPingEvent; this.NetConnector.ClientConnect += this.ClientConnect; this.NetConnector.ClientDisconnect += this.ClientDisconnect; try { this.Map = new Map(parser.Get("Map.MapFile"), parser.Get("Map.JumpPointFile"), parser.Get("Map.SpecFile")); this.Map.Tileset = parser.Get("Map.Tileset"); this.Map.ID = parser.Get("Game.Map.ID"); this.Map.Name = parser.Get("Game.Map.Name"); } catch(MapParseException e) { Log.Write("Error while parsing map: " + e.Message, Log.ErrorLevel.CRITICAL); throw new InitializationException("Can't load map"); } Log.Write("Connecting to the Head Server", Log.ErrorLevel.NOTICE); this.Client = new ClientConnector(address, port); this.Client.Auth(username, password); this.Client.Send("mapserver" , new string[]{ this.Map.ID, this.Map.Name, parser.Get("Network.Port") }); }
//Constructor. Binds all events and connects to the database. public HeadServer(Parser parser) : base(parser) { Log.Write("Initializing Head Server specific features.", Log.ErrorLevel.DEBUG); Log.Write("Connecting to database...", Log.ErrorLevel.NOTICE); string DBAddr = parser.Get("Database.Address"); int DBPort = int.Parse(parser.Get("Database.Port")); string DBUsername = parser.Get("Database.Username"); string DBPassword = parser.Get("Database.Password"); string DBName = parser.Get("Database.DBName"); if(!Database.Connect(DBAddr, DBPort, DBUsername, DBPassword, DBName)) throw new InitializationException("Cannot connect to database"); this.MapClients = new Dictionary<int, MapServerClient>(); this.Challenges = new Dictionary<Guid, Character>(); Log.Write("Binding HeadServer related events...", Log.ErrorLevel.DEBUG); Events.MapServer += this.MapServerEvent; Events.Auth += this.AuthEvent; Events.CharacterList += this.CharacterListEvent; Events.SetCharacter += this.SetCharacterEvent; Events.CharacterInfo += this.CharacterInfoEvent; this.NetConnector.ClientDisconnect += this.ClientDisconnect; }
//Loads a map public void Load(string mapfile, string jumpfile, string specfile) { FileStream fp; BinaryReader rdmap; try { fp = new FileStream(mapfile, FileMode.Open); rdmap = new BinaryReader(fp); } catch(FileNotFoundException e) { throw new MapParseException(e.Message); } try { int i = 0; //Reading the file while(rdmap.PeekChar() != -1) { //Position Coordinate c = new Coordinate(); c.X = rdmap.ReadInt16(); c.Y = rdmap.ReadInt16(); char[] chunkData = rdmap.ReadChars(16); if(chunkData.Length < 16) throw new MapParseException("Unexpected end of stream: " + chunkData.Length); char[,] chunk = new char[4,4]; int x = 0, y = 0; foreach(char cell in chunkData) { chunk[y,x] = cell; x++; if(x == 4) { x = 0; y++; } } Chunk chunkStruct = new Chunk(c, chunk); this.Chunks.Add(c, chunkStruct); i++; } Log.Write("Loaded " + i + " chunks", Log.ErrorLevel.DEBUG); } catch(IOException e) { throw new MapParseException(e.Message); } Parser p = new Parser(); p.Parse(specfile); this.StartPoint = new Coordinate(); this.StartPoint.X = short.Parse(p.Get("Spec.StartX")); this.StartPoint.Y = short.Parse(p.Get("Spec.StartY")); }
public MainClass() { this.stop = false; this.parser = new Parser(); }