Skip to content

Commit

Permalink
Merge pull request #24 from lologarithm/client_server
Browse files Browse the repository at this point in the history
v1.1.0  Add server!
  • Loading branch information
lologarithm authored Aug 21, 2021
2 parents 4b7c272 + 256a0aa commit 8ecbea2
Show file tree
Hide file tree
Showing 39 changed files with 137 additions and 42 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
icons
wowsim
.vscode
5 changes: 5 additions & 0 deletions buildrelease.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

GOOS=windows GOARCH=amd64 go build -o wowsim.exe web.go
GOOS=darwin GOARCH=amd64 go build -o wowsim-amd64-darwin web.go
GOOS=linux go build -o wowsim-amd64-linux web.go
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/lologarithm/wowsim

go 1.15
go 1.16
9 changes: 2 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"encoding/json"
"flag"
"fmt"
"github.com/lologarithm/wowsim/tbc"
"io/ioutil"
"log"
"net/http"
"time"

"github.com/lologarithm/wowsim/tbc"
)

// Maps agentType flag values to actual enum types
Expand Down Expand Up @@ -109,15 +109,10 @@ func main() {
var agentTypeStr = flag.String("agentType", "", "Custom comma separated agent type to simulate.\n\tFor Example: --rotation=3LB1CL")
var duration = flag.Float64("duration", 300, "Custom fight duration in seconds.")
var iterations = flag.Int("iter", 10000, "Custom number of iterations for the sim to run.")
var runWebUI = flag.Bool("web", false, "Use to run sim in web interface instead of in terminal")
var configFile = flag.String("config", "", "Specify an input configuration.")

flag.Parse()

if *runWebUI {
log.Printf("Closing: %s", http.ListenAndServe(":3333", nil))
}

simRequest := tbc.SimRequest{}
if *configFile != "" {
fileData, err := ioutil.ReadFile(*configFile)
Expand Down
34 changes: 17 additions & 17 deletions ui/components/gear.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,22 +258,22 @@ class GearUI {

// For now hardcode an icon.
var slotToIcon = {
"equiphead": "../icons/Armor/INV_Helmet_06.png",
"equipneck": "../icons/Armor/INV_Jewelry_Necklace_07.png",
"equipshoulder": "../icons/Armor/INV_Shoulder_14.png",
"equipback": "../icons/Armor/INV_Misc_Cape_16.png",
"equipchest": "../icons/Armor/INV_Chest_Chain_04.png",
"equipwrist": "../icons/Armor/INV_Bracer_09.png",
"equiphands": "../icons/Armor/INV_Gauntlets_26.png",
"equipwaist": "../icons/Armor/INV_Belt_19.png",
"equiplegs": "../icons/Armor/INV_Pants_03.png",
"equipfeet": "../icons/Armor/INV_Boots_Wolf.png",
"equipfinger1": "../icons/Armor/INV_Jewelry_Ring_04.png",
"equipfinger2": "../icons/Armor/INV_Jewelry_Ring_05.png",
"equiptrinket1": "../icons/Armor/INV_Jewelry_Talisman_09.png",
"equiptrinket2": "../icons/Armor/INV_Jewelry_Talisman_10.png",
"equipweapon": "../icons/Weapons/INV_Sword_39.png",
"equipoffhand": "../icons/Armor/INV_Shield_20.png",
"equiptotem": "../icons/Spells/Spell_Nature_InvisibilityTotem.png"
"equiphead": "icons/Armor/INV_Helmet_06.png",
"equipneck": "icons/Armor/INV_Jewelry_Necklace_07.png",
"equipshoulder": "icons/Armor/INV_Shoulder_14.png",
"equipback": "icons/Armor/INV_Misc_Cape_16.png",
"equipchest": "icons/Armor/INV_Chest_Chain_04.png",
"equipwrist": "icons/Armor/INV_Bracer_09.png",
"equiphands": "icons/Armor/INV_Gauntlets_26.png",
"equipwaist": "icons/Armor/INV_Belt_19.png",
"equiplegs": "icons/Armor/INV_Pants_03.png",
"equipfeet": "icons/Armor/INV_Boots_Wolf.png",
"equipfinger1": "icons/Armor/INV_Jewelry_Ring_04.png",
"equipfinger2": "icons/Armor/INV_Jewelry_Ring_05.png",
"equiptrinket1": "icons/Armor/INV_Jewelry_Talisman_09.png",
"equiptrinket2": "icons/Armor/INV_Jewelry_Talisman_10.png",
"equipweapon": "icons/Weapons/INV_Sword_39.png",
"equipoffhand": "icons/Armor/INV_Shield_20.png",
"equiptotem": "icons/Spells/Spell_Nature_InvisibilityTotem.png"
}

14 changes: 7 additions & 7 deletions ui/components/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,12 @@ class SocketsComponent {

// For now hardcode an icon.
var gemToIcon = {
1: "../icons/Gems/Gem_Diamond_07.png",
2: "../icons/Gems/Gem_BloodGem_02.png",
3: "../icons/Gems/Gem_AzureDraenite_02.png", // blue
4: "../icons/Gems/Gem_GoldenDraenite_02.png", // yellow
5: "../icons/Gems/Gem_DeepPeridot_02.png", // green
6: "../icons/Gems/Gem_FlameSpessarite_02.png", // orange
7: "../icons/Gems/Gem_EbonDraenite_02.png", // purple
1: "icons/Gems/Gem_Diamond_07.png",
2: "icons/Gems/Gem_BloodGem_02.png",
3: "icons/Gems/Gem_AzureDraenite_02.png", // blue
4: "icons/Gems/Gem_GoldenDraenite_02.png", // yellow
5: "icons/Gems/Gem_DeepPeridot_02.png", // green
6: "icons/Gems/Gem_FlameSpessarite_02.png", // orange
7: "icons/Gems/Gem_EbonDraenite_02.png", // purple
}

File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
4 changes: 2 additions & 2 deletions ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ <h4>Standard Elemental Build</h4>
<div id="pullout">
<div class="dtl" style="height:100%;width:100%;padding:10px">
<div id="themetoggle" style="height:20px; bottom: 10px; cursor: pointer;" onclick="toggletheme()">
<text>v1.0.12</text>
<img id="themebulb" style="height: 20px; width: 20px" src="../icons/light-bulb.svg"></img>
<text>v1.1.0</text>
<img id="themebulb" style="height: 20px; width: 20px" src="icons/light-bulb.svg"></img>
</div>
<div style="margin-top:10px">
<label style="margin-top:10px">Phase: <select id="phasesel" onchange="changePhaseFilter(event)" value="1">
Expand Down
Binary file modified ui/lib.wasm
Binary file not shown.
33 changes: 33 additions & 0 deletions ui/networker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
postMessage({
msg: "ready"
});

var workerID = "";

addEventListener('message', async (e) => {
const msg = e.data.msg;
const payload = e.data.payload;

if (msg == "apiCall") {
const id = e.data.id;
let url = '/api'

let response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: JSON.stringify(payload)
});

let result = await response.json();
postMessage({
msg: "apiCall",
payload: result,
id: id,
});
} else if (msg == "setID") {
workerID = payload;
postMessage({ msg: "idconfirm" })
}
}, false);
2 changes: 1 addition & 1 deletion ui/ui.css
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
}
.enchslot {
margin-right: 1em;
background-image: url("../icons/Trade/Trade_Engraving.png");
background-image: url("icons/Trade/Trade_Engraving.png");
background-size: contain;
height: 1.1em;
width: 1.1em;
Expand Down
4 changes: 2 additions & 2 deletions ui/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ window.addEventListener("mousemove", (e) => {
var theme = "dark";
function toggletheme() {
if (theme == "light") {
document.getElementById("themebulb").src = "../icons/light-bulb.svg";
document.getElementById("themebulb").src = "icons/light-bulb.svg";
document.children[0].children[1].classList.remove("lighttheme")
document.children[0].classList.remove("lighttheme")

Expand All @@ -1036,7 +1036,7 @@ function toggletheme() {

theme = "dark";
} else {
document.getElementById("themebulb").src = "../icons/lightbulb.svg";
document.getElementById("themebulb").src = "icons/lightbulb.svg";

document.children[0].children[1].classList.remove("uk-light")
document.children[0].classList.remove("darktheme")
Expand Down
71 changes: 67 additions & 4 deletions web.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,83 @@
package main

import (
"bytes"
"embed"
"encoding/json"
"flag"
"io/ioutil"
"log"
"net/http"
"strings"
"time"

"github.com/lologarithm/wowsim/tbc"
)

func init() {
fs := http.FileServer(http.Dir("."))
//go:embed ui
var uifs embed.FS

func main() {
var useFS = flag.Bool("usefs", false, "Use local file system and wasm. Set to true for dev")
var host = flag.String("host", ":3333", "URL to host the interface on.")

flag.Parse()

var fs http.Handler
if *useFS {
log.Printf("Using local file system for development.")
fs = http.FileServer(http.Dir("."))
} else {
log.Printf("Embedded file server running.")
fs = http.FileServer(http.FS(uifs))
}

http.HandleFunc("/", func(resp http.ResponseWriter, req *http.Request) {
resp.Header().Add("Cache-Control", "no-cache")
if strings.HasSuffix(req.URL.Path, ".wasm") {
resp.Header().Set("content-type", "application/wasm")
} else if strings.HasSuffix(req.URL.Path, "/ui.js") {
var uijs []byte
var err error
if *useFS {
// read file straight off disk
uijs, err = ioutil.ReadFile("./ui/ui.js")
} else {
uijs, err = uifs.ReadFile("ui/ui.js")
// modify so that simworker is replaced with networker.
uijs = bytes.Replace(uijs, []byte(`this.worker = new window.Worker('simworker.js');`), []byte(`this.worker = new window.Worker('networker.js');`), 1)
}
if err != nil {
log.Printf("Failed to open file..., %s", err)
}
resp.Write(uijs)
return
}
log.Printf("Serving: %s", req.URL.String())
fs.ServeHTTP(resp, req)
})
// http.HandleFunc("/simtbc", simTBCPage)

http.HandleFunc("/api", handleAPI)

log.Printf("Launching interface on http://localhost%s/ui", *host)

log.Printf("Closing: %s", http.ListenAndServe(*host, nil))
}

func handleAPI(w http.ResponseWriter, r *http.Request) {
st := time.Now()
// Assumes input is a JSON object as a string
request := tbc.ApiRequest{}
body, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(body, &request)
if err != nil {
panic("Error parsing request: " + err.Error() + "\nRequest: " + string(body))
}
result := tbc.ApiCall(request)
resultData, err := json.Marshal(result)
if err != nil {
panic("Error marshaling result: " + err.Error())
}
w.Write(resultData)

log.Printf("API request took %v", time.Since(st))
}

0 comments on commit 8ecbea2

Please sign in to comment.