Skip to content

Commit

Permalink
functions to edit realmlist/settings file/launch wow
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimi-Dun-Morogh committed Jul 12, 2023
1 parent a456bc0 commit 4a18c01
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 34 deletions.
11 changes: 9 additions & 2 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import * as Sciter from "@sciter";
* @type {object}
* @property {string} tbcFolderPath
* @property {string} wotlkFolderPath
* @property {string} wotlkRealmlist
* @property {string} appMode
*/

Expand All @@ -48,6 +49,7 @@ class DB {
appSettings: {
tbcFolderPath: '',
wotlkFolderPath: '',
wotlkRealmlist: '',
appMode: 'tbc'
},
accounts: []
Expand Down Expand Up @@ -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)
Expand Down
61 changes: 35 additions & 26 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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 = `<option ${realm.selected ? 'selected=""': ''} key=${realm.id}>${realm.realm}</option>`;
Expand All @@ -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 = `
<select|list.select#acc-select>
${data.reduce((acc,el)=>acc+=`<option ${el.selected ? 'selected=""': ''} key="${el.id}">${el.name}</option>`, '')}
Expand All @@ -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')`;
}
Expand All @@ -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')`;
Expand Down
32 changes: 26 additions & 6 deletions src/settings-win.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,31 @@ class Settings {
const data = db.getAppSettings();
const html = `
<h1>enter path to WoW folders</h1>
<h3.input-title>TBC WOW PATH</h3>
<h3.input-title>TBC WOW FOLDER PATH</h3>
<div.settings-input-wrap>
<input|text(textEdit) .settings-input value="${data.tbcFolderPath}" key="tbcFolderPath"/> <button#settings-btn .path-edit .btn><icon|i-tick .center /></button>
</div>
<div.test>
<h3.input-title>WoTLK WOW PATH</h3>
<h3.input-title>WoTLK WOW FOLDER PATH</h3>
</div>
<div.settings-input-wrap>
<input|text(textEdit) key="wotlkFolderPath" .settings-input value="${data.wotlkFolderPath}" novalue="D:\World of Warcraft 3.3.5a(example)"/> <button#settings-btn .btn><icon|i-tick .path-edit .center/></button>
<input|text(textEdit) key="wotlkFolderPath" .settings-input value="${data.wotlkFolderPath}" novalue="D:\World of Warcraft 3.3.5a(example)"/> <button#settings-btn .path-edit .btn ><icon|i-tick .path-edit .center/></button>
</div>
<h3.input-title>WoTLK REALMLIST FOLDER PATH</h3>
<div.settings-input-wrap>
<input|text(textEdit) key="wotlkFolderPath" .settings-input value="${data.wotlkRealmlist}" /> <button#settings-btn .realmlist-path-edit.btn ><icon|i-tick .center/></button>
</div>
`;
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':
Expand Down Expand Up @@ -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) {
Expand All @@ -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 () {
Expand Down
8 changes: 8 additions & 0 deletions src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,11 @@ height: 300px;
display: block;
font-weight: bold;
}

.input-ok {
border-color: green;

}
.btn-ok {
background-color: green;
}
33 changes: 33 additions & 0 deletions src/wow.js
Original file line number Diff line number Diff line change
@@ -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()
}
}


0 comments on commit 4a18c01

Please sign in to comment.