add mutexes

This commit is contained in:
Settel 2021-08-09 10:36:19 +02:00
parent 0585c60063
commit b2d86fcb52
12 changed files with 87 additions and 21 deletions

View File

@ -6,6 +6,9 @@ import (
)
func (app Application) GetGameById(id string) (*game.Game, error) {
app.mu.Lock()
defer app.mu.Unlock()
gm := app.games[id]
if gm == nil {
return nil, fmt.Errorf("unknown id")

View File

@ -6,6 +6,9 @@ import (
)
func (app Application) GetUserById(id string) (*user.User, error) {
app.mu.Lock()
defer app.mu.Unlock()
usr := app.users[id]
if usr == nil {
return nil, fmt.Errorf("unknown id")
@ -14,6 +17,9 @@ func (app Application) GetUserById(id string) (*user.User, error) {
}
func (app Application) GetUserByAuthcode(authcode string) (*user.User, error) {
app.mu.Lock()
defer app.mu.Unlock()
for _, usr := range app.users {
if usr.GetAuthCode() == authcode {
return usr, nil

View File

@ -12,6 +12,10 @@ func (app Application) loadGames() error {
if err != nil {
return err
}
app.mu.Lock()
defer app.mu.Unlock()
for _, file := range files {
id := file.Name()
fileName := path.Join(dirName, id, "game.json")

View File

@ -12,6 +12,10 @@ func (app Application) loadUsers() error {
if err != nil {
return err
}
app.mu.Lock()
defer app.mu.Unlock()
for _, file := range files {
fileName := path.Join(dirName, file.Name())
if usr, err := user.NewUserFromFile(fileName); err != nil {

View File

@ -4,9 +4,11 @@ import (
"sirlab.de/go/knyt/applicationConfig"
"sirlab.de/go/knyt/game"
"sirlab.de/go/knyt/user"
"sync"
)
type Application struct {
mu sync.Mutex
config applicationConfig.ApplicationConfig
users map[string]*user.User
games map[string]*game.Game

View File

@ -25,11 +25,19 @@ func (eng *Engine) Run() {
time.Sleep(1 * time.Second)
}
eng.doSomething()
}
}
func (eng *Engine) doSomething() {
eng.mu.Lock()
defer eng.mu.Unlock()
eng.versionRef++
fmt.Printf("game %s: %d\n", eng.id, eng.versionRef)
data := syncdata.SyncData{
VersionRef: eng.versionRef,
}
eng.obs.Update(data)
}
}

View File

@ -3,11 +3,13 @@ package engine
import (
"github.com/imkira/go-observer"
"net/http"
"sync"
)
type HandleFunc func(http.ResponseWriter, *http.Request)
type Engine struct {
mu sync.Mutex
id string
versionRef int
obs observer.Property

View File

@ -14,7 +14,10 @@ func (eng *Engine) SyncHandler(w http.ResponseWriter, r *http.Request) {
versionRef = -1
}
eng.mu.Lock()
stream := eng.obs.Observe()
eng.mu.Unlock()
var value syncdata.SyncData
for {
value = stream.Value().(syncdata.SyncData)

View File

@ -41,10 +41,16 @@ func (gm *Game) GetEngine() *engine.Engine {
}
func (gm *Game) UpdatePlayerTimestamp(usr *user.User) {
gm.mu.Lock()
defer gm.mu.Unlock()
gm.playerTimestamp[usr.GetId()] = time.Now()
}
func (gm *Game) GetActivePlayers() []string {
gm.mu.Lock()
defer gm.mu.Unlock()
players := make([]string, 0)
now := time.Now()

View File

@ -2,10 +2,12 @@ package game
import (
"sirlab.de/go/knyt/engine"
"sync"
"time"
)
type Game struct {
mu sync.Mutex
id string
name string
eng *engine.Engine

View File

@ -1,5 +1,9 @@
package user
import (
"sync"
)
const (
ROLE_ADMIN = "admin"
ROLE_GAMEMASTER = "gamemaster"
@ -7,6 +11,7 @@ const (
)
type User struct {
mu sync.Mutex
id string
authcode string
name string

View File

@ -31,34 +31,55 @@ func NewUserFromFile(fileName string) (*User, error) {
}
}
func (user *User) GetId() string {
return user.id
func (usr *User) GetId() string {
usr.mu.Lock()
defer usr.mu.Unlock()
return usr.id
}
func (user *User) GetAuthCode() string {
return user.authcode
func (usr *User) GetAuthCode() string {
usr.mu.Lock()
defer usr.mu.Unlock()
return usr.authcode
}
func (user *User) GetName() string {
return user.name
func (usr *User) GetName() string {
usr.mu.Lock()
defer usr.mu.Unlock()
return usr.name
}
func (user *User) GetRole() string {
return user.role
func (usr *User) GetRole() string {
usr.mu.Lock()
defer usr.mu.Unlock()
return usr.role
}
func (user *User) GetGameId() string {
return user.gameId
func (usr *User) GetGameId() string {
usr.mu.Lock()
defer usr.mu.Unlock()
return usr.gameId
}
func (user *User) IsPlayer() bool {
func (usr *User) IsPlayer() bool {
return true
}
func (user *User) IsGamemaster() bool {
return user.role == ROLE_GAMEMASTER || user.role == ROLE_ADMIN
func (usr *User) IsGamemaster() bool {
usr.mu.Lock()
defer usr.mu.Unlock()
return usr.role == ROLE_GAMEMASTER || usr.role == ROLE_ADMIN
}
func (user *User) IsAdmin() bool {
return user.role == ROLE_ADMIN
func (usr *User) IsAdmin() bool {
usr.mu.Lock()
defer usr.mu.Unlock()
return usr.role == ROLE_ADMIN
}