aorecomp

Anarchy Online classic in the browser

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


server/item_source_pages.go (12319B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/json"
      6 	"fmt"
      7 	"html"
      8 	"maps"
      9 	"net/http"
     10 	"slices"
     11 	"strconv"
     12 	"strings"
     13 )
     14 
     15 type sourceMapLocation struct {
     16 	Name      string     `json:"name"`
     17 	Playfield uint32     `json:"playfield"`
     18 	Position  [3]float32 `json:"position"`
     19 	Loot      bool       `json:"loot,omitempty"`
     20 	Vending   bool       `json:"vending,omitempty"`
     21 }
     22 
     23 func sourceOverview(w http.ResponseWriter, sources []*itemSource) {
     24 	// Plot each known overworld position once, regardless of source name or height.
     25 	locations := make([]sourceMapLocation, 0)
     26 	type mapPoint struct {
     27 		playfield uint32
     28 		x, z      float32
     29 	}
     30 	seen := make(map[mapPoint]int)
     31 	unmapped := false
     32 	for _, source := range sources {
     33 		for _, location := range source.locations {
     34 			if _, known := gamedata.MapCoordinates.Playfields[location.playfield]; !known {
     35 				unmapped = true
     36 				continue
     37 			}
     38 			key := mapPoint{location.playfield, location.position[0], location.position[2]}
     39 			index, exists := seen[key]
     40 			if !exists {
     41 				index = len(locations)
     42 				locations = append(locations, sourceMapLocation{Name: location.name, Playfield: location.playfield, Position: location.position})
     43 				seen[key] = index
     44 			}
     45 			locations[index].Loot = locations[index].Loot || source.kind == "Loot"
     46 			locations[index].Vending = locations[index].Vending || source.kind == "Shop"
     47 		}
     48 	}
     49 	if len(locations) == 0 {
     50 		return
     51 	}
     52 	data, err := json.Marshal(locations)
     53 	if err != nil {
     54 		panic(err)
     55 	}
     56 	fmt.Fprintf(w, "<section><h2>Source locations on Rubi-Ka</h2><p>%d unique mapped locations. Indoor sources are shown at their outdoor entrances.</p>", len(locations))
     57 	fmt.Fprint(w, "<p aria-label='Map legend'><span aria-hidden='true' style='display:inline-block;width:12px;height:12px;border:1px solid #222;border-radius:50%;background:#f4b942'></span> Loot &nbsp; <span aria-hidden='true' style='display:inline-block;width:12px;height:12px;border:1px solid #222;border-radius:50%;background:#55b8ff'></span> Vending &nbsp; <span aria-hidden='true' style='display:inline-block;width:12px;height:12px;border:1px solid #222;border-radius:50%;background:#21db87'></span> Other. Split markers have both loot and vending.</p>")
     58 	if unmapped {
     59 		fmt.Fprint(w, "<p>Locations without a known overworld position are listed in the source details below.</p>")
     60 	}
     61 	fmt.Fprintf(w, "<script type='application/json' data-item-sources>%s</script>", data)
     62 	fmt.Fprint(w, "<div data-world-map data-map-overview><p data-map-status>Loading maps...</p><p data-map-location></p><canvas data-map-canvas width='1000' height='480' style='height:240px;min-height:0' aria-label='Item source overworld overview'></canvas></div><script src='admin/control-map.js' defer></script></section>")
     63 }
     64 
     65 func sourceMap(w http.ResponseWriter, sources []*itemSource) {
     66 	locations := make([]sourceMapLocation, 0)
     67 	seen := make(map[sourceMapLocation]bool)
     68 	for _, source := range sources {
     69 		for _, location := range source.locations {
     70 			marker := sourceMapLocation{source.kind + ": " + location.name, location.playfield, location.position, source.kind == "Loot", source.kind == "Shop"}
     71 			if !seen[marker] {
     72 				locations = append(locations, marker)
     73 				seen[marker] = true
     74 			}
     75 		}
     76 	}
     77 	if len(locations) == 0 {
     78 		return
     79 	}
     80 	data, err := json.Marshal(locations)
     81 	if err != nil {
     82 		panic(err)
     83 	}
     84 	fmt.Fprint(w, "<section><h2>Acquisition locations</h2><ul>")
     85 	for index, location := range locations {
     86 		fmt.Fprintf(w, "<li><a href='admin/map?playfield=%d&amp;x=%g&amp;z=%g' data-map-source='%d'>%s - %s (%d), X %.1f Z %.1f</a></li>", location.Playfield, location.Position[0], location.Position[2], index, html.EscapeString(location.Name), html.EscapeString(gamedata.Playfields[location.Playfield].Name), location.Playfield, location.Position[0], location.Position[2])
     87 	}
     88 	fmt.Fprintf(w, "</ul><script type='application/json' data-item-sources>%s</script>", data)
     89 	fmt.Fprint(w, "<div data-world-map><p data-map-status>Loading maps...</p><p data-map-location>Drag to pan. Pinch or scroll to zoom.</p><canvas data-map-canvas width='1000' height='480' aria-label='Item acquisition map'></canvas></div><script src='admin/control-map.js' defer></script>")
     90 	fmt.Fprint(w, "</section>")
     91 }
     92 
     93 func (server *server) itemTemplatePage(w http.ResponseWriter, r *http.Request) {
     94 	template, err := strconv.ParseUint(r.PathValue("template"), 10, 32)
     95 	definition := gamedata.ItemTemplates[uint32(template)]
     96 	if err != nil || definition == nil || definition.Quality == nil {
     97 		http.NotFound(w, r)
     98 		return
     99 	}
    100 	index := server.itemCatalog()
    101 	page, _ := strconv.Atoi(r.FormValue("page"))
    102 	sources, count := index.sourcePage(definition.ID, page)
    103 	server.adminPageStart(w, adminName(definition.Name), "Items")
    104 	fmt.Fprintf(w, "<p><a href='admin/items/catalog'>Item catalog</a></p><h1>%s%s</h1><table><tr><th scope='row'>Description</th><td>%s</td></tr><tr><th scope='row'>Mapped sources (QL family)</th><td>%s</td></tr></table>", itemIcon(definition), html.EscapeString(adminName(definition.Name)), html.EscapeString(definition.Description), html.EscapeString(itemSourceSummary(index.families[itemFamily(definition.ID)])))
    105 	if count > adminPageSize {
    106 		fmt.Fprint(w, "<h2>Source locations</h2><p>The map and location links show one page at a time.</p>")
    107 		adminPagination(w, r, count)
    108 	}
    109 	sourceOverview(w, sources)
    110 	fmt.Fprint(w, "<h2>Possible sources (QL family)</h2><table><tr><th>Type</th><th>Icon</th><th>Source</th><th>Offered QL</th><th>Details</th></tr>")
    111 	for _, source := range sources {
    112 		name := html.EscapeString(adminName(source.name))
    113 		icon := ""
    114 		if source.shop != 0 {
    115 			icon = itemIcon(gamedata.ItemTemplates[source.shop])
    116 			name = fmt.Sprintf("<a href='admin/shops/%d'>%s</a>", source.shop, name)
    117 		}
    118 		if source.ingredients[0] != 0 {
    119 			icon = itemIcon(gamedata.ItemTemplates[source.ingredients[0]]) + itemIcon(gamedata.ItemTemplates[source.ingredients[1]])
    120 			name = catalogItemLink(source.ingredients[0]) + " + " + catalogItemLink(source.ingredients[1])
    121 		}
    122 		quality := fmt.Sprintf("%d", source.quality[0])
    123 		if source.quality[0] != source.quality[1] {
    124 			quality = fmt.Sprintf("%d-%d", source.quality[0], source.quality[1])
    125 		}
    126 		if source.quality[0] == 0 {
    127 			quality = "Mission level"
    128 		}
    129 		fmt.Fprintf(w, "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>", source.kind, icon, name, quality)
    130 		if source.kind == "Loot" || source.shop != 0 || len(source.locations) != 0 {
    131 			fmt.Fprint(w, "<table>")
    132 		}
    133 		if source.kind == "Loot" {
    134 			fmt.Fprintf(w, "<tr><th scope='row'>Chance per roll</th><td>%s</td></tr><tr><th scope='row'>NPC levels</th><td>%d-%d</td></tr>", itemLootChance(source), source.level[0], source.level[1])
    135 			if source.pool != "" {
    136 				fmt.Fprintf(w, "<tr><th scope='row'>Pool selection</th><td>%d%% chance to select one of %d-%d eligible families from %s. Item probability varies with NPC level.</td></tr>", source.chance, source.candidates[0], source.candidates[1], html.EscapeString(source.pool))
    137 			}
    138 			if source.evidence != "" {
    139 				fmt.Fprintf(w, "<tr><th scope='row'>Evidence</th><td>%s</td></tr>", html.EscapeString(source.evidence))
    140 			}
    141 			if source.inference != "" {
    142 				fmt.Fprintf(w, "<tr><th scope='row'>Inferred</th><td>%s</td></tr>", html.EscapeString(source.inference))
    143 			}
    144 		}
    145 		if source.shop != 0 {
    146 			fmt.Fprintf(w, "<tr><th scope='row'>Access</th><td>%s</td></tr>", html.EscapeString(shopAccess(gamedata.ItemTemplates[source.shop])))
    147 		}
    148 		if len(source.locations) != 0 {
    149 			fmt.Fprint(w, "<tr><th scope='row'>Locations</th><td><ul>")
    150 			for _, location := range source.locations {
    151 				fmt.Fprintf(w, "<li><a href='admin/map?playfield=%d&amp;x=%g&amp;z=%g'>%s - %s (%d), X %.1f Z %.1f</a></li>", location.playfield, location.position[0], location.position[2], html.EscapeString(adminName(location.name)), html.EscapeString(gamedata.Playfields[location.playfield].Name), location.playfield, location.position[0], location.position[2])
    152 			}
    153 			fmt.Fprint(w, "</ul></td></tr>")
    154 		}
    155 		if source.kind == "Loot" || source.shop != 0 || len(source.locations) != 0 {
    156 			fmt.Fprint(w, "</table>")
    157 		}
    158 		fmt.Fprint(w, "</td></tr>")
    159 	}
    160 	fmt.Fprint(w, "</table>")
    161 	if count > adminPageSize {
    162 		adminPagination(w, r, count)
    163 	}
    164 	itemCatalogProperties(w, ownedItem{Type: definition.HeaderWords[0], Template: definition.ID, Quality: *definition.Quality}, definition)
    165 	fmt.Fprint(w, "</main></body></html>")
    166 }
    167 
    168 func shopAccess(definition *gamedata.Item) string {
    169 	var conditions []string
    170 	for _, section := range definition.Sections {
    171 		if section.Actions == nil {
    172 			continue
    173 		}
    174 		for _, action := range *section.Actions {
    175 			if action.Type != 3 {
    176 				continue
    177 			}
    178 			var stack []string
    179 			for _, requirement := range action.Requirements {
    180 				if (requirement.Operator == 3 || requirement.Operator == 4) && len(stack) >= 2 {
    181 					operator := " OR "
    182 					if requirement.Operator == 4 {
    183 						operator = " AND "
    184 					}
    185 					stack[len(stack)-2] = "(" + stack[len(stack)-2] + operator + stack[len(stack)-1] + ")"
    186 					stack = stack[:len(stack)-1]
    187 				} else if requirement.Operator == 42 && len(stack) > 0 {
    188 					stack[len(stack)-1] = "NOT (" + stack[len(stack)-1] + ")"
    189 				} else {
    190 					operator := map[int32]string{0: "=", 1: "<", 2: ">", 24: "!="}[requirement.Operator]
    191 					if operator == "" {
    192 						operator = fmt.Sprintf("operator %d", requirement.Operator)
    193 					}
    194 					value := fmt.Sprint(requirement.Value)
    195 					if requirement.Stat == 33 {
    196 						value = map[int32]string{0: "Neutral", 1: "Clan", 2: "Omni"}[requirement.Value]
    197 					}
    198 					stack = append(stack, fmt.Sprintf("%s %s %s", gamedata.ItemStatNames[uint32(requirement.Stat)], operator, value))
    199 				}
    200 			}
    201 			if len(stack) > 0 {
    202 				conditions = append(conditions, stack[len(stack)-1])
    203 			}
    204 		}
    205 	}
    206 	if len(conditions) == 0 {
    207 		return "No faction or profession restriction"
    208 	}
    209 	return strings.Join(conditions, " AND ")
    210 }
    211 
    212 func (server *server) shopCatalogPage(w http.ResponseWriter, r *http.Request) {
    213 	template, err := strconv.ParseUint(r.PathValue("template"), 10, 32)
    214 	stock, exists := gamedata.ShopStock[uint32(template)]
    215 	if err != nil || !exists {
    216 		http.NotFound(w, r)
    217 		return
    218 	}
    219 	definition := gamedata.ItemTemplates[uint32(template)]
    220 	server.adminPageStart(w, adminName(definition.Name), "Items")
    221 	fmt.Fprintf(w, "<p><a href='admin/items/catalog'>Item catalog</a></p><h1>%s</h1><table><tr><th scope='row'>Shop template</th><td>%d</td></tr><tr><th scope='row'>Access</th><td>%s</td></tr>", itemIcon(definition)+html.EscapeString(adminName(definition.Name)), template, html.EscapeString(shopAccess(definition)))
    222 	fmt.Fprint(w, "<tr><th scope='row'>Stock pool</th><td>Possible stock</td></tr><tr><th scope='row'>Always stocked</th><td>Supplies and QL 1-10 equipment</td></tr><tr><th scope='row'>Rotating stock</th><td>Higher-QL weapons and armor</td></tr><tr><th scope='row'>Rotation interval</th><td>30 minutes</td></tr><tr><th scope='row'>Inclusion chance per rotating row</th><td>75%</td></tr><tr><th scope='row'>QL variation</th><td>Up to 10% within supported range</td></tr><tr><th scope='row'>Open-trade stock and prices</th><td>Fixed</td></tr></table>")
    223 	// Each catalog repeats its placements per item; one source supplies the map.
    224 	sources := server.itemCatalog().families
    225 	for _, family := range slices.Sorted(maps.Keys(sources)) {
    226 		found := false
    227 		for _, source := range sources[family] {
    228 			if source.shop == uint32(template) {
    229 				sourceMap(w, []*itemSource{source})
    230 				found = true
    231 				break
    232 			}
    233 		}
    234 		if found {
    235 			break
    236 		}
    237 	}
    238 	adminFilter(w, r)
    239 	var entries []adminTableRow
    240 	for _, entry := range stock {
    241 		low, high := shopQualityRange(entry)
    242 		availability := "Always stocked"
    243 		if low != high {
    244 			availability = "Rotating"
    245 		}
    246 		entries = append(entries, adminTableRow{
    247 			values: []string{"", adminName(gamedata.ItemTemplates[entry.Template].Name), fmt.Sprint(entry.Template), fmt.Sprint(entry.Quality), fmt.Sprintf("%d-%d", low, high), availability},
    248 			markup: fmt.Sprintf("<tr><td>%s</td><td>%s</td><td>%d</td><td>%d</td><td>%d-%d</td><td>%s</td></tr>", itemIcon(gamedata.ItemTemplates[entry.Template]), catalogItemLink(entry.Template), entry.Template, entry.Quality, low, high, availability),
    249 		})
    250 	}
    251 	adminTable(w, r, []string{"Icon", "Item", "Template", "Base QL", "Possible QL", "Availability"}, entries)
    252 	fmt.Fprint(w, "</main></body></html>")
    253 }