took up coding a small card game in PowerShell to familiarize myself with the software for work and it grew with more things I wanted to add. I thought about doing Multiplayer and stumbled across SSH. have been fiddling around with it for a while and it got me doubting myself a bit that it is even possible to do a multiplayer "game" in PowerShell. I have tried looking for anything related to multiplayer games in PowerShell and found little to none.
so here is my question.
Im setting up an ability to either Host/Join a game in the multiplayer section. selecting host checks if you have OpenSSH installed. installs it if you don't. sets a firewall exception. then pulls your user and IP together and gives that to the host to give to others to join off.
then with that ip the joiners enter it and get connected to the host and their files get shared.
problems I am facing:
Can only connect in LAN though this method. if port forwarding is required to be done in the router and it cant be scripted in PowerShell then that cant be done because the user cant be expected to do it.
When connected through SSH the script stops. this is due to the SSH opening a new command prompt that needs to be put back into PowerShell. how do I overcome this?
When transferring files Data between the computers starts. I need to make a check every interval for updates in the data to see if the other players have done anything. like finished their turn and its now mine. how would I make a check like that?
I'm very new to PowerShell and I understand that its intended purpose is not to make games. this is all for the sake of me learning its abilities in a fun way. if it cant be done it cant be done. and I know I'm asking allot right now. I just need some pointers cause I have no-one else to talk to about this cause no-one understands PowerShell where I work. Thank you all in advance
Here is the code I have so far regarding the SSH. keep in mind I'm not the best at all in this
new-Item -Path 'C:\Program Files' -Name "CardZdata" -ItemType directory
$path = 'C:\Program Files\CardZdata'
$hostippath = ($path + '\' + 'hostip' + '.xml')
#Main Menu
clear
Write-Output 'Gamemode Selection'
Write-Output '-------------------'
Write-Output 'Singleplayer [s]'
Write-Output 'Multiplayer SSH [m]'
Write-output 'Multiplayer LAN [l]'
$gamemode = read-host
if ($gamemode -eq 's')
{
clear
Write-Output 'Gamemode Selection'
Write-Output '-------------------'
Write-Output 'Blackjack [b]'
Write-Output 'Poker [p]'
}
elseif ($gamemode -eq 'm')
{
clear
$test = Get-WindowsCapability -Online | ? Name -like 'OpenSSH.Client*'
if ($test.State -eq "NotPresent")
{
Write-Output 'SSH (Client) is not installed on your PC. it is needed to connect to the server.'
Write-Output 'do you wish to install it? [y/n]'
$SSHchoice = Read-Host
if ($SSHchoice = 'y')
{
# Install the OpenSSH Client
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
}
else
{
Write-Output 'Sorry, you cant play Multiplayer without SSH.'
break
}
$test = Get-WindowsCapability -Online | ? Name -like 'OpenSSH.Client*'
if ($test.State -eq "NotPresent")
{
Write-Output "Error - Installation Failed"
break
}
}
clear
Write-Output 'Multiplayer SSH connects you to games hosted by others. or you can host them yourself.'
Write-Output 'Uninstall SHH client [u] !Warning!- You wont be able to connect to Multiplayer servers'
Write-Output ''
Write-Output 'Gamemode Selection'
Write-Output '-------------------'
Write-Output 'Host [h]'
Write-Output 'Join [j]'
$hj = Read-Host
if ($hj -eq 'h')
{
$test = Get-WindowsCapability -Online | ? Name -like 'OpenSSH.Server*'
if ($test.State -eq "NotPresent")
{
Write-Output 'SSH (Server) is not installed on your PC. it is needed to connect to the server.'
Write-Output 'do you wish to install it? [y/n]'
$SSHchoice = Read-Host
if ($SSHchoice = 'y')
{
# Install the OpenSSH Client
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
}
$test = Get-WindowsCapability -Online | ? Name -like 'OpenSSH.Server*'
if ($test.State -eq "NotPresent")
{
Write-Output "Error - Installation Failed"
break
}
else
{
Write-Output 'Sorry, you cant Host Multiplayer without the SSH Server.'
break
}
}
#StartMultiplayer
Write-Output '!Starting Multiplayer Server On Local Host!'
Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'
$test = Get-NetFirewallRule -Name sshd
if ($test.Name -ne "sshd")
{
Write-Output 'Setting Port'
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
}
Restart-Service sshd
Write-Output 'Server Set Up Successfully!'
$userip = (Get-NetIPAddress -AddressFamily IPv4 -SuffixOrigin Dhcp).IPAddress
Write-Output 'Give this to users:'
$hostip = ($env:UserName + "@" + $userip)
$hostip
$hostip | export-clixml $hostippath -force
Read-Host
}
if ($hj -eq 'j')
{
Write-Output 'Join Server'
Write-Output '-------------------'
Write-Output 'Server list [s] - WorkInProgress'
Write-Output 'Direct Connect [d]'
$sd = Read-Host
if ($sd -eq 's')
{
Write-Output 'why?'
Read-Host
}
if ($sd -eq 'd')
{
Write-Output 'Enter a code that is given by the Host'
Write-Output 'Example: [username@ipaddress]'
$hostip = read-host
$hostip | export-clixml $hostippath -force
}
}
Read-Host
}
elseif ($gamemode -eq 'l')
{
clear
Write-Output 'Gamemode Selection'
Write-Output '-------------------'
}
Here is the main area:
#StartMultiplayer
Write-Output '!Starting Multiplayer Server On Local Host!'
Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'
$test = Get-NetFirewallRule -Name sshd
if ($test.Name -ne "sshd")
{
Write-Output 'Setting Port'
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
}
Restart-Service sshd
Write-Output 'Server Set Up Successfully!'
$userip = (Get-NetIPAddress -AddressFamily IPv4 -SuffixOrigin Dhcp).IPAddress
Write-Output 'Give this to users:'
$hostip = ($env:UserName + "@" + $userip)
$hostip
$hostip | export-clixml $hostippath -force
Read-Host