From 45c7d0269a43455daa784f74bbf775587dddaf78 Mon Sep 17 00:00:00 2001 From: giongto35 Date: Sat, 4 May 2019 17:33:53 +0800 Subject: [PATCH] Save load from cloud --- emulator/director.go | 10 ++++++---- emulator/gameview.go | 41 +++++++++++++++++++++++++++++------------ handler/room/room.go | 35 +++++++++++++++++++---------------- 3 files changed, 54 insertions(+), 32 deletions(-) diff --git a/emulator/director.go b/emulator/director.go index 29696885..770e5227 100644 --- a/emulator/director.go +++ b/emulator/director.go @@ -111,18 +111,20 @@ func (d *Director) PlayGame(path string) { d.SetView(NewGameView(console, path, hash, d.imageChannel, d.audioChannel, d.inputChannel)) } -func (d *Director) SaveGame() error { +// SaveGame creates save events and doing extra step for load +func (d *Director) SaveGame(saveExtraFunc func() error) error { if d.hash != "" { - d.view.Save(d.hash) + d.view.Save(d.hash, saveExtraFunc) return nil } else { return nil } } -func (d *Director) LoadGame() error { +// LoadGame creates load events and doing extra step for load +func (d *Director) LoadGame(loadExtraFunc func() error) error { if d.hash != "" { - d.view.Load(d.hash) + d.view.Load(d.hash, loadExtraFunc) return nil } else { return nil diff --git a/emulator/gameview.go b/emulator/gameview.go index a6100d62..c9e96c17 100644 --- a/emulator/gameview.go +++ b/emulator/gameview.go @@ -3,6 +3,7 @@ package emulator import ( "image" + "log" "github.com/giongto35/cloud-game/emulator/nes" ) @@ -45,14 +46,19 @@ type GameView struct { // equivalent to the list key pressed const above keyPressed [NumKeys * 2]bool - savingPath string - loadingPath string + savingJob *job + loadingJob *job imageChannel chan *image.RGBA audioChannel chan float32 inputChannel chan int } +type job struct { + path string + extraFunc func() error +} + func NewGameView(console *nes.Console, title, hash string, imageChannel chan *image.RGBA, audioChannel chan float32, inputChannel chan int) *GameView { gameview := &GameView{ console: console, @@ -136,26 +142,37 @@ func (view *GameView) Update(t, dt float64) { view.imageChannel <- console.Buffer() } -func (view *GameView) Save(hash string) { +func (view *GameView) Save(hash string, extraSaveFunc func() error) { // put saving event to queue, process in updateEvent - view.savingPath = savePath(view.hash) + view.savingJob = &job{ + path: savePath(view.hash), + extraFunc: extraSaveFunc, + } } -func (view *GameView) Load(path string) { +func (view *GameView) Load(path string, extraLoadFunc func() error) { // put saving event to queue, process in updateEvent - view.loadingPath = savePath(view.hash) + view.loadingJob = &job{ + path: savePath(view.hash), + extraFunc: extraLoadFunc, + } } func (view *GameView) UpdateEvents() { // If there is saving event, save and discard the save event - if view.savingPath != "" { - view.console.SaveState(view.savingPath) - view.savingPath = "" + log.Println(view.savingJob) + if view.savingJob != nil { + view.console.SaveState(view.savingJob.path) + // Run extra function (online saving for example) + go view.savingJob.extraFunc() + view.savingJob = nil } // If there is loading event, save and discard the load event - if view.loadingPath != "" { - view.console.LoadState(view.loadingPath) - view.loadingPath = "" + if view.loadingJob != nil { + view.console.LoadState(view.loadingJob.path) + // Run extra function (online saving for example) + go view.loadingJob.extraFunc() + view.loadingJob = nil } } diff --git a/handler/room/room.go b/handler/room/room.go index ace0c589..6f35f8e6 100644 --- a/handler/room/room.go +++ b/handler/room/room.go @@ -8,7 +8,6 @@ import ( "math/rand" "strconv" "sync" - "time" emulator "github.com/giongto35/cloud-game/emulator" storage "github.com/giongto35/cloud-game/handler/cloud-storage" @@ -142,15 +141,18 @@ func (r *Room) Close() { } func (r *Room) SaveGame() error { - // TODO: Move to game view - if err := r.director.SaveGame(); err != nil { - return err - } - time.Sleep(1000 * time.Millisecond) + onlineSaveFunc := func() error { + // Try to save the game to gCloud + // TODO: Add goroutine + if err := r.onlineStorage.SaveFile(r.director.GetHash(), r.director.GetHashPath()); err != nil { + return err + } - // Try to save the game to gCloud - // TODO: Add goroutine - if err := r.onlineStorage.SaveFile(r.director.GetHash(), r.director.GetHashPath()); err != nil { + return nil + } + + // TODO: Move to game view + if err := r.director.SaveGame(onlineSaveFunc); err != nil { return err } @@ -158,9 +160,7 @@ func (r *Room) SaveGame() error { } func (r *Room) LoadGame() error { - err := r.director.LoadGame() - time.Sleep(1000 * time.Millisecond) - if err != nil { + onlineLoadFunc := func() error { // TODO: Put in GoRoutine // If the game is not on local server // Try to load from gcloud @@ -171,12 +171,15 @@ func (r *Room) LoadGame() error { // Save the data fetched from gcloud to local server ioutil.WriteFile(r.director.GetHashPath(), data, 0644) // Reload game again - err = r.director.LoadGame() - if err != nil { - return err - } + //err = r.director.LoadGame(nil) + //if err != nil { + //return err + //} + return nil } + err := r.director.LoadGame(onlineLoadFunc) + return err }