Skip to content

Commit

Permalink
refactor: eliminado metodo draw de bucle de eventos
Browse files Browse the repository at this point in the history
  • Loading branch information
user committed Jun 11, 2024
1 parent a7da48c commit 1aceb9d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 38 deletions.
2 changes: 1 addition & 1 deletion public/game/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<script
language="javascript"
type="text/javascript"
src="./sketch.js?v3"
src="./sketch.js"
></script>
<style>
main {
Expand Down
67 changes: 30 additions & 37 deletions src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,42 +82,7 @@ export default async function (programFQN: Name, options: Options): Promise<void
}

export const getGameInterpreter = (environment: Environment, io: Server): Interpreter => {

Check warning on line 84 in src/commands/run.ts

View workflow job for this annotation

GitHub Actions / test

'io' is defined but never used. Allowed unused args must match /^_/u
const nativesAndDraw = {
...natives,
draw: {
drawer: {
*apply() {
try {
const game = interpreter?.object('wollok.game.game')
const visuals = getVisuals(game, interpreter)
io.emit('visuals', visuals)

const gameSounds = game.get('sounds')?.innerCollection ?? []
const mappedSounds = gameSounds.map(sound =>
[
sound.id,
sound.get('file')!.innerString!,
sound.get('status')!.innerString!,
sound.get('volume')!.innerNumber!,
sound.get('loop')!.innerBoolean!,
])
io.emit('updateSound', { soundInstances: mappedSounds })
} catch (error: any) {
logger.error(failureDescription(error instanceof WollokException ? error.message : 'Exception while executing the program'))
const debug = logger.getLevel() <= logger.levels.DEBUG
if (debug) logger.error(error)
interpreter.send('stop', gameSingleton)
}
},
},
},
}

const interpreter = interpret(environment, nativesAndDraw)

const gameSingleton = interpreter?.object(GAME_MODULE)
const drawer = interpreter.object('draw.drawer')
interpreter.send('onTick', gameSingleton, interpreter.reify(17), interpreter.reify('renderizar'), drawer)
const interpreter = interpret(environment, natives)

Check warning on line 85 in src/commands/run.ts

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 spaces but found 3

return interpreter
}
Expand Down Expand Up @@ -221,6 +186,8 @@ export const eventsFor = (io: Server, interpreter: Interpreter, dynamicDiagramCl
try {
const tsStart = performance.now()

Check failure on line 187 in src/commands/run.ts

View workflow job for this annotation

GitHub Actions / test

Mixed spaces and tabs
interpreter.send('flushEvents', gameSingleton, interpreter.reify(timer))

draw(interpreter, io)
const elapsed = performance.now() - tsStart
tEvents += elapsed

Expand All @@ -229,10 +196,11 @@ export const eventsFor = (io: Server, interpreter: Interpreter, dynamicDiagramCl
timer += elapsed > flushInterval ? elapsed : flushInterval
muestras += 1


// cada 30 muestras se imprime por consola el tiempo promedio
// que tardó en procesar todos los eventos
if(muestras >= 30) {
logger.debug(`flushEvents: ${tEvents / muestras} ms`)
logger.debug(`flushEvents: ${(tEvents / muestras).toFixed(2)} ms`)

Check failure on line 203 in src/commands/run.ts

View workflow job for this annotation

GitHub Actions / test

Mixed spaces and tabs
muestras = 0

Check failure on line 204 in src/commands/run.ts

View workflow job for this annotation

GitHub Actions / test

Mixed spaces and tabs
tEvents = 0

Check failure on line 205 in src/commands/run.ts

View workflow job for this annotation

GitHub Actions / test

Mixed spaces and tabs
}
Expand Down Expand Up @@ -311,3 +279,28 @@ export const gameHost = (host: string): string => host ?? DEFAULT_HOST
export const dynamicDiagramPort = (port: string): string => `${+gamePort(port) + 1}`

const drawDefinition = () => parse.File('draw.wlk').tryParse('object drawer{ method apply() native }')


const draw = (interpreter: Interpreter, io: Server) => {
const game = interpreter?.object('wollok.game.game')
try {
const visuals = getVisuals(game, interpreter)
io.emit('visuals', visuals)

const gameSounds = game.get('sounds')?.innerCollection ?? []
const mappedSounds = gameSounds.map(sound =>
[
sound.id,
sound.get('file')!.innerString!,
sound.get('status')!.innerString!,
sound.get('volume')!.innerNumber!,
sound.get('loop')!.innerBoolean!,
])
io.emit('updateSound', { soundInstances: mappedSounds })
} catch (error: any) {
logger.error(failureDescription(error instanceof WollokException ? error.message : 'Exception while executing the program'))
const debug = logger.getLevel() <= logger.levels.DEBUG
if (debug) logger.error(error)
interpreter.send('stop', game)
}
}

Check warning on line 306 in src/commands/run.ts

View workflow job for this annotation

GitHub Actions / test

Newline not allowed at end of file

0 comments on commit 1aceb9d

Please sign in to comment.