add mutexes
This commit is contained in:
parent
0585c60063
commit
b2d86fcb52
@ -6,6 +6,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (app Application) GetGameById(id string) (*game.Game, error) {
|
func (app Application) GetGameById(id string) (*game.Game, error) {
|
||||||
|
app.mu.Lock()
|
||||||
|
defer app.mu.Unlock()
|
||||||
|
|
||||||
gm := app.games[id]
|
gm := app.games[id]
|
||||||
if gm == nil {
|
if gm == nil {
|
||||||
return nil, fmt.Errorf("unknown id")
|
return nil, fmt.Errorf("unknown id")
|
||||||
|
@ -6,6 +6,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (app Application) GetUserById(id string) (*user.User, error) {
|
func (app Application) GetUserById(id string) (*user.User, error) {
|
||||||
|
app.mu.Lock()
|
||||||
|
defer app.mu.Unlock()
|
||||||
|
|
||||||
usr := app.users[id]
|
usr := app.users[id]
|
||||||
if usr == nil {
|
if usr == nil {
|
||||||
return nil, fmt.Errorf("unknown id")
|
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) {
|
func (app Application) GetUserByAuthcode(authcode string) (*user.User, error) {
|
||||||
|
app.mu.Lock()
|
||||||
|
defer app.mu.Unlock()
|
||||||
|
|
||||||
for _, usr := range app.users {
|
for _, usr := range app.users {
|
||||||
if usr.GetAuthCode() == authcode {
|
if usr.GetAuthCode() == authcode {
|
||||||
return usr, nil
|
return usr, nil
|
||||||
|
@ -12,6 +12,10 @@ func (app Application) loadGames() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app.mu.Lock()
|
||||||
|
defer app.mu.Unlock()
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
id := file.Name()
|
id := file.Name()
|
||||||
fileName := path.Join(dirName, id, "game.json")
|
fileName := path.Join(dirName, id, "game.json")
|
||||||
|
@ -12,6 +12,10 @@ func (app Application) loadUsers() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app.mu.Lock()
|
||||||
|
defer app.mu.Unlock()
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
fileName := path.Join(dirName, file.Name())
|
fileName := path.Join(dirName, file.Name())
|
||||||
if usr, err := user.NewUserFromFile(fileName); err != nil {
|
if usr, err := user.NewUserFromFile(fileName); err != nil {
|
||||||
|
@ -4,9 +4,11 @@ import (
|
|||||||
"sirlab.de/go/knyt/applicationConfig"
|
"sirlab.de/go/knyt/applicationConfig"
|
||||||
"sirlab.de/go/knyt/game"
|
"sirlab.de/go/knyt/game"
|
||||||
"sirlab.de/go/knyt/user"
|
"sirlab.de/go/knyt/user"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Application struct {
|
type Application struct {
|
||||||
|
mu sync.Mutex
|
||||||
config applicationConfig.ApplicationConfig
|
config applicationConfig.ApplicationConfig
|
||||||
users map[string]*user.User
|
users map[string]*user.User
|
||||||
games map[string]*game.Game
|
games map[string]*game.Game
|
||||||
|
@ -25,11 +25,19 @@ func (eng *Engine) Run() {
|
|||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
eng.doSomething()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (eng *Engine) doSomething() {
|
||||||
|
eng.mu.Lock()
|
||||||
|
defer eng.mu.Unlock()
|
||||||
|
|
||||||
eng.versionRef++
|
eng.versionRef++
|
||||||
fmt.Printf("game %s: %d\n", eng.id, eng.versionRef)
|
fmt.Printf("game %s: %d\n", eng.id, eng.versionRef)
|
||||||
data := syncdata.SyncData{
|
data := syncdata.SyncData{
|
||||||
VersionRef: eng.versionRef,
|
VersionRef: eng.versionRef,
|
||||||
}
|
}
|
||||||
|
|
||||||
eng.obs.Update(data)
|
eng.obs.Update(data)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,13 @@ package engine
|
|||||||
import (
|
import (
|
||||||
"github.com/imkira/go-observer"
|
"github.com/imkira/go-observer"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HandleFunc func(http.ResponseWriter, *http.Request)
|
type HandleFunc func(http.ResponseWriter, *http.Request)
|
||||||
|
|
||||||
type Engine struct {
|
type Engine struct {
|
||||||
|
mu sync.Mutex
|
||||||
id string
|
id string
|
||||||
versionRef int
|
versionRef int
|
||||||
obs observer.Property
|
obs observer.Property
|
||||||
|
@ -14,7 +14,10 @@ func (eng *Engine) SyncHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
versionRef = -1
|
versionRef = -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
eng.mu.Lock()
|
||||||
stream := eng.obs.Observe()
|
stream := eng.obs.Observe()
|
||||||
|
eng.mu.Unlock()
|
||||||
|
|
||||||
var value syncdata.SyncData
|
var value syncdata.SyncData
|
||||||
for {
|
for {
|
||||||
value = stream.Value().(syncdata.SyncData)
|
value = stream.Value().(syncdata.SyncData)
|
||||||
|
@ -41,10 +41,16 @@ func (gm *Game) GetEngine() *engine.Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (gm *Game) UpdatePlayerTimestamp(usr *user.User) {
|
func (gm *Game) UpdatePlayerTimestamp(usr *user.User) {
|
||||||
|
gm.mu.Lock()
|
||||||
|
defer gm.mu.Unlock()
|
||||||
|
|
||||||
gm.playerTimestamp[usr.GetId()] = time.Now()
|
gm.playerTimestamp[usr.GetId()] = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gm *Game) GetActivePlayers() []string {
|
func (gm *Game) GetActivePlayers() []string {
|
||||||
|
gm.mu.Lock()
|
||||||
|
defer gm.mu.Unlock()
|
||||||
|
|
||||||
players := make([]string, 0)
|
players := make([]string, 0)
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
@ -2,10 +2,12 @@ package game
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"sirlab.de/go/knyt/engine"
|
"sirlab.de/go/knyt/engine"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Game struct {
|
type Game struct {
|
||||||
|
mu sync.Mutex
|
||||||
id string
|
id string
|
||||||
name string
|
name string
|
||||||
eng *engine.Engine
|
eng *engine.Engine
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
package user
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ROLE_ADMIN = "admin"
|
ROLE_ADMIN = "admin"
|
||||||
ROLE_GAMEMASTER = "gamemaster"
|
ROLE_GAMEMASTER = "gamemaster"
|
||||||
@ -7,6 +11,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
|
mu sync.Mutex
|
||||||
id string
|
id string
|
||||||
authcode string
|
authcode string
|
||||||
name string
|
name string
|
||||||
|
@ -31,34 +31,55 @@ func NewUserFromFile(fileName string) (*User, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (user *User) GetId() string {
|
func (usr *User) GetId() string {
|
||||||
return user.id
|
usr.mu.Lock()
|
||||||
|
defer usr.mu.Unlock()
|
||||||
|
|
||||||
|
return usr.id
|
||||||
}
|
}
|
||||||
|
|
||||||
func (user *User) GetAuthCode() string {
|
func (usr *User) GetAuthCode() string {
|
||||||
return user.authcode
|
usr.mu.Lock()
|
||||||
|
defer usr.mu.Unlock()
|
||||||
|
|
||||||
|
return usr.authcode
|
||||||
}
|
}
|
||||||
|
|
||||||
func (user *User) GetName() string {
|
func (usr *User) GetName() string {
|
||||||
return user.name
|
usr.mu.Lock()
|
||||||
|
defer usr.mu.Unlock()
|
||||||
|
|
||||||
|
return usr.name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (user *User) GetRole() string {
|
func (usr *User) GetRole() string {
|
||||||
return user.role
|
usr.mu.Lock()
|
||||||
|
defer usr.mu.Unlock()
|
||||||
|
|
||||||
|
return usr.role
|
||||||
}
|
}
|
||||||
|
|
||||||
func (user *User) GetGameId() string {
|
func (usr *User) GetGameId() string {
|
||||||
return user.gameId
|
usr.mu.Lock()
|
||||||
|
defer usr.mu.Unlock()
|
||||||
|
|
||||||
|
return usr.gameId
|
||||||
}
|
}
|
||||||
|
|
||||||
func (user *User) IsPlayer() bool {
|
func (usr *User) IsPlayer() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (user *User) IsGamemaster() bool {
|
func (usr *User) IsGamemaster() bool {
|
||||||
return user.role == ROLE_GAMEMASTER || user.role == ROLE_ADMIN
|
usr.mu.Lock()
|
||||||
|
defer usr.mu.Unlock()
|
||||||
|
|
||||||
|
return usr.role == ROLE_GAMEMASTER || usr.role == ROLE_ADMIN
|
||||||
}
|
}
|
||||||
|
|
||||||
func (user *User) IsAdmin() bool {
|
func (usr *User) IsAdmin() bool {
|
||||||
return user.role == ROLE_ADMIN
|
usr.mu.Lock()
|
||||||
|
defer usr.mu.Unlock()
|
||||||
|
|
||||||
|
return usr.role == ROLE_ADMIN
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user