public void HandleClientUsername(ByteReader data) { if (connection.username != null && connection.username.Length != 0) { return; } string username = data.ReadString(); if (username.Length < 3 || username.Length > 15) { Player.Disconnect(MpDisconnectReason.UsernameLength); return; } if (!Player.IsArbiter && !UsernamePattern.IsMatch(username)) { Player.Disconnect(MpDisconnectReason.UsernameChars); return; } if (Server.GetPlayer(username) != null) { Player.Disconnect(MpDisconnectReason.UsernameAlreadyOnline); return; } connection.username = username; Server.SendNotification("MpPlayerConnected", Player.Username); Server.SendChat($"{Player.Username} has joined."); if (!Player.IsArbiter) { if (!givenColors.TryGetValue(username, out ColorRGB color)) { givenColors[username] = color = PlayerColors[givenColors.Count % PlayerColors.Length]; } Player.color = color; } var writer = new ByteWriter(); writer.WriteByte((byte)PlayerListAction.Add); writer.WriteRaw(Player.SerializePlayerInfo()); Server.SendToAll(Packets.Server_PlayerList, writer.ToArray()); SendWorldData(); }
public void SendPlayerList() { var writer = new ByteWriter(); writer.WriteByte((byte)PlayerListAction.List); writer.WriteInt32(Server.PlayingPlayers.Count()); foreach (var player in Server.PlayingPlayers) { writer.WriteRaw(player.SerializePlayerInfo()); } conn.Send(Packets.Server_PlayerList, writer.ToArray()); }
public void HandleClientUsername(ByteReader data) { if (connection.username != null && connection.username.Length != 0) { return; } string username = data.ReadString(); if (username.Length < 3 || username.Length > 15) { Player.Disconnect("MpInvalidUsernameLength"); return; } if (!Player.IsArbiter && !UsernamePattern.IsMatch(username)) { Player.Disconnect("MpInvalidUsernameChars"); return; } if (Server.GetPlayer(username) != null) { Player.Disconnect("MpInvalidUsernameAlreadyPlaying"); return; } connection.username = username; Server.SendNotification("MpPlayerConnected", Player.Username); Server.SendChat($"{Player.Username} has joined."); var writer = new ByteWriter(); writer.WriteByte((byte)PlayerListAction.Add); writer.WriteRaw(Player.SerializePlayerInfo()); Server.SendToAll(Packets.Server_PlayerList, writer.GetArray()); }