summaryrefslogtreecommitdiff
path: root/index.js
blob: c53b521a5d9607f29619a5aee000c3675a11e80c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const express = require('express');
const path = require('path');
const fs = require('fs');

const MAP_LAYERS=2;

const app = express();
app.route('/').get((req, res) => {
  res.sendFile(path.join(__dirname, 'index.html'));
});

app.route('/levels').get((req, res) => {
  fs.readFile(path.join(__dirname, 'level-all.bbiy'), 'utf8', (err, data) => {
    if (err) {
      console.log(err);
      return;
    }

    const levels = [];
    const lines = data.split('\r\n');
    do {
      const levelName = lines.shift();
      if (!levelName) {
        break;
      }
      const [xDim, yDim] = lines.shift().split(' x ').map((x) => parseInt(x));

      let level = Array(yDim).fill(null).map(() => Array(xDim).fill(null).map(() => []));
      for (let i = 0; i < MAP_LAYERS; i++) {
        for (let y = 0; y < yDim; y++) {
          const line = lines.shift().split('');
          for (let x = 0; x < xDim; x++) {
            if (line[x] !== ' ') {
              level[y][x].push(line[x]);
            }
          }
        }
      }
      levels.push({
        levelName,
        gridSize: {xDim, yDim},
        level,
      });
    } while (lines.length);

    // Send the array of objects
    res.send(levels);
  });
});

app.use(express.static('.'));

app.listen(3000, () => {
  console.log('Listening on 3000');
});