<!DOCTYPE html>
<html >
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="pyramid web application">
    <meta name="author" content="Pylons Project">

    <title>Free Online Gambling</title>

    <!-- Bootstrap core CSS -->
    <link href="//oss.maxcdn.com/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this scaffold -->
    <link href="/css/theme.css" rel="stylesheet">
    <link href="/css/docs.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="//oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]-->
    <script>
        window.the_game = null
        // let socket = new WebSocket("wss://cowiz.struppi.name/ws");
        if (window.location.hostname == '127.0.0.1') {
            ws_schema = 'ws://';
        }
        else {
            ws_schema = 'wss://';
        }
        let socket = new WebSocket(ws_schema+window.location.host+'/ws');
        window.game_socket = socket

        socket.onopen = function(e) {
            //alert("[open] Connection established");
            //alert("Sending to server");
            //socket.send("My name is John");
            let player = "{{player}}"
            var msg = {
                //type: "message",
                //fct: "requestInit",
                type: "message",
                fct: "getHand",
                player: player,
            };
            let t = JSON.stringify(msg);
            window.game_socket.send(t);
        };

        socket.onmessage = function(event) {
            let msg = JSON.parse(event.data);
            let fct = msg.fct;
            if (fct == "reload") {
                location.reload(true);
            }
            else if (fct == "getHand") {
                window.hand = msg.cards;
                let player = "{{player}}";
                var msg_out = {
                    type: "message",
                    fct: "requestInit",
                    player: player,
                };
                let t = JSON.stringify(msg_out);
                window.game_socket.send(t);
            }
            else if (fct == "startGame") {
                let names = msg.names
                setupGame(names, window.hand)
                //alert('game is set up')
            }
            else if (fct == "cardPlayed") {
                let player_id = msg.player;
                let card_idx = msg.card_idx;
                let card = msg.card;
                let num_players = msg.num_players;
                cardPlayed(player_id, card_idx, card, num_players)
            }
            else {
                alert('unknown command: '+event.data);
            }
        };

        socket.onclose = function(event) {
            if (event.wasClean) {
                alert(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
                //alert('closed clean');
            } else {
                //alert('closed unclean');
            }
        };

        socket.onerror = function(error) {
            alert("[error]: "+error.message);
        };


        function playCard(card) {
            let player = "{{player}}";
            let c = card.toString();
            var msg = {
                type: "message",
                fct: "playcard",
                player: player,
                card: c
            };
            let t = JSON.stringify(msg);
            window.game_socket.send(t);
        }

        function cardPlayed(player, card_idx, card, num_players) {
            let myself = "{{player}}";
            let player_idx = ((player + num_players) - myself) % num_players;
            window.the_game.play_card(player_idx, card_idx, card)
        }


    </script>

    <style>
        * {padding: 0; margin: 0}
        @font-face {
            font-family: "Wizzta";
            src: url("/js-cardgame/font/wizzta-vada-webfont.woff2") format("woff2"),
                 url("/js-cardgame/font/wizzta-vada-webfont.woff") format("woff");
        }
    </style>


</head>

<body>

<div class="starter-template">
    <div class="container">
        <div class="row">
            <div class="col-md-2">
            </div>
            <div class="col-md-10">
                <div>{% block content %}{% endblock %}</div>
            </div>
        </div>
<!--
        <div class="row">
            <div class="links">
                <ul>
                    <li><i class="glyphicon glyphicon-cog icon-muted"></i><a
                            href="https://github.com/mikeckennedy/python-jumpstart-course-demos"
                            target="_blank">Github Project</a></li>
                    <li><i class="glyphicon glyphicon-globe icon-muted"></i><a href="https://twitter.com/talkpython"
                                                                               target="_blank">Twitter</a>
                    </li>
                </ul>
            </div>
        </div>
        <div class="row">
            <div class="copyright">
                Copyright &copy; Talk Python Training
            </div>
        </div>
-->
    </div>
</div>


<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<!-- <script src="https://oss.maxcdn.com/libs/jquery/1.10.2/jquery.min.js"></script> -->
<script src="/js-cardgame/jquery-3.5.1.min.js"></script>
<script src="https://oss.maxcdn.com/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>
</body>
