public static async void Login(MewPewSocket socket, Message[] parameters)
        {
            string mail = Message.StringValueOfKey(parameters, "mail");
            string pass = Message.StringValueOfKey(parameters, "pass");

            if (string.IsNullOrWhiteSpace(mail) || string.IsNullOrEmpty(mail) || string.IsNullOrWhiteSpace(pass) || string.IsNullOrEmpty(pass))
            {
                socket.SendData("loginfailed?err=data", true);
                return;
            }

            object[] query = await SelectOne("password", "users", $"mail = '{mail}'", 1);

            if (query.Length == 0 || (string)query[0] != pass)
            {
                socket.SendData("loginfailed?err=auth", true);
                return;
            }

            long joinedAt = -1;

            query = await SelectOne("joinedat", "mewpew", $"mail = '{mail}'", 1);

            if (query.Length > 0 && query[0] != null)
            {
                joinedAt = (long)query[0];
            }

            string res     = "loginsuccess?firsttime=";
            User   newUser = new User(socket, mail);
            bool   success;

            if (joinedAt == -1)
            {
                res += "true";

                success = await InsertOne("mewpew", "(id, mail, joinedat, credits, ship, inventory, location)",
                                          $"('{newUser.Id}', '{mail}', {DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}, {10000}, '', '', 'Alpha Centauri')");
            }
            else
            {
                res += "false";

                query = await SelectOne("id, credits, ship, inventory", "mewpew", $"mail = '{mail}'", 4);

                if (query.Length > 0)
                {
                    newUser.Id        = (string)query[0];
                    newUser.Credits   = (int)query[1];
                    newUser.Ship      = JsonConvert.DeserializeObject <PlayerShip>((string)query[2]);
                    newUser.Inventory = JsonConvert.DeserializeObject <Item[]>((string)query[3]);
                }
            }

            success = await UpdateOne("mewpew", "active=true", $"id = '{newUser.Id}'");

            socket.UserData = newUser;
            Program.activeUsers.Add(newUser);
            socket.SendData(res, true);
        }
Beispiel #2
0
        public async static Task GetDataUdp()
        {
            string ip   = "unknown";
            int    port = -1;

            Byte[] res = new Byte[256];
            try
            {
                res = udp.Receive(ref generalEndpoint);
                string msg = Encoding.UTF8.GetString(res);
                ip   = generalEndpoint.Address.ToString();
                port = generalEndpoint.Port;
                MewPewSocket socket = sockets.Find(element => element.IP == ip);
                if (socket == null)
                {
                    return;
                }

                socket.Port = port;
                Console.WriteLine($"Data Recieved: {msg} from {ip}:{port} via UDP");
                Task.Run(() => UDPRouter.Route(socket, msg));
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error on UDP socket {ip}:{port} - {e}");
            }
        }
        public static async void SelectShip(MewPewSocket socket, Message[] parameters)
        {
            int shipIdx = Message.IntValueOfKey(parameters, "idx");

            socket.UserData.Ship = new Sparrow(socket.UserData.Id);
            bool success = await UpdateOne("mewpew", $"ship = '{JsonConvert.SerializeObject(socket.UserData.Ship)}'", $"mail = '{socket.UserData.Username}'");
        }
Beispiel #4
0
        public void SendScene(MewPewSocket socket)
        {
            foreach (SceneObject element in Scenery)
            {
            }

            foreach (User element in Players)
            {
                if (element.Id == socket.UserData.Id)
                {
                    continue;
                }

                if (element.Status != "Roam")
                {
                    continue;
                }

                socket.SendData($"spawnnonplayer?" +
                                $"map={Name}" +
                                $"&id={element.Id}" +
                                $"&mesh={element.Ship.Mesh}" +
                                $"&pos={Message.ParseVector3(element.pos)}" +
                                $"&rot={Message.ParseVector3(element.rot)}" +
                                $"&vel={Message.ParseVector3(element.vel)}" +
                                $"&avel={Message.ParseVector3(element.aVel)}");
            }

            socket.SendData($"readytospawn?map={Name}");
        }
