Example #1
0
        public void AddPlayer(NetConnection connection, PlayerInformation playerInfo)
        {
            String denyReason;
            Byte slot = AllocateSlot(playerInfo, out denyReason);

            // could not allocate the player if AllocateSlot returns DummySlot
            if (slot == ProtocolInformation.DummySlot)
            {
                Log.InfoFormat("Player \"{0}\" from {1} tried to join, but was rejected ({2}).",
                               playerInfo.Callsign, connection, denyReason);
                connection.Deny(denyReason);
                return;
            }

            // we can now approve the player if we get here
            connection.Approve();

            // add player to our list
            players[slot] = new Player(this, slot, connection, playerInfo);

            // and tell everyone else about this awesome new player
            Log.DebugFormat("Sending MsgAddPlayer to everyone else about player #{0}", slot);

            NetOutgoingMessage packet = Server.CreateMessage();

            MsgAddPlayerPacket message = new MsgAddPlayerPacket(players[slot].PlayerInfo, false);

            packet.Write((Byte)message.MsgType);
            message.Write(packet);

            Log.DebugFormat("MsgAddPlayer Compiled ({0} bytes) and being sent to {1} recipients",
                            packet.LengthBytes, players.Count - 1);

            // send to everyone except our new player, we let Player itself decide when to send the state to the new guy
            Server.SendToAll(packet, connection, NetDeliveryMethod.ReliableOrdered, 0);
        }
Example #2
0
        public void RemovePlayer(Player player, String reason)
        {
            Log.InfoFormat("Removing player #{0} ({1})", player.Slot, reason);

            // nuke player from the dictionary
            players.Remove(player.Slot);

            // now let's tell all the other players the dude left
            NetOutgoingMessage packet = Server.CreateMessage();

            MsgRemovePlayerPacket message =
                new MsgRemovePlayerPacket(player.Slot, reason);

            packet.Write((Byte)message.MsgType);
            message.Write(packet);

            // send to all
            Server.SendToAll(packet, null, NetDeliveryMethod.ReliableOrdered, 0);
            
            // disposing of player would be a good idea
            if (player.Connection != null)
                player.Connection.Disconnect(reason);
        }