feat: user last logged in/created

This commit is contained in:
Settel 2022-04-12 14:16:03 +02:00
parent 4fc4730e34
commit 6305c7beda
12 changed files with 141 additions and 49 deletions

View File

@ -22,7 +22,10 @@ export default {
],
components: true,
modules: ['@nuxtjs/axios'],
plugins: [{ src: '~/plugins/engine', mode: 'client' }],
plugins: [
{ src: '~/plugins/engine', mode: 'client' },
{ src: '~/plugins/formatter', mode: 'client' },
],
axios: { proxy: true },
publicRuntimeConfig: {
serverBaseUrl: '/',

View File

@ -6,6 +6,10 @@
<td>{{ gameinfo.name }}</td>
<td><div class="admin-tile-gameinfo__edit-game-name" @click="editName()"></div></td>
</tr>
<tr>
<td>Created:</td>
<td colspan="2">{{ $formatter.date(gameinfo.created) }}</td>
</tr>
<tr>
<td># Players:</td>
<td colspan="2">{{ players.length }}</td>

View File

@ -22,10 +22,10 @@
v-for="player in players"
:key="player.id"
>
<td>{{ player.name }}</td>
<td>{{ player.name }}{{ player.role === 'gamemaster' ? ' 👑' : '' }}</td>
<td>{{ player.numQuotes }}</td>
<td>{{ player.score }}</td>
<td>{{ player.isPlaying ? (player.isIdle ? 'idle' : 'active') : '-'}}</td>
<td>{{ getPlayerStatus(player) }}</td>
</tr>
<tr>
<td colspan="4">
@ -55,7 +55,19 @@ export default {
this.$emit('reload')
this.isReloading = true
setTimeout(() => { this.isReloading = false }, 500)
},
getPlayerStatus(player) {
if (player.isPlaying && !player.isIdle) {
return 'active'
} else {
if (player.lastLoggedIn) {
return this.$formatter.datetime(player.lastLoggedIn)
} else if (player.isPlaying) {
return 'idle'
}
}
return '-'
},
},
}
</script>

View File

@ -0,0 +1,27 @@
export default (context, inject) => {
const leftPad2Digits = (val) => val < 10 ? '0' + val : '' + val
const date = (time) => {
if (!time) return
const d = new Date(time * 1000)
return `${1900 + d.getYear()}-` +
`${leftPad2Digits(d.getMonth() + 1)}-` +
`${leftPad2Digits(d.getDate())}`
}
const datetime = (time) => {
if (!time) return
const d = new Date(time * 1000)
return `${date(time)}, ` +
`${leftPad2Digits(d.getHours())}:` +
`${leftPad2Digits(d.getMinutes())}:` +
`${leftPad2Digits(d.getSeconds())}`
}
inject('formatter', {
date,
datetime,
})
}

View File

@ -31,6 +31,8 @@ func (app *Application) GetGameInfo(usr *user.User, w http.ResponseWriter, r *ht
return
} else {
gameInfo.Players[i].AuthCode = playerUser.GetAuthCode()
gameInfo.Players[i].Created = playerUser.GetCreated()
gameInfo.Players[i].LastLoggedIn = playerUser.GetLastLoggedIn()
}
}

View File

@ -21,6 +21,9 @@ func (app *Application) SyncHandler(usr *user.User, w http.ResponseWriter, r *ht
return
}
usr.UpdateHeartbeat()
usr.SaveUser()
app.updatePlayerIsConnected(usr)
eng := gm.GetEngine()
eng.SyncHandler(w, r)

View File

