func (session *Session) login(accID int64) { userData := new(UserData) skeleton.Go(func() { user := new(model.User) user.AccId = accID has, err := model.Engine.Get(user) if err != nil { log.Error("load acc %v data error: %v", accID, err) userData = nil m := &msg.ClosePush{Code: msg.ClosePush_INNER.Enum()} session.WriteMsg(m) session.Close() return } if has { userData.User = user } else { err := userData.initValue(accID) if err != nil { log.Error("init acc %v data error: %v", accID, err) userData = nil m := &msg.ClosePush{Code: msg.ClosePush_INNER.Enum()} session.WriteMsg(m) session.Close() return } } }, func() { // network closed if session.state == Session_Logout { session.logout(accID) return } // db error session.state = Session_InGame if userData == nil { return } // ok session.data = userData userIDSessions[userData.User.Id] = session session.UserData().(*AgentInfo).userID = userData.User.Id session.onLogin() session.autoSaveDB() }) }
func (data *UserData) initValue(accID int64) error { user := new(model.User) user.AccId = accID affected, err := model.Engine.Insert(user) if err != nil { return fmt.Errorf("insert user error: %v", err) } if affected == 1 { data.User = user } else { return fmt.Errorf("insert user failed") } return nil }