aboutsummaryrefslogtreecommitdiffhomepage
path: root/desktop/sources/scripts/source.js
blob: 77a5ab13e0e1daf7e6438669f1a9c573b355a885 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'use strict'

function Source (terminal) {
  const Orca = require('../../core/orca')
  const fs = require('fs')
  const { dialog, app } = require('electron').remote

  this.path = null

  this.new = function () {
    console.log('New')

    this.path = null

    terminal.rooms = { hall: new Orca(terminal) }
    terminal.room = terminal.rooms.hall
    terminal.enter()
    terminal.resize()
    terminal.history.reset()
  }

  this.open = function () {
    console.log('Open')
    let paths = dialog.showOpenDialog(app.win, { properties: ['openFile'], filters: [{ name: 'Orca Machines', extensions: ['orca'] }] })
    if (!paths) { console.log('Nothing to load') }
    this.path = paths[0]
    this.read(paths[0])
  }

  this.save = function (as = false) {
    console.log('Save')
    if (this.path && !as) {
      this.write(this.path)
    } else {
      dialog.showSaveDialog((path) => {
        if (path === undefined) { return }
        if (path.indexOf('.orca') < 0) { path += '.orca' }
        terminal.source.write(path)
        terminal.source.path = path
      })
    }
  }

  this.revert = function () {
    console.log('Revert')
    this.read(this.path)
  }

  // I/O

  this.write = function (path) {
    fs.writeFile(path, this.generate(), (err) => {
      if (err) { alert('An error ocurred updating the file' + err.message); console.log(err) }
    })
  }

  this.read = function (path) {
    fs.readFile(path, 'utf8', (err, data) => {
      if (err) throw err
      const rooms = this.parse(data)
      terminal.load(rooms)
      terminal.history.record()
    })
  }

  // Converters

  this.generate = function (rooms = terminal.rooms) {
    let html = `${rooms.hall}\n\n`
    for (const id in rooms) {
      if (id !== 'hall') {
        const room = rooms[id]
        html += `${id}\n\n${room}\n\n`
      }
    }
    return html.trim()
  }

  this.parse = function (text) {
    const lines = text.split('\n')
    const blocks = { hall: [] }
    const rooms = { hall: [] }
    const room = []
    let key = 'hall'
    // Blocks
    for (const id in lines) {
      const line = lines[id].trim()
      if (line.length === 0) { continue }
      if (line.length === 1) { key = line; continue }
      if (!blocks[key]) { blocks[key] = [] }
      blocks[key].push(line)
    }
    // Rooms
    for (const id in blocks) {
      const block = blocks[id]
      const w = block[0].length
      const h = block.length
      const s = block.join('\n').trim()
      rooms[id] = new Orca(terminal).load(w, h, s)
    }
    return rooms
  }

  // Etc

  this.name = function () {
    const parts = this.path.split('/')
    return parts[parts.length - 1].replace('.orca', '').trim()
  }

  this.toString = function () {
    return this.path ? this.name() : 'blank'
  }
}

module.exports = Source