feat: remove game (WIP)

This commit is contained in:
Settel 2022-12-15 10:40:28 +01:00
parent decb7ad693
commit e152d33da0
5 changed files with 46 additions and 10 deletions

View File

@ -130,6 +130,7 @@ const disableGame = async (): Promise<void> => {
}
const removeGame = async (): Promise<void> => {
if (confirm('remove game and wipe all of it\'s data?')) {
console.log(1)
await useEngine().removeGame()
}
}

View File

@ -44,6 +44,7 @@ export async function disableGame(this: EngineContext): Promise<void> {
}
export async function removeGame(this: EngineContext): Promise<void> {
console.log(2)
const userInfoStore = useUserinfoStore()
await this.callApi('/api/removeGame', {
g: userInfoStore.gameId,

View File

@ -3,7 +3,10 @@ package application
import (
"fmt"
"net/http"
"os"
"path"
"sirlab.de/go/knowyt/game"
"sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/user"
)
@ -23,15 +26,43 @@ func (app *Application) RemoveGame(usr *user.User, w http.ResponseWriter, r *htt
return
}
log.Info("remove game %s\n", gm.GetId())
delete(app.games, gameRef)
// if true {
// err := fmt.Errorf("not yet implmemented (%s)", gm.GetId())
// log.ErrorLog(err)
// w.WriteHeader(http.StatusInternalServerError)
// fmt.Fprintf(w, "couldn't save game state")
// }
if err := app.removeGame(gm); err != nil {
log.ErrorLog(err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "failed to remove game\n")
return
}
fmt.Fprintf(w, "ok")
}
func (app *Application) removeGame(gm *game.Game) error {
log.Info("remove game %s\n", gm.GetId())
gameInfo := gm.GetGameInfo()
for _, player := range gameInfo.Players {
log.Info("remove player %s (%s)\n", player.Id, player.Name)
if err := app.removePlayerById(player.Id, gm); err != nil {
return err
}
}
gameBaseDir := path.Join(app.config.DataDir, "games", gm.GetId())
stateFilename := path.Join(gameBaseDir, "state.json")
if err := os.Remove(stateFilename); err != nil {
return err
}
if err := gm.RemoveGame(); err != nil {
return err
}
app.mu.Lock()
delete(app.games, gm.GetId())
app.mu.Unlock()
log.Info("game %s done\n", gm.GetId())
return nil
}

View File

@ -25,5 +25,4 @@ func SaveFile(filename string, data []byte) error {
}
return nil
}

View File

@ -76,6 +76,10 @@ func (gm *Game) SaveGame() error {
return nil
}
func (gm *Game) RemoveGame() error {
return os.Remove(gm.filename)
}
func (gm *Game) StartEngine() {
gm.eng.Run(gm.populateSyncDataCb)
}