76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
package actions
|
|
|
|
import (
|
|
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/temp_models/db"
|
|
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/temp_models/json"
|
|
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/transport"
|
|
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/types"
|
|
)
|
|
|
|
type WsSessionMsg struct {
|
|
SessionId string `json:"sessionId"`
|
|
UserId string `json:"userId,omitempty"`
|
|
}
|
|
|
|
func (msg WsSessionMsg) Action(tr *transport.FoundryTransport, status *types.FoundryStatus) error {
|
|
if status.IsActive {
|
|
tr.Logger.Info("Session msg", "userid", msg.UserId)
|
|
if msg.UserId == "" {
|
|
err := tr.LogInToWorld()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tr.Logger.Info("World is started. Succesfully logged into world")
|
|
close(tr.ReadChan.Reconnect())
|
|
return nil
|
|
}
|
|
|
|
// select {
|
|
// case <-tr.LoggedInChan:
|
|
// return ErrUserChannelIsClosed
|
|
// default:
|
|
// tr.LoggedInChan <- true
|
|
// }
|
|
|
|
// time.Sleep(3 * time.Second)
|
|
// types.CloseChannel(tr.LoggedInChan)
|
|
|
|
go msg.OnActiveWorld(tr)
|
|
return nil
|
|
}
|
|
|
|
if tr.IsDbInit {
|
|
return nil
|
|
}
|
|
tr.IsDbInit = true
|
|
|
|
go tr.FillDBWithFoundryData()
|
|
return nil
|
|
}
|
|
|
|
func (msg WsSessionMsg) OnActiveWorld(tr *transport.FoundryTransport) error {
|
|
msgJson, err := tr.GetJsonData("world")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
tr.Logger.Info("Game data received")
|
|
|
|
gameJson, err := json.ParseGame(msgJson)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var foundryStateDb db.Game
|
|
gameJson.ToDB(&foundryStateDb)
|
|
|
|
err = foundryStateDb.Insert(tr.DB)
|
|
if err != nil {
|
|
tr.ReadChan.Err() <- &types.FoundryError{Direction: types.WriterCode, Err: err, Type: types.DbCode}
|
|
}
|
|
|
|
tr.Logger.Info("Game data succesfully inserted to DB")
|
|
|
|
return nil
|
|
}
|