mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-22 17:47:11 +00:00
Save load from cloud
This commit is contained in:
parent
19d822f217
commit
45c7d0269a
3 changed files with 54 additions and 32 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue