feat: create game (WIP)
This commit is contained in:
parent
db7bfaac8b
commit
30904da68d
@ -22,7 +22,13 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</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>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
</div>
|
</div>
|
||||||
@ -44,9 +50,10 @@ export default {
|
|||||||
closeModal() {
|
closeModal() {
|
||||||
this.showModal = false
|
this.showModal = false
|
||||||
},
|
},
|
||||||
createGame() {
|
async createGame() {
|
||||||
this.showModal = false
|
this.showModal = false
|
||||||
this.$router.push('/gamemaster')
|
const user = await this.$engine.createGame(this.name, this.teamname)
|
||||||
|
console.log(user)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
6
client/src/plugins/engine/createGame.js
Normal file
6
client/src/plugins/engine/createGame.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
export default async function(name, teamname) {
|
||||||
|
return await this.callApi('/api/createGame', {
|
||||||
|
name,
|
||||||
|
teamname,
|
||||||
|
})
|
||||||
|
}
|
@ -19,6 +19,7 @@ import getMyQuotes from './getMyQuotes'
|
|||||||
import saveQuote from './saveQuote'
|
import saveQuote from './saveQuote'
|
||||||
import createQuote from './createQuote'
|
import createQuote from './createQuote'
|
||||||
import removeQuote from './removeQuote'
|
import removeQuote from './removeQuote'
|
||||||
|
import createGame from './createGame'
|
||||||
|
|
||||||
export default (context, inject) => {
|
export default (context, inject) => {
|
||||||
const engine = {
|
const engine = {
|
||||||
@ -48,6 +49,7 @@ export default (context, inject) => {
|
|||||||
finishGame,
|
finishGame,
|
||||||
parseSyncData,
|
parseSyncData,
|
||||||
saveSelection,
|
saveSelection,
|
||||||
|
createGame,
|
||||||
}
|
}
|
||||||
|
|
||||||
inject('engine', engine)
|
inject('engine', engine)
|
||||||
|
59
server/src/application/createGame.go
Normal file
59
server/src/application/createGame.go
Normal 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")
|
||||||
|
}
|
31
server/src/application/createUser.go
Normal file
31
server/src/application/createUser.go
Normal 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
|
||||||
|
}
|
26
server/src/application/modifyUser.go
Normal file
26
server/src/application/modifyUser.go
Normal 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
|
||||||
|
}
|
@ -2,10 +2,7 @@ package application
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/google/uuid"
|
|
||||||
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
|
||||||
"sirlab.de/go/knowyt/game"
|
"sirlab.de/go/knowyt/game"
|
||||||
"sirlab.de/go/knowyt/user"
|
"sirlab.de/go/knowyt/user"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -78,45 +75,3 @@ func (app *Application) updateScore(gm *game.Game, id string, scoreString string
|
|||||||
}
|
}
|
||||||
return nil
|
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
|
|
||||||
}
|
|
||||||
|
@ -20,6 +20,7 @@ func main() {
|
|||||||
mux.PublicHandleFunc("/__intern__/exit", handler.Exit)
|
mux.PublicHandleFunc("/__intern__/exit", handler.Exit)
|
||||||
mux.PublicHandleFunc("/api/login", mux.Login)
|
mux.PublicHandleFunc("/api/login", mux.Login)
|
||||||
mux.PublicHandleFunc("/api/logout", mux.Logout)
|
mux.PublicHandleFunc("/api/logout", mux.Logout)
|
||||||
|
mux.PublicHandleFunc("/api/createGame", app.CreateGame)
|
||||||
mux.PrivateHandleFunc("/api/cameo", mux.Cameo)
|
mux.PrivateHandleFunc("/api/cameo", mux.Cameo)
|
||||||
mux.PrivateHandleFunc("/api/userinfo", mux.GetUserInfo)
|
mux.PrivateHandleFunc("/api/userinfo", mux.GetUserInfo)
|
||||||
mux.PrivateHandleFunc("/api/gameinfo", app.GetGameInfo)
|
mux.PrivateHandleFunc("/api/gameinfo", app.GetGameInfo)
|
||||||
|
Loading…
Reference in New Issue
Block a user