feat: admin interface shows number of quotes per player

This commit is contained in:
Settel 2022-02-27 21:45:28 +01:00
parent f667f4cf00
commit f2aa568b09
6 changed files with 85 additions and 29 deletions

View File

@ -17,7 +17,7 @@ export default {
<style lang="scss"> <style lang="scss">
.admin-tile { .admin-tile {
width: 300px; min-width: 240px;
margin: 16px; margin: 16px;
&__title { &__title {

View File

@ -26,14 +26,27 @@
<td>Name:</td> <td>Name:</td>
<td>{{ gameinfo.name }}</td> <td>{{ gameinfo.name }}</td>
</tr> </tr>
<tr>
<td># Players:</td>
<td>{{ players.length }}</td>
</tr>
</table> </table>
</AdminTile> </AdminTile>
<AdminTile title="Players"> <AdminTile title="Players">
<ul> <table>
<li>Player #1</li> <tr>
<li>Player #2</li> <th>Name</th>
<li>Player #3</li> <th>#&nbsp;Quotes</th>
</ul> <th>Score</th>
<th>Status</th>
</tr>
<tr class="page-admin__player" @click="editPlayer(player.id)" v-for="player in players" :key="player.id">
<td>{{ player.name }}</td>
<td>{{ player.quotes.length }}</td>
<td>{{ player.score }}</td>
<td>{{ player.isPlaying ? (player.isIdle ? 'idle' : 'active') : '-'}}</td>
</tr>
</table>
</AdminTile> </AdminTile>
</div> </div>
</template> </template>
@ -56,18 +69,28 @@ export default {
}, },
gameinfo() { gameinfo() {
return this.$store.state.engine.gameinfo || {} return this.$store.state.engine.gameinfo || {}
} },
players() {
const gameinfo = this.$store.state.engine.gameinfo || {}
const players = [...gameinfo.players || []]
return players.sort((a, b) => { return a.name.localeCompare(b.name) })
},
}, },
methods: { methods: {
login() { login() {
this.$router.push({ path: '/' }) this.$router.push({ path: '/' })
}, },
editPlayer(id) {
alert(id)
},
}, },
} }
</script> </script>
<style lang="scss"> <style lang="scss">
.page-admin { .page-admin {
color: #ffffff;
&__tiles { &__tiles {
display: flex; display: flex;
margin: 24px; margin: 24px;
@ -76,6 +99,12 @@ export default {
&__back-button { &__back-button {
margin: 16px; margin: 16px;
} }
color: #ffffff;
&__player {
cursor: pointer;
&:hover {
color: #a0a0a0;
}
}
} }
</style> </style>

View File

@ -25,14 +25,7 @@ func (app *Application) GetQuotes(usr *user.User, w http.ResponseWriter, r *http
return return
} }
quotesInfo, err := gm.GetQuotesInfoByUser(usr) quotesInfo := gm.GetQuotesInfoByUser(usr)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "forbidden")
return
}
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
jsonString, _ := json.Marshal(quotesInfo) jsonString, _ := json.Marshal(quotesInfo)
fmt.Fprintf(w, string(jsonString)) fmt.Fprintf(w, string(jsonString))

View File

@ -3,11 +3,32 @@ package game
import () import ()
func (gm *Game) GetGameInfo() *GameInfoJson { func (gm *Game) GetGameInfo() *GameInfoJson {
gameInfo := gm.initGameInfoJson()
for i, _ := range gameInfo.Players {
gameInfo.Players[i].Quotes = gm.getQuotesInfoByUserId(gameInfo.Players[i].Id)
}
return gameInfo
}
func (gm *Game) initGameInfoJson() *GameInfoJson {
gm.mu.Lock() gm.mu.Lock()
defer gm.mu.Unlock() defer gm.mu.Unlock()
gameInfo := GameInfoJson{ gameInfo := GameInfoJson{
Name: gm.name, Name: gm.name,
Players: make([]PlayerInfoJson, 0),
} }
for _, player := range gm.players {
gameInfo.Players = append(gameInfo.Players, PlayerInfoJson{
Id: player.id,
Name: player.name,
Score: player.score,
IsPlaying: player.isPlaying,
IsIdle: player.isIdle,
})
}
return &gameInfo return &gameInfo
} }

View File

@ -4,20 +4,15 @@ import (
"sirlab.de/go/knyt/user" "sirlab.de/go/knyt/user"
) )
type Quote struct { func (gm *Game) GetQuotesInfoByUser(usr *user.User) *QuotesInfo {
Id string `json:"id"` usrId := usr.GetId()
Quote string `json:"quote"` return &QuotesInfo{Quotes: gm.getQuotesInfoByUserId(usrId)}
} }
type QuotesInfo struct { func (gm *Game) getQuotesInfoByUserId(usrId string) []Quote {
Quotes []Quote `json:"quotes"`
}
func (gm *Game) GetQuotesInfoByUser(usr *user.User) (*QuotesInfo, error) {
gm.mu.Lock() gm.mu.Lock()
defer gm.mu.Unlock() defer gm.mu.Unlock()
usrId := usr.GetId()
quotes := make([]Quote, 0) quotes := make([]Quote, 0)
for _, quote := range gm.quotes { for _, quote := range gm.quotes {
if quote.GetSourceId() == usrId { if quote.GetSourceId() == usrId {
@ -28,5 +23,5 @@ func (gm *Game) GetQuotesInfoByUser(usr *user.User) (*QuotesInfo, error) {
} }
} }
return &QuotesInfo{Quotes: quotes}, nil return quotes
} }

View File

@ -70,9 +70,27 @@ type GameJson struct {
Name string `json:"name"` Name string `json:"name"`
} }
type Quote struct {
Id string `json:"id"`
Quote string `json:"quote"`
}
type QuotesInfo struct {
Quotes []Quote `json:"quotes"`
}
type PlayerInfoJson struct {
Id string `json:"id"`
Name string `json:"name"`
Score int `json:"score"`
IsPlaying bool `json:"isPlaying"`
IsIdle bool `json:"isIdle"`
Quotes []Quote `json:"quotes"`
}
type GameInfoJson struct { type GameInfoJson struct {
Name string `json:"name"` Name string `json:"name"`
Players map[string]playerInfo `json:"players"` Players []PlayerInfoJson `json:"players"`
} }
type GameStateJson struct { type GameStateJson struct {