Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import datetime
- from random import randint
- from os import listdir,remove,system,fork,rename,mkdir
- from time import time,ctime,sleep
- from hashlib import md5
- #CONST
- PET_ALIEN = """
- ****
- ******
- **$****$**
- ********
- ****
- ** \n\n"""
- PET_TURTLE = """
- **** $$
- *******
- ** ** \n\n"""
- PET_HUMAN = """
- $$
- ****
- ****
- **
- **\n\n"""
- HOST = ''
- PORT = 7272
- def configuration():
- print "New Folders are going to be created\n"
- PATH = raw_input("please select the path: ")
- mkdir("%s/Users" %(PATH))
- mkdir("%s/Stats" %(PATH))
- mkdir("%s/Log" %(PATH))
- data_conf = open("/home/pygotchi/pygotchi.txt","w")
- data_conf.writelines(PATH)
- data_conf.close()
- def activity(name,time1,opt,info_server,type_hour,PATH):
- if opt == '1':
- list = listdir("%s/Users/" %(PATH))
- for i in range(0,len(list)):
- num = i+1
- conn.send("User %s: %s\n" %(num,list[i].split(".txt")[0]))
- i+=1
- elif opt == '2':
- conn.send(info_server)
- conn.send(type_hour)
- elif opt == '3':
- conn.send("To: ")
- data = conn.recv(1024)
- dest = data
- conn.send("Message: ")
- data = conn.recv(1024)
- mess = data
- mex_var = "MESSAGE\nFrom: %s\nTo: %s\nMessage: %s" %(name,dest,mess)
- search = listdir("%s/Users/" %(PATH))
- num = 0
- for i in range(0,len(search)):
- if search[i].find("_message.txt") != -1:
- num += 1
- else:
- pass
- db = open("%s/Users/%s_%s_message.txt" %(PATH,(num+1),dest),"w")
- db.writelines(mex_var)
- db.close()
- conn.send("Message send to %s\n!" %(dest))
- elif opt == '4':
- search = listdir("%s/Users/" %(PATH))
- num = 0
- for i in range(0,len(search)):
- if search[i].find("%s_message.txt" %(name)) != -1:
- num += 1
- else:
- pass
- conn.send("Found %s new messages\n" %(num))
- conn.send("Which wanna read? [0 to exit]\n---> ")
- data = conn.recv(1024)
- reader = data
- if data == '0':
- pass
- else:
- db = open("%s/Users/%s_%s_message.txt" %(PATH,reader,name),"r")
- db1 = db.readlines()
- db.close()
- mex = ''
- for i in range(0,len(db1)):
- mex += db1[i]
- conn.send(mex)
- conn.send("\n")
- conn.send("Delete? [y/n]\n---> ")
- data = conn.recv(1024)
- if data == "y":
- remove("%s/Users/%s_%s_message.txt" %(PATH,reader,name))
- else:
- rename("%s/%s_%s_message.txt" %(PATH,reader,name),"/home/bughardy/tamagotchi/Users/%s_%s_read.txt" %(reader,name))
- elif opt == '5':
- while 1:
- db = open("%s/Users/%s.txt" %(PATH,name),"r")
- db1 = open("%s/Stats/%s_st.txt" %(PATH,name),"r")
- db2 = db.readlines()
- db3 = db1.readlines()
- type = db2[3].split()[4]
- HEAL = db3[0].split()[1]
- HAPPINESS = db3[1].split()[1]
- CLEANILESS = db3[2].split()[1]
- db.close()
- db1.close()
- db = open("%s/Users/%s.txt" %(PATH,name),"r")
- db1 = db.readlines()
- db.close()
- creation = db1[5].split("-")
- creation = datetime.date(int(creation[0]),int(creation[1]),int(creation[2]))
- expire = datetime.timedelta(days=int(db1[4].split()[1]))
- today = datetime.date.today()
- age = (today-creation)
- if str(age).find("day") != -1:
- eta = "Age: "+str(age).split()[0]
- if int(str(age).split()[0]) == 1:
- eta += ' day\n'
- else:
- eta += ' days\n'
- else:
- eta = "Age: 0 day\n"
- details(HEAL,HAPPINESS,CLEANILESS,eta)
- if type == "PET_ALIEN":
- conn.send(PET_ALIEN)
- elif type == "PET_TURTLE":
- conn.send(PET_TURTLE)
- elif type == "PET_HUMAN":
- conn.send(PET_HUMAN)
- conn.send("What do you want to do with you're pet?\n")
- conn.send("[1] Eat [2] Play [3] Clean [4] Quit\n---> ")
- data = conn.recv(1024)
- if data.find("1") != -1:
- conn.send("GOOD! You're pet now is full of food!\n")
- HEAL = "HEAL: 100\n"
- HAPPINESS = "HAPPINESS: "+HAPPINESS+"\n"
- CLEANILESS = "CLEANILESS: "+CLEANILESS+"\n"
- elif data.find("2") != -1:
- conn.send("GOOD! Your pet now is very happy!\n")
- HEAL = "HEAL: "+HEAL+"\n"
- HAPPINESS = "HAPPINESS: "+HAPPINESS+"\n"
- CLEANILESS = "CLEANILESS: 100\n"
- elif data.find("3") != -1:
- conn.send("GOOD! Your pet now is so cute and clean!\n")
- HEAL = "HEAL: "+HEAL+"\n"
- CLEANILESS = "CLEANILESS: "+CLEANILESS+"\n"
- HAPPINESS = "HAPPINESS: 100\n"
- elif data.find("4") != -1:
- HEAL = "HEAL: "+HEAL+"\n"
- CLEANILESS = "CLEANILESS: "+CLEANILESS+"\n"
- HAPPINESS = "HAPPINESS: "+HAPPINESS+"\n"
- break
- db1 = open("%s/Stats/%s_st.txt" %(PATH,name),"w")
- db1.writelines(HEAL)
- db1.writelines(HAPPINESS)
- db1.writelines(CLEANILESS)
- db1.close()
- elif opt == '6':
- conn.send("Connectio Closed")
- conn.close()
- quit = 1
- else:
- conn.send("Invalid Option\n")
- quit = 0
- return quit
- def newid(log,PATH):
- ip = "IP --> "+addr[0]+"\n"
- conn.send("Insert New User ID: ")
- data = conn.recv(1020)
- id = data.split("\r\n")[0]
- user = "USER --> "+id+"\n"
- conn.send("password query")
- data = conn.recv(1024)
- password = data.split("\r\n")[0]
- password = "PASS --> "+password+"\n"
- conn.send("Insert your pet's name: ")
- data = conn.recv(1024)
- PET_NAME = "PET --> "+data
- conn.send("Which pet would you like: \n 1] ALIEN %s\n 2] TURTLE %s\n 3] HUMAN %s\n---> " %(PET_ALIEN,PET_TURTLE,PET_HUMAN))
- data = conn.recv(1024)
- if data.find("1") != -1:
- PET_TYPE = "TYPE --> PET_ALIEN\n"
- HEAL = "HEAL: 100\n"
- HAPPINESS = "HAPPINESS: 100\n"
- CLEANILESS = "CLEANILESS: 100\n"
- EXPIRE ="EXPIRE: "+str(10+randint(-4,4))+"\n"
- conn.send("Now you've got an alien!\n")
- elif data.find("2") != -1:
- PET_TYPE = "TYPE --> PET_TURTLE\n"
- HEAL = "HEAL: 100\n"
- HAPPINESS = "HAPPINESS: 100\n"
- CLEANILESS = "CLEANILESS: 100\n"
- EXPIRE ="EXPIRE: "+str(12+randint(-2,2))+"\n"
- conn.send("Now you've got a turtle!\n")
- elif data.find("3") != -1:
- PET_TYPE = "TYPE --> PET_HUMAN\n"
- HEAL = "HEAL: 100\n"
- HAPPINESS = "HAPPINESS: 100\n"
- CLEANILESS = "CLEANILESS: 100\n"
- EXPIRE = "EXPIRE: "+str(8+randint(-3,3))+"\n"
- conn.send("Now you've got an human!\n")
- reg = open("%s/Users/%s.txt" %(PATH,id),"w")
- reg1 = open("%s/Stats/%s_st.txt" %(PATH,id),"w")
- reg.writelines(ip)
- reg.writelines(user)
- reg.writelines(password)
- reg.writelines(PET_NAME)
- reg.writelines(PET_TYPE)
- reg.writelines(EXPIRE)
- reg.writelines(str(datetime.date.today()))
- reg1.writelines(HEAL)
- reg1.writelines(HAPPINESS)
- reg1.writelines(CLEANILESS)
- reg.close()
- reg1.close()
- log += ctime()+" New Registration from %s" %(ip)
- savelog(log)
- conn.send("Registration Complete, restart to start to play!\n")
- conn.send("Connection Closed")
- conn.close()
- def savelog(log,PATH):
- log_write = open("%s/Log/log.txt" %(PATH),"a")
- log_write.writelines(log)
- log_write.close()
- def details(HEAL,CLEANILESS,HAPPINESS,age):
- conn.send(age)
- conn.send("HEAL: %s\n" %(HEAL))
- conn.send("HAPPINESS: %s\n" %(HAPPINESS))
- conn.send("CLEANILESS: %s\n" %(CLEANILESS))
- def checker(time1,PATH):
- files = listdir("%s/Users/" %(PATH))
- for i in range(0,len(files)):
- if files[i].find("_death.txt") != -1 or files[i].find("_message.txt") != -1 or files[i].find("_read.txt") != -1:
- continue
- else:
- db = open("%s/Users/%s" %(PATH,files[i]),"r")
- db1 = db.readlines()
- type = db1[3].split()[4]
- db.close()
- files[i] = files[i].split(".txt")[0]
- update(files[i],type,time1,PATH)
- i+=1
- i=0
- def update(file,type,time1,PATH):
- db = open("%s/Stats/%s_st.txt" %(PATH,file),"r")
- db3 = db.readlines()
- db.close()
- HEAL = db3[0].split()[1]
- HAPPINESS = db3[1].split()[1]
- CLEANILESS = db3[2].split()[1]
- HEAL = int(HEAL)
- HAPPINESS = int(HAPPINESS)
- CLEANILESS = int(CLEANILESS)
- db = open("%s/Users/%s.txt" %(PATH,file),"r")
- db1 = db.readlines()
- db.close()
- creation = db1[5].split("-")
- creation = datetime.date(int(creation[0]),int(creation[1]),int(creation[2]))
- expire = datetime.timedelta(days=int(db1[4].split()[1]))
- today = datetime.date.today()
- age = (today-creation)
- if HEAL<=0 or HAPPINESS<=0 or CLEANILESS<=0 or age >= expire:
- death(file)
- else:
- time2 = list(str(time()))[:-2]
- time1 = list(str(time1))[:-2]
- time3 = ''
- time4 = ''
- for i in range(0,len(time1)-1):
- time3 += time1[i]
- time4 += time2[i]
- i+=1
- time3, time4 = int(time3), int(time4)
- type_hour = period()
- if type_hour == 'day':
- k = (time4-time3)/1800
- else:
- k = (time4-time3)/3600
- if type == "PET_ALIEN":
- HEAL -= 5*k
- HAPPINESS -= 2*k
- CLEANILESS -= 3*k
- elif type == "PET_TURTLE":
- HEAL -= 2*k
- HAPPINESS -= 1*k
- CLEANILESS -= 10*k
- elif type == "PET_HUMAN":
- HEAL -= 10*k
- HAPPINESS -= 5*k
- CLEANILESS -= 2*k
- db1 = open("%s/Stats/%s_st.txt" %(PATH,file),"w")
- HEAL = "HEAL: "+str(HEAL)+"\n"
- HAPPINESS = "HAPPINESS: "+str(HAPPINESS)+"\n"
- CLEANILESS = "CLEANILESS: "+str(CLEANILESS)+"\n"
- written = HEAL+HAPPINESS+CLEANILESS
- db1.writelines(written)
- db1.close()
- def death(file,PATH):
- files = listdir("%s/Users/" %(PATH))
- remove("%s/Stats/%s_st.txt" %(PATH,file))
- remove("%s/Users/%s.txt" %(PATH,file))
- mex = open("%s/Users/%s_death.txt" %(PATH,file),"w")
- mex.writelines("You're pet is death!!\nYour account is been deleted, make one new to restart to play!\n\n")
- mex.close()
- def period():
- hour = ctime().split()[3].split(":")
- if int(hour[0]) >= 0 and int(hour[0]) <= 8:
- type_hour = 'night'
- else:
- type_hour = 'day'
- return type_hour
- #MAKING SOCKET
- s = socket.socket()
- s.bind((HOST,PORT))
- s.listen(1)
- #MAKING LOG AND VAR
- log = ''
- log += ctime()+" ]--STARTING TAMAGOTCHI SERVER--[\n"
- time1 = ctime()
- name_server = "BUGHARDY SERVER, 1"
- owner_server = "bughardy"
- version_server = '1.1.0 (beta)'
- info_server = "Server: "+name_server+"\nStarted: "+time1+"\nOwner: "+owner_server+"\nVersion: "+version_server+"\n"
- #MAIN
- conf = raw_input("Do you need to configure the server? [y]: ")
- if conf == "n":
- pass
- else:
- configuration()
- pid = fork()
- while 1:
- PATH = open("/home/pygotchi/pygotchi.txt","r").readlines()[0]
- type_hour = period()
- if type_hour == "day":
- type_hour = "Attivita': Diurna\n"
- else:
- type_hour = "Attivita': Notturna\n"
- sleep(0.5)
- if pid != 0:
- name = ''
- quit = 0
- text = open("%s/Log/log.txt" %(PATH),"a")
- conn, addr = s.accept()
- log += ctime()+" Request connection from "+addr[0]+"\n"
- conn.send("id query")
- data = conn.recv(1024)
- user = data.split("\r\n")[0]
- system("clear")
- print log
- try:
- db = open("%s/Users/%s.txt" %(PATH,user),"r")
- list_users_pass = db.readlines()
- conn.send("password query")
- data = conn.recv(1024)
- password = data.split("\r\n")[0]
- password_db = list_users_pass[2].split()[2]
- if password == password_db:
- conn.send("\n\nAccess Guaranted\n")
- conn.send("\nWelcome %s\n\n" %(user))
- time1 = time()
- name = list_users_pass[3].split()[2]
- conn.send("menu query")
- while 1:
- data = conn.recv(1024)
- choose = data
- quit = activity(user,time1,choose,info_server,type_hour,PATH)
- if quit == 1:
- break
- else:
- conn.send("menu query")
- log += time.ctime()+" User: %s logged in, with IP: %s\n" %(user,addr[0])
- system("clear")
- print log
- savelog(log,PATH)
- else:
- conn.send("\nAccess Denied\n\n")
- log += ctime()+" User: %s wrong password, with IP: %s\n" %(user,addr[0])
- conn.send("Connection Closed\n")
- system("clear")
- print log
- savelog(log)
- conn.close()
- except:
- try:
- contr = listdir("%s/Users/" %(PATH))
- for i in range(0,len(contr)):
- if contr[i].find("_death.txt") != -1:
- dead = 1
- died = open("%s/Users/%s" %(PATH,contr[i]),"r")
- mex = died.readlines()
- mex = mex[0]+mex[1]
- died.close()
- conn.send(mex)
- remove("%s/Users/%s" %(PATH,contr[i]))
- conn.send("Connection Closed\n")
- conn.close()
- i+=1
- conn.send("This User Doesn't Exist, do you want make a new one? [y/n] ")
- data = conn.recv(1024)
- if data.find("y") != -1:
- newid(log,PATH)
- else:
- conn.send("Connection Closed\n")
- conn.close()
- except:
- pass
- else:
- time1 = time()
- while 1:
- #try:
- checker(time1,PATH)
- sleep(10)
- #except:
- # sleep(20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement