@@ -22,7 +22,6 @@ <h2 id="day-counter">Day 1</h2>
2222 < button class ="button " id ="next-day "> Proceed to Next Day</ button >
2323 </ main >
2424 < script src ="events.js "> </ script >
25- < script type ="module " src ="script.js "> </ script >
2625 <!-- The script to collect participants follows -->
2726 < script >
2827 function loadParticipants ( ) {
@@ -122,7 +121,97 @@ <h2 id="day-counter">Day 1</h2>
122121}
123122
124123}
124+ //the code to run random events follows...?
125+ let currentDay = 1 ;
126+ let pastEvents = [ ] ; // store event IDs
127+ let participants = JSON . parse ( localStorage . getItem ( "participants" ) ) || [ ] ;
128+
129+ function appendEventLog ( message ) {
130+ const logDiv = document . getElementById ( "event-log" ) ;
131+ if ( logDiv ) {
132+ logDiv . innerHTML += `<p>${ message } </p>` ;
133+ } else {
134+ console . error ( "Event log div not found." ) ;
135+ }
136+ }
137+
138+
139+ function runDayEvents ( ) {
140+ console . log ( `===== DAY ${ currentDay } =====` ) ;
141+
142+ // Loop over each event
143+ events . forEach ( event => {
144+ const validPools = event . valid ( participants ) ;
145+
146+ if ( validPools . length === 0 ) {
147+ console . log ( `No valid targets for event ${ event . id } ` ) ;
148+ return ;
149+ }
150+
151+ // Pick one random target or pair
152+ const chosen = validPools [ Math . floor ( Math . random ( ) * validPools . length ) ] ;
153+
154+ if ( Array . isArray ( chosen ) ) {
155+ // Multi-target event (like servant vs servant or servant-master betrayal)
156+ event . effects ( ...chosen , participants ) ;
157+
158+ // Generate event text using provided names
159+ const eventText = processEventTemplate ( event . description , {
160+ servant1 : chosen [ 0 ] ,
161+ servant2 : chosen [ 1 ] ,
162+ master : chosen [ 1 ] , // if second is master, works for betrayal template
163+ } ) ;
164+
165+ console . log ( eventText ) ;
166+ appendEventLog ( `Day ${ currentDay } : ${ eventText } ` ) ;
167+ } else {
168+ // Single-target event (just a master)
169+ const master = chosen ;
170+ const servant = participants . find ( p => p . type === "servant" && p . masterId === master . id ) ;
171+
172+ event . effects ( master , servant , participants ) ;
173+
174+ const eventText = processEventTemplate ( event . description , {
175+ master,
176+ servant,
177+ } ) ;
125178
179+ console . log ( eventText ) ;
180+ appendEventLog ( `Day ${ currentDay } : ${ eventText } ` ) ;
181+ }
182+ } ) ;
183+
184+ currentDay ++ ;
185+ }
186+
187+
188+ // Button for next days
189+ document . getElementById ( "next-day" ) . addEventListener ( "click" , ( ) => {
190+ runDayEvents ( ) ; // runs once for each alive master
191+ } ) ;
192+
193+
194+
195+ // eventRunner follows
196+
197+ function processEventTemplate ( template , context ) {
198+ return template . replace ( / \{ ( [ a - z A - Z 0 - 9 _ . ] + ) \} / g, ( _ , key ) => {
199+ const parts = key . split ( "." ) ;
200+ let value = context ;
201+ for ( const part of parts ) {
202+ if ( value && part in value ) {
203+ value = value [ part ] ;
204+ } else {
205+ return `{${ key } }` ; // leave if missing
206+ }
207+ }
208+ return value ;
209+ } ) ;
210+ }
211+ const eventText = processEventTemplate ( selectedEvent . description , {
212+ master : master ,
213+ servant : servant
214+ } ) ;
126215 </ script >
127216</ body >
128217</ html >
0 commit comments