From 4a18c018f6377c32f59161659f9cf3d94c96a4b2 Mon Sep 17 00:00:00 2001 From: Dimi-Dun-Morogh Date: Wed, 12 Jul 2023 06:13:35 +0300 Subject: [PATCH] functions to edit realmlist/settings file/launch wow --- src/db.js | 11 ++++++-- src/main.js | 61 ++++++++++++++++++++++++++------------------- src/settings-win.js | 32 +++++++++++++++++++----- src/styles.css | 8 ++++++ src/wow.js | 33 ++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 34 deletions(-) create mode 100644 src/wow.js diff --git a/src/db.js b/src/db.js index b71c9b8..9a7eaaa 100644 --- a/src/db.js +++ b/src/db.js @@ -30,6 +30,7 @@ import * as Sciter from "@sciter"; * @type {object} * @property {string} tbcFolderPath * @property {string} wotlkFolderPath + * @property {string} wotlkRealmlist * @property {string} appMode */ @@ -48,6 +49,7 @@ class DB { appSettings: { tbcFolderPath: '', wotlkFolderPath: '', + wotlkRealmlist: '', appMode: 'tbc' }, accounts: [] @@ -176,18 +178,23 @@ class DB { return this.root.appSettings.appMode; } /** - * + * * @returns {appSettings} */ getAppSettings() { return this.root.appSettings } + setWotlkRealmlist(realmPath) { + console.log(realmPath) + this.root.appSettings.wotlkRealmlist = realmPath; + this.storage.commit(); + } /** * * @param {"wotlkFolderPath" | "tbcFolderPath"} mode tbcFolderPath | wotlkFolderPath * @param {string} path wow folder path */ - setWoWPath(mode, path) { + setWoWPath(mode, path='') { this.root.appSettings[mode] = path; this.storage.commit(); console.log(this.root.appSettings) diff --git a/src/main.js b/src/main.js index 8d28a35..ba25334 100644 --- a/src/main.js +++ b/src/main.js @@ -1,19 +1,10 @@ -// import * as sys from "@sys"; // '@' is mandatory - - -// const myButton = document.querySelector("#launch-btn") -// myButton.addEventListener('click',()=>{ - - -// //env.exec("D:\\WoW_TBC_2.4.3\\Wow.exe") -// // sys.spawn(["Wow.exe"]) -// console.log('hello') - -// } -// ) import { db } from './db'; +import { + Wow +} from './wow'; + const settingsWin = (screenName) => { Window.this.modal({ url: __DIR__ + "settings-window.htm", @@ -28,9 +19,9 @@ const settingsWin = (screenName) => { function renderRealmSelect() { const root = document.querySelector('#realm-container'); const data = db.getRealmLists(); - const isThereSelected = data.some(el=>el.selected); - if(!isThereSelected && data.length > 0) data[0].selected = true; - let options = ''; + const isThereSelected = data.some(el => el.selected); + if (!isThereSelected && data.length > 0) data[0].selected = true; + let options = ''; data.forEach(realm => { const option = ``; @@ -44,11 +35,11 @@ function renderRealmSelect() { root.innerHTML = html; } -function renderAccSelect(){ +function renderAccSelect() { const root = document.querySelector('#acc-select-wrap'); const data = db.getAllAccs(); - const isThereSelected = data.some(el=>el.selected); - if(!isThereSelected && data.length > 0) data[0].selected = true; + const isThereSelected = data.some(el => el.selected); + if (!isThereSelected && data.length > 0) data[0].selected = true; const html = ` ${data.reduce((acc,el)=>acc+=``, '')} @@ -61,9 +52,9 @@ function appMode() { const data = db.getAppMode(); console.log(data) const btns = document.querySelector('#wow-v-select').querySelectorAll('button');; - btns.forEach(el=>{ - if(el.getAttribute("key") === data) el.state.checked = true; - else (el.state.checked = false) + btns.forEach(el => { + if (el.getAttribute("key") === data) el.state.checked = true; + else(el.state.checked = false) }) document.body.style.backgroundImage = `url('bg${data}.jpg')`; } @@ -84,25 +75,43 @@ const accountsBtn = document.querySelector("#accounts-btn") accountsBtn.addEventListener('click', () => settingsWin('accounts_settings')) +document.querySelector('#launch-btn').addEventListener('click', async () => { + const { + appMode, + wotlkFolderPath, + tbcFolderPath, + wotlkRealmlist + } = db.getAppSettings() + const wowPath = appMode === 'tbc' ? tbcFolderPath : wotlkFolderPath; + const realmPath = appMode === 'tbc' ? tbcFolderPath : wotlkRealmlist; + + const realm = document.querySelector('#realm-select').$("option:current")?.innerText + const acc = document.querySelector('#acc-select').$("option:current")?.innerText + await Wow.addAccLogin(wowPath,acc) + await Wow.realmlistChange(realmPath, realm); + Wow.launchWow(wowPath) +}) + + -document.on("db-update",()=>{ +document.on("db-update", () => { //re render renderRealmSelect(); renderAccSelect() }) -document.on("click", "select#realm-select", (e)=>{ +document.on("click", "select#realm-select", (e) => { const key = e.target.getAttribute("key"); db.realmListSelected(key) }) -document.on("click", "select#acc-select", (e)=>{ +document.on("click", "select#acc-select", (e) => { const key = e.target.getAttribute("key"); db.selectAccount(key) }) -document.on("click", "#wow-v-select",(e)=>{ +document.on("click", "#wow-v-select", (e) => { const key = e.target.getAttribute("key"); db.setAppMode(key); document.body.style.backgroundImage = `url('bg${key}.jpg')`; diff --git a/src/settings-win.js b/src/settings-win.js index e36935c..cecb14d 100644 --- a/src/settings-win.js +++ b/src/settings-win.js @@ -59,22 +59,31 @@ class Settings { const data = db.getAppSettings(); const html = `

enter path to WoW folders

- TBC WOW PATH + TBC WOW FOLDER PATH - WoTLK WOW PATH + WoTLK WOW FOLDER PATH - + + WoTLK REALMLIST FOLDER PATH + + + `; this.rootHtml.innerHTML = html; } + toggleGreen(btn, input){ + btn.style.setProperty("background-color", "green") + input.style.setProperty("border-color", "green") + } + renderSettings(pageStr) { switch (pageStr) { case 'realmlist_settings': @@ -106,7 +115,14 @@ document.on('click', 'button.realm-edit', function (e) { const inputValue = parent.querySelector('input').value; const id = parent.getAttribute('key'); db.editRealmList(id, inputValue); - settings.realmListsHTML(); + settings.toggleGreen(e.target, e.target.parentElement.querySelector('input')) +}); + +document.on('click', '.realmlist-path-edit', function (e) { + const parent = e.target.parentElement; + const inputValue = parent.querySelector('input').value; + db.setWotlkRealmlist(inputValue); + settings.toggleGreen(e.target, e.target.parentElement.querySelector('input')) }); document.on('click', '#add-realmlist-btn', function (e) { @@ -130,12 +146,16 @@ document.on('click', '.acc-edit', (e) => { const id = e.target.parentElement.getAttribute('key'); const name = e.target.parentElement.querySelector('input').value; db.editAccName(id, name); - settings.accountsHTML(); + +// settings.accountsHTML(); + settings.toggleGreen(e.target, e.target.parentElement.querySelector('input')) + }); document.on('click', '.path-edit', e => { const input = e.target.parentElement.querySelector('input') - db.setWoWPath( input.getAttribute("key"),input.value) + db.setWoWPath( input.getAttribute("key"),input.value); + settings.toggleGreen(e.target, input) }) document.on('ready', function () { diff --git a/src/styles.css b/src/styles.css index b743a16..f107c90 100644 --- a/src/styles.css +++ b/src/styles.css @@ -138,3 +138,11 @@ height: 300px; display: block; font-weight: bold; } + +.input-ok { + border-color: green; + +} +.btn-ok { + background-color: green; +} \ No newline at end of file diff --git a/src/wow.js b/src/wow.js new file mode 100644 index 0000000..4bdf3f2 --- /dev/null +++ b/src/wow.js @@ -0,0 +1,33 @@ +import * as env from '@env' +import * as sys from "@sys"; +import {encode, decode} from "@sciter"; + +export class Wow { + static launchWow(path) { + const exepath = path+`\/Wow.exe`; + env.exec(exepath) + } + + static async realmlistChange(path, realmlist) { + if(!realmlist) return; + const realmlistpath = `${path}\/realmlist.wtf`; + const file = sys.fs.openSync(realmlistpath, 'w'); + const buffer = encode(realmlist, "utf-8") + await file.write(buffer); + await file.close() + } + + static async addAccLogin(path, login) { + if(!login) return; + const settingsPath = `${path}\\WTF\\Config.wtf`; + const file = await sys.fs.open(settingsPath, 'as+'); + const arrayBuffer = await file.read() + const decoded = decode(arrayBuffer, "utf-8"); + // SET accountName + const res = decoded.split('\n').filter(el=>!el.includes("SET accountName")).join("\n")+`SET accountName ${login}`; + await file.write(encode(res)); + await file.close() + } +} + +