# -*- coding: utf-8 -*-
import sys, os
import threading
import random
import time
import socket
import pickle
import copy
from twisted.protocols import basic
from twisted.internet import reactor, protocol
HOST = '127.0.0.1'
PORT = 5314
def sauver(plant, champ, gold, temp, graine, chplant):
valeurs = (plant, champ, gold, temp, graine, chplant)
fic = file("save.sav", "w")
pickle.dump(valeurs, fic)
def charger():
fic = file("save.sav", "r")
valeurs = pickle.load(fic)
print "Partie charg�"
return valeurs
class Joueur:
def __init__(self,nom,x,y,carte):
self.nom = nom
self.x = x
self.y = y
self.carte = carte
return
def change(self,nom,x,y,carte):
self.nom = nom
self.x = x
self.y = y
self.carte = carte
return
class Echo(protocol.Protocol):
"""This is just about the simplest possible protocol"""
def connectionMade(self):
self.transport.write("An apple a day keeps the doctor away\r\n")
self.transport.loseConnection()
def dataReceived(self, data):
print data
"As soon as any data is received, write it back."
mess = data.split('/0')
if mess[0] == 'conn':
self.nom = mess[1]
fic = open(self.nom+".perso", "r")
self.valeur = pickle.load(fic)
fic.close()
print self.valeur
self.nom, self.password, self.x, self.y, self.carte = self.valeur
self.t = threading.Thread(target=self.periode)#,args=())
self.t.start()
self.transport.write("conn/0"+self.nom+"/0"+str(self.x)+"/0"+str(self.y)+"/0"+str(self.carte))
j = Joueur(self.nom,self.x,self.y,self.carte)
clients.append(j)
self.ref = j
if mess[0] == 'touche':
if mess[1] == 'haut':
self.y -= 32
if mess[1] == 'bas':
self.y += 32
if mess[1] == 'gauche':
self.x -= 32
if mess[1] == 'droite':
self.x += 32
self.transport.loseConnection()
def periode (self):
while 1:
time.sleep(0.1)
for i in clients:
self.transport.write("coords/0"+str(i.nom)+'/0'+str(i.x)+'/0'+str(i.y)+'/0'+str(i.carte))
self.ref.change(self.nom,self.x,self.y,self.carte)
clients = []
def main():
"""This runs the protocol on port 8000"""
while 1:
factory = protocol.ServerFactory()
factory.protocol = Echo
reactor.listenTCP(PORT,factory)
reactor.run()
# this only runs if the module was *not* imported
if __name__ == '__main__':
main()