-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomegga.plugin.js
executable file
·108 lines (91 loc) · 3.2 KB
/
omegga.plugin.js
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
const {chat: { sanitize }} = OMEGGA_UTIL;
const CooldownProvider = require('./util.cooldown.js');
const fontParser = require('./util.fontParser.js');
const font = fontParser(__dirname + '/font.brs');
// sudoku generator library
const Sudoku = require('./lib.sudoku.js');
const DIFFICULTIES = ['easy', 'medium', 'hard', 'very hard'];
class SudokuPlugin {
constructor(omegga, config) {
this.config = config;
this.omegga = omegga;
this.cooldown = CooldownProvider(5000);
}
init() {
this.omegga.on('chatcmd:sudoku', this.cmdSudoku.bind(this));
}
stop() {
this.omegga.removeAllListeners('chatcmd:sudoku');
}
toOne(name, ...messages) {
// TODO: maybe make this broadcast if in A4
if (this.omegga.version === 'a4')
this.omegga.broadcast(...messages);
else
this.omegga.whisper(name, ...messages);
}
async cmdSudoku(name, ...args) {
const player = Omegga.getPlayer(name);
if (
this.config['only-authorized'] && !player.isHost() &&
!this.config['authorized-users'].some(p => player.id === p.id)
) return;
if (!this.cooldown(name)) return;
try {
const player = this.omegga.getPlayer(name);
let [x, y, z] = await player.getPosition();
// snap coords to grid
x = Math.round(x/10)*10+5;
y = Math.round(y/10)*10+5;
z = Math.round(z/4)*4;
// generate a sudoku
const generatedSudoku = Sudoku(DIFFICULTIES.includes(args.join(' ')) ? args.join(' ') : 'medium');
// get the board and analysis
const analysis = generatedSudoku.analyzeBoard();
const sudokuBoard = generatedSudoku.getBoard();
const chars = [];
for (let i = 0; i < 9; i++) {
// build red numbers off to the side
chars.push({
char: (1 + i) + '',
pos: [-1 * 7 * 10, i * 7 * 10, -4],
color: [255, 0, 0, 255],
});
// render the sudoku in a 7x7 plate grid
for (let j = 0; j < 9; j++) {
if(typeof sudokuBoard[j * 9 + i].val === 'number') {
chars.push({
char: sudokuBoard[j * 9 + i].val + ' ',
pos: [i * 7 * 10, j * 7 * 10, 0],
color: [0, 0, 0, 255],
});
}
}
}
// generate alternating colored plates underneath each subgrid
let grid = [];
for (let i = 0; i < 3; i++)
for (let j = 0; j < 3; j++)
grid.push({
asset: 'PB_DefaultTile',
position: [(7 * 3 * i + 7) * 10, (7 * 3 * j + 7) * 10 + 10, 0],
size: [7 * 3 * 5, 7 * 3 * 5, 2],
color: (i + j * 3) % 2 ? [255, 255, 255, 255] : [150, 150, 150, 255],
});
// write the save
this.omegga.loadSaveData(font.grid(chars, {
shift: [x, y, z - 22],
author: {
id: player.id,
name,
},
bricks: grid,
}), {quiet: true});
this.toOne(name, `"Generated <b>${sanitize(name)}</> a sudoku: ${analysis.level} (${analysis.score})"`)
} catch (e) {
console.log(e);
this.toOne(name, `"Could not find ${sanitize(name)}"`);
}
}
}
module.exports = SudokuPlugin;