-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
203 lines (167 loc) · 3.98 KB
/
index.js
File metadata and controls
203 lines (167 loc) · 3.98 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const { pipe, isInRange, awsd, QUIT } = require('./utils')
const EventEmitter = require('events')
const BACKGROUND = ' '
const TOP_AND_BOTTOM = '-'
const LEFT_AND_RIGHT = '|'
const exitEmitter = new EventEmitter()
exitEmitter.on('exit', () => process.exit())
function run({
initialState,
update,
toCommon,
renderMap,
inputMap = awsd,
header,
footer,
timeBetweenRender = 100,
isDebug = true,
}) {
let currentState = initialState
const bridge = {
input: null,
}
process.stdin.setRawMode(true)
process.stdin.on('data', buffer => {
let key = buffer.toJSON().data[0].toString()
if (key == QUIT) {
process.exit()
}
if (Object.keys(inputMap).includes(key)) {
bridge.input = inputMap[key]
}
})
setInterval(() => {
currentState = update(currentState, bridge.input)
if (currentState.isGameOver) {
exitEmitter.emit('exit')
}
renderToConsole(
currentState,
toCommon,
renderMap,
header,
footer,
isDebug,
bridge.input,
)
bridge.input = null
}, timeBetweenRender)
}
function renderToConsole(
state,
toCommon,
renderMap,
header,
footer,
isDebug,
input,
) {
const { width, height } = toZeroBased(state)
const common = toCommon(state)
const base = [...Array(height + 1)].map(() =>
Array(width + 3).fill(BACKGROUND),
)
// prettier-ignore
let screen = pipe(base)
(
drawToScreen(common, renderMap, toZeroBased(state)),
showLeftAndRightBorder,
joinChars
)
if (isDebug) {
console.log(state)
console.log('input', input)
} else {
console.log('\033[2J')
}
header && header(state)
console.log(getVerticalBorder(width + 3))
console.log(screen)
console.log(getVerticalBorder(width + 3))
footer && footer(state)
}
function toZeroBased(state) {
return {
width: state.view.width -1,
height: state.view.height -1,
}
}
function drawToScreen(state, renderMap, view) {
return screen => {
state.forEach(obj => {
if (isOnScreen(obj, view)) {
screen[obj.y][obj.x + 1] = renderMap[obj.tag]
}
})
return screen.reverse()
}
}
function isOnScreen(obj, view) {
const { width, height } = view
return isInRange(0, obj.x, width) && isInRange(0, obj.y, height)
}
function showLeftAndRightBorder(screen) {
return screen.map(line =>
line.map((c, idx, arr) =>
idx == 0 || idx == arr.length - 1 ? LEFT_AND_RIGHT : c,
),
)
}
function joinChars(screen) {
return screen
.map((line, idx, arr) =>
idx == arr.length - 1 ? line.join('') : line.join('') + '\n',
)
.join('')
}
function getVerticalBorder(width) {
return Array(width).fill(TOP_AND_BOTTOM).join('')
}
function validateArgs(args) {
const {
initialState,
update,
toCommon,
renderMap,
inputMap,
header,
footer,
timeBetweenRender,
isDebug,
} = args
const msg = property =>
`You missed the '${property}' property from run's config, or it is not `
const isObject = value =>
!!value && typeof value == 'object' && !Array.isArray(value)
const isFunction = value => !!value && typeof value === 'function'
if (!isObject(initialState)) {
throw Error(msg('initialState') + 'an object.')
}
if (!isFunction(update)) {
throw Error(msg('update') + 'a function.')
}
if (!isFunction(toCommon)) {
throw Error(msg('toCommon') + 'a function.')
}
if (!isObject(renderMap)) {
throw Error(msg('renderMap') + 'an object.')
}
// optional
if (inputMap && !isObject(inputMap)) {
throw Error('inputMap is not an object.')
}
if (header && !isFunction(header)) {
throw Error('header is not a function.')
}
if (footer && !isFunction(footer)) {
throw Error('footer is not a function.')
}
if (timeBetweenRender && isNaN(timeBetweenRender)) {
throw Error('timeBetweenRender is not a number.')
}
if (isDebug && typeof isDebug === "boolean") {
throw Error('isDebug is not a boolean.')
}
run(args)
}
exports.run = validateArgs