feat: create game

This commit is contained in:
Settel 2022-04-09 22:25:52 +02:00
parent 30904da68d
commit 3f7a27b2de
5 changed files with 50 additions and 10 deletions

View File

@ -8,7 +8,7 @@
logout/switch session logout/switch session
</div> </div>
</template> </template>
<form v-else> <template v-else>
<input <input
v-model="authCode" v-model="authCode"
class="playbutton__authinput" class="playbutton__authinput"
@ -21,7 +21,7 @@
<div class="playbutton__errormessage" v-if="errorMessage"> <div class="playbutton__errormessage" v-if="errorMessage">
{{ errorMessage }} {{ errorMessage }}
</div> </div>
</form> </template>
</div> </div>
</div> </div>
</template> </template>

View File

@ -3,13 +3,15 @@ package application
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/google/uuid"
"math/rand" "math/rand"
// "github.com/google/uuid"
"net/http" "net/http"
"os"
"path"
"sirlab.de/go/knowyt/game"
"sirlab.de/go/knowyt/user"
"strconv" "strconv"
"time" "time"
// "path"
"sirlab.de/go/knowyt/game"
) )
type PlayerInfo struct { type PlayerInfo struct {
@ -35,7 +37,7 @@ func (app *Application) CreateGame(w http.ResponseWriter, r *http.Request) {
return return
} }
_, err = app.createUser(gm, name, authcode) _, err = app.createUser(gm, name, user.ROLE_GAMEMASTER, authcode)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
fmt.Printf("%v\n", err) fmt.Printf("%v\n", err)
@ -55,5 +57,28 @@ func (app *Application) CreateGame(w http.ResponseWriter, r *http.Request) {
} }
func (app *Application) createGame(gamename string) (*game.Game, error) { func (app *Application) createGame(gamename string) (*game.Game, error) {
return nil, fmt.Errorf("createGame() not yet implemented") gameId := uuid.NewString()
gameDirName := path.Join(app.config.DataDir, "games", gameId)
gameFileName := path.Join(gameDirName, "game.json")
quotesDirName := path.Join(gameDirName, "quotes")
if err := os.Mkdir(gameDirName, 0777); err != nil {
return nil, err
}
if err := os.Mkdir(quotesDirName, 0777); err != nil {
return nil, err
}
fmt.Printf("createGame(\"%s\": \"%s\")\n", gameId, gamename)
gm, err := game.NewGame(gameId, gameFileName, gamename)
if err != nil {
return nil, err
}
if err := gm.SaveGame(); err != nil {
return nil, err
}
app.addGame(gm)
go gm.StartEngine()
return gm, nil
} }

View File

@ -8,7 +8,7 @@ import (
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
) )
func (app *Application) createUser(gm *game.Game, name, authcode string) (string, error) { func (app *Application) createUser(gm *game.Game, name, role, authcode string) (string, error) {
if authcode != "" { if authcode != "" {
if _, err := app.GetUserByAuthcode(authcode); err == nil { if _, err := app.GetUserByAuthcode(authcode); err == nil {
return "", fmt.Errorf("%s authcode already in use", gm.GetId()) return "", fmt.Errorf("%s authcode already in use", gm.GetId())
@ -19,7 +19,7 @@ func (app *Application) createUser(gm *game.Game, name, authcode string) (string
userNewUuid := uuid.NewString() userNewUuid := uuid.NewString()
fileName := path.Join(dirName, userNewUuid+".json") fileName := path.Join(dirName, userNewUuid+".json")
newUser := user.CreateUser(fileName, gm.GetId()) newUser := user.CreateUser(fileName, gm.GetId())
newUser.SetRole(user.ROLE_PLAYER) newUser.SetRole(role)
newUser.SetName(name) newUser.SetName(name)
newUser.SetAuthcode(authcode) newUser.SetAuthcode(authcode)
if err := newUser.SaveUser(); err != nil { if err := newUser.SaveUser(); err != nil {

View File

@ -41,7 +41,8 @@ func (app *Application) SavePlayer(usr *user.User, w http.ResponseWriter, r *htt
return return
} }
} else { } else {
if newPlayerId, err := app.createUser(gm, name, authcode); err != nil { newPlayerId, err := app.createUser(gm, name, user.ROLE_PLAYER, authcode)
if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
fmt.Printf("%v\n", err) fmt.Printf("%v\n", err)
fmt.Fprintf(w, "server error") fmt.Fprintf(w, "server error")

View File

@ -32,6 +32,20 @@ func NewGameFromFile(id, fileName string) (*Game, error) {
} }
} }
func NewGame(id, fileName, name string) (*Game, error) {
gm := Game{
id: id,
filename: fileName,
name: name,
eng: engine.NewEngine(),
players: make(map[string]playerInfo, 0),
state: STATE_IDLE,
quotes: make(map[string]*quote.Quote, 0),
}
return &gm, nil
}
func (gm *Game) SaveGame() error { func (gm *Game) SaveGame() error {
gm.mu.Lock() gm.mu.Lock()
defer gm.mu.Unlock() defer gm.mu.Unlock()