Beispiel #5
0
        public static void Route(MewPewSocket socket, string msg)
        {
            string[] msgParts = msg.Split('?');

            Message[] parameters = new Message[0];
            if (msgParts.Length > 1 && msgParts[1] != "")
            {
                parameters = Message.Parse(msgParts[1]);
            }

            string route = msgParts[0];

            switch (route)
            {
            case "launch":
                socket.SendData($"launch?map={socket.UserData.Location}");
                return;

            case "login":
                DBController.Login(socket, parameters);
                return;

            case "selectship":
                DBController.SelectShip(socket, parameters);
                return;

            case "reqmyshipdata":
                socket.SendData($"sendmyshipdata?data={JsonConvert.SerializeObject(socket.UserData.Ship)}");
                return;

            case "maploaded":
                DBController.ChangeMap(socket, parameters);
                return;

            case "disconnect":
                socket.Disconnect();
                return;

            case "shipspawned":
                Program.maps.Find(element => element.Name == socket.UserData.Location).SpawnNewShip(parameters);
                return;

            case "returnpoll":
                socket.Ping = true;
                return;

            default:
                return;
            }
        }
Beispiel #6
0
        static async Task Main(string[] args)
        {
            config = new ConfigurationBuilder()
                     .AddJsonFile("appsettings.json", true, true)
                     .Build();

            IPAddress ipAddress = IPAddress.Parse(config["ip"]);
            int       tcPort    = int.Parse(config["tcPort"]);
            int       udPort    = int.Parse(config["udPort"]);

            tcpListener = new TcpListener(ipAddress, tcPort);
            tcpListener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, false);
            tcpListener.Start();

            listening       = true;
            udp             = new UdpClient(udPort);
            generalEndpoint = new IPEndPoint(IPAddress.Any, udPort);
            sockets         = new List <MewPewSocket>();
            activeUsers     = new List <User>();
            maps            = new List <Map>();
            await DBController.InitDb();

            maps.Add(new Map("Alpha Centauri"));

            Console.WriteLine($"Server listening on {ipAddress}:{tcPort} in {env} mode");

            await Task.Run(async() => {
                while (listening)
                {
                    if (tcpListener.Pending())
                    {
                        MewPewSocket socket = new MewPewSocket(tcpListener.AcceptTcpClient());
                        sockets.Add(socket);
                    }

                    if (udp.Available > 0)
                    {
                        await Task.Run(GetDataUdp);
                    }

                    Task.Delay(10);
                }
            });

            tcpListener.Stop();
        }
        public static async void ChangeMap(MewPewSocket socket, Message[] parameters)
        {
            string status  = Message.StringValueOfKey(parameters, "status");
            string mapName = Message.StringValueOfKey(parameters, "map");
            bool   success = await UpdateOne("mewpew", $"location='{mapName}'", $"id = '{socket.UserData.Id}'");

            Map oldMap = Program.maps.Find(element => element.Name == socket.UserData.Location);

            oldMap.Players.Remove(socket.UserData);

            socket.UserData.Location = mapName;

            Map mapItem = Program.maps.Find(element => element.Name == mapName);

            mapItem.Players.Add(socket.UserData);
            socket.UserData.Status = status;
            mapItem.SendScene(socket);
        }
Beispiel #8
0
 public User(MewPewSocket socket, string username, string id = null)
 {
     Active    = true;
     Status    = "Hangar";
     Location  = "Alpha Centauri";
     Username  = username;
     Credits   = 10000;
     Socket    = socket;
     Inventory = new Item[100];
     if (id == null)
     {
         Id = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
         Id = Id.Replace("==", String.Empty);
     }
     else
     {
         Id = id;
     }
 }
Beispiel #9
0
        public static void Route(MewPewSocket socket, string msg)
        {
            string[] msgParts = msg.Split('?');

            Message[] parameters = new Message[0];
            if (msgParts.Length > 1 && msgParts[1] != "")
            {
                parameters = Message.Parse(msgParts[1]);
            }

            string route = msgParts[0];

            switch (route)
            {
            case "usp":
                Program.maps.Find(element => element.Name == socket.UserData.Location).UpdateShip(parameters);
                return;

            default:
                return;
            }
        }