Advertisement
Guest User

Untitled

a guest
Aug 9th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.62 KB | None | 0 0
  1. import datetime
  2. from random import randint
  3. from os import listdir,remove,system,fork,rename,mkdir
  4. from time import time,ctime,sleep
  5. from hashlib import md5
  6.  
  7. #CONST
  8. PET_ALIEN = """
  9.            ****
  10.           ******
  11.         **$****$**
  12.          ********
  13.            ****
  14.             **  \n\n"""
  15. PET_TURTLE = """
  16.           ****  $$
  17.          *******
  18.         **    ** \n\n"""
  19.  
  20. PET_HUMAN = """
  21.            $$
  22.           ****
  23.           ****
  24.            **
  25.            **\n\n"""
  26.  
  27. HOST = ''
  28. PORT = 7272
  29.  
  30. def configuration():
  31.     print "New  Folders are going to be created\n"
  32.     PATH = raw_input("please select the path: ")
  33.     mkdir("%s/Users" %(PATH))
  34.     mkdir("%s/Stats" %(PATH))
  35.     mkdir("%s/Log" %(PATH))
  36.     data_conf = open("/home/pygotchi/pygotchi.txt","w")
  37.     data_conf.writelines(PATH)
  38.     data_conf.close()
  39.    
  40. def activity(name,time1,opt,info_server,type_hour,PATH):
  41.    if opt == '1':
  42.       list = listdir("%s/Users/" %(PATH))
  43.       for i in range(0,len(list)):
  44.         num = i+1
  45.         conn.send("User %s: %s\n" %(num,list[i].split(".txt")[0]))
  46.         i+=1
  47.    elif opt == '2':
  48.       conn.send(info_server)
  49.       conn.send(type_hour)
  50.    elif opt == '3':
  51.       conn.send("To: ")
  52.       data = conn.recv(1024)
  53.       dest = data
  54.       conn.send("Message: ")
  55.       data = conn.recv(1024)
  56.       mess = data
  57.       mex_var = "MESSAGE\nFrom: %s\nTo: %s\nMessage: %s" %(name,dest,mess)
  58.       search = listdir("%s/Users/" %(PATH))
  59.       num = 0
  60.       for i in range(0,len(search)):
  61.         if search[i].find("_message.txt") != -1:
  62.             num += 1
  63.         else:
  64.             pass
  65.       db = open("%s/Users/%s_%s_message.txt" %(PATH,(num+1),dest),"w")
  66.       db.writelines(mex_var)
  67.       db.close()
  68.       conn.send("Message send to %s\n!" %(dest))
  69.    elif opt == '4':
  70.     search = listdir("%s/Users/" %(PATH))
  71.     num = 0
  72.     for i in range(0,len(search)):
  73.         if search[i].find("%s_message.txt" %(name)) != -1:
  74.             num += 1
  75.         else:
  76.             pass
  77.     conn.send("Found %s new messages\n" %(num))
  78.     conn.send("Which wanna read? [0 to exit]\n---> ")
  79.     data = conn.recv(1024)
  80.     reader = data
  81.         if data == '0':
  82.         pass
  83.     else:
  84.         db = open("%s/Users/%s_%s_message.txt" %(PATH,reader,name),"r")
  85.         db1 = db.readlines()
  86.         db.close()
  87.         mex = ''
  88.         for i in range(0,len(db1)):
  89.             mex += db1[i]
  90.         conn.send(mex)
  91.         conn.send("\n")
  92.         conn.send("Delete? [y/n]\n---> ")
  93.         data = conn.recv(1024)
  94.                 if data == "y":
  95.                     remove("%s/Users/%s_%s_message.txt" %(PATH,reader,name))
  96.                 else:
  97.                     rename("%s/%s_%s_message.txt" %(PATH,reader,name),"/home/bughardy/tamagotchi/Users/%s_%s_read.txt" %(reader,name))
  98.    elif opt == '5':
  99.     while 1:
  100.          db = open("%s/Users/%s.txt" %(PATH,name),"r")
  101.          db1 = open("%s/Stats/%s_st.txt" %(PATH,name),"r")
  102.          db2 = db.readlines()
  103.          db3 = db1.readlines()
  104.          type = db2[3].split()[4]
  105.          HEAL = db3[0].split()[1]
  106.          HAPPINESS = db3[1].split()[1]
  107.          CLEANILESS = db3[2].split()[1]
  108.          db.close()
  109.          db1.close()
  110.          db = open("%s/Users/%s.txt" %(PATH,name),"r")
  111.          db1 = db.readlines()
  112.          db.close()
  113.          creation = db1[5].split("-")
  114.          creation = datetime.date(int(creation[0]),int(creation[1]),int(creation[2]))
  115.          expire = datetime.timedelta(days=int(db1[4].split()[1]))
  116.          today = datetime.date.today()
  117.          age = (today-creation)
  118.          if str(age).find("day") != -1:
  119.             eta = "Age: "+str(age).split()[0]
  120.             if int(str(age).split()[0]) == 1:
  121.               eta += ' day\n'
  122.         else:
  123.           eta += ' days\n'
  124.          else:
  125.             eta = "Age: 0 day\n"
  126.      details(HEAL,HAPPINESS,CLEANILESS,eta)
  127.          if type == "PET_ALIEN":
  128.               conn.send(PET_ALIEN)
  129.          elif type == "PET_TURTLE":
  130.               conn.send(PET_TURTLE)
  131.          elif type == "PET_HUMAN":
  132.               conn.send(PET_HUMAN)
  133.          conn.send("What do you want to do with you're pet?\n")
  134.          conn.send("[1] Eat [2] Play [3] Clean [4] Quit\n---> ")
  135.          data = conn.recv(1024)
  136.          if data.find("1") != -1:
  137.             conn.send("GOOD! You're pet now is full of food!\n")
  138.             HEAL = "HEAL: 100\n"
  139.             HAPPINESS = "HAPPINESS: "+HAPPINESS+"\n"
  140.             CLEANILESS = "CLEANILESS: "+CLEANILESS+"\n"
  141.          elif data.find("2") != -1:
  142.             conn.send("GOOD! Your pet now is very happy!\n")
  143.             HEAL = "HEAL: "+HEAL+"\n"
  144.             HAPPINESS = "HAPPINESS: "+HAPPINESS+"\n"
  145.             CLEANILESS = "CLEANILESS: 100\n"
  146.          elif data.find("3") != -1:
  147.             conn.send("GOOD! Your pet now is so cute and clean!\n")
  148.             HEAL = "HEAL: "+HEAL+"\n"
  149.             CLEANILESS = "CLEANILESS: "+CLEANILESS+"\n"
  150.             HAPPINESS = "HAPPINESS: 100\n"
  151.          elif data.find("4") != -1:
  152.              HEAL = "HEAL: "+HEAL+"\n"
  153.              CLEANILESS = "CLEANILESS: "+CLEANILESS+"\n"
  154.              HAPPINESS = "HAPPINESS: "+HAPPINESS+"\n"
  155.              break
  156.          db1 = open("%s/Stats/%s_st.txt" %(PATH,name),"w")
  157.          db1.writelines(HEAL)
  158.          db1.writelines(HAPPINESS)
  159.          db1.writelines(CLEANILESS)
  160.          db1.close()
  161.    elif opt == '6':
  162.          conn.send("Connectio Closed")
  163.          conn.close()
  164.          quit = 1
  165.    else:
  166.       conn.send("Invalid Option\n")
  167.    quit = 0
  168.    return quit
  169.      
  170. def newid(log,PATH):
  171.    ip = "IP   --> "+addr[0]+"\n"
  172.    conn.send("Insert New User ID: ")
  173.    data = conn.recv(1020)
  174.    id = data.split("\r\n")[0]
  175.    user = "USER --> "+id+"\n"
  176.    conn.send("password query")
  177.    data = conn.recv(1024)
  178.    password = data.split("\r\n")[0]
  179.    password = "PASS --> "+password+"\n"
  180.    conn.send("Insert your pet's name: ")
  181.    data = conn.recv(1024)
  182.    PET_NAME = "PET  --> "+data
  183.    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))
  184.    data = conn.recv(1024)
  185.    if data.find("1") != -1:
  186.       PET_TYPE = "TYPE --> PET_ALIEN\n"
  187.       HEAL = "HEAL: 100\n"
  188.       HAPPINESS = "HAPPINESS: 100\n"
  189.       CLEANILESS = "CLEANILESS: 100\n"
  190.       EXPIRE ="EXPIRE: "+str(10+randint(-4,4))+"\n"
  191.       conn.send("Now you've got an alien!\n")
  192.    elif data.find("2") != -1:
  193.       PET_TYPE = "TYPE --> PET_TURTLE\n"
  194.       HEAL = "HEAL: 100\n"
  195.       HAPPINESS = "HAPPINESS: 100\n"
  196.       CLEANILESS = "CLEANILESS: 100\n"
  197.       EXPIRE ="EXPIRE: "+str(12+randint(-2,2))+"\n"
  198.       conn.send("Now you've got a turtle!\n")
  199.    elif data.find("3") != -1:
  200.       PET_TYPE = "TYPE --> PET_HUMAN\n"
  201.       HEAL = "HEAL: 100\n"
  202.       HAPPINESS = "HAPPINESS: 100\n"
  203.       CLEANILESS = "CLEANILESS: 100\n"
  204.       EXPIRE = "EXPIRE: "+str(8+randint(-3,3))+"\n"
  205.       conn.send("Now you've got an human!\n")
  206.    reg = open("%s/Users/%s.txt" %(PATH,id),"w")
  207.    reg1 = open("%s/Stats/%s_st.txt" %(PATH,id),"w")
  208.    reg.writelines(ip)
  209.    reg.writelines(user)
  210.    reg.writelines(password)
  211.    reg.writelines(PET_NAME)
  212.    reg.writelines(PET_TYPE)
  213.    reg.writelines(EXPIRE)
  214.    reg.writelines(str(datetime.date.today()))
  215.    reg1.writelines(HEAL)
  216.    reg1.writelines(HAPPINESS)
  217.    reg1.writelines(CLEANILESS)
  218.    reg.close()
  219.    reg1.close()
  220.    log += ctime()+" New Registration from %s" %(ip)
  221.    savelog(log)
  222.    conn.send("Registration Complete, restart to start to play!\n")
  223.    conn.send("Connection Closed")
  224.    conn.close()
  225.    
  226. def savelog(log,PATH):
  227.       log_write = open("%s/Log/log.txt" %(PATH),"a")
  228.       log_write.writelines(log)
  229.       log_write.close()
  230.  
  231. def details(HEAL,CLEANILESS,HAPPINESS,age):
  232.    conn.send(age)
  233.    conn.send("HEAL: %s\n" %(HEAL))
  234.    conn.send("HAPPINESS: %s\n" %(HAPPINESS))
  235.    conn.send("CLEANILESS: %s\n" %(CLEANILESS))
  236.    
  237.  
  238. def checker(time1,PATH):
  239.     files = listdir("%s/Users/" %(PATH))
  240.     for i in range(0,len(files)):
  241.         if files[i].find("_death.txt") != -1 or files[i].find("_message.txt") != -1 or files[i].find("_read.txt") != -1:
  242.             continue
  243.         else:
  244.            db = open("%s/Users/%s" %(PATH,files[i]),"r")
  245.            db1 = db.readlines()
  246.            type = db1[3].split()[4]
  247.            db.close()
  248.            files[i] = files[i].split(".txt")[0]
  249.            update(files[i],type,time1,PATH)
  250.         i+=1
  251.     i=0
  252.  
  253. def update(file,type,time1,PATH):
  254.     db = open("%s/Stats/%s_st.txt" %(PATH,file),"r")
  255.     db3 = db.readlines()
  256.     db.close()
  257.     HEAL = db3[0].split()[1]
  258.     HAPPINESS = db3[1].split()[1]
  259.     CLEANILESS = db3[2].split()[1]
  260.     HEAL = int(HEAL)
  261.     HAPPINESS = int(HAPPINESS)
  262.     CLEANILESS = int(CLEANILESS)
  263.     db = open("%s/Users/%s.txt" %(PATH,file),"r")
  264.     db1 = db.readlines()
  265.     db.close()
  266.     creation = db1[5].split("-")
  267.     creation = datetime.date(int(creation[0]),int(creation[1]),int(creation[2]))
  268.     expire = datetime.timedelta(days=int(db1[4].split()[1]))
  269.     today = datetime.date.today()
  270.     age = (today-creation)
  271.     if HEAL<=0 or HAPPINESS<=0 or CLEANILESS<=0 or age >= expire:
  272.        death(file)
  273.     else:
  274.        time2 = list(str(time()))[:-2]
  275.        time1 = list(str(time1))[:-2]
  276.        time3 = ''
  277.        time4 = ''
  278.        for i in range(0,len(time1)-1):
  279.            time3 += time1[i]
  280.            time4 += time2[i]
  281.            i+=1
  282.        time3, time4 = int(time3), int(time4)
  283.            type_hour = period()
  284.            if type_hour == 'day':
  285.            k = (time4-time3)/1800
  286.            else:
  287.                k = (time4-time3)/3600
  288.        if type == "PET_ALIEN":
  289.            HEAL -= 5*k
  290.            HAPPINESS -= 2*k
  291.            CLEANILESS -= 3*k
  292.        elif type == "PET_TURTLE":
  293.            HEAL -= 2*k
  294.            HAPPINESS -= 1*k
  295.            CLEANILESS -= 10*k
  296.        elif type == "PET_HUMAN":
  297.            HEAL -= 10*k
  298.            HAPPINESS -= 5*k
  299.            CLEANILESS -= 2*k
  300.        db1 = open("%s/Stats/%s_st.txt" %(PATH,file),"w")
  301.        HEAL = "HEAL: "+str(HEAL)+"\n"
  302.        HAPPINESS = "HAPPINESS: "+str(HAPPINESS)+"\n"
  303.        CLEANILESS = "CLEANILESS: "+str(CLEANILESS)+"\n"
  304.        written = HEAL+HAPPINESS+CLEANILESS
  305.        db1.writelines(written)
  306.        db1.close()
  307.  
  308. def death(file,PATH):
  309.         files = listdir("%s/Users/" %(PATH))
  310.         remove("%s/Stats/%s_st.txt" %(PATH,file))
  311.         remove("%s/Users/%s.txt" %(PATH,file))
  312.         mex = open("%s/Users/%s_death.txt" %(PATH,file),"w")
  313.         mex.writelines("You're pet is death!!\nYour account is been deleted, make one new to restart to play!\n\n")
  314.         mex.close()
  315.        
  316.  
  317. def period():
  318.     hour = ctime().split()[3].split(":")
  319.     if int(hour[0]) >= 0 and int(hour[0]) <= 8:
  320.         type_hour = 'night'
  321.     else:
  322.         type_hour = 'day'
  323.     return type_hour
  324.  
  325. #MAKING SOCKET
  326.  
  327. s = socket.socket()
  328. s.bind((HOST,PORT))
  329. s.listen(1)
  330.  
  331. #MAKING LOG AND VAR
  332.  
  333. log = ''
  334. log += ctime()+" ]--STARTING TAMAGOTCHI SERVER--[\n"
  335. time1 = ctime()
  336. name_server = "BUGHARDY SERVER, 1"
  337. owner_server = "bughardy"
  338. version_server = '1.1.0 (beta)'
  339. info_server = "Server: "+name_server+"\nStarted: "+time1+"\nOwner: "+owner_server+"\nVersion: "+version_server+"\n"
  340. #MAIN
  341. conf = raw_input("Do you need to configure the server? [y]: ")
  342. if conf == "n":
  343.     pass
  344. else:
  345.     configuration()
  346. pid = fork()
  347. while 1:
  348.    PATH = open("/home/pygotchi/pygotchi.txt","r").readlines()[0]
  349.    type_hour = period()
  350.    if type_hour == "day":
  351.        type_hour = "Attivita': Diurna\n"
  352.    else:
  353.        type_hour = "Attivita': Notturna\n"
  354.    sleep(0.5)
  355.    if pid != 0:
  356.       name = ''
  357.       quit = 0
  358.       text = open("%s/Log/log.txt" %(PATH),"a")
  359.       conn, addr = s.accept()
  360.       log += ctime()+" Request connection from "+addr[0]+"\n"
  361.       conn.send("id query")
  362.       data = conn.recv(1024)
  363.       user = data.split("\r\n")[0]
  364.       system("clear")
  365.       print log
  366.       try:
  367.          db = open("%s/Users/%s.txt" %(PATH,user),"r")
  368.          list_users_pass = db.readlines()
  369.          conn.send("password query")
  370.          data = conn.recv(1024)
  371.          password = data.split("\r\n")[0]
  372.          password_db = list_users_pass[2].split()[2]
  373.          if password == password_db:
  374.             conn.send("\n\nAccess Guaranted\n")
  375.             conn.send("\nWelcome %s\n\n" %(user))
  376.             time1 = time()      
  377.             name = list_users_pass[3].split()[2]
  378.             conn.send("menu query")
  379.             while 1:
  380.                data = conn.recv(1024)
  381.                choose = data
  382.                quit = activity(user,time1,choose,info_server,type_hour,PATH)
  383.                if quit == 1:
  384.                     break
  385.                else:
  386.                         conn.send("menu query")
  387.             log += time.ctime()+" User: %s logged in, with IP: %s\n" %(user,addr[0])
  388.             system("clear")
  389.             print log
  390.             savelog(log,PATH)
  391.          else:
  392.             conn.send("\nAccess Denied\n\n")
  393.             log += ctime()+" User: %s wrong password, with IP: %s\n" %(user,addr[0])
  394.             conn.send("Connection Closed\n")
  395.             system("clear")
  396.             print log
  397.             savelog(log)
  398.             conn.close()
  399.       except:
  400.         try:
  401.           contr = listdir("%s/Users/" %(PATH))
  402.           for i in range(0,len(contr)):
  403.              if contr[i].find("_death.txt") != -1:
  404.                 dead = 1
  405.                 died = open("%s/Users/%s" %(PATH,contr[i]),"r")
  406.                 mex = died.readlines()
  407.                 mex = mex[0]+mex[1]
  408.                 died.close()
  409.                 conn.send(mex)
  410.                 remove("%s/Users/%s" %(PATH,contr[i]))
  411.                 conn.send("Connection Closed\n")
  412.                 conn.close()
  413.              i+=1
  414.           conn.send("This User Doesn't Exist, do you want make a new one? [y/n] ")
  415.           data = conn.recv(1024)
  416.           if data.find("y") != -1:
  417.              newid(log,PATH)
  418.           else:
  419.             conn.send("Connection Closed\n")
  420.             conn.close()
  421.         except:
  422.             pass
  423.    else:
  424.       time1 = time()
  425.       while 1:
  426.         #try:
  427.         checker(time1,PATH)
  428.         sleep(10)
  429.         #except:
  430.         #   sleep(20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement