static public void OnClose(GameSocket gameSocket)
        {
            using (Database DataContext = new Database(Program.Configuration["database:connection-string"]))
            {
                Character c = DataContext.Characters.Find(gameSocket.SelectedCharacter.CharacterId);
                //DataContext.Entry(oldMap).CurrentValues.SetValues(m);
                c.CoordX = gameSocket.SelectedCharacter.BoundingBox.X;
                c.CoordY = gameSocket.SelectedCharacter.BoundingBox.Y;
                DataContext.SaveChanges();
            }

            SimulationMap sm = Program.SimulatedMaps.FirstOrDefault(smm => smm.Map.MapId == gameSocket.SelectedCharacter.CurrentMapId);

            sm.RemoveCharacter(gameSocket.SelectedCharacter);

            Network.GamingConnections.Remove(gameSocket);
        }
Пример #2
0
        static public void OnMessage(IWebSocketConnection socket, string msg)
        {
            Log.Information("Message " + msg + " from host " + socket.ConnectionInfo.ClientIpAddress);
            if (msg.StartsWith("game.currentmap.request"))
            {
                using (Database DataContext = new Database(Program.Configuration["database:connection-string"]))
                {
                    Map[]  mi   = DataContext.Maps.ToArray();
                    string json = JsonConvert.SerializeObject(mi);
                    //Console.WriteLine(json);
                    socket.Send("currentmap.data|" + json);
                }
            }
            else if (msg.StartsWith("editor.zones.list"))
            {
                using (Database DataContext = new Database(Program.Configuration["database:connection-string"]))
                {
                    Zone[] zones = DataContext.Zones.ToArray();
                    string json  = JsonConvert.SerializeObject(zones);
                    socket.Send("zones.list|" + json);
                }
            }
            else if (msg.StartsWith("editor.zones.delete"))
            {
                string[] arguments = msg.Split("|");
                using (Database DataContext = new Database(Program.Configuration["database:connection-string"]))
                {
                    Zone zone = new Zone {
                        ZoneId = arguments[1]
                    };
                    DataContext.Zones.Attach(zone);
                    DataContext.Zones.Remove(zone);
                    DataContext.SaveChanges();
                }
                socket.Send("zone.deleted");
                //TODO: Reload zones
            }
            else if (msg.StartsWith("editor.zones.new"))
            {
                string[] arguments   = msg.Split("|");
                int      newZoneSize = 0;
                Int32.TryParse(arguments[2], out newZoneSize);

                using (Database DataContext = new Database(Program.Configuration["database:connection-string"]))
                {
                    Zone zone = new Zone(arguments[1], newZoneSize);
                    DataContext.Zones.Add(zone);
                    DataContext.SaveChanges();
                }
                socket.Send("zone.created");
                //TODO: Reload zones
            }
            else if (msg.StartsWith("editor.zone.map.new"))
            {
                string[] arguments   = msg.Split("|");
                int      rowIndex    = 0;
                int      columnIndex = 0;
                string   newMapJson  = "";
                Int32.TryParse(arguments[2], out rowIndex);
                Int32.TryParse(arguments[3], out columnIndex);

                using (Database DataContext = new Database(Program.Configuration["database:connection-string"]))
                {
                    Zone zone = DataContext.Zones.SingleOrDefault(z => z.ZoneId == arguments[1]);
                    Map  m    = new Map(arguments[4], 32);
                    DataContext.Maps.Add(m);
                    DataContext.SaveChanges();
                    zone.SetMapAt(m, rowIndex, columnIndex);
                    DataContext.SaveChanges();
                    newMapJson = JsonConvert.SerializeObject(m);
                }
                socket.Send("map.created|" + newMapJson);
                //TODO: Reload zones and maps
            }
            else if (msg.StartsWith("editor.map.update"))
            {
                string[] arguments = msg.Split("|");
                Map      m         = JsonConvert.DeserializeObject <Map>(arguments[1]);
                Console.WriteLine(m.ToString());
                Console.WriteLine(m.Name);
                using (Database DataContext = new Database(Program.Configuration["database:connection-string"]))
                {
                    Map oldMap = DataContext.Maps.Find(m.MapId);
                    DataContext.Entry(oldMap).CurrentValues.SetValues(m);
                    DataContext.SaveChanges();
                }
                socket.Send("server.map.updated");
            }
            else if (msg.StartsWith("client.socket.auth"))
            {
                //Disables login while on edition mode.
                if (Program.EditionMode)
                {
                    socket.Send("server.socket.notok");
                    return;
                }

                string[] arguments = msg.Split("|");
                string   username  = arguments[1];
                string   token     = arguments[2];
                using (Database DataContext = new Database(Program.Configuration["database:connection-string"]))
                {
                    Account a = DataContext.Accounts.Include("Characters.Class").SingleOrDefault(acc => acc.Username == username);
                    if (a == null)
                    {
                        socket.Send("server.socket.error");
                    }
                    else if (a.Token.Equals(token))
                    {
                        AuthSocket duplicated = null;
                        foreach (AuthSocket asock in Network.AuthedConnections)
                        {
                            if (asock.Account.AccountId.Equals(a.AccountId))
                            {
                                if (!asock.Socket.IsAvailable)
                                {
                                    duplicated = asock;
                                    Network.AuthedConnections.Remove(duplicated);
                                }
                                socket.Send("server.socket.error");
                                return;

                                /*asock.Socket.Close();
                                 * duplicated = asock;*/
                            }
                        }
                        GameSocket duplicatedG = null;
                        foreach (GameSocket gs in Network.GamingConnections)
                        {
                            if (gs.Account.AccountId.Equals(a.AccountId))
                            {
                                if (!gs.Socket.IsAvailable)
                                {
                                    duplicatedG = gs;
                                    Network.GamingConnections.Remove(duplicatedG);
                                }
                                socket.Send("server.socket.error");
                                return;
                            }
                        }
                        Network.Connections.Remove(socket);
                        Network.AuthedConnections.Add(new AuthSocket(a, socket));
                        socket.Send("server.socket.ok");
                    }
                }
            }
            else if (msg.StartsWith("editor.class.list"))
            {
                using (Database DataContext = new Database(Program.Configuration["database:connection-string"]))
                {
                    List <Class> classes = DataContext.Classes.ToList();
                    socket.Send("server.class.list|" + JsonConvert.SerializeObject(classes));
                }
            }
            else if (msg.StartsWith("editor.class.create"))
            {
                using (Database DataContext = new Database(Program.Configuration["database:connection-string"]))
                {
                    string[] arguments = msg.Split("|");
                    Class    c         = new Class(arguments[1], Int32.Parse(arguments[2]), Int32.Parse(arguments[3]), arguments[4], "Code this part");
                    DataContext.Classes.Add(c);
                    DataContext.SaveChanges();
                    socket.Send("server.class.created");
                }
            }
            else if (msg.StartsWith("editor.class.update"))
            {
                using (Database DataContext = new Database(Program.Configuration["database:connection-string"]))
                {
                    string[] arguments = msg.Split("|");
                    Class    c         = new Class(arguments[2], Int32.Parse(arguments[3]), Int32.Parse(arguments[4]), arguments[5], "Update:Code this part");
                    c.ClassId = arguments[1];
                    Class oldClass = DataContext.Classes.Find(c.ClassId);
                    DataContext.Entry(oldClass).CurrentValues.SetValues(c);
                    DataContext.SaveChanges();
                    socket.Send("server.class.updated");
                }
            }
        }
