Fixed server-side crashes during job assignment if a client hasn't sent any job preferences

This commit is contained in:
Joonas Rikkonen
2018-07-06 19:42:33 +03:00
parent f684f76862
commit 740d201fc3
@@ -2065,10 +2065,18 @@ namespace Barotrauma.Networking
if (jobPrefab != null) jobPreferences.Add(jobPrefab); if (jobPrefab != null) jobPreferences.Add(jobPrefab);
} }
sender.CharacterInfo = new CharacterInfo(Character.HumanConfigFile, sender.Name, gender); sender.CharacterInfo = new CharacterInfo(Character.HumanConfigFile, sender.Name, gender)
sender.CharacterInfo.HeadSpriteId = headSpriteId; {
HeadSpriteId = headSpriteId
};
//if the client didn't provide job preferences, we'll use the preferences that are randomly assigned in the Client constructor
Debug.Assert(sender.JobPreferences.Count > 0);
if (jobPreferences.Count > 0)
{
sender.JobPreferences = jobPreferences; sender.JobPreferences = jobPreferences;
} }
}
public void AssignJobs(List<Client> unassigned, bool assignHost) public void AssignJobs(List<Client> unassigned, bool assignHost)
{ {
@@ -2112,6 +2120,7 @@ namespace Barotrauma.Networking
//if any of the players has chosen a job that is Always Allowed, give them that job //if any of the players has chosen a job that is Always Allowed, give them that job
for (int i = unassigned.Count - 1; i >= 0; i--) for (int i = unassigned.Count - 1; i >= 0; i--)
{ {
if (unassigned[i].JobPreferences.Count == 0) continue;
if (!unassigned[i].JobPreferences[0].AllowAlways) continue; if (!unassigned[i].JobPreferences[0].AllowAlways) continue;
unassigned[i].AssignedJob = unassigned[i].JobPreferences[0]; unassigned[i].AssignedJob = unassigned[i].JobPreferences[0];
unassigned.RemoveAt(i); unassigned.RemoveAt(i);
@@ -2140,7 +2149,7 @@ namespace Barotrauma.Networking
} }
} }
//find a suitable job for the rest of the players //attempt to give the clients a job they have in their job preferences
foreach (Client c in unassigned) foreach (Client c in unassigned)
{ {
foreach (JobPrefab preferredJob in c.JobPreferences) foreach (JobPrefab preferredJob in c.JobPreferences)
@@ -2153,11 +2162,14 @@ namespace Barotrauma.Networking
assignedClientCount[preferredJob]++; assignedClientCount[preferredJob]++;
break; break;
} }
//none of the jobs the client prefers are available anymore }
else if (preferredJob == c.JobPreferences.Last()) }
//give random jobs to rest of the clients
foreach (Client c in unassigned)
{ {
//find all jobs that are still available //find all jobs that are still available
var remainingJobs = JobPrefab.List.FindAll(jp => assignedClientCount[preferredJob] < jp.MaxNumber && c.Karma >= jp.MinKarma); var remainingJobs = JobPrefab.List.FindAll(jp => assignedClientCount[jp] < jp.MaxNumber && c.Karma >= jp.MinKarma);
//all jobs taken, give a random job //all jobs taken, give a random job
if (remainingJobs.Count == 0) if (remainingJobs.Count == 0)
@@ -2182,8 +2194,6 @@ namespace Barotrauma.Networking
} }
} }
} }
}
}
private Client FindClientWithJobPreference(List<Client> clients, JobPrefab job, bool forceAssign = false) private Client FindClientWithJobPreference(List<Client> clients, JobPrefab job, bool forceAssign = false)
{ {