Error handling

master
Aaron Fischer 1 year ago
parent 8f331b3ff1
commit 9a30dec1c0
  1. 8
      cmd/kartograph-map-generator/main.go
  2. 11
      pkg/generator/svg.go
  3. 19
      pkg/generator/world.go
  4. 27
      pkg/web/service.go
  5. 9
      pkg/web/static/generator.js
  6. 4
      pkg/web/templates/index.tpl.html

@ -5,6 +5,7 @@ import (
"f00860/kartograph-map-editor/pkg/web"
"flag"
"fmt"
"log"
"time"
)
@ -29,7 +30,12 @@ func main() {
return
}
world := generator.New(size, wastelands, mountains, ruins, seed)
world, err := generator.New(size, wastelands, mountains, ruins, seed)
if err != nil {
log.Fatal(err)
return
}
switch output {
case "svg":

@ -7,6 +7,17 @@ import (
"github.com/ajstarks/svgo"
)
func ErrorSVG(err error, width int, height int) string {
buf := bytes.NewBufferString("")
canvas := svg.New(buf)
canvas.Start(width, height)
canvas.Text(width/2, height/2, err.Error(), "text-anchor:middle;font-size:3px;font-family:Courier New;font-weight:light;")
canvas.End()
return buf.String()
}
func (w World) SVG() string {
// DIN-A5
width := 148 * 3

@ -91,7 +91,7 @@ func (w World) JSON() string {
return string(output)
}
func New(size int, numWastelands int, numMountains int, numRuins int, seed string) World {
func New(size int, numWastelands int, numMountains int, numRuins int, seed string) (World, error) {
InitSeed(seed)
w := World{
@ -102,7 +102,9 @@ func New(size int, numWastelands int, numMountains int, numRuins int, seed strin
Seed: seed,
}
// TODO: Return error wenn kein Platz mehr da ist
if size < 3 || size > 25 {
return World{}, errors.New("Spielfeldgröße nicht im Bereich 3..25.")
}
// All empty for start
for i := 0; i < w.Size*w.Size; i++ {
@ -114,6 +116,10 @@ func New(size int, numWastelands int, numMountains int, numRuins int, seed strin
var wastelands []int
if numWastelands > 0 {
if numWastelands > size*size {
return World{}, errors.New("Zu viele Ödlandfelder.")
}
startPos := roll(w.Size-1) + roll(w.Size-1)*w.Size
w.place(WastelandTerritory, startPos)
wastelands = append(wastelands, startPos)
@ -150,6 +156,10 @@ func New(size int, numWastelands int, numMountains int, numRuins int, seed strin
candidates = append(candidates, pos)
}
}
if len(candidates) < 1 {
return World{}, errors.New("Zu viele Berge.")
}
w.place(MountainTerritory, randomItem(candidates))
}
@ -165,10 +175,13 @@ func New(size int, numWastelands int, numMountains int, numRuins int, seed strin
candidates = append(candidates, pos)
}
}
if len(candidates) < 1 {
return World{}, errors.New("Zu viele Ruinen.")
}
w.place(RuinsTerritory, randomItem(candidates))
}
return w
return w, nil
}
func InitSeed(seed string) {

@ -3,6 +3,7 @@ package web
import (
"embed"
"f00860/kartograph-map-editor/pkg/generator"
"fmt"
"html/template"
"io"
"log"
@ -33,7 +34,7 @@ func Start(address string, port int) {
}
}
func worldFromRequest(req *http.Request) generator.World {
func worldFromRequest(req *http.Request) (generator.World, error) {
size, err := strconv.Atoi(req.URL.Query().Get("s"))
if err != nil {
size = 11
@ -59,16 +60,30 @@ func worldFromRequest(req *http.Request) generator.World {
}
func mapHandler(w http.ResponseWriter, req *http.Request) {
world := worldFromRequest(req)
log.Printf("GET /map.svg?%v (%v)", req.URL.Query().Encode(), req.RemoteAddr)
var SVGContent string
world, err := worldFromRequest(req)
if err != nil {
SVGContent = generator.ErrorSVG(err, 100, 100)
} else {
SVGContent = world.SVG()
}
w.Header().Set("Content-Type", "image/svg+xml")
w.Header().Set("Content-Length", strconv.Itoa(len(world.SVG())))
io.WriteString(w, world.SVG())
w.Header().Set("Content-Length", strconv.Itoa(len(SVGContent)))
io.WriteString(w, SVGContent)
}
func printHandler(w http.ResponseWriter, req *http.Request) {
world := worldFromRequest(req)
log.Printf("GET /print?%v (%v)", req.URL.Query().Encode(), req.RemoteAddr)
world, err := worldFromRequest(req)
if err != nil {
fmt.Print(err)
return
}
tpl, err := template.ParseFS(templateFiles, "templates/print.tpl.html")
if err != nil {
@ -80,7 +95,7 @@ func printHandler(w http.ResponseWriter, req *http.Request) {
func indexHandler(w http.ResponseWriter, req *http.Request) {
seed := generator.RandomSeed()
world := generator.New(11, 7, 5, 6, seed)
world, _ := generator.New(11, 7, 5, 6, seed)
log.Printf("GET / (%v) Seed: %v", req.RemoteAddr, seed)
tpl, err := template.ParseFS(templateFiles, "templates/index.tpl.html")

@ -7,19 +7,20 @@ document.addEventListener('DOMContentLoaded', function() {
let map = document.querySelector('.map')
// Generate new random seed
let newMapUrl = '/map.svg?seed=' + seed +
let newMapParams = '?seed=' + seed +
'&s=' + size +
'&w=' + wastelands +
'&m=' + mountains +
'&r=' + ruins
document.querySelector('.map').setAttribute('src', newMapUrl);
document.querySelector('#map-link').setAttribute('href', newMapUrl);
document.querySelector('.map').setAttribute('src', '/map.svg' + newMapParams);
document.querySelector('#print-button').setAttribute('data-target', '/print' + newMapParams);
document.querySelector('#map-button').setAttribute('data-target', '/map.svg' + newMapParams);
document.querySelector('#map-link').setAttribute('data-target', '/map.svg' + newMapParams);
};
let actionButtons = document.querySelectorAll('button[class="action"]');
Array.prototype.forEach.call(actionButtons, function(action, i) {
action.addEventListener('click', function(e) {
console.log("adsf");
document.location.href = e.target.getAttribute('data-target');
});
});

@ -49,8 +49,8 @@
<input type="button" name="refresh" value="Neu generieren">
</form>
<button class="action" data-target="/print?seed={{ .Seed }}&s={{ .Size }}&w={{ .Wastelands }}&r={{ .Ruins }}&m={{ .Mountains }}">Drucken</button>
<button class="action" data-target="/map.svg?seed={{ .Seed }}&s={{ .Size }}&w={{ .Wastelands }}&r={{ .Ruins }}&m={{ .Mountains }}">Direktlink</button>
<button class="action" id="print-button" data-target="/print?seed={{ .Seed }}&s={{ .Size }}&w={{ .Wastelands }}&r={{ .Ruins }}&m={{ .Mountains }}">Drucken</button>
<button class="action" id="map-button" data-target="/map.svg?seed={{ .Seed }}&s={{ .Size }}&w={{ .Wastelands }}&r={{ .Ruins }}&m={{ .Mountains }}">Direktlink</button>
<p>2011 <a href="https://aaron-fischer.net/">Aaron Fischer</a></p>
</body>

Loading…
Cancel
Save