How do I use information from Roblox Games API? - lua

So I am trying to make some code that gets information about games using the Roblox API. I do get data back when I send a request, but I can't figure out a way to use the data in the code. Because when I decode it, it looks like this:
{
["data"] = ▼ {
[1] = ▼ {
["allowedGearCategories"] = {},
["allowedGearGenres"] = ▶ {...},
["copyingAllowed"] = false,
["createVipServersAllowed"] = false,
["created"] = <creation date>,
["creator"] = ▶ {...},
["description"] = "",
["favoritedCount"] = 0,
["genre"] = "Comedy",
["id"] = <the id>,
["isAllGenre"] = false,
["isFavoritedByUser"] = false,
["isGenreEnforced"] = true,
["maxPlayers"] = 10,
["name"] = <the name>,
["playing"] = 0,
["rootPlaceId"] = <the id>,
["studioAccessToApisAllowed"] = false,
["universeAvatarType"] = "PlayerChoice",
["updated"] = <update date>,
["visits"] = 0
}
}
}
I censored some of the information about which game it is, but that information isn't important here. The important thing is that it says "1" under "data", and if I type that into the code, it uses it as a number. So I can't access anything further down in the list than "data".
Here is my code, if it helps:
local universe = Proxy:Get("https://api.roblox.com/universes/get-universe-containing-place?placeid="..placeId).body
local universeDecoded = http:JSONDecode(universe)
local universeId = universeDecoded.UniverseId
local placeInfo = http:JSONDecode(Proxy:Get("https://games.roblox.com/v1/games?universeIds="..universeId).body)
print(placeInfo.data)
Also, sorry for not knowing a lot of programming words, so I say stuff like "further down in the list". But I hope it's clear what I mean.

I figured it out myself. I just had to write
print(placeInfo.data[1])

Related

Trying to figure out how to Grab part of a table. (hard to explain fully)

