//--------------------------------------------------------------------- // Cell->Cell的请求 async Task ICellPlayer.s2sBotEnterWorld(string player_etguid) { NewPlayerInfo new_player_info = new NewPlayerInfo(); new_player_info.account_id = ""; new_player_info.account_name = ""; new_player_info.gender = true; new_player_info.is_bot = true; new_player_info.et_player_guid = player_etguid; var grain = GF.GetGrain <ICellPlayer>(new Guid(player_etguid)); await grain.c2sEnterWorld(new_player_info); }
//--------------------------------------------------------------------- // Client->Cell的请求 async Task <EntityData> ICellPlayer.c2sEnterWorld(NewPlayerInfo new_player_info) { Logger.Info("c2sEnterWorld() GrainId={0}", this.GetPrimaryKey().ToString()); // 新建角色 if (EtPlayer == null) { if (AsyncStream == null) { IStreamProvider stream_provider = GetStreamProvider(StringDef.SMSProvider); AsyncStream = stream_provider.GetStream <StreamData>(this.GetPrimaryKey(), "Friend"); } if (CouchbaseQue == null) { CouchbaseQue = new CouchbaseQue("EtPlayer", new_player_info.et_player_guid); } bool exist = true; ulong player_id = 100; //do //{ // player_id = (ulong)CellApp.Instance.Rd.next(1000000, 9999999); // var grain_playerservice = GF.GetGrain<ICellPlayerService>(0); // exist = await grain_playerservice.playerIdExist(player_id); //} while (exist); EbLog.Note("-------------------------------"); EbLog.Note("新创建玩家"); EbLog.Note("AccountName=" + new_player_info.account_name); EbLog.Note("PlayerId=" + player_id); EbLog.Note("-------------------------------"); var et_player_data = new EntityData(); et_player_data.entity_type = typeof(EtPlayer).Name; et_player_data.entity_guid = new_player_info.et_player_guid; et_player_data.list_component = new List <ComponentData>(); et_player_data.cache_data = new Dictionary <string, object>(); et_player_data.cache_data["NewPlayer"] = true; et_player_data.cache_data["NewPlayerInfo"] = new_player_info; et_player_data.cache_data["NewPlayerId"] = player_id; EtPlayer = EntityMgr.Instance.genEntity <EtPlayer, GrainCellPlayer>(et_player_data, this); State = EtPlayer.genEntityData4SaveDb(); await WriteStateAsync(); } return(EtPlayer.genEntityData4NetSync((byte)NodeType.Client)); }
//--------------------------------------------------------------------- // 客户端请求 async Task <MethodData> ICellClientService.c2sRequest(MethodData method_data) { MethodData result = new MethodData(); result.method_id = MethodType.None; var account_request = EbTool.protobufDeserialize <AccountRequest>(method_data.param1); switch (account_request.id) { case AccountRequestId.EnterWorld: // 请求进入游戏世界 { var enterworld_request = EbTool.protobufDeserialize <ClientEnterWorldRequest>(account_request.data); string info = string.Format("客户端请求进入游戏世界\nAccId={0}, AccName={1}, Token={2}", enterworld_request.acc_id, enterworld_request.acc_name, enterworld_request.token); Logger.Info(info); ClientEnterWorldResponse enterworld_response = new ClientEnterWorldResponse(); enterworld_response.result = ProtocolResult.Failed; enterworld_response.acc_id = enterworld_request.acc_id; enterworld_response.acc_name = enterworld_request.acc_name; enterworld_response.token = enterworld_request.token; AppVerifyAccountInfo app_verifyaccount_request = new AppVerifyAccountInfo(); app_verifyaccount_request.AppId = ConfigurationManager.AppSettings["UCenterAppId"]; app_verifyaccount_request.AppSecret = ConfigurationManager.AppSettings["UCenterAppSecret"]; app_verifyaccount_request.AccountId = enterworld_request.acc_id; app_verifyaccount_request.AccountToken = enterworld_request.token; // 去UCenter验证Account AppVerifyAccountResponse app_verifyaccount_response = null; try { app_verifyaccount_response = await CoUCenterSDK.AppVerifyAccountAsync(app_verifyaccount_request); } catch (Exception ex) { Logger.Info(ex.ToString()); enterworld_response.result = ProtocolResult.EnterWorldAccountVerifyFailed; goto End; } // 去UCenter获取Account对应的AppData AppAccountDataResponse app_data_response = null; try { AppAccountDataInfo app_data_info = new AppAccountDataInfo(); app_data_info.AppId = app_verifyaccount_request.AppId; app_data_info.AppSecret = app_verifyaccount_request.AppSecret; app_data_info.AccountId = app_verifyaccount_request.AccountId; app_data_info.Data = null; app_data_response = await CoUCenterSDK.AppReadAccountDataAsync(app_data_info); } catch (Exception ex) { Logger.Info(ex.ToString()); enterworld_response.result = ProtocolResult.EnterWorldAccountVerifyFailed; goto End; } // 检测Token是否过期 //TimeSpan ts = app_verifyaccount_response.now_dt - app_verifyaccount_response.last_login_dt; //if (ts.TotalSeconds > 60) //{ // enterworld_response.result = ProtocolResult.EnterWorldTokenExpire; // goto End; //} // PlayerEtGuid string new_player_etguid = ""; if (app_data_response != null && !string.IsNullOrEmpty(app_data_response.Data)) { var data_read = EbTool.jsonDeserialize <Dictionary <string, string> >(app_data_response.Data); new_player_etguid = data_read["player_etguid"]; } if (string.IsNullOrEmpty(new_player_etguid)) { // 玩家角色未创建 new_player_etguid = Guid.NewGuid().ToString(); // 去UCenter写入Account对应的AppData try { Dictionary <string, string> data_write = new Dictionary <string, string>(); data_write["player_etguid"] = new_player_etguid; AppAccountDataInfo app_data_info = new AppAccountDataInfo(); app_data_info.AppId = app_verifyaccount_request.AppId; app_data_info.AppSecret = app_verifyaccount_request.AppSecret; app_data_info.AccountId = app_verifyaccount_request.AccountId; app_data_info.Data = EbTool.jsonSerialize(data_write); await CoUCenterSDK.AppWriteAccountDataAsync(app_data_info); } catch (Exception ex) { Logger.Info(ex.ToString()); enterworld_response.result = ProtocolResult.EnterWorldAccountVerifyFailed; goto End; } } NewPlayerInfo new_player_info = new NewPlayerInfo(); new_player_info.account_id = enterworld_request.acc_id; new_player_info.account_name = enterworld_request.acc_name; new_player_info.gender = true; new_player_info.is_bot = false; new_player_info.et_player_guid = new_player_etguid; // 角色进入游戏 ICellPlayer grain_player = GrainFactory.GetGrain <ICellPlayer>(new Guid(new_player_etguid)); enterworld_response.et_player_data = await grain_player.c2sEnterWorld(new_player_info); enterworld_response.result = ProtocolResult.Success; End: Ps.AccountResponse account_response; account_response.id = AccountResponseId.EnterWorld; account_response.data = EbTool.protobufSerialize <ClientEnterWorldResponse>(enterworld_response); result.method_id = MethodType.s2cAccountResponse; result.param1 = EbTool.protobufSerialize <Ps.AccountResponse>(account_response); } break; default: break; } return(result); }