From a46faf716e985d2e68f0b65733fc5dcfc8054485 Mon Sep 17 00:00:00 2001 From: Shingo INADA Date: Mon, 20 Apr 2020 09:38:41 +0900 Subject: [PATCH] fix shared data --- gdxsv/cmdid_string.go | 8 +++----- gdxsv/lbs_battle.go | 5 ++++- gdxsv/lbs_handler.go | 19 ++++++++++++++----- gdxsv/lbs_lobby.go | 12 ++++++------ gdxsv/mcs.go | 17 +++++++---------- gdxsv/shareddata.go | 34 ++++++++++++++++++++++++++++++++-- 6 files changed, 66 insertions(+), 29 deletions(-) diff --git a/gdxsv/cmdid_string.go b/gdxsv/cmdid_string.go index f62a84b..ae5fae1 100644 --- a/gdxsv/cmdid_string.go +++ b/gdxsv/cmdid_string.go @@ -99,11 +99,10 @@ func _() { _ = x[lbsAskMcsAddress-26902] _ = x[lbsAskMcsVersion-26903] _ = x[lbsMatchingCancel-24581] - _ = x[lbsExtNotifyMcsStatus-39168] - _ = x[lbsExtNotifyBattleUsers-39169] + _ = x[lbsExtSyncSharedData-39168] } -const _CmdID_name = "lbsLineChecklbsLogoutlbsShutDownlbsVSUserLostlbsMatchingCancellbsConnectionIDlbsAskConnectionIDlbsWarningMessagelbsLoginTypelbsUserHandlelbsUserRegistlbsUserDecidelbsAskPlatformCodelbsAskCountryCodelbsAskGameCodelbsAskGameVersionlbsLoginOklbsAskBattleResultlbsUserInfo1lbsUserInfo2lbsUserInfo3lbsUserInfo4lbsUserInfo5lbsUserInfo6lbsUserInfo7lbsUserInfo8lbsUserInfo9lbsEncodeStartlbsStartLobbylbsAskKDDIChargeslbsPostGameParameterlbsRankRankinglbsWinLoselbsDeviceDatalbsServerMoneylbsPlazaMaxlbsPlazaTitlelbsPlazaJoinlbsPlazaStatuslbsPlazaEntrylbsGoToToplbsPlazaExplainlbsLobbyJoinlbsLobbyEntrylbsPlazaExitlbsRoomMaxlbsRoomTitlelbsRoomStatuslbsRoomEntrylbsRoomCreatelbsLobbyExitlbsLobbyMatchingEntrylbsLobbyMatchingJoinlbsLobbyRemovelbsRoomExitlbsRoomLeaverlbsRoomCommerlbsMatchingEntrylbsRoomRemovelbsWaitJoinlbsRoomUserRejectlbsPutRoomNamelbsEndRoomCreatelbsPostChatMessagelbsChatMessagelbsUserSitelbsSendMaillbsRecvMaillbsManagerMessagelbsAskNewsTaglbsNewsTextlbsInvitationTaglbsRegulationHeaderlbsRegulationTextlbsRegulationFooterlbsTopRankingTaglbsTopRankingSuulbsTopRankinglbsAskPatchDatalbsPatchHeaderlbsPatchData6863lbsCalcDownloadChecksumlbsPatchPinglbsReadyBattlelbsAskMatchingJoinlbsAskPlayerSidelbsAskPlayerInfolbsAskRuleDatalbsAskBattleCodelbsAskMcsAddresslbsAskMcsVersionlbsExtNotifyMcsStatuslbsExtNotifyBattleUsers" +const _CmdID_name = "lbsLineChecklbsLogoutlbsShutDownlbsVSUserLostlbsMatchingCancellbsConnectionIDlbsAskConnectionIDlbsWarningMessagelbsLoginTypelbsUserHandlelbsUserRegistlbsUserDecidelbsAskPlatformCodelbsAskCountryCodelbsAskGameCodelbsAskGameVersionlbsLoginOklbsAskBattleResultlbsUserInfo1lbsUserInfo2lbsUserInfo3lbsUserInfo4lbsUserInfo5lbsUserInfo6lbsUserInfo7lbsUserInfo8lbsUserInfo9lbsEncodeStartlbsStartLobbylbsAskKDDIChargeslbsPostGameParameterlbsRankRankinglbsWinLoselbsDeviceDatalbsServerMoneylbsPlazaMaxlbsPlazaTitlelbsPlazaJoinlbsPlazaStatuslbsPlazaEntrylbsGoToToplbsPlazaExplainlbsLobbyJoinlbsLobbyEntrylbsPlazaExitlbsRoomMaxlbsRoomTitlelbsRoomStatuslbsRoomEntrylbsRoomCreatelbsLobbyExitlbsLobbyMatchingEntrylbsLobbyMatchingJoinlbsLobbyRemovelbsRoomExitlbsRoomLeaverlbsRoomCommerlbsMatchingEntrylbsRoomRemovelbsWaitJoinlbsRoomUserRejectlbsPutRoomNamelbsEndRoomCreatelbsPostChatMessagelbsChatMessagelbsUserSitelbsSendMaillbsRecvMaillbsManagerMessagelbsAskNewsTaglbsNewsTextlbsInvitationTaglbsRegulationHeaderlbsRegulationTextlbsRegulationFooterlbsTopRankingTaglbsTopRankingSuulbsTopRankinglbsAskPatchDatalbsPatchHeaderlbsPatchData6863lbsCalcDownloadChecksumlbsPatchPinglbsReadyBattlelbsAskMatchingJoinlbsAskPlayerSidelbsAskPlayerInfolbsAskRuleDatalbsAskBattleCodelbsAskMcsAddresslbsAskMcsVersionlbsExtSyncSharedData" var _CmdID_map = map[CmdID]string{ 24577: _CmdID_name[0:12], @@ -197,8 +196,7 @@ var _CmdID_map = map[CmdID]string{ 26901: _CmdID_name[1236:1252], 26902: _CmdID_name[1252:1268], 26903: _CmdID_name[1268:1284], - 39168: _CmdID_name[1284:1305], - 39169: _CmdID_name[1305:1328], + 39168: _CmdID_name[1284:1304], } func (i CmdID) String() string { diff --git a/gdxsv/lbs_battle.go b/gdxsv/lbs_battle.go index 9b39182..7344868 100644 --- a/gdxsv/lbs_battle.go +++ b/gdxsv/lbs_battle.go @@ -17,6 +17,7 @@ type LbsBattle struct { app *Lbs BattleCode string + McsRegion string ServerIP net.IP ServerPort uint16 Users []*DBUser @@ -44,8 +45,9 @@ func splitIPPort(addr string) (net.IP, uint16, error) { return net.ParseIP(host), uint16(portNum), nil } -func NewBattle(app *Lbs, lobbyID uint16, rule *Rule, mcsAddr string) *LbsBattle { +func NewBattle(app *Lbs, lobbyID uint16, rule *Rule, mcsRegion string, mcsAddr string) *LbsBattle { if mcsAddr == "" { + mcsRegion = "" mcsAddr = conf.BattlePublicAddr } @@ -63,6 +65,7 @@ func NewBattle(app *Lbs, lobbyID uint16, rule *Rule, mcsAddr string) *LbsBattle app: app, BattleCode: genBattleCode(), + McsRegion: mcsRegion, ServerIP: ip, ServerPort: port, Users: make([]*DBUser, 0), diff --git a/gdxsv/lbs_handler.go b/gdxsv/lbs_handler.go index 0b7f42c..18a86ff 100644 --- a/gdxsv/lbs_handler.go +++ b/gdxsv/lbs_handler.go @@ -155,8 +155,7 @@ const ( lbsMatchingCancel CmdID = 0x6005 // gdxsv extended commands - lbsExtNotifyMcsStatus CmdID = 0x9900 - lbsExtNotifyBattleUsers CmdID = 0x9901 + lbsExtSyncSharedData CmdID = 0x9900 ) func RequestLineCheck(p *LbsPeer) { @@ -1354,7 +1353,7 @@ var _ = register(lbsAskMcsVersion, func(p *LbsPeer, m *LbsMessage) { p.SendMessage(NewServerAnswer(m).Writer().Write8(10).Msg()) }) -var _ = register(lbsExtNotifyMcsStatus, func(p *LbsPeer, m *LbsMessage) { +var _ = register(lbsExtSyncSharedData, func(p *LbsPeer, m *LbsMessage) { var mcsStatus McsStatus err := json.Unmarshal(m.Reader().ReadBytes(), &mcsStatus) if err != nil { @@ -1362,7 +1361,17 @@ var _ = register(lbsExtNotifyMcsStatus, func(p *LbsPeer, m *LbsMessage) { return } - glog.Infof("mcs status: %+v", mcsStatus) + glog.Infof("update status: %+v", mcsStatus) p.app.mcs[mcsStatus.Region] = &mcsStatus - p.SendMessage(NewServerAnswer(m)) // pong + + SyncSharedDataMcsToLbs(&mcsStatus) + + var lbsStatus LbsStatus + lbsStatus.Users = GetMcsUsers() + lbsStatusJson, err := json.Marshal(lbsStatus) + if err != nil { + glog.Error(err) + return + } + p.SendMessage(NewServerAnswer(m).Writer().WriteBytes(lbsStatusJson).Msg()) }) diff --git a/gdxsv/lbs_lobby.go b/gdxsv/lbs_lobby.go index 2eb6330..6f65ce1 100644 --- a/gdxsv/lbs_lobby.go +++ b/gdxsv/lbs_lobby.go @@ -226,14 +226,12 @@ func (l *LbsLobby) CheckLobbyBattleStart() { mcsAddr = stat.PublicAddr } - b := NewBattle(l.app, l.ID, l.Rule, mcsAddr) + b := NewBattle(l.app, l.ID, l.Rule, l.McsRegion, mcsAddr) participants := l.pickLobbyBattleParticipants() for _, q := range participants { b.Add(q) q.Battle = b - AddUserWhoIsGoingTobattle( - b.BattleCode, q.UserID, q.Name, q.Team, q.SessionID) getDB().AddBattleRecord(&BattleRecord{ BattleCode: b.BattleCode, UserID: q.UserID, @@ -242,6 +240,8 @@ func (l *LbsLobby) CheckLobbyBattleStart() { Players: len(participants), Aggregate: 1, }) + AddUserWhoIsGoingTobattle( + b.BattleCode, b.McsRegion, q.UserID, q.Name, q.Team, q.SessionID) NotifyReadyBattle(q) } } @@ -307,13 +307,11 @@ func (l *LbsLobby) CheckRoomBattleStart() { mcsAddr = stat.PublicAddr } - b := NewBattle(l.app, l.ID, l.Rule, mcsAddr) + b := NewBattle(l.app, l.ID, l.Rule, l.McsRegion, mcsAddr) for _, q := range participants { b.Add(q) q.Battle = b - AddUserWhoIsGoingTobattle( - b.BattleCode, q.UserID, q.Name, q.Team, q.SessionID) getDB().AddBattleRecord(&BattleRecord{ BattleCode: b.BattleCode, UserID: q.UserID, @@ -322,6 +320,8 @@ func (l *LbsLobby) CheckRoomBattleStart() { Players: len(participants), Aggregate: 1, }) + AddUserWhoIsGoingTobattle( + b.BattleCode, b.McsRegion, q.UserID, q.Name, q.Team, q.SessionID) NotifyReadyBattle(q) } } diff --git a/gdxsv/mcs.go b/gdxsv/mcs.go index eafa94b..19348d4 100644 --- a/gdxsv/mcs.go +++ b/gdxsv/mcs.go @@ -105,7 +105,7 @@ func (mcs *Mcs) DialAndSyncWithLbs(lobbyAddr string, battlePublicAddr string, ba if err != nil { return err } - buf := NewServerNotice(lbsExtNotifyMcsStatus).Writer().WriteBytes(statusBin).Msg().Serialize() + buf := NewServerNotice(lbsExtSyncSharedData).Writer().WriteBytes(statusBin).Msg().Serialize() for sum := 0; sum < len(buf); { conn.SetWriteDeadline(time.Now().Add(time.Second)) n, err := conn.Write(buf[sum:]) @@ -143,14 +143,11 @@ func (mcs *Mcs) DialAndSyncWithLbs(lobbyAddr string, battlePublicAddr string, ba if msg != nil { switch msg.Command { - case lbsExtNotifyBattleUsers: - glog.Info("Recv lbsExtNotifyBattleUsers") - var users []McsUser - json.Unmarshal(msg.Reader().ReadBytes(), &users) - for _, u := range users { - glog.Info(u) - AddUserWhoIsGoingTobattle(u.BattleCode, u.UserID, u.Name, u.Side, u.SessionID) - } + case lbsExtSyncSharedData: + glog.Info("Recv lbsExtSyncSharedData") + var lbsStatus LbsStatus + json.Unmarshal(msg.Reader().ReadBytes(), &lbsStatus) + SyncSharedDataLbsToMcs(&lbsStatus) } } } @@ -164,7 +161,7 @@ func (mcs *Mcs) DialAndSyncWithLbs(lobbyAddr string, battlePublicAddr string, ba return ctx.Err() case <-ticker.C: status.Updated = mcs.LastUpdated() - status.Users = GetInBattleUsers() + status.Users = GetMcsUsers() err = sendMcsStatus() if err != nil { return err diff --git a/gdxsv/shareddata.go b/gdxsv/shareddata.go index 136cf80..8500702 100644 --- a/gdxsv/shareddata.go +++ b/gdxsv/shareddata.go @@ -24,6 +24,7 @@ func init() { type McsUser struct { BattleCode string `json:"battle_code,omitempty"` + McsRegion string `json:"mcs_region,omitempty"` UserID string `json:"user_id,omitempty"` Name string `json:"name,omitempty"` Side uint16 `json:"side,omitempty"` @@ -39,11 +40,16 @@ type McsStatus struct { Users []McsUser `json:"users,omitempty"` } -func AddUserWhoIsGoingTobattle(battleCode string, userID string, name string, side uint16, sessionID string) { +type LbsStatus struct { + Users []McsUser `json:"users,omitempty"` +} + +func AddUserWhoIsGoingTobattle(battleCode string, mcsRegion string, userID string, name string, side uint16, sessionID string) { sharedData.Lock() defer sharedData.Unlock() sharedData.battleUsers[sessionID] = McsUser{ BattleCode: battleCode, + McsRegion: mcsRegion, UserID: userID, Name: name, Side: side, @@ -52,7 +58,31 @@ func AddUserWhoIsGoingTobattle(battleCode string, userID string, name string, si } } -func GetInBattleUsers() []McsUser { +func SyncSharedDataMcsToLbs(status *McsStatus) { + sharedData.Lock() + defer sharedData.Unlock() + + for _, u := range status.Users { + _, ok := sharedData.battleUsers[u.SessionID] + if ok { + sharedData.battleUsers[u.SessionID] = u + } + } +} + +func SyncSharedDataLbsToMcs(status *LbsStatus) { + sharedData.Lock() + defer sharedData.Unlock() + + for _, u := range status.Users { + _, ok := sharedData.battleUsers[u.SessionID] + if !ok { + sharedData.battleUsers[u.SessionID] = u + } + } +} + +func GetMcsUsers() []McsUser { sharedData.Lock() defer sharedData.Unlock() ret := []McsUser{}