Skip to content

Commit 633d0d7

Browse files
committed
Merge pull request #10 from sshadowart/stamina-hud
Configurable Market Prices
2 parents 0133fe5 + 5bfd2a6 commit 633d0d7

File tree

6 files changed

+187
-95
lines changed

6 files changed

+187
-95
lines changed

configs/Prices.ini

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; Market prices for official XenoBot scripts
2+
; :::::::::::::::
3+
4+
; These prices are loaded by every char
5+
[Default]
6+
"golden helmet" = 250
7+
"dragon scale legs" = 3000
8+
9+
; These prices are loaded only if your character name is "Character One".
10+
; Change the name to your character name.
11+
[Character One]
12+
"longsword" = 25
13+
14+
[Character Two]
15+
"pickaxe" = 2

src/001-constants.lua

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local MINIMUM_XENO_VERSION = 1169
22
local LIB_REVISION = '{{VERSION}}'
33
local LIB_CONFIG = [[{{CONFIG}}]]
4+
local LIB_PRICES_CONFIG = [[{{PRICES_CONFIG}}]]
45
local FOLDER_SETTINGS_PATH = '..\\Settings\\'
56
local FOLDER_LOGS_PATH = '..\\Log\\'
67
local FOLDER_CONFIG_PATH = '..\\Configs\\'

src/002-states.lua

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ local _script = {
1818
town = "{{SCRIPT_TOWN}}",
1919
vocation = "{{SCRIPT_VOCATION}}",
2020
configHash = "{{SCRIPT_CONFIG_HASH}}",
21+
pricesConfigHash = "{{PRICES_CONFIG_HASH}}",
2122

2223
townexit = nil,
2324
channel = nil,

src/006-hud.lua

+7-3
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ Hud = (function()
360360
end
361361
end
362362

363+
local function getItemValue(lootID)
364+
return _config['Prices'][lootID] or xeno.getItemValue(lootID)
365+
end
366+
363367
local function hudTrack(type, itemid, amount, skipUpdate)
364368
-- Loot / Supplies
365369
local hudItem = _hud.index[type][itemid]
@@ -374,8 +378,9 @@ Hud = (function()
374378
--local hudValue = values and formatNumber((itemValue / (values.d * 60)) * count) .. ' gp' or '--'
375379

376380
-- Get current count and values
377-
local calculateValue = type == 'Supplies' and xeno.getItemCost or xeno.getItemValue
381+
local calculateValue = type == 'Supplies' and xeno.getItemCost or getItemValue
378382
local itemValue = calculateValue(itemid)
383+
379384
local hudCount = (hudItem.rawCount or 0) + amount
380385
local hudValue = (hudItem.rawValue or 0) + (itemValue * amount)
381386

@@ -395,7 +400,6 @@ Hud = (function()
395400
else
396401
text = formatNumber(hudCount) .. ' (' .. formatNumber(hudValue) .. ' gp)'
397402
end
398-
399403
hudItemUpdate(type, itemid, text, skipUpdate)
400404
end
401405

@@ -459,7 +463,7 @@ Hud = (function()
459463
-- If difference is positive and corpse open, add to totals
460464
if difference > 0 and isCorpseOpen() then
461465
-- Add to overall total
462-
local value = (xeno.getItemValue(lootID) * difference)
466+
local value = (getItemValue(lootID) * difference)
463467
totalQueryValue = totalQueryValue + value
464468
hudTrack('Loot', lootID, difference)
465469
end

src/007-ini.lua

+66-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ Ini = (function()
2525
section = s
2626
tbl[section] = tbl[section] or {}
2727
end
28+
2829
local key, value = string.match(line, "^([^%s]+)%s+=%s+([^;]+)")
30+
31+
-- If the first try didnt work, check for a multi-word key
32+
if not key then
33+
key, value = string.match(line, '"(.+)"%s+=%s+([^;]*)')
34+
end
2935
if key and value then
3036
-- Type casting
3137
if tonumber(value) ~= nil then
@@ -57,6 +63,62 @@ Ini = (function()
5763
return tbl
5864
end
5965

66+
local function loadMarketPrices()
67+
local file = io.open(PRICES_CONFIG_PATH, 'r')
68+
69+
-- Found config, compare config version against embedded config
70+
if file then
71+
local match = false
72+
for line in file:lines() do
73+
if string.match(line, '^; ::' .. _script.pricesConfigHash .. '$') then
74+
match = true
75+
break
76+
end
77+
end
78+
if not match then
79+
log('Updating script config file...')
80+
file:close()
81+
file = nil
82+
end
83+
84+
-- Could not find a config anywhere (or we wanted to update)
85+
else
86+
-- Write the embedded config to disk
87+
local defaultConfig = io.open(configPath, 'w+')
88+
if defaultConfig then
89+
defaultConfig:write(LIB_PRICES_CONFIG)
90+
defaultConfig:close()
91+
else
92+
error('Could not write default config file.')
93+
end
94+
95+
-- Try again
96+
file = io.open(configPath, 'r')
97+
end
98+
99+
local priceConfig = loadIniFile(file)
100+
local prices = {}
101+
102+
-- Load default prices
103+
if priceConfig['Default'] then
104+
for name, price in pairs(priceConfig['Default']) do
105+
local id = xeno.getItemIDByName(name)
106+
prices[id] = price
107+
end
108+
end
109+
110+
-- Load and overwrite with character specific config
111+
if priceConfig[getSelfName()] then
112+
for name, price in pairs(priceConfig[getSelfName()]) do
113+
local id = xeno.getItemIDByName(name)
114+
prices[id] = price
115+
end
116+
end
117+
118+
return prices
119+
end
120+
121+
60122
local function updateSupplyConfig()
61123
local function loadBlockSection(sectionName, extras)
62124
local section = _config[sectionName]
@@ -171,7 +233,7 @@ Ini = (function()
171233
tbl['Anti Lure']['Creatures'] = lureTbl
172234
else
173235
tbl['Anti Lure']['Creatures'] = {
174-
[string.lower(lureCreatures)] = true
236+
[string.lower(lureCreatures)] = true
175237
}
176238
end
177239
end
@@ -183,6 +245,7 @@ Ini = (function()
183245
end
184246

185247
_config = tbl
248+
_config['Prices'] = loadMarketPrices()
186249
updateSupplyConfig()
187250
callback()
188251
end
@@ -270,7 +333,7 @@ Ini = (function()
270333
-- Trigger within the range
271334
['Experience'] = function(req)
272335
if req == 0 then return false end
273-
local min, max = parseRange(req)
336+
local min, max = parseRange(req)
274337
local timediff = os.time() - _script.start
275338
local gain = xeno.getSelfExperience() - _script.baseExp
276339
local hourlyexp = tonumber(math.floor(gain / (timediff / 3600))) or 0
@@ -281,7 +344,7 @@ Ini = (function()
281344
-- Trigger within the range
282345
['Profit'] = function(req)
283346
if req == 0 then return false end
284-
local min, max = parseRange(req)
347+
local min, max = parseRange(req)
285348
local timediff = os.time() - _script.start
286349
local totalLooted = _hud.index['Statistics']['Looted'].value
287350
local totalWaste = _hud.index['Statistics']['Wasted'].value

tools/bundle.js

+97-89
Original file line numberDiff line numberDiff line change
@@ -69,101 +69,109 @@ function buildFile(spawnName, luaOutputData, outputPath, outputName, buildCallba
6969

7070
// Load spawn config
7171
fs.readFile(`./configs/${spawnName}.ini`, function (err, configData) {
72-
if (err) throw err;
73-
74-
// Determine vocation from spawnName
75-
let vocationName = 'unknown';
76-
for (let i = 0; i < vocationTags.length; i++) {
77-
let tag = vocationTags[i];
78-
if (spawnName.indexOf(tag) !== -1) {
79-
vocationName = vocationsMap[tag];
80-
break;
81-
}
82-
}
83-
84-
// Build script version
85-
let version;
86-
if (process.env.TRAVIS_TAG)
87-
version = process.env.TRAVIS_TAG;
88-
else if(process.env.TRAVIS_BRANCH)
89-
version = `${process.env.TRAVIS_BRANCH}#${process.env.TRAVIS_BUILD_NUMBER}`;
90-
else
91-
version = 'local';
92-
93-
// Replace tokens
94-
const configHash = crypto.createHash('md5').update(configData).digest('hex');
95-
let data = luaOutputData.toString('utf8');
96-
97-
data = data.replace('{{VERSION}}', version);
98-
data = data.replace('{{SCRIPT_TOWN}}', townName);
99-
data = data.replace('{{SCRIPT_NAME}}', spawnName);
100-
data = data.replace('{{SCRIPT_SLUG}}', outputName);
101-
data = data.replace('{{SCRIPT_VOCATION}}', vocationName);
102-
data = data.replace('{{SCRIPT_CONFIG_HASH}}', configHash);
103-
104-
// Insert config
105-
data = data.replace('{{CONFIG}}', configData.toString('utf8').replace(':::::::::::::::', `::${configHash}`));
106-
107-
// Base 64 encode lua
108-
let encodedLua = new Buffer(data).toString('base64');
109-
let encodedReload = new Buffer(reloadScript).toString('base64');
110-
let combinedWaypoints;
11172

112-
let developmentXML = `
113-
<panel name="Scripter">
114-
<control name="RunningScriptList">
115-
<script name=".ox.${timestamp}.lua"><![CDATA[${encodedLua}]]></script>
116-
<script name=".sync.${timestamp}.lua"><![CDATA[${encodedReload}]]></script>
117-
</control>
118-
</panel>`;
119-
120-
let productionXML = `
121-
<panel name="Scripter">
122-
<control name="RunningScriptList">
123-
<script name="${outputName.replace('.xbst', '.lua')}" noprompt="1"><![CDATA[${encodedLua}]]></script>
124-
</control>
125-
</panel>`;
126-
127-
// Get all the town waypoints
128-
let townPaths = glob.sync('./waypoints/towns/*.json'),
129-
townWaypoints = [];
130-
131-
readm(townPaths, (err, towns) => {
132-
if (err) {
133-
throw err;
134-
}
73+
fs.readFile(`./configs/Prices.ini`, function (pricesErr, pricesConfigData) {
74+
if (err || pricesErr) throw err;
13575

136-
// Iterate through towns
137-
towns.forEach((waypoints) => {
138-
let townData = JSON.parse(waypoints);
139-
// Iterate through waypoints in each town
140-
townData.forEach((item) => {
141-
// Add waypoint string to array
142-
townWaypoints.push(`\n\t\t<item text="${item.label}" tag="${item.tag}"/>`);
143-
});
144-
});
14576

146-
// Combine waypoints
147-
townWaypoints.push('\n');
148-
combinedWaypoints = townWaypoints.join('');
77+
// Determine vocation from spawnName
78+
let vocationName = 'unknown';
79+
for (let i = 0; i < vocationTags.length; i++) {
80+
let tag = vocationTags[i];
81+
if (spawnName.indexOf(tag) !== -1) {
82+
vocationName = vocationsMap[tag];
83+
break;
84+
}
85+
}
14986

150-
// Combine spawn file with town waypoints
151-
let insertPoint = '<control name="WaypointList">' + os.EOL;
152-
let xbstCombinedData = xbstData.toString('utf8');
153-
xbstCombinedData = xbstCombinedData.replace(insertPoint, insertPoint + combinedWaypoints);
154-
155-
// Inject sync script for live reloading
156-
if (process.env.LIVE_RELOAD)
157-
xbstCombinedData += '\n' + developmentXML;
158-
// Production lua
87+
// Build script version
88+
let version;
89+
if (process.env.TRAVIS_TAG)
90+
version = process.env.TRAVIS_TAG;
91+
else if(process.env.TRAVIS_BRANCH)
92+
version = `${process.env.TRAVIS_BRANCH}#${process.env.TRAVIS_BUILD_NUMBER}`;
15993
else
160-
xbstCombinedData += '\n' + productionXML;
94+
version = 'local';
95+
96+
// Replace tokens
97+
const configHash = crypto.createHash('md5').update(configData).digest('hex');
98+
const pricesConfigHash = crypto.createHash('md5').update(pricesConfigData).digest('hex');
99+
let data = luaOutputData.toString('utf8');
100+
101+
data = data.replace('{{VERSION}}', version);
102+
data = data.replace('{{SCRIPT_TOWN}}', townName);
103+
data = data.replace('{{SCRIPT_NAME}}', spawnName);
104+
data = data.replace('{{SCRIPT_SLUG}}', outputName);
105+
data = data.replace('{{SCRIPT_VOCATION}}', vocationName);
106+
data = data.replace('{{SCRIPT_CONFIG_HASH}}', configHash);
107+
data = data.replace('{{PRICES_CONFIG_HASH}}', pricesConfigHash);
108+
109+
// Insert config
110+
data = data.replace('{{CONFIG}}', configData.toString('utf8').replace(':::::::::::::::', `::${configHash}`));
111+
// Insert prices config
112+
data = data.replace('{{PRICES_CONFIG}}', pricesConfigData.toString('utf8').replace(':::::::::::::::', `::${pricesConfigHash}`));
113+
114+
// Base 64 encode lua
115+
let encodedLua = new Buffer(data).toString('base64');
116+
let encodedReload = new Buffer(reloadScript).toString('base64');
117+
let combinedWaypoints;
118+
119+
let developmentXML = `
120+
<panel name="Scripter">
121+
<control name="RunningScriptList">
122+
<script name=".ox.${timestamp}.lua"><![CDATA[${encodedLua}]]></script>
123+
<script name=".sync.${timestamp}.lua"><![CDATA[${encodedReload}]]></script>
124+
</control>
125+
</panel>`;
126+
127+
let productionXML = `
128+
<panel name="Scripter">
129+
<control name="RunningScriptList">
130+
<script name="${outputName.replace('.xbst', '.lua')}" noprompt="1"><![CDATA[${encodedLua}]]></script>
131+
</control>
132+
</panel>`;
133+
134+
// Get all the town waypoints
135+
let townPaths = glob.sync('./waypoints/towns/*.json'),
136+
townWaypoints = [];
137+
138+
readm(townPaths, (err, towns) => {
139+
if (err) {
140+
throw err;
141+
}
142+
143+
// Iterate through towns
144+
towns.forEach((waypoints) => {
145+
let townData = JSON.parse(waypoints);
146+
// Iterate through waypoints in each town
147+
townData.forEach((item) => {
148+
// Add waypoint string to array
149+
townWaypoints.push(`\n\t\t<item text="${item.label}" tag="${item.tag}"/>`);
150+
});
151+
});
161152

162-
// Save XBST
163-
fs.writeFile(outputPath, xbstCombinedData, function (err) {
164-
console.log(colors.green(spawnName), outputPath);
165-
if (buildCallback)
166-
buildCallback(xbstCombinedData, timestamp);
153+
// Combine waypoints
154+
townWaypoints.push('\n');
155+
combinedWaypoints = townWaypoints.join('');
156+
157+
// Combine spawn file with town waypoints
158+
let insertPoint = '<control name="WaypointList">' + os.EOL;
159+
let xbstCombinedData = xbstData.toString('utf8');
160+
xbstCombinedData = xbstCombinedData.replace(insertPoint, insertPoint + combinedWaypoints);
161+
162+
// Inject sync script for live reloading
163+
if (process.env.LIVE_RELOAD)
164+
xbstCombinedData += '\n' + developmentXML;
165+
// Production lua
166+
else
167+
xbstCombinedData += '\n' + productionXML;
168+
169+
// Save XBST
170+
fs.writeFile(outputPath, xbstCombinedData, function (err) {
171+
console.log(colors.green(spawnName), outputPath);
172+
if (buildCallback)
173+
buildCallback(xbstCombinedData, timestamp);
174+
});
167175
});
168176
});
169177
});

0 commit comments

Comments
 (0)