Removed more networking code

This commit is contained in:
juanjp600
2016-08-30 21:30:31 -03:00
parent 7bdcc51bae
commit 55a2db7c70
5 changed files with 10 additions and 629 deletions

View File

@@ -340,126 +340,7 @@ namespace Barotrauma.Networking
//TODO: read message data
}
}
private IEnumerable<object> StartGame(NetIncomingMessage inc)
{
if (Character != null) Character.Remove();
endVoteTickBox.Selected = false;
int seed = inc.ReadInt32();
string levelSeed = inc.ReadString();
int missionTypeIndex = inc.ReadByte();
string subName = inc.ReadString();
string subHash = inc.ReadString();
string shuttleName = inc.ReadString();
string shuttleHash = inc.ReadString();
string modeName = inc.ReadString();
bool respawnAllowed = inc.ReadBoolean();
GameModePreset gameMode = GameModePreset.list.Find(gm => gm.Name == modeName);
if (gameMode == null)
{
DebugConsole.ThrowError("Game mode ''" + modeName + "'' not found!");
yield return CoroutineStatus.Success;
}
if (!GameMain.NetLobbyScreen.TrySelectSub(subName, subHash, GameMain.NetLobbyScreen.SubList))
{
yield return CoroutineStatus.Success;
}
if (!GameMain.NetLobbyScreen.TrySelectSub(shuttleName, shuttleHash, GameMain.NetLobbyScreen.ShuttleList.ListBox))
{
yield return CoroutineStatus.Success;
}
Rand.SetSyncedSeed(seed);
//int gameModeIndex = inc.ReadInt32();
GameMain.GameSession = new GameSession(GameMain.NetLobbyScreen.SelectedSub, "", gameMode, Mission.MissionTypes[missionTypeIndex]);
GameMain.GameSession.StartShift(levelSeed);
if (respawnAllowed) respawnManager = new RespawnManager(this, GameMain.NetLobbyScreen.SelectedShuttle);
//myCharacter = ReadCharacterData(inc);
//Character.Controlled = myCharacter;
List<Character> crew = new List<Character>();
byte characterCount = inc.ReadByte();
for (int i = 0; i < characterCount; i++)
{
ReadCharacterData(inc);
}
gameStarted = true;
endVoteTickBox.Visible = Voting.AllowEndVoting && myCharacter != null;
GameMain.GameScreen.Select();
AddChatMessage("Press TAB to chat. Use ''r;'' to talk through the radio.", ChatMessageType.Server);
//GameMain.GameSession.CrewManager.CreateCrewFrame(crew);
yield return CoroutineStatus.Success;
}
public IEnumerable<object> EndGame(string endMessage)
{
if (!gameStarted) yield return CoroutineStatus.Success;
if (GameMain.GameSession != null) GameMain.GameSession.gameMode.End(endMessage);
//var messageBox = new GUIMessageBox("The round has ended", endMessage, 400, 300);
gameStarted = false;
Character.Controlled = null;
GameMain.GameScreen.Cam.TargetPos = Vector2.Zero;
GameMain.LightManager.LosEnabled = false;
respawnManager = null;
float endPreviewLength = 10.0f;
if (Screen.Selected == GameMain.GameScreen)
{
var cinematic = new TransitionCinematic(Submarine.MainSub, GameMain.GameScreen.Cam, endPreviewLength);
float secondsLeft = endPreviewLength;
do
{
secondsLeft -= CoroutineManager.UnscaledDeltaTime;
yield return CoroutineStatus.Running;
} while (secondsLeft > 0.0f);
}
Submarine.Unload();
GameMain.NetLobbyScreen.Select();
myCharacter = null;
foreach (Client c in otherClients)
{
c.Character = null;
}
yield return CoroutineStatus.Success;
}
public bool HasPermission(ClientPermissions permission)
{
return false;// permissions.HasFlag(permission);
@@ -556,41 +437,13 @@ namespace Barotrauma.Networking
if (votedClient == null) return false;
Vote(VoteType.Kick, votedClient);
//Vote(VoteType.Kick, votedClient);
button.Enabled = false;
return true;
}
public void Vote(VoteType voteType, object userData)
{
NetOutgoingMessage msg = client.CreateMessage();
msg.Write((byte)voteType);
switch (voteType)
{
case VoteType.Sub:
msg.Write(((Submarine)userData).Name);
break;
case VoteType.Mode:
msg.Write(((GameModePreset)userData).Name);
break;
case VoteType.EndRound:
msg.Write((bool)userData);
break;
case VoteType.Kick:
Client votedClient = userData as Client;
if (votedClient == null) return;
msg.Write(votedClient.ID);
break;
}
client.SendMessage(msg, NetDeliveryMethod.ReliableUnordered);
}
public bool SpectateClicked(GUIButton button, object userData)
{
NetOutgoingMessage msg = client.CreateMessage();
@@ -613,129 +466,11 @@ namespace Barotrauma.Networking
return false;
}
Vote(VoteType.EndRound, tickBox.Selected);
//Vote(VoteType.EndRound, tickBox.Selected);
return false;
}
public void SendCharacterData()
{
if (characterInfo == null) return;
NetOutgoingMessage msg = client.CreateMessage();
msg.Write(characterInfo.Name);
msg.Write(characterInfo.Gender == Gender.Male);
msg.Write((byte)characterInfo.HeadSpriteId);
var jobPreferences = GameMain.NetLobbyScreen.JobPreferences;
int count = Math.Min(jobPreferences.Count, 3);
msg.Write((byte)count);
for (int i = 0; i < count; i++ )
{
msg.Write(jobPreferences[i].Name);
}
client.SendMessage(msg, NetDeliveryMethod.ReliableUnordered);
}
public Character ReadCharacterData(NetIncomingMessage inc)
{
bool noInfo = inc.ReadBoolean();
ushort id = inc.ReadUInt16();
string configPath = inc.ReadString();
Vector2 position = new Vector2(inc.ReadFloat(), inc.ReadFloat());
bool enabled = inc.ReadBoolean();
Character character = null;
if (noInfo)
{
var existingEntity = Entity.FindEntityByID(id);
if (existingEntity is AICharacter && existingEntity.ID == id)
{
return (Character)existingEntity;
}
character = Character.Create(configPath, position, null, true);
character.ID = id;
}
else
{
bool hasOwner = inc.ReadBoolean();
int ownerId = -1;
if (hasOwner)
{
ownerId = inc.ReadByte();
}
string newName = inc.ReadString();
bool hasAi = inc.ReadBoolean();
bool isFemale = inc.ReadBoolean();
int headSpriteID = inc.ReadByte();
string jobName = inc.ReadString();
JobPrefab jobPrefab = JobPrefab.List.Find(jp => jp.Name == jobName);
CharacterInfo ch = new CharacterInfo(configPath, newName, isFemale ? Gender.Female : Gender.Male, jobPrefab);
ch.HeadSpriteId = headSpriteID;
character = Character.Create(configPath, position, ch, ownerId != myID, hasAi);
character.ID = id;
if (configPath == Character.HumanConfigFile)
{
GameMain.GameSession.CrewManager.characters.Add(character);
}
if (ownerId == myID)
{
myCharacter = character;
Character.Controlled = character;
GameMain.LightManager.LosEnabled = true;
if (endVoteTickBox != null) endVoteTickBox.Visible = Voting.AllowEndVoting;
}
else
{
var characterOwner = otherClients.Find(c => c.ID == ownerId);
if (characterOwner != null) characterOwner.Character = character;
}
}
character.Enabled = enabled;
return character;
}
public override void SendChatMessage(string message, ChatMessageType? type = null)
{
if (client.ServerConnection == null) return;
type = ChatMessageType.Default;
if (Screen.Selected == GameMain.GameScreen && (myCharacter == null || myCharacter.IsDead))
{
type = ChatMessageType.Dead;
}
else
{
string command = ChatMessage.GetChatMessageCommand(message, out message).ToLowerInvariant();
if (command=="r" || command=="radio" && CanUseRadio(Character.Controlled)) type = ChatMessageType.Radio;
}
var chatMessage = ChatMessage.Create(
gameStarted && myCharacter != null ? myCharacter.Name : name,
message, (ChatMessageType)type, gameStarted ? myCharacter : null);
}
/// <summary>
/// sends some random data to the server (can be a networkevent or just something completely random)
/// use for debugging purposes

