Skip to content

Commit

Permalink
Remove history processing for server
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbuddy committed Mar 23, 2024
1 parent bc36e60 commit 19a35d4
Show file tree
Hide file tree
Showing 147 changed files with 0 additions and 33,784 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ require (
github.com/tidwall/sjson v1.2.5
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225
golang.org/x/mod v0.16.0
rogchap.com/v8go v0.9.0
)

require (
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,3 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
rogchap.com/v8go v0.9.0 h1:wYbUCO4h6fjTamziHrzyrPnpFNuzPpjZY+nfmZjNaew=
rogchap.com/v8go v0.9.0/go.mod h1:MxgP3pL2MW4dpme/72QRs8sgNMmM0pRc8DPhcuLWPAs=
149 changes: 0 additions & 149 deletions internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"rogchap.com/v8go"
)

var liveDev = os.Getenv("LIVE_DEV") == "1"
Expand Down Expand Up @@ -180,28 +179,6 @@ func (s *Server) Serve() error {
}
})

r.Post("/reprocess", func(w http.ResponseWriter, r *http.Request) {
ssd := &SaveStateData{}
if err := json.NewDecoder(r.Body).Decode(ssd); err != nil {
fmt.Printf("error: %#v\n", err)
w.WriteHeader(500)
return
}

newSaveState, err := s.reprocessHistoryFromSaveState(ssd)
if err != nil {
fmt.Printf("error: %#v\n", err)
w.WriteHeader(500)
return
}

if err := json.NewEncoder(w).Encode(newSaveState); err != nil {
fmt.Printf("error: %#v\n", err)
w.WriteHeader(500)
return
}
})

r.Get("/states", func(w http.ResponseWriter, r *http.Request) {
entries, err := os.ReadDir(saveStatesPath)
if err != nil {
Expand Down Expand Up @@ -263,13 +240,6 @@ func (s *Server) Serve() error {
w.WriteHeader(500)
return
}
saveStateData, err = s.reprocessHistoryFromSaveState(saveStateData)
if err != nil {
fmt.Printf("error: %#v\n", err)
w.WriteHeader(500)
return
}

w.WriteHeader(200)
if err := json.NewEncoder(w).Encode(saveStateData); err != nil {
fmt.Printf("error: %#v\n", err)
Expand Down Expand Up @@ -579,122 +549,3 @@ func (s *Server) getFile(n string) ([]byte, error) {
return site.ReadFile(n)
}
}

func (s *Server) reprocessHistoryFromSaveState(ss *SaveStateData) (*SaveStateData, error) {
moves := make([]*Move, len(ss.History))
setup := &SetupState{
RandomSeed: ss.RandomSeed,
Players: ss.Players,
Settings: ss.Settings,
}
for i, hi := range ss.History {
moves[i] = &Move{
Position: hi.Position,
Data: hi.Data,
}
}
req := &ReprocessRequest{Moves: moves, Setup: setup}
resp, err := s.reprocessHistory(req)
if err != nil {
return nil, err
}
history := make([]*HistoryItem, len(resp.Updates))
for i, update := range resp.Updates {
history[i] = &HistoryItem{
Seq: i,
State: update,
Data: moves[i].Data,
Position: moves[i].Position,
}
}
return &SaveStateData{
RandomSeed: ss.RandomSeed,
Settings: ss.Settings,
Players: ss.Players,
History: history,
InitialState: InitialStateHistoryItem{
State: resp.InitialState,
Players: ss.Players,
Settings: ss.Settings,
},
}, nil
}

func (s *Server) reprocessHistory(req *ReprocessRequest) (*ReprocessResponse, error) {
iso := v8go.NewIsolate()
defer func() {
iso.Dispose()
}()
gameJS, err := os.ReadFile(path.Join(s.gameRoot, s.manifest.Game.Root, s.manifest.Game.OutputFile))
if err != nil {
return nil, err
}
setup, err := json.Marshal(req.Setup)
if err != nil {
return nil, err
}
moves, err := json.Marshal(req.Moves)
if err != nil {
return nil, err
}
errs := make(chan error)
vals := make(chan *v8go.Value)
go func() {
start := time.Now()
log := v8go.NewFunctionTemplate(iso, func(info *v8go.FunctionCallbackInfo) *v8go.Value {
fmt.Printf("[reprocess history] >>> ")
args := info.Args()
for i, arg := range args {
fmt.Printf("%v", arg)
if i != len(args)-1 {
fmt.Printf(" ")
} else {
fmt.Printf("\n")
}
}
return nil
})
global := v8go.NewObjectTemplate(iso)
if err := global.Set("log", log); err != nil {
errs <- fmt.Errorf("error setting log: %w", err)
return
}
ctx := v8go.NewContext(iso, global)
if _, err := ctx.RunScript("console.log = log; console.error = log;", "load.js"); err != nil {
errs <- fmt.Errorf("error loading game: %w", err)
return
}
if _, err := ctx.RunScript(string(gameJS), "game.js"); err != nil {
errs <- fmt.Errorf("error loading game: %w", err)
return
}
if _, err := ctx.RunScript(`if (game.default) {
game = game.default
}`, "game.js"); err != nil {
errs <- fmt.Errorf("error flattening game structure: %w", err)
return
}
script := fmt.Sprintf("JSON.stringify(game.reprocessHistory(%s, %s))", string(setup), string(moves))
val, err := ctx.RunScript(script, "reprocessHistory.js")
if err != nil {
errs <- fmt.Errorf("error running reprocess: %w", err)
return
}
fmt.Printf("re-process history took %s\n", time.Since(start))
vals <- val
}()

select {
case val := <-vals:
response := &ReprocessResponse{}
if err := json.Unmarshal([]byte(val.String()), response); err != nil {
return nil, err
}
return response, nil
case err := <-errs:
return nil, err
case <-time.After(30000 * time.Millisecond):
iso.TerminateExecution() // terminate the execution
return nil, fmt.Errorf("took too long")
}
}
10 changes: 0 additions & 10 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,3 @@ golang.org/x/term
# golang.org/x/text v0.14.0
## explicit; go 1.18
golang.org/x/text/transform
# rogchap.com/v8go v0.9.0
## explicit; go 1.16
rogchap.com/v8go
rogchap.com/v8go/deps/darwin_arm64
rogchap.com/v8go/deps/darwin_x86_64
rogchap.com/v8go/deps/include
rogchap.com/v8go/deps/include/cppgc
rogchap.com/v8go/deps/include/libplatform
rogchap.com/v8go/deps/linux_arm64
rogchap.com/v8go/deps/linux_x86_64
13 changes: 0 additions & 13 deletions vendor/rogchap.com/v8go/.fossa.yml

This file was deleted.

12 changes: 0 additions & 12 deletions vendor/rogchap.com/v8go/.gitignore

This file was deleted.

6 changes: 0 additions & 6 deletions vendor/rogchap.com/v8go/.gitmodules

This file was deleted.

137 changes: 0 additions & 137 deletions vendor/rogchap.com/v8go/CHANGELOG.md

This file was deleted.

21 changes: 0 additions & 21 deletions vendor/rogchap.com/v8go/CONTRIBUTING.md

This file was deleted.

Loading

0 comments on commit 19a35d4

Please sign in to comment.