Differential D3723 Diff 16625 ps/trunk/binaries/data/mods/public/gui/gamesetup/Pages/GameSetupPage/GameSettings/PerPlayer/Dropdowns/PlayerAssignment.js
Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/gui/gamesetup/Pages/GameSetupPage/GameSettings/PerPlayer/Dropdowns/PlayerAssignment.js
Show All 21 Lines | constructor(...args) | ||||
this.aiItems = | this.aiItems = | ||||
g_Settings.AIDescriptions.filter(ai => !ai.data.hidden).map( | g_Settings.AIDescriptions.filter(ai => !ai.data.hidden).map( | ||||
this.aiItemFactory.createItem.bind(this.aiItemFactory)); | this.aiItemFactory.createItem.bind(this.aiItemFactory)); | ||||
this.values = undefined; | this.values = undefined; | ||||
this.assignedGUID = undefined; | this.assignedGUID = undefined; | ||||
this.fixedAI = undefined; | this.fixedAI = undefined; | ||||
// Build the initial list of values with undefined & AI clients. | |||||
this.rebuildList(); | |||||
g_GameSettings.playerAI.watch(() => this.render(), ["values"]); | g_GameSettings.playerAI.watch(() => this.render(), ["values"]); | ||||
g_GameSettings.playerCount.watch((_, oldNb) => this.OnPlayerNbChange(oldNb), ["nbPlayers"]); | g_GameSettings.playerCount.watch((_, oldNb) => this.OnPlayerNbChange(oldNb), ["nbPlayers"]); | ||||
// Sets up the dropdown and renders. | |||||
this.onPlayerAssignmentsChange(); | |||||
this.playerAssignmentsControl.registerClientJoinHandler(this.onClientJoin.bind(this)); | |||||
} | } | ||||
setControl() | setControl() | ||||
{ | { | ||||
this.dropdown = Engine.GetGUIObjectByName("playerAssignment[" + this.playerIndex + "]"); | this.dropdown = Engine.GetGUIObjectByName("playerAssignment[" + this.playerIndex + "]"); | ||||
this.label = Engine.GetGUIObjectByName("playerAssignmentText[" + this.playerIndex + "]"); | this.label = Engine.GetGUIObjectByName("playerAssignmentText[" + this.playerIndex + "]"); | ||||
} | } | ||||
onLoad(initData, hotloadData) | |||||
{ | |||||
if (!hotloadData && !g_IsNetworked) | |||||
this.onClientJoin("local", g_PlayerAssignments); | |||||
this.playerAssignmentsControl.updatePlayerAssignments(); | |||||
} | |||||
OnPlayerNbChange(oldNb) | OnPlayerNbChange(oldNb) | ||||
{ | { | ||||
let isPlayerSlot = Object.values(g_PlayerAssignments).some(x => x.player === this.playerIndex + 1); | let isPlayerSlot = Object.values(g_PlayerAssignments).some(x => x.player === this.playerIndex + 1); | ||||
if (!isPlayerSlot && !g_GameSettings.playerAI.get(this.playerIndex) && | if (!isPlayerSlot && !g_GameSettings.playerAI.get(this.playerIndex) && | ||||
this.playerIndex >= oldNb && this.playerIndex < g_GameSettings.playerCount.nbPlayers) | this.playerIndex >= oldNb && this.playerIndex < g_GameSettings.playerCount.nbPlayers) | ||||
{ | { | ||||
// Add AIs to unused slots by default. | // Add AIs to unused slots by default. | ||||
// TODO: we could save the settings in case the player lowers, then re-raises the # of players. | // TODO: we could save the settings in case the player lowers, then re-raises the # of players. | ||||
g_GameSettings.playerAI.set(this.playerIndex, { | g_GameSettings.playerAI.set(this.playerIndex, { | ||||
"bot": g_Settings.PlayerDefaults[this.playerIndex + 1].AI, | "bot": g_Settings.PlayerDefaults[this.playerIndex + 1].AI, | ||||
"difficulty": +Engine.ConfigDB_GetValue("user", "gui.gamesetup.aidifficulty"), | "difficulty": +Engine.ConfigDB_GetValue("user", "gui.gamesetup.aidifficulty"), | ||||
"behavior": Engine.ConfigDB_GetValue("user", "gui.gamesetup.aibehavior"), | "behavior": Engine.ConfigDB_GetValue("user", "gui.gamesetup.aibehavior"), | ||||
}); | }); | ||||
} | } | ||||
} | } | ||||
onClientJoin(newGUID, newAssignments) | |||||
{ | |||||
if (!g_IsController || this.fixedAI || newAssignments[newGUID].player != -1) | |||||
return; | |||||
// Assign the client (or only buddies if prefered) to a free slot | |||||
if (newGUID != Engine.GetPlayerGUID()) | |||||
{ | |||||
let assignOption = Engine.ConfigDB_GetValue("user", this.ConfigAssignPlayers); | |||||
if (assignOption == "disabled" || | |||||
assignOption == "buddies" && g_Buddies.indexOf(splitRatingFromNick(newAssignments[newGUID].name).nick) == -1) | |||||
return; | |||||
} | |||||
for (let guid in newAssignments) | |||||
if (newAssignments[guid].player == this.playerIndex + 1) | |||||
return; | |||||
newAssignments[newGUID].player = this.playerIndex + 1; | |||||
this.playerAssignmentsControl.assignClient(newGUID, this.playerIndex + 1); | |||||
} | |||||
onPlayerAssignmentsChange() | onPlayerAssignmentsChange() | ||||
{ | { | ||||
// Rebuild the list to account for new/removed players. | |||||
this.rebuildList(); | |||||
let newGUID; | let newGUID; | ||||
for (let guid in g_PlayerAssignments) | for (let guid in g_PlayerAssignments) | ||||
if (g_PlayerAssignments[guid].player == this.playerIndex + 1) | if (g_PlayerAssignments[guid].player == this.playerIndex + 1) | ||||
{ | { | ||||
newGUID = guid; | newGUID = guid; | ||||
break; | break; | ||||
} | } | ||||
if (this.assignedGUID === newGUID) | |||||
return; | |||||
this.assignedGUID = newGUID; | this.assignedGUID = newGUID; | ||||
this.rebuildList(); | if (this.assignedGUID && g_GameSettings.playerAI.get(this.playerIndex)) | ||||
{ | |||||
g_GameSettings.playerAI.setAI(this.playerIndex, undefined); | |||||
this.gameSettingsControl.setNetworkInitAttributes(); | |||||
} | |||||
this.render(); | this.render(); | ||||
} | } | ||||
render() | render() | ||||
{ | { | ||||
this.setEnabled(true); | this.setEnabled(true); | ||||
if (this.assignedGUID) | if (this.assignedGUID) | ||||
{ | { | ||||
this.setSelectedValue(this.assignedGUID); | this.setSelectedValue(this.assignedGUID); | ||||
return; | return; | ||||
} | } | ||||
let ai = g_GameSettings.playerAI.get(this.playerIndex); | let ai = g_GameSettings.playerAI.get(this.playerIndex); | ||||
if (ai) | if (ai) | ||||
{ | { | ||||
this.setSelectedValue(ai.bot); | this.setSelectedValue(ai.bot); | ||||
return; | return; | ||||
} | } | ||||
this.setSelectedValue(undefined); | this.setSelectedValue(undefined); | ||||
} | } | ||||
rebuildList() | rebuildList() | ||||
{ | { | ||||
Engine.ProfileStart("updatePlayerAssignmentsList"); | Engine.ProfileStart("updatePlayerAssignmentsList"); | ||||
// TODO: this particular bit is done for each row, which is unnecessarily inefficient. | |||||
this.playerItems = sortGUIDsByPlayerID().map( | this.playerItems = sortGUIDsByPlayerID().map( | ||||
this.clientItemFactory.createItem.bind(this.clientItemFactory)); | this.clientItemFactory.createItem.bind(this.clientItemFactory)); | ||||
this.values = prepareForDropdown([ | this.values = prepareForDropdown([ | ||||
...this.playerItems, | ...this.playerItems, | ||||
...this.aiItems, | ...this.aiItems, | ||||
this.unassignedItem | this.unassignedItem | ||||
]); | ]); | ||||
let selected = this.dropdown.list_data?.[this.dropdown.selected]; | |||||
this.dropdown.list = this.values.Caption; | this.dropdown.list = this.values.Caption; | ||||
this.dropdown.list_data = this.values.Value; | this.dropdown.list_data = this.values.Value; | ||||
this.setSelectedValue(selected); | |||||
Engine.ProfileStop(); | Engine.ProfileStop(); | ||||
} | } | ||||
onSelectionChange(itemIdx) | onSelectionChange(itemIdx) | ||||
{ | { | ||||
this.values.Handler[itemIdx].onSelectionChange( | this.values.Handler[itemIdx].onSelectionChange( | ||||
this.gameSettingsControl, | this.gameSettingsControl, | ||||
this.playerAssignmentsControl, | this.playerAssignmentsControl, | ||||
this.playerIndex, | this.playerIndex, | ||||
this.values.Value[itemIdx]); | this.values.Value[itemIdx]); | ||||
} | } | ||||
getAutocompleteEntries() | getAutocompleteEntries() | ||||
{ | { | ||||
return this.values.Autocomplete; | return this.values.Autocomplete; | ||||
} | } | ||||
}; | }; | ||||
PlayerSettingControls.PlayerAssignment.prototype.Tooltip = | PlayerSettingControls.PlayerAssignment.prototype.Tooltip = | ||||
translate("Select player."); | translate("Select player."); | ||||
PlayerSettingControls.PlayerAssignment.prototype.AutocompleteOrder = 100; | PlayerSettingControls.PlayerAssignment.prototype.AutocompleteOrder = 100; | ||||
PlayerSettingControls.PlayerAssignment.prototype.ConfigAssignPlayers = | |||||
"gui.gamesetup.assignplayers"; | |||||
{ | { | ||||
PlayerAssignmentItem.Client = class | PlayerAssignmentItem.Client = class | ||||
{ | { | ||||
createItem(guid) | createItem(guid) | ||||
{ | { | ||||
return { | return { | ||||
"Handler": this, | "Handler": this, | ||||
"Value": guid, | "Value": guid, | ||||
▲ Show 20 Lines • Show All 115 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator