public override void Execute(Protocol protocol, ClientToken client) { if (protocol.GetType() != typeof(LoginProtocol)) { return; } LoginProtocol loginProtocol = protocol as LoginProtocol; Console.WriteLine("登录服务运行,账号为:" + loginProtocol.userId + ",密码为:" + loginProtocol.userPassword + ";"); AccountDatabase accountDatabase = server.databases[typeof(AccountDatabase)] as AccountDatabase; //查询是否有用户名密码相符的用户 if (accountDatabase.Find(x => (x.Id == loginProtocol.userId && x.Password == loginProtocol.userPassword)) != null) { //登录成功 LoginSuccessProtocol loginSuccessProtocol = new LoginSuccessProtocol(); //生成令牌 loginSuccessProtocol.PassBadge = 0; Byte[] serializedProtocol = ProtocolHelper.ConvertProtocolToBytes(loginSuccessProtocol); Byte[] encodingData = GameProtocol.Encoder.Encode(serializedProtocol); client.WriteSendData(encodingData); } else { //登录失败 LoginFailedProtocol loginFailedProtocol = new LoginFailedProtocol(); if (accountDatabase.Find(x => (x.Id == loginProtocol.userId)) != null) { loginFailedProtocol.State = LoginFailedState.WrongPassword; } else { loginFailedProtocol.State = LoginFailedState.WrongID; } Byte[] serializedProtocol = ProtocolHelper.ConvertProtocolToBytes(loginFailedProtocol); Byte[] encodingData = GameProtocol.Encoder.Encode(serializedProtocol); client.WriteSendData(encodingData); } }
public override void Execute(Protocol protocol, ClientToken client) { if (protocol.GetType() != typeof(RegisterProtocol)) { return; } RegisterProtocol registerProtocol = protocol as RegisterProtocol; Console.WriteLine("注册服务运行,账号为:" + registerProtocol.userId + ",密码为:" + registerProtocol.userPassword + ";"); AccountDatabase accountDatabase = server.databases[typeof(AccountDatabase)] as AccountDatabase; //检查是否ID已经被注册 if (accountDatabase.Find(x => (x.Id == registerProtocol.userId)) != null) { //已被注册,返回注册失败 RegisterFailedProtocol registerFailedProtocol = new RegisterFailedProtocol(); registerFailedProtocol.state = RegisterFailedState.IDAlreadyExist; Byte[] serializedProtocol = ProtocolHelper.ConvertProtocolToBytes(registerFailedProtocol); Byte[] encodingData = GameProtocol.Encoder.Encode(serializedProtocol); client.WriteSendData(encodingData); } else { //注册成功 RegisterSuccessProtocol registerSuccessProtocol = new RegisterSuccessProtocol(); registerSuccessProtocol.PassBadge = 0; //写入数据库 accountDatabase.Write(new Account() { Id = registerProtocol.userId, Password = registerProtocol.userPassword }); Byte[] serializedProtocol = ProtocolHelper.ConvertProtocolToBytes(registerSuccessProtocol); Byte[] encodingData = GameProtocol.Encoder.Encode(serializedProtocol); client.WriteSendData(encodingData); } }
private void RegisterDatabase() { //注册账号数据库 databases[typeof(AccountDatabase)] = new AccountDatabase(); }