finish world initialization, set up init on startup and on shutdown, move all models to main directory

This commit is contained in:
lbenedar
2026-04-24 18:04:35 +03:00
parent 07025afefc
commit fd65631be1
125 changed files with 1534 additions and 2802 deletions

View File

@@ -1,8 +1,8 @@
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"
"time"
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/transport"
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/types"
)
@@ -14,29 +14,7 @@ type WsSessionMsg struct {
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
return msg.OnActiveWorld(tr)
}
if tr.IsDbInit {
@@ -49,27 +27,36 @@ func (msg WsSessionMsg) Action(tr *transport.FoundryTransport, status *types.Fou
}
func (msg WsSessionMsg) OnActiveWorld(tr *transport.FoundryTransport) error {
msgJson, err := tr.GetJsonData("world")
tr.Logger.Info("Session msg", "userid", msg.UserId)
if msg.UserId != "" {
return msg.HandleLoggedInUser(tr)
}
err := tr.LogInToWorld()
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")
tr.Logger.Info("World is started. Succesfully logged into world")
close(tr.ReadChan.Reconnect())
return nil
}
func (msg WsSessionMsg) HandleLoggedInUser(tr *transport.FoundryTransport) error {
if tr.LoggedInChan == nil {
tr.Logger.Info("World had been started before application was started. Run insertion of world data")
go tr.InsertGameToDB()
return nil
}
select {
case <-tr.LoggedInChan:
return ErrUserChannelIsClosed
default:
tr.LoggedInChan <- true
}
time.Sleep(3 * time.Second)
types.CloseChannel(tr.LoggedInChan)
return nil
}