diff --git a/core/apiimpl.go b/core/apiimpl.go index 402f0fb..13610d2 100644 --- a/core/apiimpl.go +++ b/core/apiimpl.go @@ -128,14 +128,14 @@ func (sub *subTavern) Invite(w http.ResponseWriter, r *http.Request) { Invitee bson.M `bson:"invitee"` } if err := readBsonDoc(r.Body, &reqdoc); err != nil { - logger.Error("Invite failed. readBsonDoc returns err :", err) + logger.Println("Invite failed. readBsonDoc returns err :", err) w.WriteHeader(http.StatusInternalServerError) return } result, err := group.Invite(gid, mid, reqdoc.Inviter, reqdoc.Invitee) if err != nil { - logger.Error("Invite failed. group.Invite returns err :", err) + logger.Println("Invite failed. group.Invite returns err :", err) w.WriteHeader(http.StatusInternalServerError) return } @@ -191,7 +191,7 @@ func (sub *subTavern) AcceptInvitation(w http.ResponseWriter, r *http.Request) { err := group.AcceptInvitation(gid, mid, member) if err != nil { - logger.Error("AcceptInvitation failed. group.AcceptInvitation returns err :", err) + logger.Println("AcceptInvitation failed. group.AcceptInvitation returns err :", err) w.WriteHeader(http.StatusInternalServerError) return } diff --git a/core/group_memory.go b/core/group_memory.go index fcdebfb..31ba010 100644 --- a/core/group_memory.go +++ b/core/group_memory.go @@ -21,10 +21,12 @@ import ( type accountID = primitive.ObjectID type ticketID = primitive.ObjectID type groupID = primitive.ObjectID -type Body = bson.M func init() { gob.Register(memberDoc{}) + gob.Register(groupDoc{}) + gob.Register(Invitation{}) + gob.Register(InvitationFail{}) } func makeTid(gid groupID, in accountID) string { @@ -44,33 +46,51 @@ type Invitation struct { // 플레이어한테 공유하는 멤버 정보 type memberDoc struct { - Body `json:",inline"` - Invite bool `json:"_invite"` - InviteExpire int64 `json:"_invite_exp"` + Body bson.M `json:"_body"` + Invite bool `json:"_invite"` + InviteExpire int64 `json:"_invite_exp"` } type InvitationFail bson.M type groupDoc struct { - Body `json:",inline"` + Body bson.M `json:"_body"` Members map[string]*memberDoc `json:"_members"` InCharge string `json:"_incharge"` + Gid string `json:"_gid"` - rh *RedisonHandler - id groupID - idhex string + rh *RedisonHandler + id groupID } -type groupDocWithId struct { - *groupDoc `json:",inline"` - Gid string `json:"_gid"` +func (p groupDoc) MarshalJSON() ([]byte, error) { + if len(p.Gid) == 0 { + p.Gid = p.id.Hex() + } + + // Turn p into a map + type groupDoc_ groupDoc // prevent recursion + b, _ := json.Marshal(groupDoc_(p)) + + var m map[string]json.RawMessage + _ = json.Unmarshal(b, &m) + + // Add tags to the map, possibly overriding struct fields + for k, v := range p.Body { + // if overriding struct fields is not acceptable: + // if _, ok := m[k]; ok { continue } + b, _ = json.Marshal(v) + m[k] = b + } + + return json.Marshal(m) } func (gd *groupDoc) strid() string { - if len(gd.idhex) == 0 { - gd.idhex = gd.id.Hex() + if len(gd.Gid) == 0 { + gd.Gid = gd.id.Hex() } - return gd.idhex + return gd.Gid } func (gd *groupDoc) tid(in accountID) string { @@ -282,11 +302,8 @@ func (gm *groupInMemory) Invite(gid groupID, mid accountID, inviterDoc bson.M, i gm.sendUpstreamMessage(&wshandler.UpstreamMessage{ Target: "@" + mid.Hex(), - Body: groupDocWithId{ - groupDoc: gd, - Gid: gd.strid(), - }, - Tag: []string{"GroupDocFull"}, + Body: gd, + Tag: []string{"GroupDocFull"}, }) } @@ -296,11 +313,10 @@ func (gm *groupInMemory) Invite(gid groupID, mid accountID, inviterDoc bson.M, i } // 초대 중 표시 - success, err := gm.rh.SetNX(gm.rh.ctx, targetid.Hex(), mid.Hex(), time.Duration(gm.InviteExpire)*time.Second).Result() + _, err = gm.rh.SetNX(gm.rh.ctx, targetid.Hex(), mid.Hex(), time.Duration(gm.InviteExpire)*time.Second).Result() if err != nil { return "", err } - logger.Println("invitation key :", targetid.Hex(), success) // invitee에게 알림 gm.sendUpstreamMessage(&wshandler.UpstreamMessage{ @@ -358,20 +374,18 @@ func (gm *groupInMemory) AcceptInvitation(gid groupID, mid accountID, member bso return err } - logger.Println(full) - var temp []groupDoc + var temp []*groupDoc err = json.Unmarshal([]byte(full.(string)), &temp) if err != nil { return err } + test, _ := json.Marshal(temp[0]) + logger.Println(string(test)) gm.sendUpstreamMessage(&wshandler.UpstreamMessage{ Target: "@" + mid.Hex(), - Body: groupDocWithId{ - groupDoc: &temp[0], - Gid: gd.strid(), - }, - Tag: []string{"GroupDocFull"}, + Body: temp[0], + Tag: []string{"GroupDocFull"}, }) }