Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- from _thread import *
- from Objects import ServerCont, Player
- import pygame
- import sys
- import pickle
- host = '192.168.1.65'
- port = 5000
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.bind((host, port))
- s.listen(2)
- print('Server started')
- connected = set()
- games = {}
- idCount = 0
- connections = {}
- def threaded_client(c, p, gameId):
- global idCount, games
- c.send(str.encode(str(p)))
- reply = ''
- while True:
- try:
- data = pickle.loads(c.recv(4096 * 2))
- if gameId in games:
- cont = games[gameId]
- if not data:
- break
- else:
- if data != 'get' and not isinstance(data, Player):
- cont.changedirCl(p, data)
- if p == 1 and cont.run and data == 'get':
- cont.move_snakes_mul(20)
- if isinstance(data, Player):
- if p == 0:
- cont.p0 = data
- elif p == 1:
- cont.p1 = data
- else:
- reply = cont
- c.sendall(pickle.dumps(reply))
- if not cont.run:
- break
- else:
- break
- except Exception as e:
- print(e)
- print(type(e))
- break
- print('Lost connection')
- print('Closing game', gameId)
- idCount -= 1
- try:
- del games[gameId]
- except Exception as e:
- print(e)
- print(type(e))
- c.close()
- while True:
- c, addr = s.accept()
- print(f'Connected to {addr} with Id {idCount}')
- idCount += 1
- p = 0
- gameId = (idCount - 1) // 2
- if idCount % 2 == 1:
- games[gameId] = ServerCont(gameId)
- print('Creating a new Game...')
- else:
- p = 1
- games[gameId].ready = True
- start_new_thread(threaded_client, (c, p, gameId))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement