-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PIXEL selling and buyEnergy #706
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
|
||
const {debugLog} = require('./logging'); | ||
const pixelLog = (message) => { | ||
debugLog('pixel', message); | ||
}; | ||
|
||
module.exports.handlePixel = function() { | ||
if (typeof PIXEL !== 'undefined') { | ||
// generate PIXEL | ||
if (Game.cpu.bucket >= PIXEL_CPU_COST + config.pixel.minBucketAfter) { | ||
global.load = Math.round(Game.cpu.getUsed()); | ||
Game.cpu.generatePixel(); | ||
const pixel = Game.resources[PIXEL] || ''; | ||
const deltaPixel = Game.time - (global.data.pixel || 0); | ||
pixelLog(`PIXEL generated!\tCurrent ${pixel + 1}\t delta: ${deltaPixel} \t load: ${global.load}`); | ||
global.data.pixel = Game.time; | ||
} | ||
|
||
// sell PIXEL, make money to buy energy | ||
if (config.pixel.sell && Game.resources[PIXEL] > config.pixel.minPixelAmount) { | ||
const buyOrder = _.sortBy(Game.market.getAllOrders({type: 'buy', resourceType: PIXEL}), (o) => -o.price)[0]; | ||
if (buyOrder) { | ||
const history = Game.market.getHistory(PIXEL); | ||
const lastDay = history[history.length - 1]; | ||
if (lastDay) { | ||
const minPriceToSell = (lastDay.avgPrice - (config.pixel.allowedSalesHistoryDeviation * lastDay.stddevPrice)).toPrecision(3); | ||
if (buyOrder.price >= minPriceToSell) { | ||
pixelLog(`PIXEL pices: ${buyOrder.price}\tamount: ${buyOrder.amount}\tlastDay.avgPrice: ${lastDay.avgPrice}\tlastDay.stddevPrice: ${lastDay.stddevPrice}\tminPrice: ${minPriceToSell}`); | ||
const amount = Math.min(buyOrder.amount, Game.resources[PIXEL] - config.pixel.minPixelAmount); | ||
Game.market.deal(buyOrder.id, amount); | ||
pixelLog(`PIXEL sold!\t${buyOrder.price} @ ${amount}`); | ||
} | ||
} | ||
} else { | ||
pixelLog(`PIXEL has no bestOrder ${buyOrder}`); | ||
} | ||
} | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,6 +152,9 @@ global.config = { | |
|
||
pixel: { | ||
enabled: false, | ||
sell: true, | ||
allowedSalesHistoryDeviation: 0.05, | ||
minPixelAmount: 500, | ||
minBucketAfter: 2500, | ||
}, | ||
|
||
|
@@ -289,11 +292,15 @@ global.config = { | |
maxBuyPrice: 0.5, | ||
// buyByOwnOrders: true, | ||
buyOrderPriceMultiplicand: 0.5, | ||
|
||
// buy energy, let make use of our credits | ||
buyEnergy: { | ||
enabled: true, | ||
allowedSalesHistoryDeviation: 0.05, | ||
}, | ||
// buy power if we have more credits than config.market.minCredits | ||
buyPower: false, | ||
// 300M credits | ||
minCredits: 300000000, | ||
// 300K credits | ||
minCredits: 300000, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should have as minCredits the price of an access key, which is currently around 500M. What do you think? |
||
// disable to use power only in gathered room | ||
sendPowerOwnRoom: true, | ||
// equalizes the energy between your rooms via terminal | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
|
||
|
||
const {debugLog} = require('./logging'); | ||
|
||
/** | ||
|
@@ -68,7 +66,7 @@ function getQuest(transaction, data) { | |
/** | ||
* haveActiveQuest | ||
* | ||
* @return {bool} | ||
* @return {boolean} | ||
*/ | ||
function haveActiveQuest() { | ||
if (!global.data.activeQuest) { | ||
|
@@ -93,7 +91,7 @@ module.exports.haveActiveQuest = haveActiveQuest; | |
* getQuestFromTransactionDescription | ||
* | ||
* @param {object} description | ||
* @return {bool} | ||
* @return {boolean} | ||
*/ | ||
function getQuestFromTransactionDescription(description) { | ||
let data; | ||
|
@@ -107,7 +105,6 @@ function getQuestFromTransactionDescription(description) { | |
debugLog('quests', 'Quest transaction: No type'); | ||
return false; | ||
} | ||
console.log(JSON.stringify(data)); | ||
for (const key of ['type', 'action', 'id']) { | ||
if (!data[key]) { | ||
debugLog('quests', `Incoming transaction no Quest: No ${key}`); | ||
|
@@ -129,17 +126,22 @@ function getQuestFromTransactionDescription(description) { | |
* checkQuestForAcceptance | ||
* | ||
* @param {object} transaction | ||
* @return {bool} | ||
* @return {boolean} | ||
*/ | ||
function checkQuestForAcceptance(transaction) { | ||
Memory.quests = Memory.quests || {}; | ||
transaction.description = transaction.description || JSON.stringify({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand this change. |
||
type: 'quest', | ||
action: 'apply', | ||
id: _.first(Memory.quests), | ||
}); | ||
const data = getQuestFromTransactionDescription(transaction.description); | ||
if (!data) { | ||
return false; | ||
} | ||
if (Memory.quests[data.id]) { | ||
console.log(`Quest already ongoing ${JSON.stringify(data)}`); | ||
return; | ||
return false; | ||
} | ||
const quest = getQuest(transaction, data); | ||
console.log(`Found quest acceptance on transaction ${JSON.stringify(quest)}`); | ||
|
@@ -158,6 +160,7 @@ function checkQuestForAcceptance(transaction) { | |
const terminalResponse = room.terminal.send(RESOURCE_ENERGY, 100, transaction.from, JSON.stringify(response)); | ||
console.log(`terminalResponse ${JSON.stringify(terminalResponse)}`); | ||
// TODO find reserver in room and remove quest hint | ||
return true; | ||
} | ||
|
||
module.exports.checkQuestForAcceptance = checkQuestForAcceptance; | ||
|
@@ -166,35 +169,46 @@ module.exports.checkQuestForAcceptance = checkQuestForAcceptance; | |
* checkAppliedQuestForAcceptance | ||
* | ||
* @param {object} transaction | ||
* @return {bool} | ||
* @return {boolean} | ||
*/ | ||
function checkAppliedQuestForAcceptance(transaction) { | ||
let response; | ||
try { | ||
const response = JSON.parse(transaction.description); | ||
if (!response.type) { | ||
debugLog('quests', `No type: ${JSON.stringify(response)}`); | ||
} | ||
if (response.type !== 'quest') { | ||
debugLog('quests', `Wrong type: ${JSON.stringify(response)}`); | ||
try { | ||
response = JSON.parse(transaction.description); | ||
} catch (e) { | ||
debugLog('quests', e.toString(), JSON.stringify(transaction)); | ||
return false; | ||
} | ||
if (response.action) { | ||
debugLog('quests', `Action exist type: ${JSON.stringify(response)}`); | ||
return false; | ||
if (response) { | ||
if (!response.type) { | ||
debugLog('quests', `No type: ${JSON.stringify(response)}`); | ||
return false; | ||
} | ||
if (response.type !== 'quest') { | ||
debugLog('quests', `Wrong type: ${JSON.stringify(response)}`); | ||
return false; | ||
} | ||
if (response.action) { | ||
debugLog('quests', `Action exist type: ${JSON.stringify(response)}`); | ||
return false; | ||
} | ||
debugLog('quests', `Quest accept transaction: ${JSON.stringify(response)}`); | ||
if (!haveActiveQuest()) { | ||
debugLog('quests', 'No active quest'); | ||
return false; | ||
} | ||
global.data.activeQuest.state = 'active'; | ||
global.data.activeQuest.accept = response; | ||
debugLog('quests', `activeQuest: ${JSON.stringify(global.data.activeQuest)}`); | ||
return true; | ||
} | ||
debugLog('quests', `Quest accept transaction: ${JSON.stringify(response)}`); | ||
if (!haveActiveQuest()) { | ||
debugLog('quests', 'No active quest'); | ||
return false; | ||
} | ||
global.data.activeQuest.state = 'active'; | ||
global.data.activeQuest.accept = response; | ||
debugLog('quests', `activeQuest: ${JSON.stringify(global.data.activeQuest)}`); | ||
} catch (e) { | ||
console.log('checkAppliedQuestForAcceptance'); | ||
console.log(e); | ||
console.log(e.stack); | ||
return false; | ||
} | ||
} | ||
|
||
module.exports.checkAppliedQuestForAcceptance = checkAppliedQuestForAcceptance; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about the
brain
prefix anymore. I would just call itpixel.js