From 847a333d60bb4afa17a01a6f987d00934a85e872 Mon Sep 17 00:00:00 2001 From: bluesaxman Date: Wed, 2 Sep 2020 15:26:46 -0600 Subject: [PATCH] Codebase Nuke... things got too tangled --- Server/server.pl | 109 +++++++++++++++++++++++++++++------------------ UI/index.html | 63 ++++++++++++++++++--------- 2 files changed, 110 insertions(+), 62 deletions(-) diff --git a/Server/server.pl b/Server/server.pl index 42775f3..a4fa8b2 100755 --- a/Server/server.pl +++ b/Server/server.pl @@ -5,7 +5,10 @@ use POSIX; use Net::WebSocket::Server; use JSON; -my $origin = 'ws://localhost'; +use Data::Dumper; + +#########################Game Logic starts here########################## + my %sessions = (); sub generateSession { @@ -17,7 +20,7 @@ sub generateSession { print "Adding session ".$newSession." to database\n"; $sessions{$newSession} = (); $sessions{$newSession}{"id"} = $newSession; - $sessions{$newSession}{"users"} = []; + $sessions{$newSession}{"users"} = (); $sessions{$newSession}{"decks"} = (); $sessions{$newSession}{"pools"} = (); @@ -25,33 +28,65 @@ sub generateSession { return $newSession; } -sub joinSession { - my ($sessionID, $conn) = @_; - print "Connection attempts to use session ".$sessionID."\n"; - unless ( grep( /$sessionID/, keys %sessions ) ) { - print "Session ".$sessionID." does not yet exsist, creating...\n"; - $sessionID = generateSession($sessionID); - } - push(@{$sessions{$sessionID}{"users"}}, $conn->{"initHand"}->res->key); - $conn->{"currentSession"} = $sessions{$sessionID}; - print "A client has connected to session: ".$conn->{"currentSession"}{"id"}."\n"; +sub generateDeck { + $DDF = from_json(shift); +# Add proper handling for reading of DDFs } -#sub shuffleDeck { -# my @deck = @_; -# my $index = 0; -# for (@deck) { -# my $swapCardIndex = floor(rand() * @deck); -# my $swapCard = @deck[$swapCardIndex]; -# @deck[$swapCardIndex] = $_; -# $deck[$index] = $swapCard; -# $index++; -# } -#} +sub shuffleDeck { + my @deck = @_; + my $index = 0; + for (@deck) { + my $swapCardIndex = floor(rand() * @deck); + my $swapCard = @deck[$swapCardIndex]; + @deck[$swapCardIndex] = $_; + $deck[$index] = $swapCard; + $index++; + } + return @deck; +} + +sub addUser { + my ($sessionID,$userID) = @_; +} + +sub delUser { + my ($sessionID,$userID) = @_; +} + +sub addCards { + my ($sessionID,$deckID, $DDF) = @_; +} + +sub addDeck { + my ($sessionID,$deckID) = @_; + my %newDeck = { + cards => [] + } + $sessions{$sessionID}{"decks"}{$deckID} = %newDeck; +} + +sub delDeck { + my ($sessionID,$deckID) = @_; + $sessions{$sessionID}{"decks"}{$deckID} = undef; +} + +sub addPool {} + +sub delPool {} -Net::WebSocket::Server->new( +########################Game Logic ends here############################# + +#########################Server Logic below############################## + +my $origin = 'ws://localhost'; + +sub joinSession { +} + +my $server = Net::WebSocket::Server->new( listen => 8080, on_connect => sub { my ($serv, $conn) = @_; @@ -63,10 +98,8 @@ Net::WebSocket::Server->new( ready => sub { my ($conn) = @_; #strip the / off of the resource request and then assign just the request to $session - my $session = (split("/", $conn->{"initHand"}->req->resource_name))[1]; - unless ($session) { $conn->send_utf8(generateSession()); $conn->disconnect(); print "Severed client, no session given\n"; } - joinSession($session,$conn); - #Check for user ID, if none $conn->disconnect("noid",generateUser()); + #Check for game ID, if none generate one and send it then disconnect. + #Check for user ID, if none $conn->disconnect("noid",generateUser()); }, utf8 => sub { my ($conn, $msg) = @_; @@ -74,19 +107,13 @@ Net::WebSocket::Server->new( my $messageData = ""; eval { $messageData = from_json($msg) }; if ($@) { $conn->send_utf8('{"error":1, "message":"ERROR: Invalid json"}'); return 0;} - if ($messageData->{"type"} eq "message") { - print "'".$messageData->{"message"}."' sent in session ".$sessionID."\n"; - for ($conn->server->connections) { - if ($_->{"currentSession"}{"id"} == $sessionID) { $_->send_utf8(to_json($messageData)); } - } - } - if ($messageData->{"type"} eq "update") { - print "Updating session ".$sessionID."\n"; - for ($conn->server->connections) { - if ($_->{"currentSession"}{"id"} == $sessionID) {$_->send_utf(to_json($messageData)); } - } - } }, + disconnect => sub { + my ($conn, $code, $reason) = @_; + } + } ); }, -)->start; +); + +$server->start; diff --git a/UI/index.html b/UI/index.html index bf30a00..f1e0b51 100644 --- a/UI/index.html +++ b/UI/index.html @@ -9,6 +9,7 @@