/// <summary> /// Will close the current connection to TW2 /// </summary> /// <returns>Was the connection closed successfully</returns> public async Task <bool> CloseConnection() { IsClosingConnection = true; if (_keepAlive) { LastMsgSendTimer.Stop(); } if (IsConnected) { AddToConnectionLog("Closing connection"); if (Client != null) { var result = await Client?.Stop(WebSocketCloseStatus.NormalClosure, "Closed by command"); } } else { return(false); } PingCount = 0; ClientHasBeenSetup = false; DataEvents.InvokeConnectionStatus(false); IsClosingConnection = false; return(true); }
/// <summary> /// Will attempt to login with the account provided, if in test-mode then the connection will be closed immediately after. /// </summary> /// <param name="account">The account with which to login</param> /// <param name="testMode">Will close the connection immediately after if true</param> /// <returns>The connection result</returns> public async Task <ConnectResult> LoginWithAccount(Account account, bool testMode = false) { // Make sure to reset the connection and deleting it. await _socketManager.StopConnection(true); ConnectResult result = await _socketManager.StartConnection(account.ToConnectData()); if (!result.IsConnected) { return(result); } result = await SendAccountAuthentication(account.ToConnectData()); await SendSystemIdentify(); result.IsConnected = await SendCharacterSelect(account.DefaultCharacter); if (testMode) { await _socketManager.StopConnection(true); } else { DataEvents.InvokeConnectionStatus(true); } return(result); }
public async Task <ConnectResult> SendAccountAuthentication(ConnectData connectData) { var response = new SocketMessage(); if (_socketManager.IsReconnecting) { response = await SendStandardMessage( RouteProvider.AUTHENTICATION_RECONNECT, RouteProvider.AuthenticationReconnect(connectData, _socketManager.ActiveCharacterId)); } else { response = await SendStandardMessage(RouteProvider.LOGIN, RouteProvider.Login(connectData)); } var loginData = SocketUtilities.ParseDataFromResponse <LoginDataDTO>(response.Response); ConnectResult connectResult = _socketManager.GetConnectResult(); connectResult.IsConnected = true; connectResult.AccessToken = loginData?.AccessToken; connectResult.TW2AccountId = loginData?.PlayerId; _socketManager.ConnectData.AccessToken = loginData?.AccessToken; DataEvents.InvokeLoginDataAvailable(loginData); DataEvents.InvokeConnectionResult(connectResult); return(connectResult); }
private async Task <bool> SendGetGroups() { var response = await SendStandardMessage(RouteProvider.GET_GROUPS); var groups = SocketUtilities.ParseDataFromResponse <GroupsDTO>(response.Response)?.Groups?.ToList(); DataEvents.InvokeGroupsDataAvailable(groups.ToList <IGroup>()); return(groups.Count > 0); }
private void Filter_Click(object sender, EventArgs e) { if (flag == true) { DataEvents.Sort(DataEvents.Columns[2], ListSortDirection.Ascending); } if (flag == false) { DataEvents.Sort(DataEvents.Columns[2], ListSortDirection.Descending); } flag = !flag; }
private void DataEvents_MouseClick(object sender, MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Right) { var h = DataEvents.HitTest(e.X, e.Y); Point a = new Point(e.X, e.Y); if (h.Type == DataGridViewHitTestType.Cell) { contextMenu.Show(DataEvents, a); Position[0] = h.RowIndex; Position[1] = h.ColumnIndex; } } }
public void UpdateLoginData(ILoginData loginData) { using (var db = GetDBContext()) { // Update the logged in account Account account = GetAccount(loginData.Name); if (account != null) { db.Attach(account); account.TW2AccountID = loginData.PlayerId; account.IsConfirmed = true; db.SaveChanges(); } } ParseWorlds(loginData); ParseCharacters(loginData); DataEvents.InvokeLoginDataIsUpdated(); }
private async Task <bool> SendGetCharacterInfo() { var response = await SendStandardMessage(RouteProvider.CHARACTER_GETINFO); var characterData = SocketUtilities.ParseDataFromResponse <CharacterDataDTO>(response.Response); if (characterData != null) { foreach (IVillage village in characterData.Villages) { village.CharacterId = _socketManager.ActiveCharacterId; village.WorldId = _socketManager.ActiveWorldId; } DataEvents.InvokeCharacterDataAvailable(characterData); } // Take the first village as the active village, its unclear how TW2 determines te active village. // This is done somewhere in javascript but not communicated through websockets. // TODO Find out how the active village is determined activeVillage = characterData.Villages[0]; return(characterData != null); }
public bool UpdateVillages(List <IVillage> villages) { if (villages.Count == 0) { return(false); } using (var db = GetDBContext()) { // It is assumed that all incoming villages always are on the same world. var world = new World(); if (!string.IsNullOrEmpty(villages[0].WorldId)) { world = db.Worlds.Find(villages[0].WorldId); } foreach (IVillage village in villages) { var villageEntity = _mapper.Map <Village>(village); int characterId = villageEntity.CharacterId ?? default; var entity = db.Villages.FirstOrDefault(item => item.Id == villageEntity.Id); if (entity != null) { if (entity == villageEntity) { continue; } db.Update(entity); // Make changes on entity entity.Name = villageEntity.Name; entity.X = villageEntity.X; entity.Y = villageEntity.Y; entity.Points = villageEntity.Points; } else { entity = villageEntity; db.Add(entity); } //Relationships entity.World = world; entity.WorldId = world.WorldId; if (entity.CharacterId != null) { entity.Character = db.Characters.FirstOrDefault( x => x.CharacterId == characterId && x.WorldId == entity.WorldId); } } db.SaveChanges(); } DataEvents.InvokeVillagesUpdated(); return(true); }
public SocketOperation(params DataEvent[] DataEvents) { Operations = DataEvents.ToList(); }
public void StopConnection() { DataEvents.InvokeConnectionStatus(false); StopConnectionEvent?.Invoke(this, EventArgs.Empty); }
public void SetConnectionResult(ConnectResult connectResult) { DataEvents.InvokeConnectionResult(connectResult); }
public NetworkMessage(DataEvents messageType, object message) { Sender = GlobalConfigInfo.ThisNode; MessageType = messageType; Message = message; }