laneya2

cave exploration game
git clone https://git.ce9e.org/laneya2.git

commit
0c90216e35c4613f3a5a7cfbec237ce63ca14f0f
parent
ce90d27efadfe333a7a6c08980aaad059d26a93a
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2024-10-05 08:45
pass item definitions to client

Diffstat

M items.go 16 ++++++++--------
M server.go 7 +++++++
M static/main.js 4 +---

3 files changed, 16 insertions, 11 deletions


diff --git a/items.go b/items.go

@@ -9,14 +9,14 @@ const (
    9     9 )
   10    10 
   11    11 type Item struct {
   12    -1 	Type        uint
   13    -1 	Value       uint
   14    -1 	Health      uint
   15    -1 	HealthTotal uint
   16    -1 	Attack      float64
   17    -1 	Defense     float64
   18    -1 	LineOfSight int
   19    -1 	Speed       int
   -1    12 	Type        uint    `json:"type"`
   -1    13 	Value       uint    `json:"value"`
   -1    14 	Health      uint    `json:"health"`
   -1    15 	HealthTotal uint    `json:"healthTotal"`
   -1    16 	Attack      float64 `json:"attack"`
   -1    17 	Defense     float64 `json:"defense"`
   -1    18 	LineOfSight int     `json:"lineOfSight"`
   -1    19 	Speed       int     `json:"speed"`
   20    20 }
   21    21 
   22    22 var Items = map[string]Item{

diff --git a/server.go b/server.go

@@ -2,6 +2,7 @@ package main
    2     2 
    3     3 import (
    4     4 	"context"
   -1     5 	"encoding/json"
    5     6 	"flag"
    6     7 	"fmt"
    7     8 	"log"
@@ -124,6 +125,11 @@ func serveWs(w http.ResponseWriter, r *http.Request) {
  124   125 	go player.readPump()
  125   126 }
  126   127 
   -1   128 func serveItems(w http.ResponseWriter, r *http.Request) {
   -1   129 	w.Header().Set("Content-Type", "application/json")
   -1   130 	json.NewEncoder(w).Encode(Items)
   -1   131 }
   -1   132 
  127   133 func serve(addr string) {
  128   134 	http.HandleFunc("GET /ws/{id}", serveWs)
  129   135 
@@ -166,6 +172,7 @@ func main() {
  166   172 		http.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
  167   173 			http.ServeFile(w, r, "index.html")
  168   174 		})
   -1   175 		http.HandleFunc("GET /items/", serveItems)
  169   176 		http.Handle("GET /static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
  170   177 	}
  171   178 

diff --git a/static/main.js b/static/main.js

@@ -18,9 +18,7 @@ var COLORS = {
   18    18     'pile': 3,
   19    19 };
   20    20 
   21    -1 var ITEMS = {
   22    -1     'potion': {health: 10},
   23    -1 };
   -1    21 var ITEMS = await fetch('/items/').then(r => r.json());
   24    22 
   25    23 var inRect = function(pos, rect, withWalls) {
   26    24     if (withWalls) {