So, what i am trying to do is... well it's best to show. oh and This is with Fivem and trying to modify an existing resource, but...
`BMProducts = {
{
["lostItems"] = {
[1] = { name = "weapon_shotgun", price = 1500, crypto = 2500, amount = 1 },
},
}
}
table.insert(BMProducts, {
["ballasItems"] = {
[1] = { name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
},
})
Config.Products = BMProducts`
And I have a another config, that i need to pull the Correct table, but now sure entirely how
`["products"] = Config.Products["ballasItems"],`
Is what I have but it won't read it, due to what I assume is what i saw when debugging, that when inserting to the table, it assigns a number, ie;
[1] = {lostitems = {... [2] = {ballasItems = {...
One that works, but what my ultimate goal is to make the code plug and play with the table inserts, is this
`BMProducts =
{
["lostItems"] = {
[1] = { name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
},
["ballasItems"] = {
[1] = { name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
},
}`
which works with the config above because the way just above does not assign numbers and not inserting into a table. Any ideas how i can go about setting that config for the correct Products table?
When i try it with the table insert, and with the
`["products"] = Config.Products["ballasItems"],`
It can't find the table, which is due to what i assume the table format being different than what it was, which was the code block at the bottom
so my main thing, is to get
`["products"] = Config.Products["ballasItems"],`
to = the correct table when there is a table insert.
If you don't want to table.insert, then don't table.insert:
BMProducts["ballasItems"] = {
{ name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
}
Now I think you told to not modify the config, so another approach is to just use that additional array index when indexing:
["products"] = Config.Products[2]["ballasItems"]
Which obviously assumes that the config never changes and the entry with ballasItems is on index 2.
If you do not know the index, you may want to iterate over Config.Products and look for the right product.
for i,v in ipairs(Config.Products) do
if v["ballasItems"] then
end
end
You may also consider the case where two or more products match.

How to use values ​from one module script in another module script?

Faced such problem: it is necessary to use values ​​from other module script in one module script. Both module scripts contain tables with float, boolean and string values. How in theory can this be done and is it possible at all? And if not, what are the alternative solutions? I read on the forum that this can be done, but how exactly was not explained anywhere.
For example.
One module script:
local characters = {
["CharacterOne"] = {
["Health"] = 100,
["InGroup"] = false,
["Eating"] = {"Fruit", "Meat"}
};
["CharacterTwo"] = {
["Health"] = 260,
["InGroup"] = true,
["Eating"] = {"Meat"}
}
}
return characters
Two module script:
local food = {
["Fruit"] = {
["Saturation"] = 20,
["Price"] = 2
},
["Meat"] = {
["Saturation"] = 50,
["Price"] = 7
}
}
return food
The documentation for ModuleScripts tells you how to use them in other source containers: the require function.
So assuming that you've named your ModuleScripts Characters and Food respectively, and assuming that they are siblings in a folder, you could utilize the require function like this :
local Food = require(script.Parent.Food)
local characters = {
["CharacterOne"] = {
["Health"] = 100,
["InGroup"] = false,
["Eating"] = { Food.Fruit, Food.Meat }
};
["CharacterTwo"] = {
["Health"] = 260,
["InGroup"] = true,
["Eating"] = { Food.Meat }
}
}
return characters

Minimal NeoVim lua config for using nvim-cmp with lsp

I'm trying to write my own NeoVim Lua based config, stripped down to the minimum I need and with the goal to understand at least most of the config. LSP setup is working fine and is the only source I configured for nvim-cmp:
local cmp = require("cmp")
cmp.setup {
sources = {
{ name = 'nvim_lsp' }
}
}
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities)
After some startup delay the completion is working in the sense that I see popups with proposed completions, based on information from LSP.
But I cannot select any of the proposed completions. I can just continue to type which reduces the proposed completions, but I cannot use tab, arrow keys, ... to select an entry from the popup. I saw in the docs that one can define keyboard mappings but cannot make sense out of them. They are all rather sophisticated, require a snippet package to be installed, ...
I would prefer to select the next completion via tab and to navigate them via arrow key. "Enter" should select the current one.
Could somebody show me a minimal configuration for this setup or point me to more "basic" docs?
Nvim-cmp requires you to set the mapping for tab and other keys explicitly, here is my working example:
local cmp = require'cmp'
local lspkind = require'lspkind'
cmp.setup({
snippet = {
expand = function(args)
-- For `ultisnips` user.
vim.fn["UltiSnips#Anon"](args.body)
end,
},
mapping = cmp.mapping.preset.insert({
['<Tab>'] = function(fallback)
if cmp.visible() then
cmp.select_next_item()
else
fallback()
end
end,
['<S-Tab>'] = function(fallback)
if cmp.visible() then
cmp.select_prev_item()
else
fallback()
end
end,
['<CR>'] = cmp.mapping.confirm({ select = true }),
['<C-e>'] = cmp.mapping.abort(),
['<Esc>'] = cmp.mapping.close(),
['<C-d>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
}),
sources = {
{ name = 'nvim_lsp' }, -- For nvim-lsp
{ name = 'ultisnips' }, -- For ultisnips user.
{ name = 'nvim_lua' }, -- for nvim lua function
{ name = 'path' }, -- for path completion
{ name = 'buffer', keyword_length = 4 }, -- for buffer word completion
{ name = 'omni' },
{ name = 'emoji', insert = true, } -- emoji completion
},
completion = {
keyword_length = 1,
completeopt = "menu,noselect"
},
view = {
entries = 'custom',
},
formatting = {
format = lspkind.cmp_format({
mode = "symbol_text",
menu = ({
nvim_lsp = "[LSP]",
ultisnips = "[US]",
nvim_lua = "[Lua]",
path = "[Path]",
buffer = "[Buffer]",
emoji = "[Emoji]",
omni = "[Omni]",
}),
}),
},
})
This is working great for me. The mapping part corresponds to the config for key mapping in the table. You can tweak your conf based on my config to make it work for you.

How to access nested widgets properties in Awesome

I'm trying to access the properties of the following widget:
local cpu_widget = wibox.widget{
{
max_value = 100,
paddings = 1,
border_width = 2,
widget = wibox.widget.progressbar,
},
{
font = beautiful.font_type .. "8",
widget = wibox.widget.textbox,
},
forced_height = 100,
forced_width = 20,
direction = 'east',
layout = wibox.container.rotate,
}
I've tried through the conventional way, using cpu_widget[1].value or cpu_widget[2].text, but that didn't work.
Any thoughts on how I could do that?
See the "Accessing Widgets" on https://awesomewm.org/doc/api/documentation/03-declarative-layout.md.html (I can't seem to link to this section directly)
Basically: You can add id = "bar" and id = "text" to your widgets and use these identifiers to retrieve the widgets again.
In case Elv13 ever sees this answer: You did great work on the docs!

Sorting an array of tables

This may be easier than I'm making it on myself. I am relatively new to Lua, but experienced in other languages.
I have a table that looks like this:
local state = {}
state[1] = {
show = true,
changed = true,
progressType = "static",
value = 0,
total = 9,
name = "nine",
}
state[2] = {
show = true,
changed = true,
progressType = "static",
value = 0,
total = 7,
name = "seven",
}
state[3] = {
show = true,
changed = true,
progressType = "static",
value = 0,
total = 8,
name = "eight",
}
state[4] = {
show = true,
changed = true,
progressType = "static",
value = 0,
total = 6,
name = "six",
}
What I need to do is sort each table[] entry based on the value of table.value5. I can't find any functions in the docs that expressely say they do more than just a basic table.sort so I find myself a bit stuck. Do I need to manually sort by iterating through and creating a new table with the sorted data?
I can't find any functions in the docs that expressely say they do more than just a basic table.sort so I find myself a bit stuck.
I might be misunderstanding your issue, but table.sort is exactly what you need in this case:
local state = {}
state[1] = {
total = 9,
name = "nine",
}
state[2] = {
total = 7,
name = "seven",
}
state[3] = {
total = 8,
name = "eight",
}
state[4] = {
total = 6,
name = "six",
}
-- Use table.sort with a custom anonymous function
-- to specify how to compare the nested tables.
table.sort(state, function (a, b)
return a.total < b.total
end)
for i=1, #state do
print(i, state[i].name)
end
With table.sort you can provide an optional custom function, which can be as easy or complicated as you want it to be. In this case (as per your question) it is enough to simply compare the total values of your table.

Resources