@ -6,6 +6,7 @@ import (
"os"
"sirlab.de/go/knowyt/engine"
"sirlab.de/go/knowyt/quote"
"time"
)
func NewGameFromFile(id, fileName string) (*Game, error) {
@ -22,6 +23,7 @@ func NewGameFromFile(id, fileName string) (*Game, error) {
id: id,
filename: fileName,
name: gmJson.Name,
created: gmJson.Created,
eng: engine.NewEngine(),
players: make(map[string]playerInfo, 0),
state: STATE_IDLE,
@ -40,6 +42,7 @@ func NewGame(id, fileName, name string) (*Game, error) {
eng: engine.NewEngine(),
players: make(map[string]playerInfo, 0),
state: STATE_IDLE,
created: time.Now().Unix(),
quotes: make(map[string]*quote.Quote, 0),
}
@ -52,6 +55,7 @@ func (gm *Game) SaveGame() error {
gmJson := GameJson{
Name: gm.name,
Created: gm.created,
}
if jsonBytes, err := json.Marshal(gmJson); err != nil {
return err

View File

@ -18,6 +18,7 @@ func (gm *Game) initGameInfoJson() *GameInfoJson {
gameInfo := GameInfoJson{
Name: gm.name,
Created: gm.created,
State: gm.state,
Players: make([]PlayerInfoJson, 0),
NumQuotesLeft: numQuotesLeft,
@ -28,6 +29,8 @@ func (gm *Game) initGameInfoJson() *GameInfoJson {
gameInfo.Players = append(gameInfo.Players, PlayerInfoJson{
Id: player.id,
Name: player.name,
Created: player.created,
LastLoggedIn: player.lastLoggedIn,
Score: player.score,
IsPlaying: player.isPlaying,
IsIdle: player.isIdle,

View File

@ -64,6 +64,8 @@ func (gm *Game) AddPlayer(usr *user.User) {
gm.players[usrId] = playerInfo{
id: usrId,
name: usr.GetName(),
created: usr.GetCreated(),
lastLoggedIn: usr.GetLastLoggedIn(),
isPlaying: false,
isIdle: true,
score: 0,
@ -83,6 +85,8 @@ func (gm *Game) UpdatePlayer(usr *user.User) {
}
player.name = usr.GetName()
player.created = usr.GetCreated()
player.lastLoggedIn = usr.GetLastLoggedIn()
gm.players[usrId] = player
}

View File

@ -32,6 +32,8 @@ const (
type playerInfo struct {
id string
name string
created int64
lastLoggedIn int64
isPlaying bool
isIdle bool
score int
@ -60,6 +62,7 @@ type Game struct {
id string
filename string
name string
created int64
players map[string]playerInfo
eng *engine.Engine
state string
@ -70,6 +73,7 @@ type Game struct {
type GameJson struct {
Name string `json:"name"`
Created int64 `json:"created"`
}
type Quote struct {
@ -84,6 +88,8 @@ type QuotesInfo struct {
type PlayerInfoJson struct {
Id string `json:"id"`
Name string `json:"name"`
Created int64 `json:"created"`
LastLoggedIn int64 `json:"lastLoggedIn"`
Score int `json:"score"`
IsPlaying bool `json:"isPlaying"`
IsIdle bool `json:"isIdle"`
@ -94,6 +100,7 @@ type PlayerInfoJson struct {
type GameInfoJson struct {
Name string `json:"name"`
Created int64 `json:"created"`
State string `json:"state"`
Players []PlayerInfoJson `json:"players"`
NumQuotesLeft int `json:"numQuotesLeft"`

View File

@ -18,6 +18,8 @@ type User struct {
name string
role string
gameId string
created int64
lastLoggedIn int64
cameo *User
}
@ -26,6 +28,8 @@ type UserJson struct {
Name string `json:"name"`
Role string `json:"role"`
GameId string `json:"game"`
Created int64 `json:"created"`
LastLoggedIn int64 `json:"lastLoggedIn"`
}
type UserinfoJson struct {

View File

@ -6,6 +6,7 @@ import (
"os"
"path"
"strings"
"time"
)
func NewUserFromFile(fileName string) (*User, error) {
@ -28,6 +29,8 @@ func NewUserFromFile(fileName string) (*User, error) {
role: userJson.Role,
authcode: userJson.Authcode,
gameId: userJson.GameId,
lastLoggedIn: userJson.LastLoggedIn,
created: userJson.Created,
}, nil
}
}
@ -39,6 +42,8 @@ func CreateUser(fileName, gameId string) *User {
id: id,
filename: fileName,
gameId: gameId,
lastLoggedIn: 0,
created: time.Now().Unix(),
}
}
@ -51,6 +56,8 @@ func (usr *User) SaveUser() error {
Authcode: usr.authcode,
Role: usr.role,
GameId: usr.gameId,
Created: usr.created,
LastLoggedIn: usr.lastLoggedIn,
}
if jsonBytes, err := json.Marshal(userJson); err != nil {
return err
@ -153,3 +160,15 @@ func (usr *User) GetCameo() *User {
return usr.cameo
}
func (usr *User) GetCreated() int64 {
return usr.created
}
func (usr *User) GetLastLoggedIn() int64 {
return usr.lastLoggedIn
}
func (usr *User) UpdateHeartbeat() {
usr.lastLoggedIn = time.Now().Unix()
}