View File

@@ -717,153 +717,6 @@ namespace Barotrauma.Networking
return true;
}
public override void SendChatMessage(string message, ChatMessageType? type = null)
{
List<Client> recipients = new List<Client>();
Client targetClient = null;
if (type == null)
{
type = gameStarted && myCharacter != null ? ChatMessageType.Default : ChatMessageType.Server;
}
string command = ChatMessage.GetChatMessageCommand(message, out message).ToLowerInvariant();
if (command=="dead" || command=="d")
{
type = ChatMessageType.Dead;
}
else if (command=="radio" || command=="r")
{
if (CanUseRadio(Character.Controlled)) type = ChatMessageType.Radio;
}
else if (command != "")
{
targetClient = connectedClients.Find(c =>
command == c.name.ToLowerInvariant() ||
(c.Character != null && command == c.Character.Name.ToLowerInvariant()));
if (targetClient == null)
{
AddChatMessage("Player ''" + command + "'' not found!", ChatMessageType.Error);
return;
}
}
if (targetClient != null)
{
recipients.Add(targetClient);
}
else
{
foreach (Client c in connectedClients)
{
if (type != ChatMessageType.Dead || (c.Character == null || c.Character.IsDead)) recipients.Add(c);
}
}
var chatMessage = ChatMessage.Create(
gameStarted && myCharacter != null ? myCharacter.Name : name,
message, (ChatMessageType)type, gameStarted ? myCharacter : null);
AddChatMessage(chatMessage);
if (!server.Connections.Any()) return;
SendChatMessage(chatMessage, recipients);
}
public void SendChatMessage(ChatMessage chatMessage, Client recipient)
{
}
public void SendChatMessage(ChatMessage chatMessage, List<Client> recipients)
{
}
private void ReadCharacterData(NetIncomingMessage message)
{
Client sender = connectedClients.Find(c => c.Connection == message.SenderConnection);
if (sender == null) return;
string name = "";
Gender gender = Gender.Male;
int headSpriteId = 0;
try
{
name = message.ReadString();
gender = message.ReadBoolean() ? Gender.Male : Gender.Female;
headSpriteId = message.ReadByte();
}
catch
{
name = "";
gender = Gender.Male;
headSpriteId = 0;
}
if (sender.characterInfo != null)
{
//clients can't change their character's name once it's been set
name = sender.characterInfo.Name;
}
List<JobPrefab> jobPreferences = new List<JobPrefab>();
int count = message.ReadByte();
for (int i = 0; i < Math.Min(count, 3); i++)
{
string jobName = message.ReadString();
JobPrefab jobPrefab = JobPrefab.List.Find(jp => jp.Name == jobName);
if (jobPrefab != null) jobPreferences.Add(jobPrefab);
}
sender.characterInfo = new CharacterInfo(Character.HumanConfigFile, name, gender);
sender.characterInfo.HeadSpriteId = headSpriteId;
sender.jobPreferences = jobPreferences;
}
public void WriteCharacterData(NetOutgoingMessage msg, string name, Character c)
{
msg.Write(c.Info == null);
msg.Write(c.ID);
msg.Write(c.ConfigPath);
msg.Write(c.WorldPosition.X);
msg.Write(c.WorldPosition.Y);
msg.Write(c.Enabled);
if (c.Info != null)
{
Client client = connectedClients.Find(cl => cl.Character == c);
if (client != null)
{
msg.Write(true);
msg.Write(client.ID);
}
else if (myCharacter == c)
{
msg.Write(true);
msg.Write((byte)0);
}
else
{
msg.Write(false);
}
msg.Write(name);
msg.Write(c is AICharacter);
msg.Write(c.Info.Gender == Gender.Female);
msg.Write((byte)c.Info.HeadSpriteId);
msg.Write(c.Info.Job == null ? "" : c.Info.Job.Name);
}
}
public void AssignJobs(List<Client> unassigned)
{

View File

@@ -288,7 +288,7 @@ namespace Barotrauma.Networking
shuttleSteering.TargetVelocity = Vector2.Zero;
server.SendChatMessage(ChatMessage.Create("", "Transportation shuttle dispatched", ChatMessageType.Server, null), server.ConnectedClients);
//server.SendChatMessage(ChatMessage.Create("", "Transportation shuttle dispatched", ChatMessageType.Server, null), server.ConnectedClients);
RespawnCharacters();

View File

@@ -718,7 +718,7 @@ namespace Barotrauma
return false;
}
GameMain.Client.Vote(voteType, userData);
//GameMain.Client.Vote(voteType, userData);
return true;
}
@@ -1014,7 +1014,6 @@ namespace Barotrauma
if (GameMain.NetworkMember.CharacterInfo == null) return true;
GameMain.NetworkMember.CharacterInfo.HeadSpriteId += dir;
if (GameMain.Client != null) GameMain.Client.SendCharacterData();
UpdatePreviewPlayer(GameMain.NetworkMember.CharacterInfo);
@@ -1025,7 +1024,6 @@ namespace Barotrauma
{
Gender gender = (Gender)obj;
GameMain.NetworkMember.CharacterInfo.Gender = gender;
if (GameMain.Client != null) GameMain.Client.SendCharacterData();
UpdatePreviewPlayer(GameMain.NetworkMember.CharacterInfo);
return true;
@@ -1137,8 +1135,7 @@ namespace Barotrauma
(listBox.children[i] as GUITextBlock).Text = (i+1) + ". " + (listBox.children[i].UserData as JobPrefab).Name;
}
if (GameMain.Client!=null) GameMain.Client.SendCharacterData();
}
public bool TrySelectSub(string subName, string md5Hash, GUIListBox subList)
@@ -1201,128 +1198,6 @@ namespace Barotrauma
return true;
}
public void WriteData(NetOutgoingMessage msg)
{
Submarine selectedSub = subList.SelectedData as Submarine;
if (selectedSub==null)
{
msg.Write("");
msg.Write("");
}
else
{
msg.Write(Path.GetFileName(selectedSub.Name));
msg.Write(selectedSub.MD5Hash.Hash);
}
if (SelectedShuttle == null)
{
msg.Write("");
msg.Write("");
}
else
{
msg.Write(Path.GetFileName(SelectedShuttle.Name));
msg.Write(SelectedShuttle.MD5Hash.Hash);
}
msg.Write(ServerName);
msg.Write(serverMessage.Text);
msg.WriteRangedInteger(0, 2, (int)GameMain.Server.TraitorsEnabled);
msg.WriteRangedInteger(0, Mission.MissionTypes.Count-1, MissionTypeIndex);
msg.Write((byte)modeList.SelectedIndex);
msg.Write(LevelSeed);
msg.Write(GameMain.Server != null && GameMain.Server.AutoRestart);
msg.Write(GameMain.Server == null ? 0.0f : GameMain.Server.AutoRestartTimer);
msg.Write((byte)(playerList.CountChildren));
for (int i = 0; i < playerList.CountChildren; i++)
{
string clientName = playerList.children[i].UserData as string;
msg.Write(clientName==null ? "" : clientName);
}
}
public void ReadData(NetIncomingMessage msg)
{
string subName = "", subHash = "";
string shuttleName = "", shuttleHash = "";
int modeIndex = 0;
//float durationScroll = 0.0f;
string newSeed = "";
bool autoRestart = false;
float restartTimer = 0.0f;
YesNoMaybe traitorsEnabled;
try
{
subName = msg.ReadString();
subHash = msg.ReadString();
shuttleName = msg.ReadString();
shuttleHash = msg.ReadString();
ServerName = msg.ReadString();
serverMessage.Text = msg.ReadString();
traitorsEnabled = (YesNoMaybe)msg.ReadRangedInteger(0, 2);
SetMissionType(msg.ReadRangedInteger(0, Mission.MissionTypes.Count - 1));
modeIndex = msg.ReadByte();
newSeed = msg.ReadString();
autoRestart = msg.ReadBoolean();
restartTimer = msg.ReadFloat();
int playerCount = msg.ReadByte();
playerList.ClearChildren();
for (int i = 0; i < playerCount; i++)
{
AddPlayer(msg.ReadString());
}
}
catch (Exception e)
{
DebugConsole.ThrowError("Failed to read lobby update message ("+e.Message+")");
return;
}
if (!string.IsNullOrWhiteSpace(subName) && !GameMain.NetworkMember.Voting.AllowSubVoting) TrySelectSub(subName, subHash, subList);
if (!string.IsNullOrWhiteSpace(shuttleName)) TrySelectSub(shuttleName, shuttleHash, shuttleList.ListBox);
if (!GameMain.NetworkMember.Voting.AllowModeVoting)
{
//SelectMode(modeList.children[modeIndex], modeList.children[modeIndex].UserData);
modeList.Select(modeIndex, true);
GameModePreset modePreset = modeList.children[modeIndex].UserData as GameModePreset;
missionTypeBlock.Visible = modePreset.Name == "Mission";
}
SetTraitorsEnabled(traitorsEnabled);
autoRestartBox.Selected = autoRestart;
autoRestartTimer = restartTimer;
//durationBar.BarScroll = durationScroll;
LevelSeed = newSeed;
}
}
}

View File

@@ -60,7 +60,7 @@ namespace Barotrauma
public void RegisterVote(NetIncomingMessage inc, List<Client> connectedClients)
{
byte voteTypeByte = inc.ReadByte();
/*byte voteTypeByte = inc.ReadByte();
VoteType voteType = VoteType.Unknown;
try
{
@@ -115,7 +115,7 @@ namespace Barotrauma
break;
}
GameMain.Server.UpdateVoteStatus();
GameMain.Server.UpdateVoteStatus();*/
}
public void UpdateVoteTexts(List<Client> clients, VoteType voteType)
@@ -210,87 +210,5 @@ namespace Barotrauma
UpdateVoteTexts(connectedClients, VoteType.Sub);
}
public void WriteData(NetOutgoingMessage msg, List<Client> voters)
{
msg.Write(allowSubVoting);
if (allowSubVoting)
{
List<Pair<object, int>> voteList = GetVoteList(VoteType.Sub, voters);
msg.Write((byte)voteList.Count);
foreach (Pair<object, int> vote in voteList)
{
if (vote.Second < 1 || vote.First==null) continue;
msg.Write((byte)vote.Second);
msg.Write(((Submarine)vote.First).Name);
}
}
msg.Write(AllowModeVoting);
if (allowModeVoting)
{
List<Pair<object, int>> voteList = GetVoteList(VoteType.Mode, voters);
msg.Write((byte)voteList.Count);
foreach (Pair<object, int> vote in voteList)
{
if (vote.Second < 1 || vote.First == null) continue;
msg.Write((byte)vote.Second);
msg.Write(((GameModePreset)vote.First).Name);
}
}
msg.Write(AllowEndVoting);
if (AllowEndVoting)
{
msg.Write((byte)voters.Count(v => v.GetVote<bool>(VoteType.EndRound)));
msg.Write((byte)voters.Count);
}
msg.Write(AllowVoteKick);
}
public void ReadData(NetIncomingMessage msg)
{
AllowSubVoting = msg.ReadBoolean();
if (allowSubVoting)
{
foreach (Submarine sub in Submarine.SavedSubmarines)
{
SetVoteText(GameMain.NetLobbyScreen.SubList, sub, 0);
}
int votableCount = msg.ReadByte();
for (int i = 0; i < votableCount; i++)
{
int votes = msg.ReadByte();
string subName = msg.ReadString();
Submarine sub = Submarine.SavedSubmarines.Find(sm => sm.Name == subName);
SetVoteText(GameMain.NetLobbyScreen.SubList, sub, votes);
}
}
AllowModeVoting = msg.ReadBoolean();
if (allowModeVoting)
{
int votableCount = msg.ReadByte();
for (int i = 0; i < votableCount; i++)
{
int votes = msg.ReadByte();
string modeName = msg.ReadString();
GameModePreset mode = GameModePreset.list.Find(m => m.Name == modeName);
SetVoteText(GameMain.NetLobbyScreen.ModeList, mode, votes);
}
}
AllowEndVoting = msg.ReadBoolean();
if (AllowEndVoting)
{
GameMain.NetworkMember.EndVoteCount = msg.ReadByte();
GameMain.NetworkMember.EndVoteMax = msg.ReadByte();
}
AllowVoteKick = msg.ReadBoolean();
}
}
}