Codebase Nuke... things got too tangled
This commit is contained in:
parent
0bc7705a25
commit
847a333d60
107
Server/server.pl
107
Server/server.pl
@ -5,7 +5,10 @@ use POSIX;
|
|||||||
use Net::WebSocket::Server;
|
use Net::WebSocket::Server;
|
||||||
use JSON;
|
use JSON;
|
||||||
|
|
||||||
my $origin = 'ws://localhost';
|
use Data::Dumper;
|
||||||
|
|
||||||
|
#########################Game Logic starts here##########################
|
||||||
|
|
||||||
my %sessions = ();
|
my %sessions = ();
|
||||||
|
|
||||||
sub generateSession {
|
sub generateSession {
|
||||||
@ -17,7 +20,7 @@ sub generateSession {
|
|||||||
print "Adding session ".$newSession." to database\n";
|
print "Adding session ".$newSession." to database\n";
|
||||||
$sessions{$newSession} = ();
|
$sessions{$newSession} = ();
|
||||||
$sessions{$newSession}{"id"} = $newSession;
|
$sessions{$newSession}{"id"} = $newSession;
|
||||||
$sessions{$newSession}{"users"} = [];
|
$sessions{$newSession}{"users"} = ();
|
||||||
$sessions{$newSession}{"decks"} = ();
|
$sessions{$newSession}{"decks"} = ();
|
||||||
$sessions{$newSession}{"pools"} = ();
|
$sessions{$newSession}{"pools"} = ();
|
||||||
|
|
||||||
@ -25,33 +28,65 @@ sub generateSession {
|
|||||||
return $newSession;
|
return $newSession;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub joinSession {
|
sub generateDeck {
|
||||||
my ($sessionID, $conn) = @_;
|
$DDF = from_json(shift);
|
||||||
print "Connection attempts to use session ".$sessionID."\n";
|
# Add proper handling for reading of DDFs
|
||||||
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 shuffleDeck {
|
sub shuffleDeck {
|
||||||
# my @deck = @_;
|
my @deck = @_;
|
||||||
# my $index = 0;
|
my $index = 0;
|
||||||
# for (@deck) {
|
for (@deck) {
|
||||||
# my $swapCardIndex = floor(rand() * @deck);
|
my $swapCardIndex = floor(rand() * @deck);
|
||||||
# my $swapCard = @deck[$swapCardIndex];
|
my $swapCard = @deck[$swapCardIndex];
|
||||||
# @deck[$swapCardIndex] = $_;
|
@deck[$swapCardIndex] = $_;
|
||||||
# $deck[$index] = $swapCard;
|
$deck[$index] = $swapCard;
|
||||||
# $index++;
|
$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,
|
listen => 8080,
|
||||||
on_connect => sub {
|
on_connect => sub {
|
||||||
my ($serv, $conn) = @_;
|
my ($serv, $conn) = @_;
|
||||||
@ -63,9 +98,7 @@ Net::WebSocket::Server->new(
|
|||||||
ready => sub {
|
ready => sub {
|
||||||
my ($conn) = @_;
|
my ($conn) = @_;
|
||||||
#strip the / off of the resource request and then assign just the request to $session
|
#strip the / off of the resource request and then assign just the request to $session
|
||||||
my $session = (split("/", $conn->{"initHand"}->req->resource_name))[1];
|
#Check for game ID, if none generate one and send it then disconnect.
|
||||||
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 user ID, if none $conn->disconnect("noid",generateUser());
|
||||||
},
|
},
|
||||||
utf8 => sub {
|
utf8 => sub {
|
||||||
@ -74,19 +107,13 @@ Net::WebSocket::Server->new(
|
|||||||
my $messageData = "";
|
my $messageData = "";
|
||||||
eval { $messageData = from_json($msg) };
|
eval { $messageData = from_json($msg) };
|
||||||
if ($@) { $conn->send_utf8('{"error":1, "message":"ERROR: Invalid json"}'); return 0;}
|
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;
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
</body>
|
</body>
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
|
window.server = "ws://localhost:8080/";
|
||||||
window.gameState = {};
|
window.gameState = {};
|
||||||
window.gameState.deck = [];
|
window.gameState.deck = [];
|
||||||
window.gameState,pool = [];
|
window.gameState,pool = [];
|
||||||
@ -27,6 +28,31 @@ function updateEvent() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sendGameState() {
|
||||||
|
gameSession.send('{"type":"update","state":'+JSON.stringify(window.gameState)+'}');
|
||||||
|
}
|
||||||
|
|
||||||
|
function startGameSession() {
|
||||||
|
window.gameSession = new WebSocket(server+gameID);
|
||||||
|
gameSession.onmessage = function (event) {
|
||||||
|
var message = JSON.parse(event.data);
|
||||||
|
if (message.state) { gameState = message.state; updateEvent(); }
|
||||||
|
if (message.userID) { window.userID = message.userID; }
|
||||||
|
if (message.message) { console.log(message.message); }
|
||||||
|
if (message.command) {
|
||||||
|
if ("requestState" == message.command) {
|
||||||
|
sendGameState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
gameSession.onconnect = function () {
|
||||||
|
gameSession.send('{"type":"update","command":"requestState"}');
|
||||||
|
};
|
||||||
|
gameSession.onclose = function (event) {
|
||||||
|
console.log(event);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function generateDeck(DDF) {
|
function generateDeck(DDF) {
|
||||||
var cards = [""];
|
var cards = [""];
|
||||||
for (var attribute in DDF) {
|
for (var attribute in DDF) {
|
||||||
@ -42,12 +68,7 @@ function generateDeck(DDF) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function shuffleDeck(deck) {
|
function shuffleDeck(deck) {
|
||||||
deck.forEach(function (card,index) {
|
gameSession.send('{"type":"action","action":"shuffle"}');
|
||||||
var swapCardIndex = Math.floor( Math.random() * deck.length );
|
|
||||||
var swapCard = deck[swapCardIndex];
|
|
||||||
deck[swapCardIndex] = card;
|
|
||||||
deck[index] = swapCard;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function deckFromJSON(ourFile) {
|
function deckFromJSON(ourFile) {
|
||||||
@ -70,7 +91,7 @@ buttonAdd("#menu","upload","Upload Deck", function () {
|
|||||||
window.gameState.deck = [];
|
window.gameState.deck = [];
|
||||||
deckFromJSON(ourFile);
|
deckFromJSON(ourFile);
|
||||||
window.hand = [];
|
window.hand = [];
|
||||||
gameSession.send('{"state":'+JSON.stringify(window.gameState)+'}');
|
sendGameState();
|
||||||
}
|
}
|
||||||
myFile.readAsText(f[0]);
|
myFile.readAsText(f[0]);
|
||||||
});
|
});
|
||||||
@ -81,25 +102,25 @@ buttonAdd("#menu","add","Add Deck", function () {
|
|||||||
myFile.onload = function (file) {
|
myFile.onload = function (file) {
|
||||||
var ourFile = JSON.parse(file.target.result);
|
var ourFile = JSON.parse(file.target.result);
|
||||||
deckFromJSON(ourFile);
|
deckFromJSON(ourFile);
|
||||||
gameSession.send('{"state":'+JSON.stringify(window.gameState)+'}');
|
sendGameState();
|
||||||
}
|
}
|
||||||
myFile.readAsText(f[0]);
|
myFile.readAsText(f[0]);
|
||||||
});
|
});
|
||||||
}, null, "div");
|
}, null, "div");
|
||||||
buttonAdd("#menu","shuffle", "Shuffle Deck", function () {
|
buttonAdd("#menu","shuffle", "Shuffle Deck", function () {
|
||||||
shuffleDeck(window.gameState.deck);
|
shuffleDeck(window.gameState.deck);
|
||||||
gameSession.send('{"state":'+JSON.stringify(window.gameState)+'}');
|
sendGameState();
|
||||||
}, null, "div");
|
}, null, "div");
|
||||||
buttonAdd("#menu","shuffleAll", "Shuffle Hand to Deck", function () {
|
buttonAdd("#menu","shuffleAll", "Shuffle Hand to Deck", function () {
|
||||||
window.gameState.deck = window.gameState.deck.concat(window.hand);
|
window.gameState.deck = window.gameState.deck.concat(window.hand);
|
||||||
window.hand = [];
|
window.hand = [];
|
||||||
shuffleDeck(window.gameState.deck);
|
shuffleDeck(window.gameState.deck);
|
||||||
gameSession.send('{"state":'+JSON.stringify(window.gameState)+'}');
|
sendGameState();
|
||||||
}, null, "div");
|
}, null, "div");
|
||||||
buttonAdd("#menu","draw", "Draw Card", function () {
|
buttonAdd("#menu","draw", "Draw Card", function () {
|
||||||
if (window.gameState.decks[0].length > 0) {
|
if (window.gameState.deck.length > 0) {
|
||||||
window.hand.unshift(window.gameState.decks[0].shift());
|
window.hand.unshift(window.gameState.deck.shift());
|
||||||
gameSession.send('{"state":'+JSON.stringify(window.gameState)+'}');
|
sendGameState();
|
||||||
}
|
}
|
||||||
}, null, "div");
|
}, null, "div");
|
||||||
elementPlace("body","display",null,"div");
|
elementPlace("body","display",null,"div");
|
||||||
@ -115,16 +136,16 @@ popupDialog("gettingStarted","Welcome to Deckard and company", false, inputDialo
|
|||||||
var tempses = new WebSocket(server);
|
var tempses = new WebSocket(server);
|
||||||
tempses.onmessage = function (event) {
|
tempses.onmessage = function (event) {
|
||||||
var message = JSON.parse(event.data);
|
var message = JSON.parse(event.data);
|
||||||
if (message.id) { gameID = message.id; }
|
if (message.id) { window.gameID = message.id; }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
window.gameSession = new WebSocket(server+gameID);
|
if (window.gameID) {
|
||||||
gameSession.onmessage = function (event) {
|
startGameSession();
|
||||||
var message = JSON.parse(event.data);
|
} else {
|
||||||
if (message.state) { gameState = message.state; updateEvent(); }
|
tempses.onclose = function () {
|
||||||
if (message.message) { console.log(message.message); }
|
startGameSession();
|
||||||
if (message.command) {}
|
|
||||||
};
|
};
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user