func (this *WorldServer) DoUserOffline(msg *msgcmd.Cmd, session *libnet.Session) { if user, found := usermap.Find(msg.Args[0]); found { //设置用户状态 if userinfo, ok := user.(*common.UserInfo); ok { userinfo.State = 0 userinfo.UpdateTimestamp() println("[MS --> WS]:用户下线,", userinfo.Name) //广播所有用户,有用户下线 bcMsg := msgcmd.NewCmd(msgcmd.MCMD_W2M_BROADCAST) bcMsg.AddArg(msgcmd.IntToString(msgcmd.ECMD_BROADCAST_USEROFFLINE)) bcMsg.AddArg(userinfo.Name) this.Broadcast(bcMsg) } else { println("World Server Error: (*common.UserInfo) 类型断言 失败. ") logFile.Printf("World Server Error: (*common.UserInfo) 类型断言 失败. ") return } } }
func (this *WorldServer) DoLogin(msg *msgcmd.Cmd, session *libnet.Session) { resmsg := msgcmd.NewCmd(msgcmd.MCMD_W2M_LOGIN) resmsg.AddArg(msg.Args[2]) var userinfo *common.UserInfo var isInUserMap = false var result bool if user, ok := usermap.Find(msg.Args[0]); ok { if userinfo, result = user.(*common.UserInfo); !result { println("World Server Error: (*common.UserInfo) 类型断言 失败. ") logFile.Printf("World Server Error: (*common.UserInfo) 类型断言 失败. ") return } isInUserMap = true } else { //查找是否已经存在该用户名 row := this.database.QueryData(`SELECT * FROM users WHERE name=?`, msg.Args[0]) userinfo = common.NewUserInfo(session) if err := row.Scan(&userinfo.ID, &userinfo.Name, &userinfo.Pwd, &userinfo.RegDate); err == sql.ErrNoRows { //登陆失败,没有该用户信息 resmsg.AddArg("0") if err = session.Send(resmsg); err != nil { println("World Server Error: 发送登陆回馈信息 失败. ", err.Error()) logFile.Printf("World Server Error: 发送登陆回馈信息 失败. ", err.Error()) return } } } //找到该用户数据,开始验证密码信息 if userinfo.Name == msg.Args[0] && userinfo.Pwd == msg.Args[1] { //登陆成功 resmsg.AddArg("1") resmsg.AddArg(userinfo.GetIDbyString()) resmsg.AddArg(userinfo.Name) if err := session.Send(resmsg); err != nil { println("World Server Error: 发送登陆回馈信息 失败. ", err.Error()) logFile.Printf("World Server Error: 发送登陆回馈信息 失败. ", err.Error()) return } userinfo.State = 1 } else { //登陆失败 resmsg.AddArg("0") if err := session.Send(resmsg); err != nil { println("World Server Error: 发送登陆回馈信息 失败. ", err.Error()) logFile.Printf("World Server Error: 发送登陆回馈信息 失败. ", err.Error()) return } userinfo.State = 0 } if userinfo.State == 1 { //有用户上线,广播所有用户 bcMsg := msgcmd.NewCmd(msgcmd.MCMD_W2M_BROADCAST) bcMsg.AddArg(msgcmd.IntToString(msgcmd.ECMD_BROADCAST_USERLOGIN)) bcMsg.AddArg(userinfo.Name) this.Broadcast(bcMsg) } //保存当前用户所在的MS会话地址 userinfo.Session = session //添加到usermap中 if !isInUserMap { usermap.Insert(userinfo.Name, userinfo) } }