wshandler와 분리 중

This commit is contained in:
2023-07-06 00:53:53 +09:00
parent 3c14e7e4c5
commit 8d0f21077d
7 changed files with 108 additions and 1422 deletions

View File

@ -132,147 +132,6 @@ func (sub *subTavern) JoinGroup(w http.ResponseWriter, r *http.Request) {
}
}
func (sub *subTavern) EnterCandidateChannel(w http.ResponseWriter, r *http.Request) {
typename, _ := common.ReadStringFormValue(r.Form, "type")
if _, ok := sub.groups[typename]; !ok {
logger.Println("EnterCandidateChannel failed. group type is missing :", r)
w.WriteHeader(http.StatusBadRequest)
return
}
midobj, ok := common.ReadObjectIDFormValue(r.Form, "mid")
if !ok {
logger.Println("EnterCandidateChannel failed. mid is missing :", r)
w.WriteHeader(http.StatusBadRequest)
return
}
gidobj, ok := common.ReadObjectIDFormValue(r.Form, "gid")
if !ok {
logger.Println("EnterCandidateChannel failed. gid is missing :", r)
w.WriteHeader(http.StatusBadRequest)
return
}
// candidate channel은 big endian 최상위 비트가 1
gidobj[0] |= 0x80
if conn := sub.wsh.Conn(sub.region, midobj); conn != nil {
richConnOuter{wsh: sub.wsh, rc: conn}.JoinTag(sub.region, gidobj, midobj, typename)
} else {
sub.wshRpc.caller.One(midobj).JoinTag(sub.region, gidobj, midobj, typename)
}
}
func (sub *subTavern) LeaveCandidateChannel(w http.ResponseWriter, r *http.Request) {
typename, _ := common.ReadStringFormValue(r.Form, "type")
if _, ok := sub.groups[typename]; !ok {
logger.Println("EnterCandidateChannel failed. group type is missing :", r)
w.WriteHeader(http.StatusBadRequest)
return
}
midobj, ok := common.ReadObjectIDFormValue(r.Form, "mid")
if !ok {
logger.Println("EnterCandidateChannel failed. mid is missing :", r)
w.WriteHeader(http.StatusBadRequest)
return
}
gidobj, ok := common.ReadObjectIDFormValue(r.Form, "gid")
if !ok {
logger.Println("EnterCandidateChannel failed. gid is missing :", r)
w.WriteHeader(http.StatusBadRequest)
return
}
// candidate channel은 big endian 최상위 비트가 1
gidobj[0] |= 0x80
if conn := sub.wsh.Conn(sub.region, midobj); conn != nil {
richConnOuter{wsh: sub.wsh, rc: conn}.LeaveTag(sub.region, gidobj, midobj, typename)
} else {
sub.wshRpc.caller.One(midobj).LeaveTag(sub.region, gidobj, midobj, typename)
}
}
func (sub *subTavern) EnterGroupChannel(w http.ResponseWriter, r *http.Request) {
typename, _ := common.ReadStringFormValue(r.Form, "type")
group := sub.groups[typename]
if group == nil {
logger.Println("EnterGroupChannel failed. group type is missing :", r)
w.WriteHeader(http.StatusBadRequest)
return
}
midobj, ok := common.ReadObjectIDFormValue(r.Form, "mid")
if !ok {
logger.Println("EnterGroupChannel failed. mid is missing :", r)
w.WriteHeader(http.StatusBadRequest)
return
}
gidobj, ok := common.ReadObjectIDFormValue(r.Form, "gid")
if !ok {
logger.Println("EnterGroupChannel failed. gid is missing :", r)
w.WriteHeader(http.StatusBadRequest)
return
}
tid := group.FindTicketID(gidobj, midobj)
if tid.IsZero() {
logger.Println("EnterGroupChannel failed. tid is zero")
w.WriteHeader(http.StatusBadRequest)
return
}
if conn := sub.wsh.Conn(sub.region, midobj); conn != nil {
richConnOuter{wsh: sub.wsh, rc: conn}.JoinTag(sub.region, gidobj, tid, typename)
} else {
sub.wshRpc.caller.One(midobj).JoinTag(sub.region, gidobj, tid, typename)
}
writeBsonDoc(w, primitive.M{"_id": tid})
}
func (sub *subTavern) SetStateInGroup(w http.ResponseWriter, r *http.Request) {
gid, ok := common.ReadObjectIDFormValue(r.Form, "gid")
if !ok {
logger.Println("SetStateInGroup failed. tag is missing :", r)
w.WriteHeader(http.StatusBadRequest)
return
}
mid, ok := common.ReadObjectIDFormValue(r.Form, "mid")
if !ok {
logger.Println("SetStateInGroup failed. mid form value is missing")
w.WriteHeader(http.StatusBadRequest)
return
}
state, ok := common.ReadStringFormValue(r.Form, "state")
if !ok {
logger.Println("SetStateInGroup failed. state is missing :", r)
w.WriteHeader(http.StatusBadRequest)
return
}
typename, ok := common.ReadStringFormValue(r.Form, "type")
if !ok {
logger.Println("SetStateInGroup failed. type is missing :", r)
w.WriteHeader(http.StatusBadRequest)
return
}
var doc bson.M
if err := readBsonDoc(r.Body, &doc); err != nil {
logger.Error("SetStateInGroup failed. readBsonDoc err :", err)
w.WriteHeader(http.StatusBadRequest)
return
}
tid := doc["_id"].(primitive.ObjectID)
if conn := sub.wsh.Conn(sub.region, mid); conn != nil {
richConnOuter{wsh: sub.wsh, rc: conn}.SetStateInTag(sub.region, gid, tid, state, typename)
} else {
sub.wshRpc.caller.One(mid).SetStateInTag(sub.region, gid, tid, state, typename)
}
}
// Invite : 초대
// - type : 초대 타입 (required)
// - from : 초대하는 자 (required)
@ -490,119 +349,6 @@ func (sub *subTavern) QueryInvitations(w http.ResponseWriter, r *http.Request) {
}
}
func (sub *subTavern) TurnGroupOnline(w http.ResponseWriter, r *http.Request) {
// group을 online 상태로 만든다.
// 요청을 보내는 클라이언트의 conn이 끊이면 online에서 제거한다.
// online인 group을 가지고 뭘 할지는 게임이 알아서...
typename, _ := common.ReadStringFormValue(r.Form, "type")
group := sub.groups[typename]
if group == nil {
logger.Println("TurnGroupOnline failed. group type is missing :", r.Form)
w.WriteHeader(http.StatusBadRequest)
return
}
gid, ok := common.ReadObjectIDFormValue(r.Form, "_id")
if !ok {
logger.Println("TurnGroupOnline failed. group id '_id' form value is missing :", r.Form)
w.WriteHeader(http.StatusBadRequest)
return
}
mid, ok := common.ReadObjectIDFormValue(r.Form, "mid")
if !ok {
logger.Println("TurnGroupOnline failed. mid form value is missing")
w.WriteHeader(http.StatusBadRequest)
return
}
var filter bson.M
if err := readBsonDoc(r.Body, &filter); err != nil {
logger.Error("TurnGroupOnline failed. readBsonDoc return err :", err)
w.WriteHeader(http.StatusBadRequest)
return
}
exist, err := group.Exist(gid, filter)
if err != nil {
logger.Error("TurnGroupOnline failed. FindOne return err :", err)
w.WriteHeader(http.StatusBadRequest)
return
}
if !exist {
logger.Println("TurnGroupOnline failed. filter not match", filter)
w.WriteHeader(http.StatusBadRequest)
return
}
score, ok := common.ReadFloatFormValue(r.Form, "score")
if !ok {
score = 100
}
if conn := sub.wsh.Conn(sub.region, mid); conn != nil {
err = richConnOuter{wsh: sub.wsh, rc: conn}.TurnGroupOnline(onlineGroupQueryKey(typename), gid, score)
} else {
err = sub.wshRpc.caller.One(mid).TurnGroupOnline(onlineGroupQueryKey(typename), gid, score)
}
if err != nil {
logger.Error("TurnGroupOnline failed. TurnGroupOnline err :", err)
w.WriteHeader(http.StatusBadRequest)
return
}
}
func (sub *subTavern) TurnGroupOffline(w http.ResponseWriter, r *http.Request) {
// group을 offline 상태로 만든다.
// 요청을 보내는 클라이언트의 conn이 끊이면 online에서 제거한다.
// online인 group을 가지고 뭘 할지는 게임이 알아서...
typename, _ := common.ReadStringFormValue(r.Form, "type")
group := sub.groups[typename]
if group == nil {
logger.Println("TurnGroupOffline failed. group type is missing :", r.Form)
w.WriteHeader(http.StatusBadRequest)
return
}
gid, ok := common.ReadObjectIDFormValue(r.Form, "_id")
if !ok {
logger.Println("TurnGroupOffline failed. group id '_id' form value is missing :", r.Form)
w.WriteHeader(http.StatusBadRequest)
return
}
mid, ok := common.ReadObjectIDFormValue(r.Form, "mid")
if !ok {
logger.Println("TurnGroupOffline failed. mid form value is missing")
w.WriteHeader(http.StatusBadRequest)
return
}
// onlinename := onlineGroupQueryKey(typename)
// if onClose := conn.UnregistOnCloseFunc(onlinename); onClose != nil {
// onClose()
// } else {
// gid, ok := common.ReadStringFormValue(form, "_id")
// if ok {
// sub.redisSync.ZRem(context.Background(), onlinename, gid)
// }
// }
var err error
if conn := sub.wsh.Conn(sub.region, mid); conn != nil {
err = richConnOuter{wsh: sub.wsh, rc: conn}.TurnGroupOffline(onlineGroupQueryKey(typename), gid)
} else {
err = sub.wshRpc.caller.One(mid).TurnGroupOffline(onlineGroupQueryKey(typename), gid)
}
if err != nil {
logger.Error("TurnGroupOffline failed. TurnGroupOnline err :", err)
w.WriteHeader(http.StatusBadRequest)
return
}
}
func (sub *subTavern) QueryOnlineGroup(w http.ResponseWriter, r *http.Request) {
typename, _ := common.ReadStringFormValue(r.Form, "type")
group := sub.groups[typename]
@ -690,11 +436,7 @@ func (sub *subTavern) QueryOnlineState(w http.ResponseWriter, r *http.Request) {
return
}
state, err := sub.wsh.GetState(mid)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
state := sub.wsh.GetState(sub.region, mid)
w.Write([]byte(state))
}
@ -706,13 +448,7 @@ func (sub *subTavern) IsOnline(w http.ResponseWriter, r *http.Request) {
return
}
ok, err := sub.wsh.IsOnline(mid)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if ok {
if state := sub.wsh.GetState(sub.region, mid); len(state) > 0 {
w.Write([]byte("true"))
} else {
w.Write([]byte("false"))