Skip to content

Commit

Permalink
Add Reset with 0 key
Browse files Browse the repository at this point in the history
  • Loading branch information
sergystepanov committed Dec 2, 2024
1 parent 7134782 commit 954bb23
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const (
RecordGame PT = 110
GetWorkerList PT = 111
ErrNoFreeSlots PT = 112
ResetGame PT = 113
RegisterRoom PT = 201
CloseRoom PT = 202
IceCandidate = WebrtcIce
Expand Down Expand Up @@ -120,6 +121,8 @@ func (p PT) String() string {
return "GetWorkerList"
case ErrNoFreeSlots:
return "NoFreeSlots"
case ResetGame:
return "ResetGame"
case RegisterRoom:
return "RegisterRoom"
case CloseRoom:
Expand Down
6 changes: 5 additions & 1 deletion pkg/api/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ type (
LoadGameRequest[T Id] struct {
StatefulRoom[T]
}
LoadGameResponse string
LoadGameResponse string
ResetGameRequest[T Id] struct {
StatefulRoom[T]
}
ResetGameResponse string
SaveGameRequest[T Id] struct {
StatefulRoom[T]
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/coordinator/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ func (u *User) HandleRequests(info HasServerInfo, conf config.CoordinatorConfig)
return api.ErrMalformed
}
u.HandleChangePlayer(*rq)
case api.ResetGame:
rq := api.Unwrap[api.ResetGameRequest[com.Uid]](payload)
if rq == nil {
return api.ErrMalformed
}
u.HandleResetGame(*rq)
case api.RecordGame:
if !conf.Recording.Enabled {
return api.ErrForbidden
Expand Down
7 changes: 7 additions & 0 deletions pkg/coordinator/userhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ func (u *User) HandleQuitGame(rq api.GameQuitRequest[com.Uid]) {
}
}

func (u *User) HandleResetGame(rq api.ResetGameRequest[com.Uid]) {
if rq.Room.Rid != u.w.RoomId {
return
}
u.w.ResetGame(u.Id())
}

func (u *User) HandleSaveGame() error {
resp, err := u.w.SaveGame(u.Id())
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/coordinator/workerapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (w *Worker) ChangePlayer(id com.Uid, index int) (*api.ChangePlayerResponse,
w.Send(api.ChangePlayer, api.ChangePlayerRequest[com.Uid]{StatefulRoom: StateRoom(id, w.RoomId), Index: index}))
}

func (w *Worker) ResetGame(id com.Uid) {
w.Notify(api.ResetGame, api.ResetGameRequest[com.Uid]{StatefulRoom: StateRoom(id, w.RoomId)})
}

func (w *Worker) RecordGame(id com.Uid, rec bool, recUser string) (*api.RecordGameResponse, error) {
return api.UnwrapChecked[api.RecordGameResponse](
w.Send(api.RecordGame, api.RecordGameRequest[com.Uid]{StatefulRoom: StateRoom(id, w.RoomId), Active: rec, User: recUser}))
Expand Down
2 changes: 2 additions & 0 deletions pkg/worker/caged/libretro/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Emulator interface {
Input(player int, device byte, data []byte)
// Scale returns set video scale factor
Scale() float64
Reset()
}

type Frontend struct {
Expand Down Expand Up @@ -307,6 +308,7 @@ func (f *Frontend) HashPath() string { return f.storage.GetSavePath
func (f *Frontend) IsPortrait() bool { return f.nano.IsPortrait() }
func (f *Frontend) KbMouseSupport() bool { return f.nano.KbMouseSupport() }
func (f *Frontend) PixFormat() uint32 { return f.nano.Video.PixFmt.C }
func (f *Frontend) Reset() { f.mu.Lock(); defer f.mu.Unlock(); f.nano.Reset() }
func (f *Frontend) RestoreGameState() error { return f.Load() }
func (f *Frontend) Rotation() uint { return f.nano.Rot }
func (f *Frontend) SRAMPath() string { return f.storage.GetSRAMPath() }
Expand Down
4 changes: 4 additions & 0 deletions pkg/worker/caged/libretro/nanoarch/nanoarch.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ void bridge_retro_unload_game(void *f) {
((void (*)(void)) f)();
}

void bridge_retro_reset(void *f) {
((void (*)(void)) f)();
}

void bridge_retro_run(void *f) {
((void (*)(void)) f)();
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/worker/caged/libretro/nanoarch/nanoarch.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ func (n *Nanoarch) CoreLoad(meta Metadata) {
retroSetInputState = loadFunction(coreLib, "retro_set_input_state")
retroSetAudioSample = loadFunction(coreLib, "retro_set_audio_sample")
retroSetAudioSampleBatch = loadFunction(coreLib, "retro_set_audio_sample_batch")
retroReset = loadFunction(coreLib, "retro_reset")
retroRun = loadFunction(coreLib, "retro_run")
retroLoadGame = loadFunction(coreLib, "retro_load_game")
retroUnloadGame = loadFunction(coreLib, "retro_unload_game")
Expand Down Expand Up @@ -396,6 +397,10 @@ func (n *Nanoarch) Shutdown() {
C.free(unsafe.Pointer(n.cSystemDirectory))
}

func (n *Nanoarch) Reset() {
C.bridge_retro_reset(retroReset)
}

func (n *Nanoarch) Run() {
if n.LibCo {
C.same_thread(retroRun)
Expand Down Expand Up @@ -595,6 +600,7 @@ var (
coreLib unsafe.Pointer
retroInit unsafe.Pointer
retroLoadGame unsafe.Pointer
retroReset unsafe.Pointer
retroRun unsafe.Pointer
retroSetAudioSample unsafe.Pointer
retroSetAudioSampleBatch unsafe.Pointer
Expand Down
1 change: 1 addition & 0 deletions pkg/worker/caged/libretro/nanoarch/nanoarch.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void bridge_retro_deinit(void *f);
void bridge_retro_get_system_av_info(void *f, struct retro_system_av_info *si);
void bridge_retro_get_system_info(void *f, struct retro_system_info *si);
void bridge_retro_init(void *f);
void bridge_retro_reset(void *f);
void bridge_retro_run(void *f);
void bridge_retro_set_audio_sample(void *f, void *callback);
void bridge_retro_set_audio_sample_batch(void *f, void *callback);
Expand Down
6 changes: 6 additions & 0 deletions pkg/worker/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ func (c *coordinator) HandleRequests(w *Worker) chan struct{} {
} else {
out = c.HandleChangePlayer(*dat, w)
}
case api.ResetGame:
dat := api.Unwrap[api.ResetGameRequest[com.Uid]](x.Payload)
if dat == nil {
return api.ErrMalformed
}
c.HandleResetGame(*dat, w)
case api.RecordGame:
if dat := api.Unwrap[api.RecordGameRequest[com.Uid]](x.Payload); dat == nil {
err, out = api.ErrMalformed, api.EmptyPacket
Expand Down
8 changes: 8 additions & 0 deletions pkg/worker/coordinatorhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ func (c *coordinator) HandleQuitGame(rq api.GameQuitRequest[com.Uid], w *Worker)
}
}

func (c *coordinator) HandleResetGame(rq api.ResetGameRequest[com.Uid], w *Worker) api.Out {
if r := w.router.FindRoom(rq.Rid); r != nil {
room.WithEmulator(r.App()).Reset()
return api.OkPacket
}
return api.ErrPacket
}

func (c *coordinator) HandleSaveGame(rq api.SaveGameRequest[com.Uid], w *Worker) api.Out {
r := w.router.FindRoom(rq.Rid)
if r == nil {
Expand Down
2 changes: 2 additions & 0 deletions web/js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const endpoints = {
GAME_RECORDING: 110,
GET_WORKER_LIST: 111,
GAME_ERROR_NO_FREE_SLOTS: 112,
GAME_RESET: 113,

APP_VIDEO_CHANGE: 150,
}
Expand Down Expand Up @@ -319,6 +320,7 @@ export const api = {
}
},
load: () => packet(endpoints.GAME_LOAD),
reset: (roomId) => packet(endpoints.GAME_RESET, {room_id: roomId}),
save: () => packet(endpoints.GAME_SAVE),
setPlayerIndex: (i) => packet(endpoints.GAME_SET_PLAYER_INDEX, i),
start: (game, roomId, record, recordUser, player) => packet(endpoints.GAME_START, {
Expand Down
3 changes: 3 additions & 0 deletions web/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,9 @@ const app = {
room.reset();
window.location = window.location.pathname;
break;
case KEY.RESET:
api.game.reset(room.id)
break;
case KEY.STATS:
stats.toggle();
break;
Expand Down
3 changes: 2 additions & 1 deletion web/js/input/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ const defaultMap = Object.freeze({
KeyH: KEY.HELP,
Backslash: KEY.STATS,
Digit9: KEY.SETTINGS,
KeyT: KEY.DTOGGLE
KeyT: KEY.DTOGGLE,
Digit0: KEY.RESET,
});

let keyMap = {};
Expand Down
1 change: 1 addition & 0 deletions web/js/input/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ export const KEY = {
L3: 'l3',
R3: 'r3',
REC: 'rec',
RESET: 'reset',
}

0 comments on commit 954bb23

Please sign in to comment.