|
|
Line 1: |
Line 1: |
| local json = require("Module:GEPrices/data.json") | | -- Module table |
| | local prices = {} |
|
| |
|
| -- Function to read data from JSON file | | -- Function to get price by item name |
| local function readJSON(filename)
| | function prices.getPrice(itemName) |
| local file = io.open(filename, "r") -- Open file in read mode | | local data = mw.loadData("Module:GEPrices/data") |
| if not file then return nil end -- If file doesn't exist, return nil
| | return data[itemName] or nil |
| | |
| local content = file:read("*a") -- Read the entire content
| |
| file:close() -- Close the file
| |
| return json.decode(content) -- Decode JSON content and return
| |
| end
| |
| | |
| -- Main function
| |
| local function main()
| |
| local filename = ('GEPrices/data.json') -- JSON file name
| |
| local data = readJSON(filename) -- Read data from JSON file
| |
| if not data then | |
| print("Error: Could not read JSON file")
| |
| return
| |
| end
| |
| | |
| -- Take input from user
| |
| io.write("Enter the name of the item: ")
| |
| local itemName = io.read()
| |
| | |
| -- Search for the item in the data
| |
| local price = data[itemName]
| |
| | |
| if price then
| |
| print("Price of", itemName, ":", price)
| |
| else
| |
| print("Item not found")
| |
| end
| |
| end | | end |
|
| |
|
| main() -- Run the main function
| | return prices |