helper class, config management

This commit is contained in:
Flummi 2023-07-12 08:11:27 +02:00
parent 5f0cea9cf1
commit a9710113d4
Signed by: Flummi
GPG Key ID: AA2AEF822A6F4817
3 changed files with 96 additions and 35 deletions

View File

@ -1,4 +1,8 @@
{
"bot": {
"autojoin": true,
"debug": true
},
"clients": [{
"type": "irc",
"enabled": true,

42
src/inc/helper.mjs Normal file
View File

@ -0,0 +1,42 @@
import fs from 'node:fs/promises';
let _config;
try {
_config = JSON.parse(await fs.readFile('config.json'));
} catch(err) {
throw err;
}
export default new class {
suits = { "♠": "S", "♣": "C", "♦": "D", "♥": "H" };
stripColors = msg => msg.replace(/\x03\d{0,2}(,\d{0,2}|\x02\x02)?/g, '');
rand = (max = 1) => ~~(Math.random() * (max - 1) + 1);
config = {
get(k) {
return _config.bot[k];
},
async set(k, v) {
_config.bot[k] = v;
await this.writeConfig();
return this.get(k);
},
getFull() {
return _config;
},
async writeConfig() {
try {
await fs.writeFile('config.json', JSON.stringify(_config, null, 2));
return true;
} catch(err) {
throw err;
}
}
};
parseCards(msg, output = []) {
for(const c of this.stripColors(msg).match(/(\w+)([♠♣️♦♥️️])/g))
output.push(c.length === 3 ? `T${this.suits[c[2]]}` : `${c[0]}${this.suits[c[1]]}`);
return output;
};
};

View File

@ -1,24 +1,9 @@
import cuffeo from 'cuffeo';
import handranker from './inc/handranker.mjs';
import cfg from '../config.json' assert { type: 'json' };
import helper from './inc/helper.mjs';
const bot = await new cuffeo(cfg.clients);
const suits = { "♠": "S", "♣": "C", "♦": "D", "♥": "H" };
const stripColors = msg => msg.replace(/\x03\d{0,2}(,\d{0,2}|\x02\x02)?/g, '');
const rand = (max = 1) => ~~(Math.random() * (max - 1) + 1);
const parseCards = msg => {
const output = [];
for(const c of stripColors(msg).match(/(\w+)([♠♣️♦♥️️])/g)) {
if(c.length === 3)
output.push(`T${suits[c[2]]}`);
else
output.push(`${c[0]}${suits[c[1]]}`);
}
return output;
};
const cfg = helper.config;
const bot = await new cuffeo(cfg.getFull().clients);
const env = {
gamestate: 'preflop', // [preflop,flop,turn,river]
@ -34,22 +19,51 @@ const env = {
bot.on("notice", msg => {
if(msg.match(/^Your hand/)) {
env.hand = parseCards(msg);
console.log('hand:', env.hand);
env.hand = helper.parseCards(msg);
if(cfg.get('debug'))
console.log('hand:', env.hand);
}
});
bot.on("message", e => {
bot.on("message", async e => {
if(e.channel !== '#poker') {
return;
}
if(e.message === '.schmirc help') {
await e.write(`PRIVMSG ${e.user.nick} I always say hirc schmirc, available commands are:`);
const commands = [
' .pj -- poker join',
' .pl -- poker leave',
' .bb -- bank balance',
' .bl -- bank loan',
' .d -- deal',
' .deb -- toggle debug',
' .aj -- toggle autojoin',
'While playing:',
' .s -- stack',
' .f -- fold',
];
for(const command of commands) {
await e.write(`PRIVMSG ${e.user.nick} ${command}`);
}
}
if(e.message.match(new RegExp(`^${e.self.me.nickname}: Your bank account`))) {
env.bank = +e.message.match(/is: (\d+) \(/)[1];
}
if(e.message === ".pj" || e.message === "pj" && !env.hand && !env.joined) {
if(e.message === ".deb") {
const tmp = await cfg.set('debug', !cfg.get('debug'));
return e.reply(`${!tmp} -> ${tmp}`);
}
if(e.message === ".aj") {
const tmp = await cfg.set('autojoin', !cfg.get('autojoin'));
return e.reply(`${!tmp} -> ${tmp}`);
}
if(e.message === ".pj" || (e.message === "pj" && cfg.get('autojoin')) && !env.hand && !env.joined) {
env.joined = true;
//return e.reply("pj");
return e.reply("bb\npj");
}
if(e.message === ".pl" && !env.hand) {
env.joined = false;
@ -105,7 +119,7 @@ bot.on("message", e => {
env.callamount = +e.message.match(/\((\d+) to call\)/)[1];
if(env.winchance < 4300) {
if(rand(5) === 1 && env.callamount < 80) { // bad hand, call anyway
if(helper.rand(5) === 1 && env.callamount < 80) { // bad hand, call anyway
action = 'c';
}
else { // bad hand, fold
@ -113,32 +127,32 @@ bot.on("message", e => {
}
}
else if(env.winchance > 7000) { // decent hand, raise
if(rand(5) === 1) { // 20%
action = 'r ' + (env.callamount + rand(5) * 10);
if(helper.rand(5) === 1) { // 20%
action = 'r ' + (env.callamount + helper.rand(5) * 10);
}
}
else if(env.winchance > 15000) { // good hand lol
if(rand(2) === 1) { // 50%
action = 'r ' + (env.callamount + rand(6) * 10);
if(helper.rand(2) === 1) { // 50%
action = 'r ' + (env.callamount + helper.rand(6) * 10);
}
}
else if(env.winchance > 20000) { // fuck them all
action = 'r ' + (env.callamount + rand(7) * 10);
action = 'r ' + (env.callamount + helper.rand(7) * 10);
}
}
else { // checkphase
if(env.winchance > 7000) { // decend hand, raise
if(rand(5) === 1) { // 20%
action = 'r ' + (env.callamount + rand(5) * 10);
if(helper.rand(5) === 1) { // 20%
action = 'r ' + (env.callamount + helper.rand(5) * 10);
}
}
else if(env.winchance > 15000) { // good hand lol
if(rand(2) === 1) { // 50%
action = 'r ' + (env.callamount + rand(6) * 10);
if(helper.rand(2) === 1) { // 50%
action = 'r ' + (env.callamount + helper.rand(6) * 10);
}
}
else if(env.winchance > 20000) { // fuck them all
action = 'r ' + (env.callamount + rand(7) * 10);
action = 'r ' + (env.callamount + helper.rand(7) * 10);
}
}
@ -150,7 +164,7 @@ bot.on("message", e => {
// gamestate & board changes
if(e.message.match(/^(Flop|Turn|River)/)) {
env.board = parseCards(e.message);
env.board = helper.parseCards(e.message);
env.gamestate = e.message.match(/(\w+): /)[1].toLowerCase();
const rank = handranker.evalHand([...env.board, ...env.hand]);
@ -177,5 +191,6 @@ bot.on("message", e => {
return;
}
console.log(env);
if(cfg.get('debug'))
console.log(env);
});