Browse Source

Don't show map items that overflow the map

Also; spawn point and world radius configurable.
master
Remi Reuvekamp 5 years ago
parent
commit
ce57db1849
  1. 11
      config.go
  2. 32
      http.go
  3. 24
      index.tmpl
  4. 11
      main.go

11
config.go

@ -23,9 +23,11 @@ import (
)
type config struct {
HTTPListen string
WeechatLogFilePath string
HTTPListen string
WeechatLogFilePath string
WorldRadius int
Spawn [2]int
AmountLinesSurrounding int
}
type loadConfigError struct {
@ -40,6 +42,9 @@ func (l loadConfigError) Error() string {
var defaultConfig = config{
HTTPListen: ":13375",
WeechatLogFilePath: "test-file.log",
WorldRadius: 10000,
Spawn: [2]int{0, 0},
AmountLinesSurrounding: 2,
}
// loadConfig unmarhsals the contents of the file with given filename as JSON, which

32
http.go

@ -27,6 +27,7 @@ import (
type env struct {
ops chan func([]location)
cfg config
}
type handler func(env, http.ResponseWriter, *http.Request)
@ -34,8 +35,11 @@ type handler func(env, http.ResponseWriter, *http.Request)
var rootTemplate *template.Template
type rootPage struct {
Locs []location
FilterName string
Locs []location
FilterName string
WorldRadius, WorldDiameter int
MaxX, MinX, MaxZ, MinZ int
Spawn [2]int
}
func init() {
@ -53,12 +57,14 @@ func init() {
func handleRoot(e env, w http.ResponseWriter, r *http.Request) {
filterName := r.FormValue("filter-name")
compareFilterName := strings.ToLower(filterName)
locsChan := make(chan []location)
e.ops <- func(locs []location) {
copiedLocs := make([]location, 0, len(locs))
for i := len(locs) - 1; i >= 0; i-- {
if filterName != "" && !strings.Contains(locs[i].CoordsMsg.User, filterName) {
// Case insensitive name filter.
if filterName != "" && !strings.Contains(strings.ToLower(locs[i].CoordsMsg.User), compareFilterName) {
continue
}
@ -70,9 +76,19 @@ func handleRoot(e env, w http.ResponseWriter, r *http.Request) {
copiedLocs := <-locsChan
radius := e.cfg.WorldRadius
p := rootPage{
Locs: copiedLocs,
FilterName: filterName,
Locs: copiedLocs,
FilterName: filterName,
WorldRadius: e.cfg.WorldRadius,
WorldDiameter: e.cfg.WorldRadius * 2,
Spawn: e.cfg.Spawn,
MinX: e.cfg.Spawn[0] - radius,
MaxX: e.cfg.Spawn[0] + radius,
MinZ: e.cfg.Spawn[1] - radius,
MaxZ: e.cfg.Spawn[1] + radius,
}
err := rootTemplate.ExecuteTemplate(w, "index.tmpl", p)
@ -87,6 +103,12 @@ func handle(e env, handler handler) http.Handler {
defer func() {
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
if ip == "127.0.0.1" {
forwardIP := r.Header.Get("X-FORWARDED-FOR")
if forwardIP != "" {
ip = forwardIP
}
}
// Log this request.
log.Printf("%-15s %.1fms %s%s\n", ip, time.Since(startTime).Seconds()*1000,

24
index.tmpl

@ -21,6 +21,7 @@
background: #eeeeee;
border: 3px solid #cccccc;
margin: 0 auto;
box-sizing: border-box;
}
div#map a {
@ -42,6 +43,10 @@
max-width: 600px;
margin: 0 auto;
}
footer, div.info.center {
text-align: center;
}
</style>
</head>
<body>
@ -54,14 +59,14 @@
</p>
<form method="get">
Filter player name:
<input type="text" name="filter-name"{{ if .FilterName }} value="{{ .FilterName }}"{{ end }} />
<input type="text" placeholder="(part of) player name" name="filter-name"{{ if .FilterName }} value="{{ .FilterName }}"{{ end }} />
<input type="submit" value="Filter" />
</form>
</div>
<div id="map">
&#160;
</div>
<div class="info">
<div class="info center">
<p>
(Black in the center is spawn.)
</p>
@ -81,6 +86,9 @@
</p>
</p>
{{ end }}
<footer>
<a href="http://git.remi.im/remi/mcleiger-coordinates">Source code</a>
</footer>
<script>
// Replace unix time in <time> tags by a time string, hopefully in the user's timezone.
let times = document.querySelectorAll("time")
@ -107,17 +115,21 @@
m.style.width = mapSize + "px"
m.style.height = mapSize + "px"
let worldSize = 20000
let worldDiameter = {{ .WorldDiameter }}
let blocksPerPixel = worldSize / mapSize
let blocksPerPixel = worldDiameter / mapSize
console.log(mapSize)
let center = [ 252, 213 ]
let center = [ {{ index .Spawn 0 }}, {{ index .Spawn 1 }} ]
let items = [
{{ range .Locs }}
{{ if eq .Coords.World "Overworld" }}
[ {{ .Coords.X }}, {{ .Coords.Z }}, "{{ .CoordsMsg.User }}" ],
{{ if and (ge .Coords.X $.MinX) (le .Coords.X $.MaxX) }}
{{ if and (ge .Coords.Z $.MinZ) (le .Coords.Z $.MaxZ) }}
[ {{ .Coords.X }}, {{ .Coords.Z }}, "{{ .CoordsMsg.User }}" ],
{{ end }}
{{ end }}
{{ end }}
{{ end }}
]

11
main.go

@ -56,13 +56,15 @@ type message struct {
Time time.Time
}
var myCoordsRegExp = regexp.MustCompile(`My coordinates are: \((?P<X>\-?[0-9]+).0, (?P<Y>\-?[0-9]+).0, (?P<X>\-?[0-9]+).0\) \- (?P<World>[a-zA-Z]+)$`)
var (
myCoordsRegExp = regexp.MustCompile(`My coordinates are: \((?P<X>\-?[0-9]+).0, (?P<Y>\-?[0-9]+).0, (?P<X>\-?[0-9]+).0\) \- (?P<World>[a-zA-Z]+)$`)
var ircMessage = regexp.MustCompile(`^(?P<Date>[0-9]{4}\-[0-9]{2}\-[0-9]{2}) (?P<Time>[0-9]{2}:[0-9]{2}:[0-9]{2}) [@+]?(?P<User>[0-9a-zA-Z_]+) (?P<Line>.*)$`)
ircMessage = regexp.MustCompile(`^(?P<Date>[0-9]{4}\-[0-9]{2}\-[0-9]{2}) (?P<Time>[0-9]{2}:[0-9]{2}:[0-9]{2}) [@+]?(?P<User>[0-9a-zA-Z_]+) (?P<Line>.*)$`)
var ircIngameMessage = regexp.MustCompile(`^\[(?P<World>[a-zA-Z_]+)\] <(?P<Name>[0-9a-zA-Z_]+)> (?P<Message>.*)$`)
ircIngameMessage = regexp.MustCompile(`^\[(?P<World>[a-zA-Z_]+)\] <(?P<Name>[0-9a-zA-Z_]+)> (?P<Message>.*)$`)
var mclBotUser = regexp.MustCompile(`^MCLBot_?$`)
mclBotUser = regexp.MustCompile(`^MCLBot_?$`)
)
var timezone, _ = time.LoadLocation("Europe/Amsterdam")
@ -106,6 +108,7 @@ func main() {
e := env{
operations,
cfg,
}
mux := http.NewServeMux()

Loading…
Cancel
Save