Skip to content

Commit

Permalink
Merge pull request #23 from FN-FAL113/Dev/un-cluster-feature
Browse files Browse the repository at this point in the history
Un/cluster server feature implemented
  • Loading branch information
FN-FAL113 committed Oct 25, 2023
2 parents 659f604 + ce30b87 commit e68e951
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 45 deletions.
33 changes: 23 additions & 10 deletions CS2ServerPicker/App.Designer.vb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions CS2ServerPicker/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<setting name="Server_Revision" serializeAs="String">
<value />
</setting>
<setting name="Is_Clustered" serializeAs="String">
<value>True</value>
</setting>
</CS2ServerPicker.My.MySettings>
</userSettings>
</configuration>
86 changes: 71 additions & 15 deletions CS2ServerPicker/App.vb
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
Public Class App

Private serverDict As New Dictionary(Of String, String)
Private serverDictClustered As New Dictionary(Of String, String)

Private serverDictUnclustered As New Dictionary(Of String, String)

Private pingObjsDict As New Dictionary(Of String, Net.NetworkInformation.Ping)

Public pendingOperation As Boolean = False
Private pendingOperation As Boolean = False

Private isClustered As Boolean = My.Settings.Is_Clustered

Public Function Get_Server_Dictionary() As Dictionary(Of String, String)
Return serverDict
Public Function Get_Server_Dictionary_Clustered() As Dictionary(Of String, String)
Return serverDictClustered
End Function

Public Function Get_Server_Dictionary_Unclustered() As Dictionary(Of String, String)
Return serverDictUnclustered
End Function

Public Function Get_Ping_Objects_Dictionary() As Dictionary(Of String, Net.NetworkInformation.Ping)
Return pingObjsDict
End Function

Public Function Get_DataGridView_Control() As DataGridView
Return MainDataGridView
Public Function Get_Pending_Operation() As Boolean
Return pendingOperation
End Function

Public Sub Set_Pending_Operation(bool As Boolean)
Expand All @@ -24,6 +32,18 @@
ProgBar.Visible = bool
End Sub

Public Function Get_Is_Clustered() As Boolean
Return isClustered
End Function

Public Sub Set_Is_Clustered(bool As Boolean)
isClustered = bool
End Sub

Public Function Get_DataGridView_Control() As DataGridView
Return MainDataGridView
End Function

Private Sub RefreshButton_Click(sender As Object, e As EventArgs) Handles RefreshButton.Click
Ping_All_Servers()
End Sub
Expand All @@ -35,7 +55,7 @@
Next
End Sub

Private Sub App_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Private Async Sub App_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim serverRevision As String = Fetch_Server_Data()

' if app failed to retrieve server data
Expand All @@ -52,26 +72,32 @@
My.Settings.Save()
My.Settings.Reload()

Should_Block_All_Servers(False)
Await Should_Block_All_Servers(False)
Else
Ping_All_Servers()
End If

If isClustered Then
ClusterButton.Text = "Uncluster"
Else
ClusterButton.Text = "Cluster"
End If
End Sub

Private Sub App_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Cancel_Pending_Ping()
End Sub

Private Sub BlockAllButton_Click(sender As Object, e As EventArgs) Handles BlockAllButton.Click
Should_Block_All_Servers(True)
Private Async Sub BlockAllButton_Click(sender As Object, e As EventArgs) Handles BlockAllButton.Click
Await Should_Block_All_Servers(True)
End Sub

Private Sub BlockSelectedButton_Click(sender As Object, e As EventArgs) Handles BlockSelectedButton.Click
Should_Block_Selected_Servers(True)
Private Async Sub UnblockAllButton_Click(sender As Object, e As EventArgs) Handles UnblockAllButton.Click
Await Should_Block_All_Servers(False)
End Sub

Private Sub UnblockAllButton_Click(sender As Object, e As EventArgs) Handles UnblockAllButton.Click
Should_Block_All_Servers(False)
Private Sub BlockSelectedButton_Click(sender As Object, e As EventArgs) Handles BlockSelectedButton.Click
Should_Block_Selected_Servers(True)
End Sub

