@@ -32,7 +32,11 @@ console.log(document.querySelectorAll(".master-img"));
3232} ) ; //this was the code for updating the master pictures
3333window . onload = function ( ) {
3434 console . log ( "Page fully loaded! Fetching Servant data..." ) ;
35- fetchServantData ( ) ; } ;
35+ fetchServantData ( ) ;
36+
37+ console . log ( "Starting Day 1 events..." ) ;
38+ runDayEvents ( ) ;
39+ } ;
3640// now the code for the servant data:
3741async function fetchServantData ( ) {
3842 const url = 'https://api.atlasacademy.io/export/NA/basic_servant.json' ;
@@ -252,57 +256,55 @@ let currentDay = 1;
252256let pastEvents = [ ] ; // store event IDs
253257let participants = JSON . parse ( localStorage . getItem ( "participants" ) ) || [ ] ;
254258
255- function processEventTemplate ( template , master , servant ) {
256- return template
257- . replace ( / { m a s t e r .n a m e } / g, master . name )
258- . replace ( / { m a s t e r .p r o n o u n s .s u b j e c t } / g, master . pronouns . subject )
259- . replace ( / { m a s t e r .p r o n o u n s .o b j e c t } / g, master . pronouns . object )
260- . replace ( / { m a s t e r .p r o n o u n s .p o s s e s s i v e } / g, master . pronouns . possessive )
261- . replace ( / { s e r v a n t .n a m e } / g, servant ? servant . name : "no servant" ) ;
262- }
263259
264260function runDayEvents ( ) {
265- const aliveMasters = participants . filter ( p => p . type === "master" && p . status === "alive" ) ;
266- const aliveServants = participants . filter ( p => p . type === "servant" && p . status === "alive" ) ;
267-
268261 console . log ( `===== DAY ${ currentDay } =====` ) ;
269262
270- aliveMasters . forEach ( master => {
271- const servant = aliveServants . find ( s => s . id === master . servantId ) ;
263+ // Loop over each event
264+ events . forEach ( event => {
265+ const validPools = event . valid ( participants ) ;
272266
273- const validEvents = events . filter ( event => {
274- const validPool = event . valid ( participants , pastEvents ) ;
275- return validPool . includes ( master ) ;
276- } ) ;
277-
278- if ( validEvents . length === 0 ) {
279- console . log ( `No valid events for ${ master . name } ` ) ;
267+ if ( validPools . length === 0 ) {
268+ console . log ( `No valid targets for event ${ event . id } ` ) ;
280269 return ;
281270 }
282271
283- const selectedEvent = validEvents [ Math . floor ( Math . random ( ) * validEvents . length ) ] ;
272+ // Pick one random target or pair
273+ const chosen = validPools [ Math . floor ( Math . random ( ) * validPools . length ) ] ;
284274
285- // Apply effects
286- selectedEvent . effects ( master , servant , participants ) ;
275+ if ( Array . isArray ( chosen ) ) {
276+ // Multi-target event (like servant vs servant or servant-master betrayal)
277+ event . effects ( ...chosen , participants ) ;
287278
288- // Save event id
289- pastEvents . push ( selectedEvent . id ) ;
279+ // Generate event text using provided names
280+ const eventText = processEventTemplate ( event . description , {
281+ servant1 : chosen [ 0 ] ,
282+ servant2 : chosen [ 1 ] ,
283+ master : chosen [ 1 ] , // if second is master, works for betrayal template
284+ } ) ;
290285
291- // Display event text
292- const eventText = processEventTemplate ( selectedEvent . description , master , servant ) ;
293- console . log ( eventText ) ;
286+ console . log ( eventText ) ;
287+ appendEventLog ( `Day ${ currentDay } : ${ eventText } ` ) ;
288+ } else {
289+ // Single-target event (just a master)
290+ const master = chosen ;
291+ const servant = participants . find ( p => p . type === "servant" && p . masterId === master . id ) ;
292+
293+ event . effects ( master , servant , participants ) ;
294+
295+ const eventText = processEventTemplate ( event . description , {
296+ master,
297+ servant,
298+ } ) ;
294299
295- const logDiv = document . getElementById ( "event-log" ) ;
296- logDiv . innerHTML += `<p>Day ${ currentDay } : ${ eventText } </p>` ;
300+ console . log ( eventText ) ;
301+ appendEventLog ( `Day ${ currentDay } : ${ eventText } ` ) ;
302+ }
297303 } ) ;
298304
299305 currentDay ++ ;
300306}
301307
302- // Run day 1 immediately after load
303- window . onload = ( ) => {
304- runDayEvents ( ) ; // runs once for each master
305- } ;
306308
307309// Button for next days
308310document . getElementById ( "next-day" ) . addEventListener ( "click" , ( ) => {
@@ -327,34 +329,7 @@ function processEventTemplate(template, context) {
327329 return value ;
328330 } ) ;
329331}
330-
331- function runRandomEvent ( participants ) {
332- const aliveMasters = participants . filter ( p => p . type === "master" && p . status === "alive" ) ;
333- const aliveServants = participants . filter ( p => p . type === "servant" && p . status === "alive" ) ;
334-
335- const randomMaster = aliveMasters [ Math . floor ( Math . random ( ) * aliveMasters . length ) ] ;
336- const randomServant = participants . find ( p => p . id === randomMaster . servantId ) ;
337-
338- const validEvents = events . filter ( event => {
339- const validPool = event . valid ( participants ) ;
340- return validPool . includes ( randomMaster ) ;
341- } ) ;
342-
343- if ( validEvents . length === 0 ) {
344- console . warn ( "No valid events available." ) ;
345- return ;
346- }
347-
348- const event = validEvents [ Math . floor ( Math . random ( ) * validEvents . length ) ] ;
349-
350- // Run effects
351- event . effects ( randomMaster , randomServant , participants ) ;
352-
353- // Generate event text
354- const eventText = processEventTemplate ( event . description , {
355- master : randomMaster ,
356- servant : randomServant
357- } ) ;
358-
359- console . log ( eventText ) ;
360- }
332+ const eventText = processEventTemplate ( selectedEvent . description , {
333+ master : master ,
334+ servant : servant
335+ } ) ;
0 commit comments