Esempio n. 1
0
    static void UpdateClientInfo(UdpClient client, uint pid, string name)
    {
        NetWerewolfPlayer player = ConnectedPlayers.FirstOrDefault(p => p.PlayerID == pid);

        if ((player == null) || (player.PlayerID != LocalPlayer.PlayerID))
        {
            player = new NetWerewolfPlayer(pid, name);
            if (ServerInstance == null)
            {
                ConnectedPlayers.Add(player);
            }
            UpdatePlayerList.Invoke(player.PlayerID, false);
        }
        else
        {
            player.Name = name;
        }
    }
Esempio n. 2
0
 Player GetPlayerFromSocket(IWebSocketConnection socket)
 {
     return(ConnectedPlayers.FirstOrDefault(x => x.Socket == socket));
 }
Esempio n. 3
0
        private void newLogin()
        {
            //When a player logs in, we expect that he sends his character info to us.
            //Normally he would only send his username and password but because we aren't using a database model
            //We'll just create the users character in their own client. Maybe database example next time
            //FYI: The connecting player can manually specify the zone he wants to connect to, easily changed by just setting the property though

            //Create new instance of player then read all the properties the client has sent into this class
            Character newlyConnectedPlayer = new Character();

            inc.ReadAllProperties(newlyConnectedPlayer);

            //If the player that is trying to connects' name already exists in our active player list
            //We will refuse his connection. This can be handy, if you want to allow only one character (obviously) or one
            //connection per IP Address, easy to modify
            if (ConnectedPlayers.FirstOrDefault(f => f.Name == newlyConnectedPlayer.Name) != null)
            {
                inc.SenderConnection.Deny("You're already connected");
                Write("Refused player with name " + newlyConnectedPlayer.Name + " because he was already connected.."); // LET IT BE KNOWN!
                return;
            }

            //We give our player a connection property so we can keep connection info about him, like IP Address/ping, etc!
            newlyConnectedPlayer.Connection = inc.SenderConnection;

            //Let it be known that this player has connected to our wonderful game
            Write(newlyConnectedPlayer.Name + " has connected!");

            //Approve this fine gentleman into our secret society
            inc.SenderConnection.Approve();

            //Add this fine lad to the list of connected players
            ConnectedPlayers.Add(newlyConnectedPlayer);


            //Now it gets a little more interesting!
            //*~*~*~*~* (SPARKLES TO MAKE IT MORE MAGICAL!) *~*~*~*~*

            //Worldstate (Can specify a name yourself) messages send the current zone state to players

            NetOutgoingMessage outmsg = Server.CreateMessage(); //We create a new message

            outmsg.Write((byte)PacketTypes.WORLDSTATE);         // Of type WORLDSTATE

            //Get the amount of players the zone the new player is in
            outmsg.Write(ConnectedPlayers.Where(f => f.CurrentZone == newlyConnectedPlayer.CurrentZone).Count()); //notice 'Count'

            //For each player in this players' zone, send him the data of the players. (The players' client will process this info and draw them on his screen)
            foreach (Character ch in ConnectedPlayers.Where(x => x.CurrentZone == newlyConnectedPlayer.CurrentZone))
            {
                outmsg.WriteAllProperties(ch);
            }
            Server.SendMessage(outmsg, inc.SenderConnection, NetDeliveryMethod.ReliableOrdered, 0); // Send the message, reliably and ordered

            // LET IT BE KNOWN!
            Write(String.Format("{0} : {1}  - has connected to the server and is in {2}", newlyConnectedPlayer.Name,
                                newlyConnectedPlayer.Connection.RemoteEndpoint.Address,
                                newlyConnectedPlayer.CurrentZone));

            //Update our UI
            updateList();
        }