Private Sub UnblockSelectedButton_Click(sender As Object, e As EventArgs) Handles UnblockSelectedButton.Click
Expand All @@ -91,10 +117,11 @@
"Note:" + Environment.NewLine +
" - Blocked servers will be automatically unblocked if server data is updated on next launch." + Environment.NewLine +
" - You may ping single or multiple selected server/s by double clicking on the highlighted region." + Environment.NewLine +
" - Servers that get un/clustered are China, India, Japan, Stockholm." + Environment.NewLine +
Environment.NewLine +
"Author: FN-FAL113 (github username)" + Environment.NewLine +
"License: GNU General Public License V3" + Environment.NewLine +
"App Version: 2.0.3",
"App Version: 2.0.4",
"App Info"
)
End Sub
Expand All @@ -118,4 +145,33 @@
Ping_Servers(MainDataGridView.SelectedRows)
End Sub

Private Async Sub ClusterButton_Click(sender As Object, e As EventArgs) Handles ClusterButton.Click
If pendingOperation Then
MessageBox.Show("Operation in progress, please wait a moment...")

Return
End If

MessageBox.Show("App will unblock all servers before " + IIf(isClustered, "unclustering", "clustering") + " servers. Please standby...", "Info")

Await Should_Block_All_Servers(False, False)

Clear_DataGridView_Rows()

If isClustered Then
ClusterButton.Text = "Cluster"

isClustered = False
Else
ClusterButton.Text = "Uncluster"

isClustered = True
End If

Load_Server_List()

My.Settings.Is_Clustered = isClustered
My.Settings.Save()
My.Settings.Reload()
End Sub
End Class
4 changes: 2 additions & 2 deletions CS2ServerPicker/My Project/AssemblyInfo.vb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ Imports System.Runtime.InteropServices
' by using the '*' as shown below:
' <Assembly: AssemblyVersion("1.0.*")>

<Assembly: AssemblyVersion("2.0.3.0")>
<Assembly: AssemblyFileVersion("2.0.3.0")>
<Assembly: AssemblyVersion("2.0.4.0")>
<Assembly: AssemblyFileVersion("2.0.4.0")>
<Assembly: NeutralResourcesLanguage("en")>
12 changes: 12 additions & 0 deletions CS2ServerPicker/My Project/Settings.Designer.vb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions CS2ServerPicker/My Project/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
<Setting Name="Server_Revision" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="Is_Clustered" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
</Settings>
</SettingsFile>
8 changes: 7 additions & 1 deletion CS2ServerPicker/Services/DataGridViewService.vb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
Public Sub Load_Server_List()
Dim i As Integer = 0

Dim serverDict As Dictionary(Of String, String) = IIf(App.Get_Is_Clustered(), App.Get_Server_Dictionary_Clustered(), App.Get_Server_Dictionary_Unclustered)

' display each server name key value from server dictionary in the datagridview control
For Each kvp As KeyValuePair(Of String, String) In App.Get_Server_Dictionary()
For Each kvp As KeyValuePair(Of String, String) In serverDict
App.Get_DataGridView_Control().Rows().Add()
App.Get_DataGridView_Control().Rows(i).Cells(0).Value = kvp.Key

Expand All @@ -16,4 +18,8 @@
row.Cells(1).Value = String.Empty
Next
End Sub

Public Sub Clear_DataGridView_Rows()
App.Get_DataGridView_Control.Rows.Clear()
End Sub
End Module
8 changes: 6 additions & 2 deletions CS2ServerPicker/Services/PingService.vb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
Public Async Sub Ping_Servers(dgRows As DataGridViewSelectedRowCollection)
' DO NOT call this procedure in a separate thread
' it references UI controls which can only be accessed in the UI thread
Dim serverDict As Dictionary(Of String, String) = IIf(App.Get_Is_Clustered(), App.Get_Server_Dictionary_Clustered(), App.Get_Server_Dictionary_Unclustered())

For Each dgRow As DataGridViewRow In dgRows
Dim serverName As String = dgRow.Cells(0).Value.ToString()
Dim address = App.Get_Server_Dictionary().Item(serverName).Split(",")(0)
Dim address = serverDict.Item(serverName).Split(",")(0)

Cancel_Pending_Ping(serverName)

Expand All @@ -24,8 +26,10 @@

Cancel_Pending_Ping()

Dim serverDict As Dictionary(Of String, String) = IIf(App.Get_Is_Clustered(), App.Get_Server_Dictionary_Clustered(), App.Get_Server_Dictionary_Unclustered())

For Each dgRow As DataGridViewRow In App.Get_DataGridView_Control().Rows()
For Each address As String In App.Get_Server_Dictionary().Item(dgRow.Cells(0).Value).Split(",")
For Each address As String In serverDict.Item(dgRow.Cells(0).Value).Split(",")
' replace host id from ip address since this will be traversed from 0 to max 8 bit value
Dim addressNoHostValue As String = address.Remove(address.LastIndexOf(".") + 1, address.Split(".")(3).Length)

Expand Down
28 changes: 16 additions & 12 deletions CS2ServerPicker/Services/ServerService.vb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ Module ServerService
For Each clusterKvp As KeyValuePair(Of String, String) In clusterDict ' traverse every cluster
For Each clusterValue As String In clusterKvp.Value.Split(",") ' traverse cluster comma-separated values
If serverName.Contains(clusterValue) Then
If Not App.Get_Server_Dictionary().ContainsKey(clusterKvp.Key) Then ' initialize cluster with values
App.Get_Server_Dictionary().Add(clusterKvp.Key, String.Join(",", ipArr))
Else ' concatenate server ip to cluter
App.Get_Server_Dictionary().Item(clusterKvp.Key) = App.Get_Server_Dictionary().Item(clusterKvp.Key) _
If Not App.Get_Server_Dictionary_Clustered().ContainsKey(clusterKvp.Key) Then ' initialize cluster with values
App.Get_Server_Dictionary_Clustered().Add(clusterKvp.Key, String.Join(",", ipArr))
Else ' concatenate server ip to cluster
App.Get_Server_Dictionary_Clustered().Item(clusterKvp.Key) = App.Get_Server_Dictionary_Clustered().Item(clusterKvp.Key) _
+ "," + String.Join(",", ipArr)
End If

Expand All @@ -62,9 +62,11 @@ Module ServerService
Next
Next

App.Get_Server_Dictionary_Unclustered().Add(serverName, String.Join(",", ipArr))

' server is not part of clustered servers
If Not serverIsClustered Then
App.Get_Server_Dictionary().Add(serverName, String.Join(",", ipArr))
App.Get_Server_Dictionary_Clustered().Add(serverName, String.Join(",", ipArr))
End If
End If
Next
Expand All @@ -79,11 +81,11 @@ Module ServerService

Public Async Sub Should_Block_Selected_Servers(block As Boolean)
Dim MainDataGridView As DataGridView = App.Get_DataGridView_Control()
Dim serverDictionary As Dictionary(Of String, String) = App.Get_Server_Dictionary()
Dim serverDictionary As Dictionary(Of String, String) = IIf(App.Get_Is_Clustered(), App.Get_Server_Dictionary_Clustered(), App.Get_Server_Dictionary_Unclustered())

Dim selectedRows As DataGridViewSelectedRowCollection = MainDataGridView.SelectedRows

If App.pendingOperation Then
If App.Get_Pending_Operation() Then
MessageBox.Show("Operation in progress, please wait a moment...")

Return
Expand Down Expand Up @@ -138,11 +140,11 @@ Module ServerService
proc.Dispose()
End Sub

Public Async Sub Should_Block_All_Servers(block As Boolean)
Dim serverDictionary As Dictionary(Of String, String) = App.Get_Server_Dictionary()
Public Async Function Should_Block_All_Servers(block As Boolean, Optional pingServers As Boolean = True) As Task
Dim serverDictionary As Dictionary(Of String, String) = IIf(App.Get_Is_Clustered(), App.Get_Server_Dictionary_Clustered(), App.Get_Server_Dictionary_Unclustered())
Dim MainDataGridView As DataGridView = App.Get_DataGridView_Control()

If App.pendingOperation Then
If App.Get_Pending_Operation() Then
MessageBox.Show("Operation in progress, please wait a moment...")

Return
Expand All @@ -157,8 +159,10 @@ Module ServerService

App.Set_Pending_Operation(False)

Ping_All_Servers()
End Sub
If pingServers Then
Ping_All_Servers()
End If
End Function

Private Sub Handle_All_Server_Block_Unblock(MainDataGridView As DataGridView, ServerDictionary As Dictionary(Of String, String), block As Boolean)
Dim proc As Process = Create_Custom_CMD_Process()
Expand Down
Loading

0 comments on commit e68e951

Please sign in to comment.