protected override bool OnHandleMessage(IMessage message, PhotonServerPeer serverPeer) { var para = new Dictionary <byte, object> { { (byte)ClientParameterCode.PeerId, message.Parameters[(byte)ClientParameterCode.PeerId] }, { (byte)ClientParameterCode.SubOperationCode, message.Parameters[(byte)ClientParameterCode.SubOperationCode] } }; var operation = new CreateCharacter(serverPeer.Protocol, message); if (!operation.IsValid) { Log.Error(operation.GetErrorMessage()); serverPeer.SendOperationResponse(new OperationResponse(message.Code) { ReturnCode = (int)ErrorCode.OperationInvalid, DebugMessage = operation.GetErrorMessage(), Parameters = para }, new SendParameters()); return(true); } try { using (var session = NHibernateHelper.OpenSession()) { using (var transaction = session.BeginTransaction()) { var user = session.QueryOver <User>().Where(u => u.Id == operation.UserId).List().FirstOrDefault(); var profile = session.QueryOver <UserProfile>().Where(up => up.UserId == user).List().FirstOrDefault(); var characters = session.QueryOver <ComplexCharacter>().Where(cc => cc.UserId == user).List(); if (profile != null && profile.CharacterSlots <= characters.Count) { serverPeer.SendOperationResponse(new OperationResponse(message.Code) { ReturnCode = (int)ErrorCode.InvalidCharacter, DebugMessage = "No free character slots", Parameters = para }, new SendParameters()); } else { var createCharacter = SerializeUtil.Deserialize <CharacterCreateDetails>(operation.CharacterCreateDetails); Server.Log.DebugFormat("NEW CHAR: NAME: {0} / SEX: {1} / CLASS: {2}", createCharacter.CharacterName, createCharacter.Sex, createCharacter.CharacterClass); var character = session.QueryOver <ComplexCharacter>().Where(cc => cc.Name == createCharacter.CharacterName).List().FirstOrDefault(); if (character != null) { transaction.Commit(); serverPeer.SendOperationResponse(new OperationResponse(message.Code) { ReturnCode = (int)ErrorCode.InvalidCharacter, DebugMessage = "Character name taken", Parameters = para }, new SendParameters()); } else { var newChar = new ComplexCharacter() { UserId = user, Name = createCharacter.CharacterName, Class = createCharacter.CharacterClass, Sex = createCharacter.Sex, Level = 0, }; session.Save(newChar); transaction.Commit(); serverPeer.SendOperationResponse(new OperationResponse(message.Code) { ReturnCode = (int)ErrorCode.OK, DebugMessage = "Character successfuly created", Parameters = para }, new SendParameters()); } } } } return(true); } catch (Exception e) { Log.Error(e); serverPeer.SendOperationResponse(new OperationResponse(message.Code) { ReturnCode = (int)ErrorCode.InvalidCharacter, DebugMessage = e.ToString(), Parameters = para }, new SendParameters()); } return(true); }
protected override bool OnHandleMessage(IMessage message, PhotonServerPeer serverPeer) { Log.DebugFormat("on handle in login server create character handler hit"); var para = new Dictionary <byte, object> { { (byte)ClientParameterCode.PeerId, message.Parameters[(byte)ClientParameterCode.PeerId] }, { (byte)ClientParameterCode.SubOperationCode, message.Parameters[(byte)ClientParameterCode.SubOperationCode] } }; var operation = new CreateCharacter(serverPeer.Protocol, message); if (!operation.IsValid) { Log.DebugFormat("operation invalid"); serverPeer.SendOperationResponse( new OperationResponse(message.Code) { ReturnCode = (int)ErrorCode.OperationInvalid, DebugMessage = operation.GetErrorMessage(), Parameters = para }, new SendParameters()); return(true); } try { using (var session = NHibernateHelper.OpenSession()) { using (var transaction = session.BeginTransaction()) { var user = session.QueryOver <User>().Where(u => u.Id == operation.UserId).List().FirstOrDefault(); var profile = session.QueryOver <UserProfile>().Where(up => up.UserId == user).List().FirstOrDefault(); var characters = session.QueryOver <ComplexCharacter>().Where(cc => cc.UserId == user).List(); if (profile != null && profile.CharacterSlots <= characters.Count) { Log.DebugFormat("profile invalid or no slots"); serverPeer.SendOperationResponse( new OperationResponse(message.Code) { ReturnCode = (int)ErrorCode.InvalidCharacter, DebugMessage = "No free character slots", Parameters = para }, new SendParameters()); } else { var mySerializer = new XmlSerializer(typeof(CharacterCreateDetails)); var reader = new StringReader(operation.CharacterCreateDetails); var createCharacter = (CharacterCreateDetails)mySerializer.Deserialize(reader); var character = session.QueryOver <ComplexCharacter>() .Where(cc => cc.Name == createCharacter.CharacterName).List().FirstOrDefault(); if (character != null) { Log.DebugFormat("null character"); transaction.Commit(); serverPeer.SendOperationResponse( new OperationResponse(message.Code) { ReturnCode = (int)ErrorCode.InvalidCharacter, DebugMessage = "Character name taken", Parameters = para }, new SendParameters()); } else { Log.DebugFormat("creating character"); var newChar = new ComplexCharacter { UserId = user, Name = createCharacter.CharacterName, Class = createCharacter.CharacterClass, Sex = createCharacter.Sex, Level = 1 }; session.Save(newChar); transaction.Commit(); serverPeer.SendOperationResponse( new OperationResponse(message.Code) { ReturnCode = (int)ErrorCode.Ok, Parameters = para }, new SendParameters()); } } } } return(true); } catch (Exception e) { Log.DebugFormat("invalid character"); Log.Error(e); serverPeer.SendOperationResponse( new OperationResponse(message.Code) { ReturnCode = (int)ErrorCode.InvalidCharacter, DebugMessage = e.ToString(), Parameters = para }, new SendParameters()); } return(true); }
protected virtual GameOperationResponse HandleOperationCreateCharacter(GameOperationRequest operationRequest, MessageParameters parameters) { var operation = new CreateCharacter(this.peer.Protocol, operationRequest); if (!operation.IsValid) { return(operation.GetErrorResponse((short)ResultCode.InvalidOperationParameter, operation.GetErrorMessage())); } ThreadPool.QueueUserWorkItem( o => this.ExecUserOperation(() => this.HandleCreateCharacter(operationRequest.ClientId, operation), operationRequest.ClientId, parameters)); return(null); }