summary refs log tree commit diff
path: root/doc/hosting-flask.md
blob: e72c0c2db23a5114c1bf285528bffb7fa7165580 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
The page provides a complete example for how to integrate the webclient into a simple website

The example website will be structured like so:

```
websrv.py
templates/play.html
static/classisphere.js
static/default.zip
static/style.css
static/jquery.js
```

## Content
#### websrv.py
```Python
from flask import Flask, render_template, request
app = Flask(__name__)

@app.route("/")
def index():
    return '<html><h1>Welcome!</h1>Click <a href="/play">here</a> to play</h1></html>'

@app.route("/play")
@app.route("/play/")
def play():
    user = request.args.get('user') or 'Singleplayer'
    ver  = request.args.get('mppass') or ''
    addr = request.args.get('ip')
    port = request.args.get('port') or '25565'

    if addr:
        args = "['%s', '%s', '%s', '%s']" % (user, ver, addr, port)
    else:
        args = "['%s']" % user
    return render_template('play.html', game_args=args)

if __name__ == "__main__":
    app.run()
```

#### templates/play.html
```HTML
{% set mobile_mode = request.user_agent.platform in ('android', 'iphone', 'ipad') %}
<html>
  <head>
    <meta name="viewport" content="width=device-width">
    <link href="/static/style.css" rel="stylesheet">
    <script src="/static/jquery.js"></script>
  </head>
  <body>
{% if mobile_mode %}
    <style>
      #body {min-height: 0px;}
      .sec {padding: 0px;}
      .row {padding: 0px;}
    </style>
{% else %}
    <div id="header">
      <div class="row">
        <a href="/"><h1 class="columns">Home</h1></a>
        <a href="/play"><h1 class="columns">Play</h1></a>
      </div>
    </div>
{% endif %}
<div id="body">
<div class="sec">
  <div class="row">
    <canvas id="canvas" style="display:block; box-sizing:border-box; border-width:0px; padding:0; margin:0 auto; background-color: black; width:100%; height:auto;"
            oncontextmenu="event.preventDefault()" tabindex=-1 width="1000" height="562"></canvas>
    <span id="logmsg" style="font-size:18px;color:#F67;"></span>
  </div>
</div>
<script type='text/javascript'>
  function resizeGameCanvas() {
    var cc_canv = $('canvas#canvas');
    var dpi = window.devicePixelRatio;
    var aspect_ratio = 16/9;

    var viewport_w = cc_canv.parent().width();
    var viewport_h = viewport_w / aspect_ratio;

    var canv_w = Math.round(viewport_w);
    var canv_h = Math.round(viewport_h);

    if (canv_h % 2) { canv_h = canv_h - 1; }
    if (canv_w % 2) { canv_w = canv_w - 1; }

{% if mobile_mode %}
    var screen_h = Math.min(window.innerHeight, window.outerHeight || window.innerHeight);
    canv_h = screen_h;
{% endif %}
     cc_canv[0].width  = canv_w * dpi;
     cc_canv[0].height = canv_h * dpi;
  }

  var Module = {
    preRun: [ resizeGameCanvas ],
    postRun: [],
    arguments: {{game_args|safe}},
    print: function(text) {
      if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
      console.log(text);
    },
    printErr: function(text) {
      if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
      console.error(text);
    },
    canvas: (function() { return document.getElementById('canvas'); })(),
    setStatus: function(text) {
      console.log(text);
      document.getElementById('logmsg').innerHTML = text;
    },
    totalDependencies: 0,
    monitorRunDependencies: function(left) {
      this.totalDependencies = Math.max(this.totalDependencies, left);
      Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
    }
  };
  Module.setStatus('Downloading...');
  window.onerror = function(msg) {
    // TODO: do not warn on ok events like simulating an infinite loop or exitStatus
    Module.setStatus('Exception thrown, see JavaScript console (' + msg + ')');
    Module.setStatus = function(text) {
      if (text) Module.printErr('[post-exception status] ' + text);
    };
  };
</script>
<script async type="text/javascript" src="/static/classisphere.js"></script>
        </div>
    </body>
</html>
```

#### static/classisphere.js
Download `cs.classicube.net/client/latest/ClassiCube.js` for this

#### static/default.zip
Download `classicube.net/static/default.zip` for this

#### static/style.css
```CSS
body { margin: 0; }

.row {
    margin-left: auto;
    margin-right: auto;
    max-width: 62.5em;
}

a { text-decoration: none; }

.columns { display: inline-block; }

.sec {
    background:#f1ecfa;
    padding:10px 0 5px;
}

#header { background-color:#5870b0; }

#header h1 {
    color:#fff;
    margin:0px 10px 0px 10px;
    width: 200px;
}
```

#### static/jquery.js
Download some version of jQuery for this. Version 2.1.1 is known to work.

## Notes

* If you don't want the game to resize to fit different resolutions, remove the `resizeGameCanvas` code.

* mobile_mode is used to deliver a minified page for mobile/tablet devices

## Results

After all this setup, you need to install the flask package for python.

Then in command prompt/terminal enter: `python websrv.py`

Then navigate to `http://127.0.0.1:5000/play` in your web browser. If all goes well you should see the web client start in singleplayer.

To start in multiplayer instead, navigate to `http://127.0.0.1:5000/play?user=test&ip=127.0.0.1&port=25565`