feat: create game (WIP)

This commit is contained in:
Settel 2022-04-09 21:27:54 +02:00
parent db7bfaac8b
commit 30904da68d
8 changed files with 135 additions and 48 deletions

View File

@ -22,7 +22,13 @@
</tr>
</table>
</div>
<Button class="create-game__modal-cta" @click="createGame">create game</Button>
<Button
class="create-game__modal-cta"
:disabled="name.length == 0 || teamname.length == 0"
@click="createGame"
>
create game
</Button>
</div>
</template>
</div>
@ -44,9 +50,10 @@ export default {
closeModal() {
this.showModal = false
},
createGame() {
async createGame() {
this.showModal = false
this.$router.push('/gamemaster')
const user = await this.$engine.createGame(this.name, this.teamname)
console.log(user)
},
},
}

View File

@ -0,0 +1,6 @@
export default async function(name, teamname) {
return await this.callApi('/api/createGame', {
name,
teamname,
})
}

View File

@ -19,6 +19,7 @@ import getMyQuotes from './getMyQuotes'
import saveQuote from './saveQuote'
import createQuote from './createQuote'
import removeQuote from './removeQuote'
import createGame from './createGame'
export default (context, inject) => {
const engine = {
@ -48,6 +49,7 @@ export default (context, inject) => {
finishGame,
parseSyncData,
saveSelection,
createGame,
}
inject('engine', engine)

View File

@ -0,0 +1,59 @@
package application
import (
"encoding/json"
"fmt"
"math/rand"
// "github.com/google/uuid"
"net/http"
"strconv"
"time"
// "path"
"sirlab.de/go/knowyt/game"
)
type PlayerInfo struct {
Status string `json:"status"`
Authcode string `json:"authcode"`
}
func (app *Application) CreateGame(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
teamname := r.URL.Query().Get("teamname")
authcode := ""
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < 6; i++ {
authcode += strconv.Itoa(rnd.Intn(1000) / 10 % 10)
}
gm, err := app.createGame(teamname)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Printf("%v\n", err)
fmt.Fprintf(w, "server error")
return
}
_, err = app.createUser(gm, name, authcode)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Printf("%v\n", err)
fmt.Fprintf(w, "server error")
return
}
playerInfo := PlayerInfo{
Status: "ok",
Authcode: authcode,
}
fmt.Printf("Name %s, Teamname %s, Authcode %s\n", name, teamname, authcode)
w.Header().Add("Content-Type", "application/json")
jsonString, _ := json.Marshal(playerInfo)
fmt.Fprintf(w, string(jsonString))
}
func (app *Application) createGame(gamename string) (*game.Game, error) {
return nil, fmt.Errorf("createGame() not yet implemented")
}

View File

@ -0,0 +1,31 @@
package application
import (
"fmt"
"github.com/google/uuid"
"path"
"sirlab.de/go/knowyt/game"
"sirlab.de/go/knowyt/user"
)
func (app *Application) createUser(gm *game.Game, name, authcode string) (string, error) {
if authcode != "" {
if _, err := app.GetUserByAuthcode(authcode); err == nil {
return "", fmt.Errorf("%s authcode already in use", gm.GetId())
}
}
dirName := path.Join(app.config.DataDir, "users")
userNewUuid := uuid.NewString()
fileName := path.Join(dirName, userNewUuid+".json")
newUser := user.CreateUser(fileName, gm.GetId())
newUser.SetRole(user.ROLE_PLAYER)
newUser.SetName(name)
newUser.SetAuthcode(authcode)
if err := newUser.SaveUser(); err != nil {
return "", err
}
app.users[newUser.GetId()] = newUser
gm.AddPlayer(newUser)
return newUser.GetId(), nil
}

View File

@ -0,0 +1,26 @@
package application
import (
"fmt"
"sirlab.de/go/knowyt/game"
)
func (app *Application) modifyUser(id string, gm *game.Game, name, authcode string) error {
if authcode != "" {
if usr, err := app.GetUserByAuthcode(authcode); err == nil && usr.GetId() != id {
return fmt.Errorf("%s authcode already in use", gm.GetId())
}
}
modifyUser := app.users[id]
if modifyUser.GetId() != id || modifyUser.GetGameId() != gm.GetId() {
return fmt.Errorf("%s couldn't find player %s in game", gm.GetId(), id)
}
modifyUser.SetName(name)
modifyUser.SetAuthcode(authcode)
if err := modifyUser.SaveUser(); err != nil {
return err
}
gm.UpdatePlayer(modifyUser)
return nil
}

View File

@ -2,10 +2,7 @@ package application
import (
"fmt"
"github.com/google/uuid"
"net/http"
"path"
"sirlab.de/go/knowyt/game"
"sirlab.de/go/knowyt/user"
"strconv"
@ -78,45 +75,3 @@ func (app *Application) updateScore(gm *game.Game, id string, scoreString string
}
return nil
}
func (app *Application) modifyUser(id string, gm *game.Game, name, authcode string) error {
if authcode != "" {
if usr, err := app.GetUserByAuthcode(authcode); err == nil && usr.GetId() != id {
return fmt.Errorf("%s authcode already in use", gm.GetId())
}
}
modifyUser := app.users[id]
if modifyUser.GetId() != id || modifyUser.GetGameId() != gm.GetId() {
return fmt.Errorf("%s couldn't find player %s in game", gm.GetId(), id)
}
modifyUser.SetName(name)
modifyUser.SetAuthcode(authcode)
if err := modifyUser.SaveUser(); err != nil {
return err
}
gm.UpdatePlayer(modifyUser)
return nil
}
func (app *Application) createUser(gm *game.Game, name, authcode string) (string, error) {
if authcode != "" {
if _, err := app.GetUserByAuthcode(authcode); err == nil {
return "", fmt.Errorf("%s authcode already in use", gm.GetId())
}
}
dirName := path.Join(app.config.DataDir, "users")
userNewUuid := uuid.NewString()
fileName := path.Join(dirName, userNewUuid+".json")
newUser := user.CreateUser(fileName, gm.GetId())
newUser.SetRole(user.ROLE_PLAYER)
newUser.SetName(name)
newUser.SetAuthcode(authcode)
if err := newUser.SaveUser(); err != nil {
return "", err
}
app.users[newUser.GetId()] = newUser
gm.AddPlayer(newUser)
return newUser.GetId(), nil
}

View File

@ -20,6 +20,7 @@ func main() {
mux.PublicHandleFunc("/__intern__/exit", handler.Exit)
mux.PublicHandleFunc("/api/login", mux.Login)
mux.PublicHandleFunc("/api/logout", mux.Logout)
mux.PublicHandleFunc("/api/createGame", app.CreateGame)
mux.PrivateHandleFunc("/api/cameo", mux.Cameo)
mux.PrivateHandleFunc("/api/userinfo", mux.GetUserInfo)
mux.PrivateHandleFunc("/api/gameinfo", app.GetGameInfo)