Skip to content

Commit

Permalink
Rename more methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sergystepanov committed Jul 10, 2023
1 parent 24db595 commit 091f19a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 44 deletions.
14 changes: 7 additions & 7 deletions pkg/worker/cloudsave.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func WithCloudStorage(room GamingRoom, storage CloudStorage) *CloudSaveRoom {
storage: storage,
}
if err := cr.Download(); err != nil {
room.GetLog().Warn().Err(err).Msg("The room is not in the cloud")
room.Log().Warn().Err(err).Msg("The room is not in the cloud")
}
return &cr
}
Expand All @@ -22,22 +22,22 @@ func (c *CloudSaveRoom) Download() error {
// saveOnlineRoomToLocal save online room to local.
// !Supports only one file of main save state.

data, err := c.storage.Load(c.GetId())
data, err := c.storage.Load(c.Id())
if err != nil {
return err
}
// Save the data fetched from a cloud provider to the local server
if data != nil {
if err := os.WriteFile(c.GetEmulator().GetHashPath(), data, 0644); err != nil {
if err := os.WriteFile(c.Emulator().GetHashPath(), data, 0644); err != nil {
return err
}
c.GetLog().Debug().Msg("Successfully downloaded cloud save")
c.Log().Debug().Msg("Successfully downloaded cloud save")
}
return nil
}

func (c *CloudSaveRoom) HasSave() bool {
_, err := c.storage.Load(c.GetId())
_, err := c.storage.Load(c.Id())
if err == nil {
return true
}
Expand All @@ -48,9 +48,9 @@ func (c *CloudSaveRoom) SaveGame() error {
if err := c.GamingRoom.SaveGame(); err != nil {
return err
}
if err := c.storage.Save(c.GetId(), c.GetEmulator().GetHashPath()); err != nil {
if err := c.storage.Save(c.Id(), c.Emulator().GetHashPath()); err != nil {
return err
}
c.GetLog().Debug().Msg("Cloud save is successful")
c.Log().Debug().Msg("Cloud save is successful")
return nil
}
18 changes: 9 additions & 9 deletions pkg/worker/coordinatorhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (c *coordinator) HandleGameStart(rq api.StartGameRequest[com.Uid], w *Worke
}
w.log.Info().Msgf("Starting game: %v", rq.Game.Name)

room := w.router.GetRoom(rq.Rid)
room := w.router.FindRoom(rq.Rid)
if room == nil {
room = NewRoom(
rq.Room.Rid,
Expand Down Expand Up @@ -119,9 +119,9 @@ func (c *coordinator) HandleGameStart(rq api.StartGameRequest[com.Uid], w *Worke
}
user.SetRoom(room)

c.RegisterRoom(room.GetId())
c.RegisterRoom(room.Id())

return api.Out{Payload: api.StartGameResponse{Room: api.Room{Rid: room.GetId()}, Record: w.conf.Recording.Enabled}}
return api.Out{Payload: api.StartGameResponse{Room: api.Room{Rid: room.Id()}, Record: w.conf.Recording.Enabled}}
}

// HandleTerminateSession handles cases when a user has been disconnected from the websocket of coordinator.
Expand All @@ -142,14 +142,14 @@ func (c *coordinator) HandleQuitGame(rq api.GameQuitRequest[com.Uid], w *Worker)
// we don't strictly need a room id form the request,
// since users hold their room reference
// !to remove rid, maybe
if room := w.router.GetRoom(rq.Rid); room != nil {
if room := w.router.FindRoom(rq.Rid); room != nil {
room.Expel(user)
}
}
}

func (c *coordinator) HandleSaveGame(rq api.SaveGameRequest[com.Uid], w *Worker) api.Out {
room := w.router.GetRoom(rq.Rid)
room := w.router.FindRoom(rq.Rid)
if room == nil {
return api.ErrPacket
}
Expand All @@ -161,7 +161,7 @@ func (c *coordinator) HandleSaveGame(rq api.SaveGameRequest[com.Uid], w *Worker)
}

func (c *coordinator) HandleLoadGame(rq api.LoadGameRequest[com.Uid], w *Worker) api.Out {
room := w.router.GetRoom(rq.Rid)
room := w.router.FindRoom(rq.Rid)
if room == nil {
return api.ErrPacket
}
Expand All @@ -174,7 +174,7 @@ func (c *coordinator) HandleLoadGame(rq api.LoadGameRequest[com.Uid], w *Worker)

func (c *coordinator) HandleChangePlayer(rq api.ChangePlayerRequest[com.Uid], w *Worker) api.Out {
user := w.router.User(rq.Id)
if user == nil || w.router.GetRoom(rq.Rid) == nil {
if user == nil || w.router.FindRoom(rq.Rid) == nil {
return api.Out{Payload: -1} // semi-predicates
}
user.SetPlayerIndex(rq.Index)
Expand All @@ -183,7 +183,7 @@ func (c *coordinator) HandleChangePlayer(rq api.ChangePlayerRequest[com.Uid], w
}

func (c *coordinator) HandleToggleMultitap(rq api.ToggleMultitapRequest[com.Uid], w *Worker) api.Out {
room := w.router.GetRoom(rq.Rid)
room := w.router.FindRoom(rq.Rid)
if room == nil {
return api.ErrPacket
}
Expand All @@ -195,7 +195,7 @@ func (c *coordinator) HandleRecordGame(rq api.RecordGameRequest[com.Uid], w *Wor
if !w.conf.Recording.Enabled {
return api.ErrPacket
}
room := w.router.GetRoom(rq.Rid)
room := w.router.FindRoom(rq.Rid)
if room == nil {
return api.ErrPacket
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/worker/recording.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ type RecordingRoom struct {
func WithRecording(room GamingRoom, rec bool, recUser string, game string, conf config.WorkerConfig) *RecordingRoom {
rr := &RecordingRoom{GamingRoom: room, rec: recorder.NewRecording(
recorder.Meta{UserName: recUser},
room.GetLog(),
room.Log(),
recorder.Options{
Dir: conf.Recording.Folder,
Fps: float64(room.GetEmulator().GetFps()),
Frequency: int(room.GetEmulator().GetSampleRate()),
Fps: float64(room.Emulator().GetFps()),
Frequency: int(room.Emulator().GetSampleRate()),
Game: game,
ImageCompressionLevel: conf.Recording.CompressLevel,
Name: conf.Recording.Name,
Expand All @@ -32,8 +32,8 @@ func WithRecording(room GamingRoom, rec bool, recUser string, game string, conf
}

func (r *RecordingRoom) captureAudio() {
handler := r.GetEmulator().GetAudio()
r.GetEmulator().SetAudio(func(samples *emulator.GameAudio) {
handler := r.Emulator().GetAudio()
r.Emulator().SetAudio(func(samples *emulator.GameAudio) {
if r.IsRecording() {
r.rec.WriteAudio(recorder.Audio{Samples: samples.Data, Duration: samples.Duration})
}
Expand All @@ -42,8 +42,8 @@ func (r *RecordingRoom) captureAudio() {
}

func (r *RecordingRoom) captureVideo() {
handler := r.GetEmulator().GetVideo()
r.GetEmulator().SetVideo(func(frame *emulator.GameFrame) {
handler := r.Emulator().GetVideo()
r.Emulator().SetVideo(func(frame *emulator.GameFrame) {
if r.IsRecording() {
r.rec.WriteVideo(recorder.Video{Image: frame.Data, Duration: frame.Duration})
}
Expand All @@ -55,7 +55,7 @@ func (r *RecordingRoom) ToggleRecording(active bool, user string) {
if r.rec == nil {
return
}
r.GetLog().Debug().Msgf("[REC] set: %v, user: %v", active, user)
r.Log().Debug().Msgf("[REC] set: %v, user: %v", active, user)
r.rec.Set(active, user)
}

Expand Down
28 changes: 14 additions & 14 deletions pkg/worker/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import (
type GamingRoom interface {
AddUser(*Session)
Close()
Emulator() emulator.Emulator
EnableAutosave(periodS int)
Expel(*Session)
GetEmulator() emulator.Emulator
GetId() string
GetLog() *logger.Logger
HasSave() bool
HasUser(*Session) bool
Id() string
LoadGame() error
Log() *logger.Logger
PollUserInput(*Session)
SaveGame() error
StartEmulator()
Expand Down Expand Up @@ -70,22 +70,22 @@ func NewRoom(id string, game games.GameMetadata, onClose func(*Room), conf confi
room.initVideo(w, h, conf.Encoder.Video)
room.initAudio(int(room.emulator.GetSampleRate()), conf.Encoder.Audio)

log.Info().Str("room", room.GetId()).
log.Info().Str("room", room.Id()).
Str("game", game.Name).
Msg("New room")
return room
}

func (r *Room) GetEmulator() emulator.Emulator { return r.emulator }
func (r *Room) GetId() string { return r.id }
func (r *Room) GetLog() *logger.Logger { return r.log }
func (r *Room) HasSave() bool { return os.Exists(r.emulator.GetHashPath()) }
func (r *Room) HasUser(u *Session) bool { return r != nil && r.users.Has(u.id) }
func (r *Room) IsEmpty() bool { return r.users.Len() == 0 }
func (r *Room) LoadGame() error { return r.emulator.LoadGameState() }
func (r *Room) SaveGame() error { return r.emulator.SaveGameState() }
func (r *Room) StartEmulator() { go r.emulator.Start() }
func (r *Room) ToggleMultitap() { r.emulator.ToggleMultitap() }
func (r *Room) Emulator() emulator.Emulator { return r.emulator }
func (r *Room) HasSave() bool { return os.Exists(r.emulator.GetHashPath()) }
func (r *Room) HasUser(u *Session) bool { return r != nil && r.users.Has(u.id) }
func (r *Room) Id() string { return r.id }
func (r *Room) IsEmpty() bool { return r.users.Len() == 0 }
func (r *Room) LoadGame() error { return r.emulator.LoadGameState() }
func (r *Room) Log() *logger.Logger { return r.log }
func (r *Room) SaveGame() error { return r.emulator.SaveGameState() }
func (r *Room) StartEmulator() { go r.emulator.Start() }
func (r *Room) ToggleMultitap() { r.emulator.ToggleMultitap() }

func (r *Room) EnableAutosave(periodSec int) {
r.log.Info().Msgf("Autosave every [%vs]", periodSec)
Expand Down
12 changes: 6 additions & 6 deletions pkg/worker/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,29 @@ type Router struct {

// Session represents WebRTC connection of the user.
type Session struct {
id com.Uid
conn *webrtc.Peer
id com.Uid
pi int // player index
room GamingRoom // back reference
}

func NewRouter() Router { return Router{users: com.NewNetMap[*Session]()} }

func (r *Router) SetRoom(room GamingRoom) { r.room = room }
func (r *Router) AddUser(user *Session) { r.users.Add(user) }
func (r *Router) AddUser(user *Session) { r.users.Add(user) }
func (r *Router) Close() {
if r.room != nil {
r.room.Close()
}
}
func (r *Router) GetRoom(id string) GamingRoom {
if r.room != nil && r.room.GetId() == id {
func (r *Router) FindRoom(id string) GamingRoom {
if r.room != nil && r.room.Id() == id {
return r.room
}
return nil
}
func (r *Router) User(uid com.Uid) *Session { sess, _ := r.users.Find(uid); return sess }
func (r *Router) Remove(user *Session) { r.users.Remove(user) }
func (r *Router) SetRoom(room GamingRoom) { r.room = room }
func (r *Router) User(uid com.Uid) *Session { sess, _ := r.users.Find(uid); return sess }

func NewSession(rtc *webrtc.Peer, id com.Uid) *Session { return &Session{id: id, conn: rtc} }

Expand Down

0 comments on commit 091f19a

Please sign in to comment.