Differential D3243 Diff 16276 binaries/data/mods/public/gui/gamesetup/Pages/GameSetupPage/GameSettings/PerPlayer/Dropdowns/PlayerAssignment.js
Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/gui/gamesetup/Pages/GameSetupPage/GameSettings/PerPlayer/Dropdowns/PlayerAssignment.js
/** | |||||
* Warning: this class handles more than most other GUI controls. | |||||
* Indeed, the logic of how to handle player assignments is here, | |||||
* as that is not really a GUI-agnostic concern | |||||
* (campaigns and other autostarting scripts should handle that themselves). | |||||
*/ | |||||
PlayerSettingControls.PlayerAssignment = class extends GameSettingControlDropdown | PlayerSettingControls.PlayerAssignment = class extends GameSettingControlDropdown | ||||
{ | { | ||||
constructor(...args) | constructor(...args) | ||||
{ | { | ||||
super(...args); | super(...args); | ||||
this.clientItemFactory = new PlayerAssignmentItem.Client(); | this.clientItemFactory = new PlayerAssignmentItem.Client(); | ||||
Lint: ESLintBear (no-use-before-define): `'PlayerAssignmentItem' was used before it was defined.` | |||||
this.aiItemFactory = new PlayerAssignmentItem.AI(); | this.aiItemFactory = new PlayerAssignmentItem.AI(); | ||||
Lint: ESLintBear (no-use-before-define) 'PlayerAssignmentItem' was used before it was defined. Lint: ESLintBear (no-use-before-define): `'PlayerAssignmentItem' was used before it was defined.` | |||||
this.unassignedItem = new PlayerAssignmentItem.Unassigned().createItem(); | this.unassignedItem = new PlayerAssignmentItem.Unassigned().createItem(); | ||||
Lint: ESLintBear (no-use-before-define) 'PlayerAssignmentItem' was used before it was defined. Lint: ESLintBear (no-use-before-define): `'PlayerAssignmentItem' was used before it was defined.` | |||||
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; | ||||
g_NewGameSettings.playerAI.watch(() => this.render(), ["values"]); | |||||
g_NewGameSettings.playerCount.watch((_, oldNb) => this.OnPlayerNbChange(oldNb), ["nbPlayers"]); | |||||
this.render(); | |||||
this.playerAssignmentsControl.registerClientJoinHandler(this.onClientJoin.bind(this)); | 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) | onLoad(initData, hotloadData) | ||||
{ | { | ||||
if (!hotloadData && !g_IsNetworked) | if (!hotloadData && !g_IsNetworked) | ||||
this.onClientJoin("local", g_PlayerAssignments); | this.onClientJoin("local", g_PlayerAssignments); | ||||
this.playerAssignmentsControl.updatePlayerAssignments(); | |||||
this.OnPlayerNbChange(0); | |||||
} | |||||
OnPlayerNbChange(oldNb) | |||||
{ | |||||
let isPlayerSlot = Object.values(g_PlayerAssignments).some(x => x.player === this.playerIndex + 1); | |||||
if (!isPlayerSlot && !g_NewGameSettings.playerAI.get(this.playerIndex) && | |||||
this.playerIndex >= oldNb && this.playerIndex < g_NewGameSettings.playerCount.nbPlayers) | |||||
{ | |||||
// Add AIs to unused slots by default. | |||||
// TODO: we could save the settings in case the player lowers, then re-raises the # of players. | |||||
g_NewGameSettings.playerAI.set(this.playerIndex, { | |||||
"bot": g_Settings.PlayerDefaults[this.playerIndex + 1].AI, | |||||
"difficulty": +Engine.ConfigDB_GetValue("user", "gui.gamesetup.aidifficulty"), | |||||
"behavior": Engine.ConfigDB_GetValue("user", "gui.gamesetup.aibehavior"), | |||||
}); | |||||
} | |||||
} | } | ||||
onClientJoin(newGUID, newAssignments) | onClientJoin(newGUID, newAssignments) | ||||
{ | { | ||||
if (!g_IsController || this.fixedAI || newAssignments[newGUID].player != -1) | if (!g_IsController || this.fixedAI || newAssignments[newGUID].player != -1) | ||||
return; | return; | ||||
let pData = this.gameSettingsControl.getPlayerData(g_GameAttributes, this.playerIndex); | |||||
if (!pData) | |||||
return; | |||||
// Assign the client (or only buddies if prefered) to a free slot | // Assign the client (or only buddies if prefered) to a free slot | ||||
if (newGUID != Engine.GetPlayerGUID()) | if (newGUID != Engine.GetPlayerGUID()) | ||||
{ | { | ||||
let assignOption = Engine.ConfigDB_GetValue("user", this.ConfigAssignPlayers); | let assignOption = Engine.ConfigDB_GetValue("user", this.ConfigAssignPlayers); | ||||
if (assignOption == "disabled" || | if (assignOption == "disabled" || | ||||
assignOption == "buddies" && g_Buddies.indexOf(splitRatingFromNick(newAssignments[newGUID].name).nick) == -1) | assignOption == "buddies" && g_Buddies.indexOf(splitRatingFromNick(newAssignments[newGUID].name).nick) == -1) | ||||
return; | return; | ||||
} | } | ||||
for (let guid in newAssignments) | for (let guid in newAssignments) | ||||
if (newAssignments[guid].player == this.playerIndex + 1) | if (newAssignments[guid].player == this.playerIndex + 1) | ||||
return; | return; | ||||
if (pData.AI) | |||||
{ | |||||
pData.AI = false; | |||||
this.gameSettingsControl.updateGameAttributes(); | |||||
this.gameSettingsControl.setNetworkGameAttributes(); | |||||
} | |||||
newAssignments[newGUID].player = this.playerIndex + 1; | newAssignments[newGUID].player = this.playerIndex + 1; | ||||
this.playerAssignmentsControl.assignClient(newGUID, this.playerIndex + 1); | this.playerAssignmentsControl.assignClient(newGUID, this.playerIndex + 1); | ||||
} | } | ||||
onPlayerAssignmentsChange() | onPlayerAssignmentsChange() | ||||
{ | { | ||||
this.assignedGUID = undefined; | this.assignedGUID = undefined; | ||||
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) | ||||
{ | { | ||||
this.assignedGUID = guid; | this.assignedGUID = guid; | ||||
break; | break; | ||||
} | } | ||||
this.playerItems = sortGUIDsByPlayerID().map( | this.playerItems = sortGUIDsByPlayerID().map( | ||||
this.clientItemFactory.createItem.bind(this.clientItemFactory)); | this.clientItemFactory.createItem.bind(this.clientItemFactory)); | ||||
this.rebuildList(); | this.rebuildList(); | ||||
this.updateSelection(); | this.render(); | ||||
} | } | ||||
onMapChange(mapData) | render() | ||||
{ | { | ||||
let mapPData = this.gameSettingsControl.getPlayerData(mapData, this.playerIndex); | this.setEnabled(true); | ||||
this.fixedAI = mapPData && mapPData.AI || undefined; | if (this.assignedGUID) | ||||
} | |||||
onGameAttributesChange() | |||||
{ | { | ||||
let pData = this.gameSettingsControl.getPlayerData(g_GameAttributes, this.playerIndex); | this.setSelectedValue(this.assignedGUID); | ||||
if (!pData) | |||||
return; | return; | ||||
if (this.fixedAI && (pData.AI === undefined || pData.AI != this.fixedAI)) | |||||
{ | |||||
pData.AI = this.fixedAI; | |||||
this.gameSettingsControl.updateGameAttributes(); | |||||
this.playerAssignmentsControl.unassignClient(this.playerIndex + 1); | |||||
} | } | ||||
} | let ai = g_NewGameSettings.playerAI.get(this.playerIndex); | ||||
if (ai) | |||||
onGameAttributesBatchChange() | |||||
{ | { | ||||
let pData = this.gameSettingsControl.getPlayerData(g_GameAttributes, this.playerIndex); | this.setSelectedValue(ai.bot); | ||||
if (!pData) | |||||
return; | return; | ||||
this.setEnabled(!this.fixedAI); | |||||
this.updateSelection(); | |||||
} | } | ||||
updateSelection() | this.setSelectedValue(undefined); | ||||
{ | |||||
let pData = this.gameSettingsControl.getPlayerData(g_GameAttributes, this.playerIndex); | |||||
if (pData && this.values) | |||||
this.setSelectedValue( | |||||
this.values.Value.findIndex((value, i) => | |||||
this.values.Handler[i].isSelected(pData, this.assignedGUID, value))); | |||||
} | } | ||||
rebuildList() | rebuildList() | ||||
{ | { | ||||
Engine.ProfileStart("updatePlayerAssignmentsList"); | Engine.ProfileStart("updatePlayerAssignmentsList"); | ||||
this.values = prepareForDropdown([ | this.values = prepareForDropdown([ | ||||
...this.playerItems, | ...this.playerItems, | ||||
...this.aiItems, | ...this.aiItems, | ||||
this.unassignedItem | this.unassignedItem | ||||
]); | ]); | ||||
this.dropdown.list = this.values.Caption; | this.dropdown.list = this.values.Caption; | ||||
this.dropdown.list_data = this.values.Value.map((value, i) => i); | this.dropdown.list_data = this.values.Value; | ||||
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, | ||||
Show All 10 Lines | |||||
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 = | PlayerSettingControls.PlayerAssignment.prototype.ConfigAssignPlayers = | ||||
"gui.gamesetup.assignplayers"; | "gui.gamesetup.assignplayers"; | ||||
class PlayerAssignmentItem | class PlayerAssignmentItem | ||||
Lint: JSHintBear 'PlayerAssignmentItem' was used before it was defined. Lint: JSHintBear: `'PlayerAssignmentItem' was used before it was defined.` | |||||
{ | { | ||||
} | } | ||||
{ | { | ||||
PlayerAssignmentItem.Client = class | PlayerAssignmentItem.Client = class | ||||
{ | { | ||||
createItem(guid) | createItem(guid) | ||||
{ | { | ||||
return { | return { | ||||
"Handler": this, | "Handler": this, | ||||
"Value": guid, | "Value": guid, | ||||
"Autocomplete": g_PlayerAssignments[guid].name, | "Autocomplete": g_PlayerAssignments[guid].name, | ||||
"Caption": setStringTags( | "Caption": setStringTags( | ||||
g_PlayerAssignments[guid].name, | g_PlayerAssignments[guid].name, | ||||
g_PlayerAssignments[guid].player == -1 ? this.ObserverTags : this.PlayerTags) | g_PlayerAssignments[guid].player == -1 ? this.ObserverTags : this.PlayerTags) | ||||
}; | }; | ||||
} | } | ||||
onSelectionChange(gameSettingsControl, playerAssignmentsControl, playerIndex, guidToAssign) | onSelectionChange(gameSettingsControl, playerAssignmentsControl, playerIndex, guidToAssign) | ||||
{ | { | ||||
let sourcePlayer = g_PlayerAssignments[guidToAssign].player - 1; | let sourcePlayer = g_PlayerAssignments[guidToAssign].player - 1; | ||||
// Swap AIs for convenience. | |||||
let ai = g_NewGameSettings.playerAI.get(playerIndex); | |||||
if (ai) | |||||
g_NewGameSettings.playerAI.swap(sourcePlayer, playerIndex); | |||||
playerAssignmentsControl.assignPlayer(guidToAssign, playerIndex); | playerAssignmentsControl.assignPlayer(guidToAssign, playerIndex); | ||||
gameSettingsControl.assignPlayer(sourcePlayer, playerIndex); | gameSettingsControl.setNetworkGameAttributes(); | ||||
} | } | ||||
isSelected(pData, guid, value) | isSelected(pData, guid, value) | ||||
{ | { | ||||
return guid !== undefined && guid == value; | return guid !== undefined && guid == value; | ||||
} | } | ||||
}; | }; | ||||
Show All 17 Lines | createItem(ai) | ||||
"Caption": setStringTags(sprintf(this.Label, { "ai": aiName }), this.Tags) | "Caption": setStringTags(sprintf(this.Label, { "ai": aiName }), this.Tags) | ||||
}; | }; | ||||
} | } | ||||
onSelectionChange(gameSettingsControl, playerAssignmentsControl, playerIndex, value) | onSelectionChange(gameSettingsControl, playerAssignmentsControl, playerIndex, value) | ||||
{ | { | ||||
playerAssignmentsControl.unassignClient(playerIndex + 1); | playerAssignmentsControl.unassignClient(playerIndex + 1); | ||||
g_GameAttributes.settings.PlayerData[playerIndex].AI = value; | g_NewGameSettings.playerAI.set(playerIndex, { | ||||
"bot": value, | |||||
"difficulty": +Engine.ConfigDB_GetValue("user", "gui.gamesetup.aidifficulty"), | |||||
"behavior": Engine.ConfigDB_GetValue("user", "gui.gamesetup.aibehavior"), | |||||
}); | |||||
gameSettingsControl.updateGameAttributes(); | gameSettingsControl.updateGameAttributes(); | ||||
gameSettingsControl.setNetworkGameAttributes(); | gameSettingsControl.setNetworkGameAttributes(); | ||||
} | } | ||||
isSelected(pData, guid, value) | isSelected(pData, guid, value) | ||||
{ | { | ||||
return !guid && pData.AI && pData.AI == value; | return !guid && pData.AI && pData.AI == value; | ||||
Show All 19 Lines | createItem() | ||||
"Caption": setStringTags(this.Label, this.Tags) | "Caption": setStringTags(this.Label, this.Tags) | ||||
}; | }; | ||||
} | } | ||||
onSelectionChange(gameSettingsControl, playerAssignmentsControl, playerIndex) | onSelectionChange(gameSettingsControl, playerAssignmentsControl, playerIndex) | ||||
{ | { | ||||
playerAssignmentsControl.unassignClient(playerIndex + 1); | playerAssignmentsControl.unassignClient(playerIndex + 1); | ||||
g_GameAttributes.settings.PlayerData[playerIndex].AI = false; | g_NewGameSettings.playerAI.setAI(playerIndex, undefined); | ||||
gameSettingsControl.updateGameAttributes(); | gameSettingsControl.updateGameAttributes(); | ||||
gameSettingsControl.setNetworkGameAttributes(); | gameSettingsControl.setNetworkGameAttributes(); | ||||
} | } | ||||
isSelected(pData, guid, value) | isSelected(pData, guid, value) | ||||
{ | { | ||||
return !guid && !pData.AI; | return !guid && !pData.AI; | ||||
} | } | ||||
}; | }; | ||||
PlayerAssignmentItem.Unassigned.prototype.Label = | PlayerAssignmentItem.Unassigned.prototype.Label = | ||||
translate("Unassigned"); | translate("Unassigned"); | ||||
PlayerAssignmentItem.Unassigned.prototype.Tags = | PlayerAssignmentItem.Unassigned.prototype.Tags = | ||||
{ "color": "140 140 140" }; | { "color": "140 140 140" }; | ||||
} | } |
Wildfire Games · Phabricator
'PlayerAssignmentItem' was used before it was defined.