Skip to content

Commit

Permalink
First attempt at implementing proper command line parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Garanas committed Dec 5, 2024
1 parent d11609e commit 1c6384b
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog/snippets/fix.6569.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- (#6569) Fix the matchmaker lobby not passing ratings, divisions and clan tags to the session
14 changes: 8 additions & 6 deletions lua/ui/lobby/autolobby/AutolobbyController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ local DebugComponent = import("/lua/shared/components/DebugComponent.lua").Debug
local AutolobbyServerCommunicationsComponent = import("/lua/ui/lobby/autolobby/components/AutolobbyServerCommunicationsComponent.lua")
.AutolobbyServerCommunicationsComponent

local AutolobbyArgumentsComponent = import("/lua/ui/lobby/autolobby/components/AutolobbyArguments.lua").AutolobbyArgumentsComponent

local AutolobbyMessages = import("/lua/ui/lobby/autolobby/AutolobbyMessages.lua").AutolobbyMessages

local AutolobbyEngineStrings = {
Expand Down Expand Up @@ -86,7 +88,7 @@ local AutolobbyEngineStrings = {
---@field DesiredPeerId UILobbyPeerId

--- Responsible for the behavior of the automated lobby.
---@class UIAutolobbyCommunications : moho.lobby_methods, DebugComponent, UIAutolobbyServerCommunicationsComponent
---@class UIAutolobbyCommunications : moho.lobby_methods, DebugComponent, UIAutolobbyServerCommunicationsComponent, UIAutolobbyArgumentsComponent
---@field Trash TrashBag
---@field LocalPeerId UILobbyPeerId # a number that is stringified
---@field LocalPlayerName string # nickname
Expand All @@ -100,7 +102,7 @@ local AutolobbyEngineStrings = {
---@field LobbyParameters? UIAutolobbyParameters # Used for rejoining functionality
---@field HostParameters? UIAutolobbyHostParameters # Used for rejoining functionality
---@field JoinParameters? UIAutolobbyJoinParameters # Used for rejoining functionality
AutolobbyCommunications = Class(MohoLobbyMethods, AutolobbyServerCommunicationsComponent, DebugComponent) {
AutolobbyCommunications = Class(MohoLobbyMethods, AutolobbyServerCommunicationsComponent, AutolobbyArgumentsComponent, DebugComponent) {

---@param self UIAutolobbyCommunications
__init = function(self)
Expand Down Expand Up @@ -147,7 +149,7 @@ AutolobbyCommunications = Class(MohoLobbyMethods, AutolobbyServerCommunicationsC
end

-- retrieve team and start spot
info.Team = tonumber(GetCommandLineArg("/team", 1)[1])
info.Team = self:GetCommandLineArgumentNumber("/team", -1)
info.StartSpot = tonumber(GetCommandLineArg("/startspot", 1)[1]) or -1 -- TODO

-- determine army color based on start location
Expand Down Expand Up @@ -321,10 +323,10 @@ AutolobbyCommunications = Class(MohoLobbyMethods, AutolobbyServerCommunicationsC
if options.Human and options.PL then
if options.DIV ~= "unlisted" then
local division = options.DIV
if options.SUBDIV and options.SUBDIV ~="" then
if options.SUBDIV and options.SUBDIV ~= "" then
division = division .. ' ' .. options.SUBDIV
end
allDivisions[options.PlayerName]= division
allDivisions[options.PlayerName] = division
end
end
end
Expand Down Expand Up @@ -526,7 +528,7 @@ AutolobbyCommunications = Class(MohoLobbyMethods, AutolobbyServerCommunicationsC
self:SendPlayerOptionToServer(ownerId, 'Faction', playerOptions.Faction)
end

-- tuck them into the game options. By all means a hack, but
-- tuck them into the game options. By all means a hack, but
-- this way they are available in both the sim and the UI
self.GameOptions.Ratings = self:CreateRatingsTable(self.PlayerOptions)
self.GameOptions.Divisions = self:CreateDivisionsTable(self.PlayerOptions)
Expand Down
127 changes: 127 additions & 0 deletions lua/ui/lobby/autolobby/components/AutolobbyArguments.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
--******************************************************************************************************
--** Copyright (c) 2024 Willem 'Jip' Wijnia
--**
--** Permission is hereby granted, free of charge, to any person obtaining a copy
--** of this software and associated documentation files (the "Software"), to deal
--** in the Software without restriction, including without limitation the rights
--** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
--** copies of the Software, and to permit persons to whom the Software is
--** furnished to do so, subject to the following conditions:
--**
--** The above copyright notice and this permission notice shall be included in all
--** copies or substantial portions of the Software.
--**
--** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
--** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
--** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
--** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
--** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
--** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
--** SOFTWARE.
--******************************************************************************************************

--- A component that represent all the supported lobby <-> server communications.
---@class UIAutolobbyArgumentsComponent
AutolobbyArgumentsComponent = ClassSimple {

--- Represent all valid command line arguments for the lobby
ArgumentKeys = {
["/init"] = true,
["/joincustom"] = true,
["/gpgnet"] = true,

-- related to player info
["/clan"] = true,
["/country"] = true,
["/numgames"] = true,

-- related to player settings
["/team"] = true,
["/uef"] = true,
["/cybran"] = true,
["/aeon"] = true,
["/seraphim"] = true,
["/startspot"] = true,

-- related to rating
["/deviation"] = true,
["/mean"] = true,

-- related to divisions
["division"] = true,
["/subdivision"] = true,

-- related to game settings
["/gameoptions"] = true,
["/players"] = true,
},

--- Verifies that it is an expected command line argument
---@param self UIAutolobbyArgumentsComponent | UIAutolobbyCommunications
---@param option string
---@return boolean
ValidCommandLineKey = function(self, option)
if not self.ArgumentKeys[option] then
self:DebugWarn("Unknown command line argument: ", option)
return false
end

return true
end,

--- Attempts to retrieve a string-like command line argument
---@param self UIAutolobbyArgumentsComponent | UIAutolobbyCommunications
---@param option string
---@param default string
---@return string
GetCommandLineArgumentString = function(self, option, default)
if not self:ValidCommandLineKey(option) then
return default
end

-- try to get the first argument
local arguments = GetCommandLineArg(option, 1)
if arguments and (not option[ arg[1] ]) then
return arg[1]
end

return default
end,

--- Attempts to retrieve a number-like command line argument
---@param self UIAutolobbyArgumentsComponent | UIAutolobbyCommunications
---@param option string
---@param default number
---@return number
GetCommandLineArgumentNumber = function(self, option, default)
if not self:ValidCommandLineKey(option) then
return default
end

-- try to get the first argument and parse it as a number
local arguments = GetCommandLineArg(option, 1)
if arguments and (not option[ arg[1] ]) then
local parsed = tonumber(arg[1])
if parsed then
return parsed
else
self:DebugWarn("Failed to parse as a number: ", arg[1], " for key ", option)
return default
end
end

return default
end,

--- Attempts to retrieve a table-like command line argument
---@param self UIAutolobbyArgumentsComponent | UIAutolobbyCommunications
---@param option string
---@return table<string, string>
GetCommandLineArgumentArray = function(self, option)
if not self:ValidCommandLineKey(option) then
return {}
end

return import("/lua/system/utils.lua").GetCommandLineArgTable(option)
end,
}

0 comments on commit 1c6384b

Please sign in to comment.