file chooser
This commit is contained in:
parent
533d5b173f
commit
75208cfe85
|
@ -34,7 +34,7 @@ func main() {
|
|||
}()
|
||||
}
|
||||
|
||||
w, err := model.Parse(*f)
|
||||
w, err := model.Parse(*f, nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
|
|
|
@ -61,16 +61,25 @@ func NewLegendsParser(file string) (*util.XMLParser, *os.File, *pb.ProgressBar,
|
|||
return d, xmlFile, bar, err
|
||||
}
|
||||
|
||||
func Parse(file string) (*DfWorld, error) {
|
||||
type LoadProgress struct {
|
||||
Message string
|
||||
ProgressBar *pb.ProgressBar
|
||||
}
|
||||
|
||||
func Parse(file string, lp *LoadProgress) (*DfWorld, error) {
|
||||
InitSameFields()
|
||||
|
||||
p, xmlFile, bar, err := NewLegendsParser(file)
|
||||
if lp != nil {
|
||||
lp.Message = "Loading " + file
|
||||
lp.ProgressBar = bar
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer xmlFile.Close()
|
||||
|
||||
world := &DfWorld{}
|
||||
var world *DfWorld
|
||||
|
||||
BaseLoop:
|
||||
for {
|
||||
|
@ -98,6 +107,10 @@ BaseLoop:
|
|||
file = strings.Replace(file, "-legends.xml", "-legends_plus.xml", 1)
|
||||
|
||||
p, xmlFile, bar, err = NewLegendsParser(file)
|
||||
if lp != nil {
|
||||
lp.Message = "Loading " + file
|
||||
lp.ProgressBar = bar
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package server
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
|
@ -46,20 +45,3 @@ func (srv *DfServer) RegisterWorldResourcePage(path string, template string, acc
|
|||
return accessor(id)
|
||||
})
|
||||
}
|
||||
|
||||
type paths struct {
|
||||
Current string
|
||||
List []fs.FileInfo
|
||||
}
|
||||
|
||||
func (srv *DfServer) renderLoading(w http.ResponseWriter, r *http.Request) {
|
||||
if srv.context.isLoading {
|
||||
err := srv.templates.Render(w, "loading.html", nil)
|
||||
if err != nil {
|
||||
fmt.Fprintln(w, err)
|
||||
fmt.Println(err)
|
||||
}
|
||||
} else {
|
||||
http.Redirect(w, r, "/load", http.StatusSeeOther)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package server
|
|||
|
||||
import (
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"io/ioutil"
|
||||
|
@ -17,10 +18,12 @@ import (
|
|||
type DfServerContext struct {
|
||||
world *model.DfWorld
|
||||
isLoading bool
|
||||
progress *model.LoadProgress
|
||||
}
|
||||
|
||||
type DfServer struct {
|
||||
router *mux.Router
|
||||
loader *loadHandler
|
||||
templates *templates.Template
|
||||
context *DfServerContext
|
||||
}
|
||||
|
@ -31,8 +34,10 @@ func StartServer(world *model.DfWorld, static embed.FS) {
|
|||
context: &DfServerContext{
|
||||
world: world,
|
||||
isLoading: false,
|
||||
progress: &model.LoadProgress{},
|
||||
},
|
||||
}
|
||||
srv.loader = &loadHandler{server: srv}
|
||||
srv.LoadTemplates()
|
||||
|
||||
srv.RegisterWorldResourcePage("/entity/{id}", "entity.html", func(id int) any { return srv.context.world.Entities[id] })
|
||||
|
@ -45,7 +50,7 @@ func StartServer(world *model.DfWorld, static embed.FS) {
|
|||
srv.RegisterWorldPage("/events", "eventTypes.html", func(p Parms) any { return srv.context.world.AllEventTypes() })
|
||||
srv.RegisterWorldPage("/events/{type}", "eventType.html", func(p Parms) any { return srv.context.world.EventsOfType(p["type"]) })
|
||||
|
||||
srv.router.PathPrefix("/load").Handler(loadHandler{server: srv})
|
||||
srv.router.PathPrefix("/load").Handler(srv.loader)
|
||||
|
||||
spa := spaHandler{staticFS: static, staticPath: "static", indexPath: "index.html"}
|
||||
srv.router.PathPrefix("/").Handler(spa)
|
||||
|
@ -106,7 +111,35 @@ type loadHandler struct {
|
|||
server *DfServer
|
||||
}
|
||||
|
||||
type loadProgress struct {
|
||||
Msg string `json:"msg"`
|
||||
Progress float64 `json:"progress"`
|
||||
Done bool `json:"done"`
|
||||
}
|
||||
|
||||
func (h loadHandler) Progress() *loadProgress {
|
||||
percent := 0.0
|
||||
p := h.server.context.progress
|
||||
if p.ProgressBar != nil {
|
||||
percent = float64(p.ProgressBar.Current()*100) / float64(p.ProgressBar.Total())
|
||||
}
|
||||
|
||||
return &loadProgress{
|
||||
Msg: h.server.context.progress.Message,
|
||||
Progress: percent,
|
||||
Done: h.server.context.world != nil,
|
||||
}
|
||||
}
|
||||
|
||||
func (h loadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/load/progress" {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
|
||||
json.NewEncoder(w).Encode(h.Progress())
|
||||
return
|
||||
}
|
||||
|
||||
path := r.URL.Query().Get("p")
|
||||
|
||||
p := &paths{
|
||||
|
@ -132,8 +165,7 @@ func (h loadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
} else {
|
||||
h.server.context.isLoading = true
|
||||
wrld, _ := model.Parse(p.Current)
|
||||
h.server.context.world = wrld
|
||||
go loadWorld(h.server, p.Current)
|
||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
@ -144,3 +176,26 @@ func (h loadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
func isLegendsXml(f fs.FileInfo) bool {
|
||||
return strings.HasSuffix(f.Name(), "-legends.xml")
|
||||
}
|
||||
|
||||
func loadWorld(server *DfServer, file string) {
|
||||
wrld, _ := model.Parse(file, server.context.progress)
|
||||
server.context.world = wrld
|
||||
server.context.isLoading = false
|
||||
}
|
||||
|
||||
type paths struct {
|
||||
Current string
|
||||
List []fs.FileInfo
|
||||
}
|
||||
|
||||
func (srv *DfServer) renderLoading(w http.ResponseWriter, r *http.Request) {
|
||||
if srv.context.isLoading {
|
||||
err := srv.templates.Render(w, "loading.html", srv.loader.Progress())
|
||||
if err != nil {
|
||||
fmt.Fprintln(w, err)
|
||||
fmt.Println(err)
|
||||
}
|
||||
} else {
|
||||
http.Redirect(w, r, "/load", http.StatusSeeOther)
|
||||
}
|
||||
}
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -8,6 +8,7 @@
|
|||
<title>{{block "title" .}}{{end}}</title>
|
||||
<link href="/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link href="/css/legends.css" rel="stylesheet">
|
||||
<script src="/js/jquery-3.6.0.min.js"></script>
|
||||
<script src="/js/bootstrap.min.js"></script>
|
||||
</head>
|
||||
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
{{template "layout.html" .}}
|
||||
|
||||
{{define "title"}}Loading{{end}}
|
||||
|
||||
{{define "content"}}
|
||||
<h1>Loading...</h1>
|
||||
<p id="msg">{{ .Msg }}</p>
|
||||
<div class="progress">
|
||||
<div id="progress" class="progress-bar" role="progressbar" style="width: {{ .Progress }}%" aria-valuenow="25" aria-valuemin="0"
|
||||
aria-valuemax="100">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
setInterval(() => {
|
||||
$.ajax({
|
||||
url: "/load/progress",
|
||||
context: document.body,
|
||||
success: function (data) {
|
||||
$("#msg").text(data.msg);
|
||||
$("#progress").css("width", data.progress + "%");
|
||||
if (data.done) {
|
||||
location.reload();
|
||||
}
|
||||
}
|
||||
})
|
||||
}, 300);
|
||||
</script>
|
||||
|
||||
{{- end }}
|
Loading…
Reference in New Issue