Пример #3
0
        static public void OnMessage(AuthSocket authSocket, string msg)
        {
            Log.Information("AuthSocket > " + msg + " from host " + authSocket.Socket.ConnectionInfo.ClientIpAddress);
            if (msg.StartsWith("client.request.account"))
            {
                authSocket.Socket.Send("server.account.info|" + JsonConvert.SerializeObject(authSocket.Account));
            }
            else if (msg.StartsWith("client.request.classes"))
            {
                using (Database DataContext = new Database(Program.Configuration["database:connection-string"]))
                {
                    List <Class> classes = DataContext.Classes.ToList();
                    authSocket.Socket.Send("server.game.classes|" + JsonConvert.SerializeObject(classes));
                }
            }
            else if (msg.StartsWith("client.character.create"))
            {
                using (Database DataContext = new Database(Program.Configuration["database:connection-string"]))
                {
                    string[] arguments = msg.Split("|");
                    string   charName  = arguments[1];
                    string   classId   = arguments[2];

                    //Checks
                    Character tmp = DataContext.Characters.FirstOrDefault(chr => chr.Name == charName);
                    if (tmp != null)
                    {
                        authSocket.Socket.Send("server.character.notcreated");
                        return;
                    }
                    Character c   = new Character(charName, DataContext.Classes.Find(classId));
                    Account   acc = DataContext.Accounts.Include("Characters.Class").SingleOrDefault(ax => ax.AccountId == authSocket.Account.AccountId);
                    acc.Characters.Add(c); //Required to update asap
                    authSocket.Account.Characters.Add(c);
                    DataContext.SaveChanges();
                    authSocket.Socket.Send("server.character.created");
                }
            }
            else if (msg.StartsWith("client.character.delete"))
            {
                using (Database DataContext = new Database(Program.Configuration["database:connection-string"]))
                {
                    string[] arguments = msg.Split("|");
                    string   charId    = arguments[1];

                    Account   acc = DataContext.Accounts.Include("Characters.Class").SingleOrDefault(ax => ax.AccountId == authSocket.Account.AccountId);
                    Character chr = acc.Characters.FirstOrDefault(ch => ch.CharacterId == charId);
                    authSocket.Account.Characters.Remove(authSocket.Account.Characters.FirstOrDefault(ch => ch.CharacterId == charId));
                    acc.Characters.Remove(chr);
                    DataContext.Characters.Remove(chr);
                    DataContext.SaveChanges();
                    authSocket.Socket.Send("server.character.deleted");
                }
            }
            else if (msg.StartsWith("client.character.select"))
            {
                using (Database DataContext = new Database(Program.Configuration["database:connection-string"]))
                {
                    string[] arguments = msg.Split("|");
                    string   charId    = arguments[1];

                    Character chr = authSocket.Account.Characters.SingleOrDefault(cx => cx.CharacterId == charId);
                    if (chr == null)
                    {
                        authSocket.Socket.Send("server.character.selectionerror");
                        return;
                    }
                    //Socket upgrade
                    GameSocket gsock = new GameSocket(authSocket.Account, authSocket.Socket, chr);
                    Network.AuthedConnections.Remove(authSocket);
                    Network.GamingConnections.Add(gsock);
                    gsock.Socket.Send("server.character.selected");
                    AddCharacterToSimulation(chr);
                }
            }
        }
        static public void OnMessage(GameSocket gameSocket, string msg)
        {
            //Log.Information("GameSocket > " + msg + " from host " + gameSocket.Socket.ConnectionInfo.ClientIpAddress);
            if (msg.StartsWith("client.char.move"))
            {
                string[]    arguments = msg.Split("|");
                int         dirValue  = Convert.ToInt32(arguments[1]);
                AABB.Movdir dir       = AABB.Movdir.TOP;
                switch (dirValue)
                {
                case 0:
                    gameSocket.SelectedCharacter.movementDirection = MovingDirection.Top;
                    dir = AABB.Movdir.TOP;
                    break;

                case 2:
                    gameSocket.SelectedCharacter.movementDirection = MovingDirection.Right;
                    dir = AABB.Movdir.RIGHT;
                    break;

                case 4:
                    gameSocket.SelectedCharacter.movementDirection = MovingDirection.Down;
                    dir = AABB.Movdir.DOWN;
                    break;

                case 6:
                    gameSocket.SelectedCharacter.movementDirection = MovingDirection.Left;
                    dir = AABB.Movdir.LEFT;
                    break;

                default:
                    break;
                }
                gameSocket.SelectedCharacter.BoundingBox.SetMovement(dir);
            }
            else if (msg.StartsWith("client.char.stop"))
            {
                gameSocket.SelectedCharacter.movementDirection = MovingDirection.Stop;
                gameSocket.SelectedCharacter.BoundingBox.StopMovement();
            }
            else if (msg.StartsWith("client.chat.message"))
            {
                string[] arguments = msg.Split("|");
                string   m         = arguments[1];
                string   message   = gameSocket.SelectedCharacter.Name + ": " + m;
                foreach (GameSocket gs in Network.GamingConnections)
                {
                    gs.Socket.Send("server.chat.message|" + message);
                }
            }
            else if (msg.StartsWith("client.map.chonmap"))
            {
                SimulationMap sm = Program.SimulatedMaps.FirstOrDefault(smm => smm.Map.MapId == gameSocket.SelectedCharacter.CurrentMapId);
                if (sm != null)
                {
                    sm.GetOtherCharsOnMap(gameSocket.SelectedCharacter);
                }
            }
            else if (msg.StartsWith("client.cast.spell1"))
            {
                SimulationMap sm = Program.SimulatedMaps.FirstOrDefault(smm => smm.Map.MapId == gameSocket.SelectedCharacter.CurrentMapId);
                if (sm != null)
                {
                    gameSocket.SelectedCharacter.CurrentResource -= 5;
                    string[] arguments = msg.Split("|");
                    sm.AddProjectile(gameSocket.SelectedCharacter, Convert.ToInt32(arguments[1]), Convert.ToInt32(arguments[2]), 4, 10, 23, 10, false);
                }
            }
            else if (msg.StartsWith("client.cast.spell2"))
            {
                SimulationMap sm = Program.SimulatedMaps.FirstOrDefault(smm => smm.Map.MapId == gameSocket.SelectedCharacter.CurrentMapId);

                if (sm != null && gameSocket.SelectedCharacter.CurrentResource > 20)
                {
                    gameSocket.SelectedCharacter.CurrentResource -= 20;
                    gameSocket.SelectedCharacter.isShielded       = true;
                    sm.NotifyAllInMap("csh|" + gameSocket.SelectedCharacter.CharacterId);
                    new Thread(new ThreadStart(() => {
                        Thread.Sleep(3000);
                        try
                        {
                            gameSocket.SelectedCharacter.isShielded = false;
                            sm.NotifyAllInMap("cnsh|" + gameSocket.SelectedCharacter.CharacterId);
                        }
                        catch (Exception e) {
                            Console.WriteLine("Couldn't find entity to stop spell2");
                        }
                    })).Start();
                }
            }
            else if (msg.StartsWith("client.cast.spell3"))
            {
                SimulationMap sm = Program.SimulatedMaps.FirstOrDefault(smm => smm.Map.MapId == gameSocket.SelectedCharacter.CurrentMapId);
                if (sm != null)
                {
                    gameSocket.SelectedCharacter.CurrentResource -= 50;
                    string[] arguments = msg.Split("|");
                    sm.AddProjectile(gameSocket.SelectedCharacter, Convert.ToInt32(arguments[1]), Convert.ToInt32(arguments[2]), 1, 20, 60, 5, true);
                }
            }
            else
            {
                Log.Error("Un-handled command from client " + msg);
            }
        }