Skip to content

Commit

Permalink
fix shared data
Browse files Browse the repository at this point in the history
  • Loading branch information
inada-s committed Apr 20, 2020
1 parent 9fbd326 commit a46faf7
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 29 deletions.
8 changes: 3 additions & 5 deletions gdxsv/cmdid_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion gdxsv/lbs_battle.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type LbsBattle struct {
app *Lbs

BattleCode string
McsRegion string
ServerIP net.IP
ServerPort uint16
Users []*DBUser
Expand Down Expand Up @@ -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
}

Expand All @@ -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),
Expand Down
19 changes: 14 additions & 5 deletions gdxsv/lbs_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ const (
lbsMatchingCancel CmdID = 0x6005

// gdxsv extended commands
lbsExtNotifyMcsStatus CmdID = 0x9900
lbsExtNotifyBattleUsers CmdID = 0x9901
lbsExtSyncSharedData CmdID = 0x9900
)

func RequestLineCheck(p *LbsPeer) {
Expand Down Expand Up @@ -1354,15 +1353,25 @@ 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 {
glog.Error(err)
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())
})
12 changes: 6 additions & 6 deletions gdxsv/lbs_lobby.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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,
Expand All @@ -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)
}
}
17 changes: 7 additions & 10 deletions gdxsv/mcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:])
Expand Down Expand Up @@ -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)
}
}
}
Expand All @@ -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
Expand Down
34 changes: 32 additions & 2 deletions gdxsv/shareddata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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,
Expand All @@ -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{}
Expand Down

0 comments on commit a46faf7

Please sign in to comment.