2020-06-09 13:48:19 -06:00
< html >
< head >
2020-09-02 11:48:56 -06:00
< title > Deckard + Company< / title >
2020-06-09 13:48:19 -06:00
< link href = "css.css" rel = "stylesheet" >
< script src = "https://labs.murkfall.net/bluesaxman/blue.js/raw/master/libs/bluecore.js" > < / script >
< / head >
< body >
2020-09-02 11:48:56 -06:00
< h1 > Welcome to Deckard + Company< h1 >
2020-06-09 13:48:19 -06:00
< / body >
< script >
2020-09-02 15:26:46 -06:00
window . server = "ws://localhost:8080/" ;
2020-09-02 11:48:56 -06:00
window . gameState = { } ;
window . gameState . deck = [ ] ;
window . gameState , pool = [ ] ;
2020-06-09 13:48:19 -06:00
window . hand = [ ] ;
function updateEvent ( ) {
window . UI . deck . innerHTML = "" ;
window . UI . hand . innerHTML = "" ;
var theDeck = elementPlace ( "#deck" , "deckDisp" , "card" , "div" ) ;
2020-09-02 11:48:56 -06:00
theDeck . innerHTML = "<div>" + window . gameState . deck . length + "</div>" ;
2020-06-09 13:48:19 -06:00
window . hand . forEach ( function ( card , index ) {
if ( index < 10 ) {
var currentCard = elementPlace ( "#hand" , null , "card" , "li" ) ;
currentCard . innerHTML = card ;
}
} ) ;
}
2020-09-02 15:26:46 -06:00
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 ) ;
} ;
}
2020-06-09 13:48:19 -06:00
function generateDeck ( DDF ) {
var cards = [ "" ] ;
for ( var attribute in DDF ) {
var tempcards = [ ] ;
cards . forEach ( function ( current ) {
DDF [ attribute ] . forEach ( function ( value ) {
tempcards . push ( current + value ) ;
} ) ;
} ) ;
cards = tempcards . slice ( ) ;
}
return cards ;
}
function shuffleDeck ( deck ) {
2020-09-02 15:26:46 -06:00
gameSession . send ( '{"type":"action","action":"shuffle"}' ) ;
2020-06-09 13:48:19 -06:00
}
function deckFromJSON ( ourFile ) {
if ( Array . isArray ( ourFile ) ) {
ourFile . forEach ( function ( deck ) {
2020-09-02 11:48:56 -06:00
window . gameState . deck = window . gameState . deck . concat ( generateDeck ( deck ) ) ;
2020-06-09 13:48:19 -06:00
} ) ;
} else {
2020-09-02 11:48:56 -06:00
window . gameState . deck = window . gameState . deck . concat ( generateDeck ( ourFile ) ) ;
2020-06-09 13:48:19 -06:00
}
}
elementPlace ( "body" , "menu" , null , "div" ) ;
buttonAdd ( "#menu" , "upload" , "Upload Deck" , function ( ) {
popupDialog ( "deckLoader" , "Select Deck definition File" , true , inputDialog , { "inputType" : "file" } , function ( f ) {
var myFile = new FileReader ( ) ;
myFile . onload = function ( file ) {
// Probably validate the file somehow befor eating it
var ourFile = JSON . parse ( file . target . result ) ;
2020-09-02 11:48:56 -06:00
window . gameState . deck = [ ] ;
2020-06-09 13:48:19 -06:00
deckFromJSON ( ourFile ) ;
window . hand = [ ] ;
2020-09-02 15:26:46 -06:00
sendGameState ( ) ;
2020-06-09 13:48:19 -06:00
}
myFile . readAsText ( f [ 0 ] ) ;
} ) ;
} , null , "div" ) ;
buttonAdd ( "#menu" , "add" , "Add Deck" , function ( ) {
popupDialog ( "deckLoader" , "Select Deck definition to add" , true , inputDialog , { "inputType" : "file" } , function ( f ) {
var myFile = new FileReader ( ) ;
myFile . onload = function ( file ) {
var ourFile = JSON . parse ( file . target . result ) ;
deckFromJSON ( ourFile ) ;
2020-09-02 15:26:46 -06:00
sendGameState ( ) ;
2020-06-09 13:48:19 -06:00
}
myFile . readAsText ( f [ 0 ] ) ;
} ) ;
} , null , "div" ) ;
buttonAdd ( "#menu" , "shuffle" , "Shuffle Deck" , function ( ) {
2020-09-02 11:48:56 -06:00
shuffleDeck ( window . gameState . deck ) ;
2020-09-02 15:26:46 -06:00
sendGameState ( ) ;
2020-06-09 13:48:19 -06:00
} , null , "div" ) ;
2020-09-02 11:48:56 -06:00
buttonAdd ( "#menu" , "shuffleAll" , "Shuffle Hand to Deck" , function ( ) {
window . gameState . deck = window . gameState . deck . concat ( window . hand ) ;
2020-06-09 13:48:19 -06:00
window . hand = [ ] ;
2020-09-02 11:48:56 -06:00
shuffleDeck ( window . gameState . deck ) ;
2020-09-02 15:26:46 -06:00
sendGameState ( ) ;
2020-06-09 13:48:19 -06:00
} , null , "div" ) ;
buttonAdd ( "#menu" , "draw" , "Draw Card" , function ( ) {
2020-09-02 15:26:46 -06:00
if ( window . gameState . deck . length > 0 ) {
window . hand . unshift ( window . gameState . deck . shift ( ) ) ;
sendGameState ( ) ;
2020-06-09 13:48:19 -06:00
}
} , null , "div" ) ;
elementPlace ( "body" , "display" , null , "div" ) ;
window . UI = { } ;
window . UI . deck = elementPlace ( "#display" , "deck" , null , "div" ) ;
window . UI . hand = elementPlace ( "#display" , "hand" , null , "ol" ) ;
2020-09-02 11:48:56 -06:00
popupDialog ( "gettingStarted" , "Welcome to Deckard and company" , false , inputDialog , { "content" : "Please enter a game ID or leave blank to start a new game" , "inputType" : "number" } ,
function ( value ) {
if ( Number . isInteger ( Number . parseInt ( value ) ) ) {
window . gameID = Number . parseInt ( value ) ;
} else {
var tempses = new WebSocket ( server ) ;
tempses . onmessage = function ( event ) {
var message = JSON . parse ( event . data ) ;
2020-09-02 15:26:46 -06:00
if ( message . id ) { window . gameID = message . id ; }
} ;
}
if ( window . gameID ) {
startGameSession ( ) ;
} else {
tempses . onclose = function ( ) {
startGameSession ( ) ;
2020-09-02 11:48:56 -06:00
} ;
}
} ) ;
2020-06-09 13:48:19 -06:00
< / script >
< / html >