@CalebEvans1/

TicTacToe Server

Python

No description

fork
loading
Files
  • main.py
  • static
  • templates
main.py
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
from flask import Flask, render_template
app = Flask(__name__)
from flask_socketio import SocketIO, emit

import time, random, os, binascii

@app.route('/')
def index():
    """Serve the index HTML"""
    return render_template('index.html')

rooms = []

roomState = [[0,0,0],[0,0,0],[0,0,0]]
roomPlayers = []
roomScore = []
roomSetting = 'Public'
roomId = ''
lastAction = 0

socketio = SocketIO(app)

exampleRoom = {"GameState": roomState,
               "PlayerA": '',
               "PlayerB": '',
               "ALastResponded": 0,
               "BLastResponded": 0,
               "turn": 0,
               "Score": roomScore,
               "ID": roomId,
               "TimeStarted": 0}

active_instances = []

@socketio.on('connect')
def on_connect():
    emit('CONNECTED')

@socketio.on('generate_key')
def gen_key():
    emit('key_generated', {'key':  binascii.hexlify(os.urandom(20)).decode()})

@socketio.on('UpdateRoom')
def update_room(msg):
    roomNumber = 0
    setup = False
    errorThrown = False
    for i in range(active_instances):
        if not errorThrown:
            if msg['ID'] == active_instances[i]['ID'] and (msg['key'] == active_instances[i]['PlayerA'] or msg['key'] == active_instances[i]['PlayerB']):
                roomNumber = i
                setup = True
                if msg['key'] == active_instances[i]['PlayerA']:
                    if active_instances[i]['BLastResponded'] + 90 < time.time():
                        emit('kicked', {'response': 'Other player got disconnected'})
                        active_instances.pop(i)
                        errorThrown = True
                    else:
                        active_instances[i]['ALastResponded'] = time.time()
                else:
                    if active_instances[i]['ALastResponded'] + 90 < time.time():
                        emit('kicked', {'response': 'Other player got disconnected'})
                        active_instances.pop(i)
                        errorThrown = True
                    else:
                        active_instances[i]['BLastResponded'] = time.time()
    if setup and not errorThrown:
        emit('roomUpdate', active_instances[roomNumber])
    elif errorThrown == False:
        emit('kicked', {'response': 'Room with the given ID and your instance key was not found.'})

@socketio.on("Update Square")
def update_board(msg):
    errorThrown
    for i in range(len(active_instances)):
        #Does something
        return True

app.run(debug=False,port=8080,host='0.0.0.0')