lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Julien Desgats <julien.desgats <at> gmail.com> writes:

> 
> Here is a minimal version (without any check, though) which convert
> camel case names into hyphen-separated strings. But the behavior can
> be unexpected if there is digits or special characters into keys.
> 
> Type = setmetatable({}, {
>   __index = function(t, key)
>     local name = key:gsub("%u%l*", function(s) return s:upper().."-"
> end) .. "MAP"
>     t.key = name -- cache the name to allow fast further access
>     return name
>   end
> })
> 
> print(Type.Arabia, Type.GoldRush)
> 
> If you really need to constraint possible input values, you can put a
> "set" of available keys in metatable and check it :
> 
> Type = setmetatable({}, {
>   keys = {
>     Arabia = true,
>     GoldRush = true,
>   },
>   __index = function(t, key)
>     if not getmetatable(t).keys[key] then return nil end -- if key is
> not allowed, return nil
>     local name = key:gsub("%u%l*", function(s) return s:upper().."-"
> end) .. "MAP"
>     t.key = name
>     return name
>   end
> })
> 
> 2011/6/11 PROXiCiDE <saiyuk7 <at> gmail.com>:
> > Hi is there a way to minimize the repeat typing of such.. -MAP at the end?
> > say...
> >
> >  Normal Version
> >      Type = {
> >         Arabia = "ARABIA-MAP",
> >         Archipelago = "ARCHIPELAGO-MAP",
> >         Baltic = "BALTIC-MAP",
> >         Coastal = "COASTAL-MAP",
> >         Continental = "CONTINENTAL-MAP",
> >         CraterLake = "CRATER-LAKE-MAP",
> >         Fortress = "FORTRESS-MAP",
> >         GoldRush = "GOLD-RUSH-MAP",
> >         Highland = "HIGHLAND-MAP",
> >         Islands = "ISLANDS-MAP",
> >         Mediterranean = "MEDITERRANEAN-MAP",
> >         Migration = "MIGRATION-MAP",
> >         Rivers = "RIVERS-MAP",
> >         TeamIslands = "TEAMISLANDS-MAP",
> >         Scenario = "SCENARIO-MAP"
> >      }
> >
> >  Minimal Version?? If possible
> >      Type = Map {
> >         Arabia,
> >         Archipelago,
> >         ....so on and so on
> >      }
> >
> >
> >
> 
> 

hi thanks for your reply, last night i practically stayed up all night trying to
work with this almost, i started to get more into it and thought about having
Enums, because before i was going to write separate SET table functions for each
type.. instead i thought about collecting them all into one function with the
use of Enums (table like)

local function ENUMTYPES(xEnumTable)
   local t = {}
   local n = 0;
   for _,v in ipairs(xEnumTable) do
      n = n + 1
      t[v] = n
   end
   return t
end

local Id = ENUMTYPES({
   "Resource",
   "Map",
   "Civ",
   "Population",
   "Difficulty",
   "Age"
})

local function SET(xTable,nID)
   local t = {}
   for _,v in ipairs(xTable) do
      if(nID == Id.Resource) then
         t[v] = string.upper(v).."-RESOURCES-START"
      elseif(nID == Id.Map) then
         local s
         if(v == "CraterLake") then
            s = "CRATER-LAKE"
         elseif(v == "GoldRush") then
            s = "GOLD-RUSH"
         else
            s = string.upper(v)
         end
         t[v] = s.."-MAP"
      elseif(nID == Id.Civ) then
         if(v == "Gaia") then
            t[v] = string.upper(v);
         else
            t[v] = string.upper(v).."-CIV"
         end
      elseif(nID == Id.Population) then
         local cap = nil;
         _,_,cap = string.find(v,"Cap(%d+)")
         if(cap ~= nil) then
            t[v] = "POPULATION-CAP-"..cap
         end
      elseif(nID == Id.Difficulty) then
         t[v] = "DIFFICULTY-"..string.upper(v)
      elseif(nID == Id.Age) then
         t[v] = string.upper(v).."-AGE-START"
      elseif(nID == Id.Victory) then
         local s
         if(v == "TimeLimit") then
            s = "TIME-LIMIT"
         else
            s = string.upper(v)
         end
         t[v] = "VICTORY-"..s
      end
   end
   return t
end

Settings = {
   Type = {
      DeathMatch = "DEATH-MATCH",
      Regicide = "REGICIDE"
   },
   
   Age = SET({
      "Dark",
      "Feudal",
      "Castle",
      "Imperial",
      "Post"
   }, Id.Age),
   
   Resources = SET({
      "Low",
      "Medium",
      "High"
   }, Id.Resource),
   
   Map = {
      Size = SET({
         "Tiny",
         "Small",
         "Medium",
         "Normal",
         "Large",
         "Giant"
      }, Id.Map),
      
      
      Type = SET( {
         "Arabia",
         "Archipelago",
         "Baltic",
         "Coastal",
         "Continental",
         "CraterLake",
         "Fortress",
         "GoldRush",
         "Highland",
         "Islands",
         "Mediterranean",
         "Migration",
         "Rivers",
         "TeamIslands",
         "Scenario"
      }, Id.Map)
   },
   
   
   Victory = SET({
      "Standard",
      "Conquest",
      "TimeLimit",
      "Score",
      "Custom"
   }, Id.Victory),
   
   Difficulty = SET( {
      "Easiest",
      "Easy",
      "Moderate",
      "Hard",
      "Hardest"
   }, Id.Difficulty),
   
   Population = SET( {
      "Cap25",
      "Cap50",
      "Cap75",
      "Cap100",
      "Cap125",
      "Cap150",
      "Cap175",
      "Cap200",
   }, Id.Population),
   
   SpeedLocked = "GAME-SPEED-LOCKED",
   TeamLock = "TEAMS-LOCKED",
   
   Civ = SET({
      "Gaia",
      "Briton",
      "Byzantine",
      "Celtic",
      "Chinese",
      "Frankish",
      "Gothic",
      "Japanese",
      "Mongol",
      "Persian",
      "Saracen",
      "Teuotonic",
      "Turkish",
      "Viking"
   }, Id.Civ)
}

print(Settings.Type.DeathMatch)
print(Settings.Map.Size.Small)

table.foreach(Settings.Map.Type,print)
print("")
table.foreach(Settings.Difficulty,print)
print("")
table.foreach(Settings.Civ,print)
print("")
table.foreach(Settings.Resources,print)
print("")
table.foreach(Settings.Map.Size,print)
print("")
table.foreach(Settings.Age,print)
print("")
table.foreach(Settings.Victory,print)

Now im not sure if this is more efficient than the approach i was going last
night, also would this require more memory overhead? because it creates a local
table inside SET but returns it.. does garbage collection destroy it? or does it
just replace it as if were just written out normaly?