1-
2-
31const { debugLog} = require ( './logging' ) ;
42
53/**
@@ -68,7 +66,7 @@ function getQuest(transaction, data) {
6866/**
6967 * haveActiveQuest
7068 *
71- * @return {bool }
69+ * @return {boolean }
7270 */
7371function haveActiveQuest ( ) {
7472 if ( ! global . data . activeQuest ) {
@@ -93,7 +91,7 @@ module.exports.haveActiveQuest = haveActiveQuest;
9391 * getQuestFromTransactionDescription
9492 *
9593 * @param {object } description
96- * @return {bool }
94+ * @return {boolean }
9795 */
9896function getQuestFromTransactionDescription ( description ) {
9997 let data ;
@@ -107,7 +105,6 @@ function getQuestFromTransactionDescription(description) {
107105 debugLog ( 'quests' , 'Quest transaction: No type' ) ;
108106 return false ;
109107 }
110- console . log ( JSON . stringify ( data ) ) ;
111108 for ( const key of [ 'type' , 'action' , 'id' ] ) {
112109 if ( ! data [ key ] ) {
113110 debugLog ( 'quests' , `Incoming transaction no Quest: No ${ key } ` ) ;
@@ -129,17 +126,22 @@ function getQuestFromTransactionDescription(description) {
129126 * checkQuestForAcceptance
130127 *
131128 * @param {object } transaction
132- * @return {bool }
129+ * @return {boolean }
133130 */
134131function checkQuestForAcceptance ( transaction ) {
135132 Memory . quests = Memory . quests || { } ;
133+ transaction . description = transaction . description || JSON . stringify ( {
134+ type : 'quest' ,
135+ action : 'apply' ,
136+ id : _ . first ( Memory . quests )
137+ } ) ;
136138 const data = getQuestFromTransactionDescription ( transaction . description ) ;
137139 if ( ! data ) {
138140 return false ;
139141 }
140142 if ( Memory . quests [ data . id ] ) {
141143 console . log ( `Quest already ongoing ${ JSON . stringify ( data ) } ` ) ;
142- return ;
144+ return false ;
143145 }
144146 const quest = getQuest ( transaction , data ) ;
145147 console . log ( `Found quest acceptance on transaction ${ JSON . stringify ( quest ) } ` ) ;
@@ -158,6 +160,7 @@ function checkQuestForAcceptance(transaction) {
158160 const terminalResponse = room . terminal . send ( RESOURCE_ENERGY , 100 , transaction . from , JSON . stringify ( response ) ) ;
159161 console . log ( `terminalResponse ${ JSON . stringify ( terminalResponse ) } ` ) ;
160162 // TODO find reserver in room and remove quest hint
163+ return true ;
161164}
162165
163166module . exports . checkQuestForAcceptance = checkQuestForAcceptance ;
@@ -166,35 +169,46 @@ module.exports.checkQuestForAcceptance = checkQuestForAcceptance;
166169 * checkAppliedQuestForAcceptance
167170 *
168171 * @param {object } transaction
169- * @return {bool }
172+ * @return {boolean }
170173 */
171174function checkAppliedQuestForAcceptance ( transaction ) {
175+ let response
172176 try {
173- const response = JSON . parse ( transaction . description ) ;
174- if ( ! response . type ) {
175- debugLog ( 'quests' , `No type: ${ JSON . stringify ( response ) } ` ) ;
176- }
177- if ( response . type !== 'quest' ) {
178- debugLog ( 'quests' , `Wrong type: ${ JSON . stringify ( response ) } ` ) ;
177+ try {
178+ response = JSON . parse ( transaction . description ) ;
179+ } catch ( e ) {
180+ debugLog ( 'quests' , e . toString ( ) , JSON . stringify ( transaction ) ) ;
179181 return false ;
180182 }
181- if ( response . action ) {
182- debugLog ( 'quests' , `Action exist type: ${ JSON . stringify ( response ) } ` ) ;
183- return false ;
183+ if ( response ) {
184+ if ( ! response . type ) {
185+ debugLog ( 'quests' , `No type: ${ JSON . stringify ( response ) } ` ) ;
186+ return false ;
187+ }
188+ if ( response . type !== 'quest' ) {
189+ debugLog ( 'quests' , `Wrong type: ${ JSON . stringify ( response ) } ` ) ;
190+ return false ;
191+ }
192+ if ( response . action ) {
193+ debugLog ( 'quests' , `Action exist type: ${ JSON . stringify ( response ) } ` ) ;
194+ return false ;
195+ }
196+ debugLog ( 'quests' , `Quest accept transaction: ${ JSON . stringify ( response ) } ` ) ;
197+ if ( ! haveActiveQuest ( ) ) {
198+ debugLog ( 'quests' , 'No active quest' ) ;
199+ return false ;
200+ }
201+ global . data . activeQuest . state = 'active' ;
202+ global . data . activeQuest . accept = response ;
203+ debugLog ( 'quests' , `activeQuest: ${ JSON . stringify ( global . data . activeQuest ) } ` ) ;
204+ return true ;
184205 }
185- debugLog ( 'quests' , `Quest accept transaction: ${ JSON . stringify ( response ) } ` ) ;
186- if ( ! haveActiveQuest ( ) ) {
187- debugLog ( 'quests' , 'No active quest' ) ;
188- return false ;
189- }
190- global . data . activeQuest . state = 'active' ;
191- global . data . activeQuest . accept = response ;
192- debugLog ( 'quests' , `activeQuest: ${ JSON . stringify ( global . data . activeQuest ) } ` ) ;
193206 } catch ( e ) {
194207 console . log ( 'checkAppliedQuestForAcceptance' ) ;
195208 console . log ( e ) ;
196209 console . log ( e . stack ) ;
197210 return false ;
198211 }
199212}
213+
200214module . exports . checkAppliedQuestForAcceptance = checkAppliedQuestForAcceptance ;
0 commit comments