mirror of
https://github.com/ammaraskar/pyCraft.git
synced 2025-01-07 16:37:59 +01:00
Progress
This commit is contained in:
parent
6318577e5c
commit
dd11f5fd4f
40
NetworkManager.py
Normal file
40
NetworkManager.py
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
import socket
|
||||||
|
import wx
|
||||||
|
import PacketManager
|
||||||
|
import urllib2
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
#Eclipse pyDev error fix
|
||||||
|
wx=wx
|
||||||
|
|
||||||
|
class ServerConnection:
|
||||||
|
|
||||||
|
def __init__(self, window, username, password, sessionID, server, port):
|
||||||
|
self.username = username
|
||||||
|
self.password = password
|
||||||
|
self.sessionID = sessionID
|
||||||
|
self.server = server
|
||||||
|
self.port = port
|
||||||
|
self.window = window
|
||||||
|
|
||||||
|
def attemptConnection(self):
|
||||||
|
self.socket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
|
||||||
|
try:
|
||||||
|
self.socket.connect ( ( self.server, self.port ) )
|
||||||
|
PacketManager.sendString("\x02", self.username + self.server + ":" + str(self.port), self.socket)
|
||||||
|
response = PacketManager.readStringFromSocket(self.socket)
|
||||||
|
serverid = response['string']
|
||||||
|
if(serverid != '-'):
|
||||||
|
url = "http://session.minecraft.net/game/joinserver.jsp?user=" + self.username + "&sessionId=" + self.sessionID + "&serverId=" + serverid
|
||||||
|
response = urllib2.urlopen(url).read()
|
||||||
|
if(response != "OK"):
|
||||||
|
self.window.connectStatus.SetLabel("Response from sessions.minecraft.net wasn't OK")
|
||||||
|
return False
|
||||||
|
PacketManager.sendLoginRequest(self.socket, self.username)
|
||||||
|
except Exception, e:
|
||||||
|
self.window.connectStatus.SetForegroundColour(wx.RED)
|
||||||
|
self.window.connectStatus.SetLabel("Connection to server failed")
|
||||||
|
traceback.print_exc()
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
@ -7,6 +7,7 @@ import PacketManager
|
|||||||
import NetworkManager
|
import NetworkManager
|
||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
|
import thread
|
||||||
import wx
|
import wx
|
||||||
import wxPython
|
import wxPython
|
||||||
|
|
||||||
@ -21,6 +22,7 @@ class Window(wx.Frame):
|
|||||||
def __init__(self, parent, title):
|
def __init__(self, parent, title):
|
||||||
self.username = ""
|
self.username = ""
|
||||||
self.sessionID = ""
|
self.sessionID = ""
|
||||||
|
self.password = ""
|
||||||
self.LoggedIn = False
|
self.LoggedIn = False
|
||||||
|
|
||||||
super(Window, self).__init__(parent, title=title,
|
super(Window, self).__init__(parent, title=title,
|
||||||
@ -36,15 +38,15 @@ class Window(wx.Frame):
|
|||||||
self.label = wx.StaticText(self, -1, label=u'Username')
|
self.label = wx.StaticText(self, -1, label=u'Username')
|
||||||
self.sizer.Add( self.label, (0,0),(1,1), wx.EXPAND )
|
self.sizer.Add( self.label, (0,0),(1,1), wx.EXPAND )
|
||||||
|
|
||||||
self.entry = wx.TextCtrl(self,-1)
|
self.entry = wx.TextCtrl(self,-1, size=(200,23))
|
||||||
self.sizer.Add(self.entry,(0,1),(1, 2),wx.EXPAND | wx.ALIGN_LEFT)
|
self.sizer.Add(self.entry,(0,1),(1, 4),wx.EXPAND | wx.ALIGN_LEFT)
|
||||||
self.Bind(wx.EVT_TEXT_ENTER, self.onPressEnterOnFields, self.entry)
|
self.Bind(wx.EVT_TEXT_ENTER, self.onPressEnterOnFields, self.entry)
|
||||||
|
|
||||||
self.label2 = wx.StaticText(self, -1, label=u'Password')
|
self.label2 = wx.StaticText(self, -1, label=u'Password')
|
||||||
self.sizer.Add( self.label2, (2,0),(1,2), wx.EXPAND)
|
self.sizer.Add( self.label2, (2,0),(1,2), wx.EXPAND)
|
||||||
|
|
||||||
self.entry2 = wx.TextCtrl(self, -1, style = wx.TE_PASSWORD)
|
self.entry2 = wx.TextCtrl(self, -1, size=(200,23), style = wx.TE_PASSWORD)
|
||||||
self.sizer.Add(self.entry2,(2,2),(2,2), wx.EXPAND | wx.ALIGN_LEFT)
|
self.sizer.Add(self.entry2,(2,2),(2,4), wx.EXPAND | wx.ALIGN_LEFT)
|
||||||
self.Bind(wx.EVT_TEXT_ENTER, self.onPressEnterOnFields, self.entry2)
|
self.Bind(wx.EVT_TEXT_ENTER, self.onPressEnterOnFields, self.entry2)
|
||||||
|
|
||||||
button = wx.Button(self,-1,label="Login")
|
button = wx.Button(self,-1,label="Login")
|
||||||
@ -52,7 +54,7 @@ class Window(wx.Frame):
|
|||||||
self.Bind(wx.EVT_BUTTON, self.OnButtonClick, button)
|
self.Bind(wx.EVT_BUTTON, self.OnButtonClick, button)
|
||||||
|
|
||||||
self.status = wx.StaticText(self, -1,)
|
self.status = wx.StaticText(self, -1,)
|
||||||
self.sizer.Add(self.status,(5,1),(5,2), wx.EXPAND)
|
self.sizer.Add(self.status,(5,2),(5,15), wx.EXPAND)
|
||||||
|
|
||||||
self.sizer.AddGrowableCol(0)
|
self.sizer.AddGrowableCol(0)
|
||||||
self.SetSizerAndFit(self.sizer)
|
self.SetSizerAndFit(self.sizer)
|
||||||
@ -85,20 +87,29 @@ class Window(wx.Frame):
|
|||||||
self.SetSizerAndFit(self.sizer)
|
self.SetSizerAndFit(self.sizer)
|
||||||
|
|
||||||
def onConnectClick(self, event):
|
def onConnectClick(self, event):
|
||||||
|
StuffEnteredIntoBox = self.AddressEntry.GetValue()
|
||||||
self.sizer.DeleteWindows()
|
self.sizer.DeleteWindows()
|
||||||
self.sizer = wx.GridBagSizer()
|
self.sizer = wx.GridBagSizer()
|
||||||
self.connectStatus = wx.StaticText(self, -1, label=u'Connecting ...')
|
self.connectStatus = wx.StaticText(self, -1, label=u'Connecting ...')
|
||||||
self.connectStatus.SetForegroundColour(wx.BLUE)
|
self.connectStatus.SetForegroundColour(wx.BLUE)
|
||||||
self.sizer.Add(self.connectStatus, (0,0), (0,0))
|
self.sizer.Add(self.connectStatus, (0,0), (0,0))
|
||||||
self.connectStatus.Center()
|
self.connectStatus.Center()
|
||||||
|
if ':' in StuffEnteredIntoBox:
|
||||||
|
StuffEnteredIntoBox = StuffEnteredIntoBox.split(":")
|
||||||
|
host = StuffEnteredIntoBox[0]
|
||||||
|
port = StuffEnteredIntoBox[1]
|
||||||
|
else:
|
||||||
|
host = StuffEnteredIntoBox
|
||||||
|
port = 25565
|
||||||
|
self.connection = NetworkManager.ServerConnection(self, self.username, self.password, self.sessionID, host, port)
|
||||||
|
thread.start_new_thread(self.connection.attemptConnection, ())
|
||||||
|
|
||||||
def OnButtonClick(self, event):
|
def OnButtonClick(self, event):
|
||||||
if(self.entry.GetValue() == ""):
|
if(self.entry.GetValue() == ""):
|
||||||
self.status.set("Enter a username sherlock")
|
self.status.SetLabel("Enter a username sherlock")
|
||||||
return
|
return
|
||||||
if(self.entry2.GetValue() == ""):
|
if(self.entry2.GetValue() == ""):
|
||||||
self.status.set("Enter a password you derp")
|
self.status.SetLabel("Enter a password you derp")
|
||||||
return
|
return
|
||||||
password = self.entry2.GetValue()
|
password = self.entry2.GetValue()
|
||||||
username = self.entry.GetValue()
|
username = self.entry.GetValue()
|
||||||
@ -113,10 +124,10 @@ class Window(wx.Frame):
|
|||||||
|
|
||||||
def onPressEnterOnFields(self, event):
|
def onPressEnterOnFields(self, event):
|
||||||
if(self.entry.GetValue() == ""):
|
if(self.entry.GetValue() == ""):
|
||||||
self.status.set("Enter a username sherlock")
|
self.status.SetLabel("Enter a username sherlock")
|
||||||
return
|
return
|
||||||
if(self.entry2.GetValue() == ""):
|
if(self.entry2.GetValue() == ""):
|
||||||
self.status.set("Enter a password you derp")
|
self.status.SetLabel("Enter a password you derp")
|
||||||
return
|
return
|
||||||
password = self.entry2.GetValue()
|
password = self.entry2.GetValue()
|
||||||
username = self.entry.GetValue()
|
username = self.entry.GetValue()
|
||||||
@ -160,6 +171,7 @@ class MinecraftLoginThread(threading.Thread):
|
|||||||
return
|
return
|
||||||
response = response.split(":")
|
response = response.split(":")
|
||||||
self.window.username = response[2]
|
self.window.username = response[2]
|
||||||
|
self.window.password = self.password
|
||||||
self.window.sessionID = response[3]
|
self.window.sessionID = response[3]
|
||||||
self.window.LoggedIn = True
|
self.window.LoggedIn = True
|
||||||
KeepConnectionAlive(self.username, self.password).start()
|
KeepConnectionAlive(self.username, self.password).start()
|
||||||
@ -172,7 +184,8 @@ class KeepConnectionAlive(threading.Thread):
|
|||||||
self.password = password
|
self.password = password
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while True:
|
while True:
|
||||||
|
time.sleep(300)
|
||||||
url = 'https://login.minecraft.net'
|
url = 'https://login.minecraft.net'
|
||||||
header = {'Content-Type' : 'application/x-www-form-urlencoded'}
|
header = {'Content-Type' : 'application/x-www-form-urlencoded'}
|
||||||
data = {'user' : self.username,
|
data = {'user' : self.username,
|
||||||
@ -187,7 +200,6 @@ class KeepConnectionAlive(threading.Thread):
|
|||||||
popup = wx.MessageBox('Keep alive to minecraft.net failed', 'Warning',
|
popup = wx.MessageBox('Keep alive to minecraft.net failed', 'Warning',
|
||||||
wx.OK | wx.ICON_ERROR)
|
wx.OK | wx.ICON_ERROR)
|
||||||
popup.ShowModal()
|
popup.ShowModal()
|
||||||
time.sleep(300)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app = wx.App()
|
app = wx.App()
|
||||||
@ -233,4 +245,3 @@ while True:
|
|||||||
PacketManager.handleIncomingPacket(mySocket)
|
PacketManager.handleIncomingPacket(mySocket)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user