Browse Source

improve error output and subject in tron

pull/582/head
Clement Denis 4 years ago
parent
commit
5521fc5915
  1. 31
      subjects/tron/index.html
  2. 7
      subjects/tron/tron.en.md

31
subjects/tron/index.html

@ -215,13 +215,13 @@ const getSha = async login => (await (await fetch(getShaUrl(login))).json()).sha
const toBlob = async r => {
if (!r.ok) throw Error(`${r.status}: ${r.statusText}`)
const code = await r.text()
return new Blob([`${code}${injectedCode}`], { type : 'text/javascript' })
const type = { type : 'text/javascript' }
const blob = new Blob([`${code}${injectedCode}`], type)
return { url: URL.createObjectURL(blob, type), code }
}
const toUrlObject = b => URL.createObjectURL(b, { type: 'text/javascript' })
const memo = {}
const fetchBlob = url => memo[url]
|| (memo[url] = fetch(url).then(toBlob).then(toUrlObject))
const memo = {}
const fetchBlob = url => memo[url] || (memo[url] = fetch(url).then(toBlob))
const getGithackUrl = async (login, sha) => {
if (!sha || sha === 'master') {
sha = localStorage[login] || (localStorage[login] = await getSha(login))
@ -317,7 +317,8 @@ const start = async ({ urls, seed }) => {
// init the worker
try {
const url = await fetchBlob(await formatURL(ai.name))
const { url, code } = await fetchBlob(await formatURL(ai.name))
ai.code = code
ai.worker = new Worker(url, { type: 'module', name: ai.name })
await new Promise((s, f) => {
ai.worker.onmessage = e => e.data === 'loaded' ? s() : f(Error(e.data))
@ -365,7 +366,23 @@ const start = async ({ urls, seed }) => {
next(ai)
}
ai.worker.onerror = () => ai.kill('AI-ERROR')
ai.worker.onerror = ({ lineno }) => {
const errorCode = ai.code.split('\n')
.slice(lineno - 5, lineno + 4)
.map((l,i) => `%c${lineno - 4 + i}| ${l}`)
.join('\n')
const styles = Array(4)
.fill('color:#888;background:black;display: block;padding:0 4px')
console.log(`${ai.name}:${lineno}\n${errorCode}`, ...[
...styles,
'color:white;background:#333;display: block;padding:0 4px',
...styles,
])
ai.kill('AI-ERROR')
}
}))
next(ais[0])

7
subjects/tron/tron.en.md

@ -23,7 +23,7 @@ You will need to create a public repository with the name `tron`. Next you need
- the AI can only move to its `left`, `forward` or its `right`.
*(Moving `backward` is suicide as it would hit its own trail !)*
- If too much CPU power is required to decide where to go, the AI dies.
- If two Ais moved to the same spot, both of them die.
- If two AIs moved to the same spot, both of them die.
- **The AI has to survive as long as it can.**
### The game ends
@ -34,8 +34,9 @@ You will need to create a public repository with the name `tron`. Next you need
- Copy the code on the file [random.js](https://raw.githubusercontent.com/01-edu/public/master/subjects/tron/ai/random.js) to your file, `ai.js`
- You may now edit the `update` function which is called each turn
> ⚠ Do not rename the `update` function ⚠ \
> as it's the function that the worker will try to run to test your AI.
> ⚠ Do not rename the `update` function ⚠ \
> as it's the function that the worker will try to run to test your AI.
### How to test your AI

Loading…
Cancel
Save