aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/status.go (4698B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/json"
      6 	"fmt"
      7 	"html"
      8 	"net/http"
      9 	"strings"
     10 )
     11 
     12 func (server *server) statusPage(w http.ResponseWriter, r *http.Request) {
     13 	w.Header().Set("Content-Type", "text/html; charset=utf-8")
     14 	fmt.Fprintf(w, `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><base href="%s"><title>Server status - Rubi-Ka</title><style>
     15 *{box-sizing:border-box}body{margin:0;background:#f4f6f8;color:#182634;font:15px system-ui,sans-serif}header{background:#142c3e;color:white;padding:16px 24px;display:flex;align-items:center;justify-content:space-between}header a{color:white}main{max-width:1500px;margin:auto;padding:24px}h1{font-size:24px;margin:0}h2{font-size:18px}a{color:#175d8d}button,select{font:inherit;padding:10px;border:1px solid #a7b8c5;border-radius:5px;background:white;color:#182634;max-width:100%%}button{cursor:pointer;min-height:44px}button:focus-visible,select:focus-visible,a:focus-visible{outline:3px solid #e0a22a;outline-offset:2px}.layout{display:grid;grid-template-columns:minmax(0,1fr) 300px;gap:24px}.map,aside{background:white;padding:16px;border:1px solid #d9e1e7;border-radius:8px;min-width:0}.controls{display:flex;gap:8px;flex-wrap:wrap;align-items:center}canvas{width:100%%;height:60vh;min-height:320px;display:block;touch-action:none;border-radius:6px}p{line-height:1.5}.muted{color:#526777}[data-status-roster] ul{padding:0;list-style:none}[data-status-roster] button{border:0;color:#175d8d;text-align:left;width:100%%;overflow-wrap:anywhere;padding:6px 0;min-height:0}[data-status-roster] button:hover{background:#e7edf2}[data-status-roster] button:disabled{color:#526777;cursor:default}small{display:block;color:#526777}@media(max-width:800px){main{padding:12px}.layout{grid-template-columns:1fr}header{padding:16px}canvas{height:50vh}}
     16 </style></head><body><header><h1>Server status</h1><a href="./">Play</a></header><main><p>Find other players on Rubi-Ka. Updates every 5 seconds.</p><div class="layout" data-world-map data-server-status><section class="map" aria-label="Player map"><div class="controls"><label>Find zone <select data-map-find><option value="">Choose a zone</option></select></label><button type="button" data-map-zoom="1" aria-label="Zoom in">+</button><button type="button" data-map-zoom="-1" aria-label="Zoom out">-</button><button type="button" data-map-center>Show world</button></div><p data-map-status role="status">Loading players...</p><canvas width="1000" height="480" aria-label="Online player map"></canvas><p class="muted">Outdoor markers show player positions. Interior markers show the entrance used, not the player's position inside.</p></section><aside><h2 data-status-total>Online players</h2><div data-status-roster>Loading...</div></aside></div></main><script src="status/map.js" defer></script></body></html>`, html.EscapeString(server.baseURL))
     17 }
     18 
     19 func statusZoneName(playfield uint32) string {
     20 	name := gamedata.Playfields[playfield].Name
     21 	if name == "" {
     22 		return fmt.Sprintf("Playfield %d", playfield)
     23 	}
     24 	if parts := strings.Split(name, "_"); len(parts) == 4 && parts[0] == "ord" && parts[1] == "smarket" {
     25 		faction := map[string]string{"clan": "Clan", "omni": "Omni", "neut": "Neutral"}[parts[2]]
     26 		return faction + " " + strings.ToUpper(parts[3][:1]) + parts[3][1:] + " Shop"
     27 	}
     28 	return strings.ReplaceAll(name, "_", " ")
     29 }
     30 
     31 func (server *server) statusPlayers(w http.ResponseWriter, r *http.Request) {
     32 	players, err := server.adminPlayers()
     33 	if err != nil {
     34 		http.Error(w, "Cannot read online players", http.StatusInternalServerError)
     35 		return
     36 	}
     37 
     38 	// Only detached online player data is public, regardless of query parameters.
     39 	online := make([]mapPlayer, 0)
     40 	for _, player := range players {
     41 		if !player.Connected {
     42 			continue
     43 		}
     44 		player.Zone = statusZoneName(player.Playfield)
     45 		if _, public := gamedata.Playfields[player.Playfield]; !public {
     46 			if mission := server.missionWorld(player.Playfield); mission != nil {
     47 				player.Zone = fmt.Sprintf("Private mission in %s #%d", statusZoneName(mission.offer.definition.Playfield), player.Playfield)
     48 				// Missions inside public interiors retain the player's outer entrance.
     49 				if _, outdoor := gamedata.MapCoordinates.Playfields[mission.offer.definition.Playfield]; outdoor || player.Entrance == nil {
     50 					player.Entrance = &mapEntrance{mission.offer.definition.Playfield, mission.offer.definition.Position}
     51 				}
     52 			}
     53 		}
     54 		online = append(online, player)
     55 	}
     56 	w.Header().Set("Content-Type", "application/json")
     57 	w.Header().Set("Cache-Control", "no-store")
     58 	json.NewEncoder(w).Encode(struct {
     59 		Players []mapPlayer `json:"players"`
     60 	}{online})
     61 }