1
0
forked from 0ad/0ad

Gamesetup ready- and PlayerAssignments-cleanup.

Split logic to onClientJoin onClientLeave.
Remove duplicate, yet slightly different ready button caption.
Rename hosts to newAssignments in the GUI message sent by the NetClient.

This was SVN commit r18299.
This commit is contained in:
elexis 2016-06-03 02:28:29 +00:00
parent 0f8b10fc0e
commit 903803ceec
2 changed files with 63 additions and 61 deletions

View File

@ -708,56 +708,55 @@ function handleGamesetupMessage(message)
*/
function handlePlayerAssignmentMessage(message)
{
let resetReady = false;
let newGUID = "";
for (let guid in message.newAssignments)
if (!g_PlayerAssignments[guid])
onClientJoin(guid, message.newAssignments);
// Report joinings
for (let guid in message.hosts)
{
if (g_PlayerAssignments[guid])
continue;
addChatMessage({ "type": "connect", "guid": guid, "username": message.hosts[guid].name });
newGUID = guid;
// Assign the new player
if (!g_IsController || message.hosts[guid].player != -1)
continue;
let freeSlot = g_GameAttributes.settings.PlayerData.findIndex((v,i) =>
Object.keys(g_PlayerAssignments).every(guid => g_PlayerAssignments[guid].player != i+1)
);
if (freeSlot != -1)
Engine.AssignNetworkPlayer(freeSlot+1, guid);
}
// Report leavings
for (let guid in g_PlayerAssignments)
{
if (message.hosts[guid])
continue;
if (!message.newAssignments[guid])
onClientLeave(guid);
addChatMessage({ "type": "disconnect", "guid": guid });
g_PlayerAssignments = message.newAssignments;
if (g_PlayerAssignments[guid].player != -1)
resetReady = true; // Observers shouldn't reset ready.
}
g_PlayerAssignments = message.hosts;
updatePlayerList();
if (g_PlayerAssignments[newGUID] && g_PlayerAssignments[newGUID].player != -1)
resetReady = true;
if (resetReady)
resetReadyData();
updateReadyUI();
sendRegisterGameStanza();
}
function onClientJoin(newGUID, newAssignments)
{
addChatMessage({
"type": "connect",
"guid": newGUID,
"username": newAssignments[newGUID].name
});
// Assign joining observers to unused player numbers
if (!g_IsController || newAssignments[newGUID].player != -1)
return;
let freeSlot = g_GameAttributes.settings.PlayerData.findIndex((v,i) =>
Object.keys(g_PlayerAssignments).every(guid => g_PlayerAssignments[guid].player != i+1)
);
if (freeSlot == -1)
return;
Engine.AssignNetworkPlayer(freeSlot + 1, newGUID);
resetReadyData();
}
function onClientLeave(guid)
{
addChatMessage({
"type": "disconnect",
"guid": guid
});
if (g_PlayerAssignments[guid].player != -1)
resetReadyData();
}
function getMapDisplayName(map)
{
let mapData = loadMapData(map);
@ -1446,7 +1445,6 @@ function updateGUIObjects()
// Game attributes include AI settings, so update the player list
updatePlayerList();
// We should have everyone confirm that the new settings are acceptable.
resetReadyData();
// Refresh AI config page
@ -1489,7 +1487,7 @@ function updateMapDescription()
let victoryTitle;
if (victoryIdx == -1)
victoryTitle = translateWithContext("victory condition", "Unknown")
victoryTitle = translateWithContext("victory condition", "Unknown");
else
{
if (g_VictoryConditions.Name[victoryIdx] == "wonder")
@ -1817,9 +1815,18 @@ function resetCivilizations()
function toggleReady()
{
g_IsReady = !g_IsReady;
setReady(!g_IsReady);
}
Engine.SendNetworkReady(+g_IsReady);
function setReady(ready, sendMessage = true)
{
g_IsReady = ready;
if (sendMessage)
Engine.SendNetworkReady(+g_IsReady);
if (g_IsController)
return;
let button = Engine.GetGUIObjectByName("startGame");
@ -1899,15 +1906,10 @@ function resetReadyData()
else if (g_IsController)
{
Engine.ClearAllPlayerReady();
g_IsReady = true;
Engine.SendNetworkReady(1);
setReady(true);
}
else
{
g_IsReady = false;
Engine.GetGUIObjectByName("startGame").caption = translate("I'm ready!");
Engine.GetGUIObjectByName("startGame").tooltip = translate("State that you accept the current settings and are ready to play!");
}
setReady(false, false);
}
/**

View File

@ -275,19 +275,19 @@ void CNetClient::PostPlayerAssignmentsToScript()
JSAutoRequest rq(cx);
JS::RootedValue msg(cx);
GetScriptInterface().Eval("({'type':'players', 'hosts':{}})", &msg);
GetScriptInterface().Eval("({'type':'players', 'newAssignments':{}})", &msg);
JS::RootedValue hosts(cx);
GetScriptInterface().GetProperty(msg, "hosts", &hosts);
JS::RootedValue newAssignments(cx);
GetScriptInterface().GetProperty(msg, "newAssignments", &newAssignments);
for (const std::pair<CStr, PlayerAssignment>& p : m_PlayerAssignments)
{
JS::RootedValue host(cx);
GetScriptInterface().Eval("({})", &host);
GetScriptInterface().SetProperty(host, "name", std::wstring(p.second.m_Name), false);
GetScriptInterface().SetProperty(host, "player", p.second.m_PlayerID, false);
GetScriptInterface().SetProperty(host, "status", p.second.m_Status, false);
GetScriptInterface().SetProperty(hosts, p.first.c_str(), host, false);
JS::RootedValue assignment(cx);
GetScriptInterface().Eval("({})", &assignment);
GetScriptInterface().SetProperty(assignment, "name", CStrW(p.second.m_Name), false);
GetScriptInterface().SetProperty(assignment, "player", p.second.m_PlayerID, false);
GetScriptInterface().SetProperty(assignment, "status", p.second.m_Status, false);
GetScriptInterface().SetProperty(newAssignments, p.first.c_str(), assignment, false);
}
PushGuiMessage(msg);