Hey!
Ich hab nen Server geschrieben der an alle neu eingeloggten Spieler die Positionen der anderen schon eingeloggten Spieler senden soll, und an die die schon eingeloggt sind die Daten des neuen Spielers senden sollen.
Das funktioniert irgendwie nicht...
Warum? (Ich habe meinen alten BB Server nämlich hiermit auf BMax umgeschrieben, mit BB hat's gefunzt...)
BlitzMax: [AUSKLAPPEN] [EINKLAPPEN] SuperStrict
Framework Vertex.BNetEx Import BRL.System Import BRL.Blitz Import BRL.Retro Import BRL.Math Import BRL.Random Import BRL.LinkedList Import BRL.PolledInput Import BRL.KeyCodes Import BRL.Timer
Include "types.bmx"
Const NET_LOGIN:Byte = 200 Const NET_LOGOUT:Byte = 202 Const NET_REG:Byte = 100 Const NET_CL_MOV_FORWARD:Byte = 10 Const NET_CL_MOV_BACK:Byte = 11 Const NET_CL_MOV_LEFT:Byte = 12 Const NET_CL_MOV_RIGHT:Byte = 13
Local p:player = New player p.name = "test_user" p.pass = "poo" p.mail = "" p.level = 1 p.class = 1 p.max_hp = 100 p.max_mp = 60 p.str = 3 p.dex = 2 p.vit = 4 p.gold = 100 p.ip = 18988 p.port = 12312
Const SERVERPORT:Int = 8000
Global udpStream:TUDPStream = New TUDPStream udpStream.Init() udpStream.SetLocalPort(SERVERPORT) udpStream.SetRemoteIp(TNetwork.IntIp("127.0.0.1"))
If Not udpStream Then Notify "CreateUDPStream Failed!";End
Print "Server started successfully!" Print "Server Port: "+SERVERPORT
While Not AppTerminate() readUDP() Wend udpStream.Close() End
Function readUDP() Local recvIP:Int recvIP = udpStream.recvMsg() If recvIP<>0 Then Local ip:Int = udpStream.getMsgIP() Local port:Short = udpStream.getMsgPort() udpStream.SetRemoteIp(ip) udpStream.SetRemotePort(port) Local rByte:Byte rByte = udpStream.ReadByte() Select rByte Case NET_REG Print "Client registrating..." Local name$ = udpStream.ReadLine$() Local pass$ = udpStream.ReadLine$() Local mail$ = udpStream.ReadLine$() ChangeDir(".\users\") Local dir:Int = ReadDir(CurrentDir()) Local nFile:String Repeat nFile$ = NextFile(dir) If nFile$="" Then Exit If FileType(nFile$)=1 Then If Instr(name$+".txt",nFile$) Then udpStream.WriteByte(9) udpStream.SendMsg() Print "User already exists" Exit Else Local file:TStream = WriteFile(name$+".txt") If Not file RuntimeError("Konnte keine Datei schreiben unter: ~n" + CurrentDir() + "/" + name$ + ".txt") WriteLine file,"[AccData]" WriteLine file,name$ WriteLine file,pass$ WriteLine file,mail$ WriteLine file,"[CharData]" WriteLine file,"!NO_PLAYER" CloseFile(file) udpStream.WriteByte(8) udpStream.SendMsg() EndIf EndIf Until nFile$ = "" CloseDir(dir) Case NET_LOGIN Local nam$ = udpStream.ReadLine() Local pas$ = udpStream.ReadLine() ChangeDir(".\users\") Local dir:Int = ReadDir(CurrentDir()) Local nFile:String Local inDB:Byte Repeat nFile$ = NextFile(dir) If nFile$="" Then Exit If FileType(nFile$)=1 Then If Instr(nam$+".txt",nFile$) Then Local file:TStream = ReadFile(nam$+".txt") For Local i:Byte = 0 To 1 ReadLine(file) Next Local password:String = ReadLine(file) CloseFile(file) If pas$=password$ Then inDB = 1 Else inDB = 0 EndIf Exit Else inDB = 0 EndIf EndIf Until nFile$ = "" CloseDir(dir) Local go:Byte If inDB<>0 Then For Local p:Player = EachIn Player.list If nam$=p.name Then go = 0 Print "You ARE logged in! Don't try to kill the server! (Shit you killed it... Bug#1)" Exit Else go = 1 Exit EndIf Next If go<>0 Then For Local p:Player = EachIn Player.list If ip<>p.ip And port<>p.port Then udpStream.WriteByte(1) udpStream.WriteInt(ip) udpStream.WriteInt(port) udpStream.WriteLine(nam$) udpStream.SetRemoteIp(p.ip) udpStream.SetRemotePort(p.port) udpStream.SendMsg() Print "Alt kriegt: "+nam$ EndIf Next For Local p:Player = EachIn Player.list If ip=p.ip And port=p.port Then udpStream.WriteByte(1) udpStream.WriteInt(p.ip) udpStream.WriteInt(p.port) udpStream.WriteLine(p.name) udpStream.SetRemoteIp(ip) udpStream.SetRemotePort(port) udpStream.SendMsg() Print "Neu kriegt: "+p.name EndIf Next Local lClient:player = LoadPlayerData(nam$,pas$) lClient.CalculateValues() lClient.ip = ip lClient.port = port EndIf EndIf Case NET_LOGOUT For Local p:player = EachIn player.list If ip = p.ip And port = p.port Then p.Save() Print "Client logged out ("+p.name+","+p.pass+")" p.Loeschen() EndIf Next For Local p:player = EachIn player.list udpStream.WriteByte(2) udpStream.WriteInt(ip) udpStream.WriteInt(port) udpStream.SetRemoteIP(p.ip) udpStream.SetRemotePort(p.port) udpStream.SendMsg() Next Case NET_CL_MOV_FORWARD For Local p:Player = EachIn player.list If ip = p.ip And port = p.port Then Local yaw:Float = udpStream.ReadFloat() p.x = p.x - (Sin(yaw)*1.0) p.z = p.z + (Cos(yaw)*1.0) Print p.x+","+p.z udpStream.WriteByte(3) udpStream.WriteFloat(p.x) udpStream.WriteFloat(p.z) udpStream.SetRemoteIP(ip) udpStream.SetRemotePort(port) udpStream.SendMsg() EndIf Next Case NET_CL_MOV_BACK For Local p:Player = EachIn player.list If ip = p.ip And port = p.port Then Local yaw:Float = udpStream.ReadFloat() p.x = p.x + (Sin(yaw)*1.0) p.z = p.z - (Cos(yaw)*1.0) Print p.x+","+p.z udpStream.WriteByte(3) udpStream.WriteFloat(p.x) udpStream.WriteFloat(p.z) udpStream.SetRemoteIP(ip) udpStream.SetRemotePort(port) udpStream.SendMsg() EndIf Next Case NET_CL_MOV_LEFT For Local p:Player = EachIn player.list If ip = p.ip And port = p.port Then Local yaw:Float = udpStream.ReadFloat() p.x# = p.x# - (Cos(yaw#)*1.0) p.z# = p.z# - (Sin(yaw#)*1.0) Print p.x#+","+p.z# udpStream.WriteByte(3) udpStream.WriteFloat(p.x#) udpStream.WriteFloat(p.z#) udpStream.SetRemoteIP(ip) udpStream.SetRemotePort(port) udpStream.SendMsg() EndIf Next Case NET_CL_MOV_RIGHT For Local p:Player = EachIn player.list If ip = p.ip And port = p.port Then Local yaw:Float = udpStream.ReadFloat() p.x# = p.x# + (Cos(yaw#)*1.0) p.z# = p.z# + (Sin(yaw#)*1.0) Print p.x#+","+p.z# udpStream.WriteByte(3) udpStream.WriteFloat(p.x#) udpStream.WriteFloat(p.z#) udpStream.SetRemoteIP(ip) udpStream.SetRemotePort(port) udpStream.SendMsg() EndIf Next Default Print "Unknown message from: " + ip End Select EndIf Local sendTime:Byte If sendTime<MilliSecs() Then sendTime = MilliSecs()+16 For Local s:Player = EachIn Player.list For Local p:Player = EachIn Player.list If p.port<>s.port Then udpStream.WriteByte(4) udpStream.WriteInt(s.ip) udpStream.WriteInt(s.port) udpStream.WriteFloat(s.x#) udpStream.WriteFloat(s.z#) udpStream.SetRemoteIP(p.ip) udpStream.SetRemotePort(p.port) udpStream.SendMsg() EndIf Next Next EndIf EndFunction
Function LoadPlayerData:player(name:String,pass:String) Local file:TStream = ReadFile(name+".txt") If Not file Then Return(Null) Else Local newClient:player = New player newClient.name = name newClient.pass = pass newClient.mail = "" While Not Eof(file) Local l_Line:String = ReadLine(file) If l_Line.startswith("[") l_Line = l_Line[1..(Len(l_Line) - 1)] Select l_Line Case "TEAM" newClient.team = Byte(ReadLine(file)) Case "GUILD" newClient.guild = Short(ReadLine(file)) Case "LEVEL" newClient.level = Short(ReadLine(file)) Case "SKILLPOINTS" newClient.skillpoints = Short(ReadLine(file)) Case "STATS" Local l_StatsArray:String[] = ReadValue(ReadLine(file),4) newClient.str = Short(l_StatsArray[0]) newClient.dex = Short(l_StatsArray[1]) newClient.vit = Short(l_StatsArray[2]) newClient.con = Short(l_StatsArray[3]) Case "GOLD" newClient.gold = Int(ReadLine(file)) EndSelect EndIf Wend CloseFile(file) Print "Daten geladen... ("+newClient.name+", "+newClient.pass+")" Return(newClient) EndIf EndFunction
Function ReadValue:String[](f_Line:String, f_Count:Byte) Local l_LastApp:Short = 0 Local l_Result:String[f_Count] For Local l_Count:Byte = 1 To f_Count Local l_CurrentApp:Short = f_Line.find(";" , l_LastApp) If l_Count < f_Count l_Result[l_Count - 1] = f_Line[l_LastApp..l_CurrentApp] Else l_Result[l_Count - 1] = f_Line[l_LastApp..] EndIf l_LastApp = l_CurrentApp + 1 Next Return(l_Result) EndFunction
Die "types.bmx":
BlitzMax: [AUSKLAPPEN] [EINKLAPPEN] Type player Global list:TList = New TList Field name:String Field pass:String Field mail:String Field level:Short Field team:Byte Field guild:Short Field class:Byte Field ep:Int Field x:Float Field z:Float Field map:Byte Field ip:Int Field port:Short Field hp:Int Field max_hp:Int Field mp:Int Field max_mp:Int Field str:Short Field dex:Short Field vit:Short Field con:Short Field skillpoints:Short Field attack_speed:Byte Field walk_speed:Byte Field gold:Int Method New() list.addLast(Self) EndMethod Method Loeschen() list.Remove(Self) EndMethod Method Save() Local file:TStream = WriteFile(Self.name+".txt") file.WriteLine("[BASEDATA]");file.WriteLine(Self.name);file.WriteLine(Self.pass);file.WriteLine(Self.mail) file.WriteLine("[POSITION]");file.WriteLine(Self.x+";"+Self.z) file.WriteLine("[LEVEL]");file.WriteLine(Self.level) file.WriteLine("[TEAM]");file.WriteLine(Self.team) file.WriteLine("[GUILD]");file.WriteLine(Self.guild) file.WriteLine("[CLASS]");file.WriteLine(Self.class) file.WriteLine("[BASE_HP]");file.WriteLine(Self.max_hp) file.WriteLine("[BASE_MP]");file.WriteLine(Self.max_mp) file.WriteLine("[SKILLPOINTS]");file.WriteLine(Self.skillpoints) file.WriteLine("[STATS]");file.WriteLine(Self.str+";"+Self.dex+";"+Self.vit+";"+Self.con) file.WriteLine("[GOLD]");file.WriteLine(Self.gold) file.WriteLine("[INVENTORY]") CloseFile(file) EndMethod Method CalculateValues() Self.max_hp=Self.vit*18+30 Self.max_mp=Self.con*16+20 For Local l_Count:Byte=0 To 9 Next EndMethod End Type
Der Client:
BlitzBasic: [AUSKLAPPEN] [EINKLAPPEN] Global recvIP,readAv,rByte,sndTime Global x#,z#
Graphics3D 640,480,32,2 SetBuffer BackBuffer()
Global cam = CreateCamera() PositionEntity cam,0,5,-10 Global player = CreateSphere(8)
SeedRnd MilliSecs() Global udpStream = CreateUDPStream()
If Not udpStream Then RuntimeError("Geht nicht...")
Global ip = INT_IP("127.0.0.1")
Type client Field x#,y#,z# Field name$ Field ip,port Field mesh End Type
login("link90","bääm") While Not KeyHit(1) Cls readUDP() sendUDP() If KeyDown(17) Then If MilliSecs()>sndTime Then sndTime = MilliSecs()+16 WriteByte udpStream,10 WriteFloat udpStream,0.0 SendUDPMsg udpStream,ip,8000 EndIf EndIf If KeyDown(31) Then If MilliSecs()>sndTime Then sndTime = MilliSecs()+16 WriteByte udpStream,11 WriteFloat udpStream,0.0 SendUDPMsg udpStream,ip,8000 EndIf EndIf If KeyDown(30) Then If MilliSecs()>sndTime Then sndTime = MilliSecs()+16 WriteByte udpStream,12 WriteFloat udpStream,0.0 SendUDPMsg udpStream,ip,8000 EndIf EndIf If KeyDown(32) Then If MilliSecs()>sndTime Then sndTime = MilliSecs()+16 WriteByte udpStream,13 WriteFloat udpStream,0.0 SendUDPMsg udpStream,ip,8000 EndIf EndIf PositionEntity player,x#,0,z# UpdateWorld() RenderWorld() Oval x#,-z#,10,10 Flip 0 Wend WriteByte udpStream,202 SendUDPMsg udpStream,ip,8000 CloseUDPStream(udpStream) End
Function readUDP() recvIP = RecvUDPMsg(udpStream) If recvIP<>0 Then rByte = ReadByte(udpStream) Select rByte Case 1 c.client = New client c\ip = ReadInt(udpStream) c\port = ReadInt(udpStream) c\name$ = ReadLine(udpStream) c\mesh = CopyEntity(player) EntityColor c\mesh,255,255,0 EntityPickMode(c\mesh,2) DebugLog(c\name$+" connected") Case 2 rIp = ReadInt(udpStream) port = ReadInt(udpStream) For c.client = Each client If c\ip = rIp And c\port = port Then FreeEntity c\mesh DebugLog("Client deleted! ("+c\name$+")") Delete c.client EndIf Next Case 3 x# = ReadFloat(udpStream) z# = ReadFloat(udpStream) Case 4 rIp = ReadInt(udpStream) port = ReadInt(udpStream) For c.client = Each client If c\ip=rIp And c\port= portThen c\x# = ReadFloat(udpStream) c\z# = ReadFloat(udpStream) EndIf Next End Select EndIf End Function
Function sendUDP() End Function
Function login(name$,pass$) WriteByte udpStream,200 WriteLine udpStream,name$ WriteLine udpStream,pass$ SendUDPMsg udpStream,ip,8000 End Function
Function register(name$,pass$,mail$) WriteByte udpStream,100 WriteLine udpStream,name$ WriteLine udpStream,pass$ WriteLine udpStream,mail$ SendUDPMsg udpStream,ip,8000 End Function
Function INT_IP(IP$) a1=Int(Left(IP$,Instr(IP$,".")-1)):IP$=Right(IP$,Len(IP$)-Instr(IP$,".")) a2=Int(Left(IP$,Instr(IP$,".")-1)):IP$=Right(IP$,Len(IP$)-Instr(IP$,".")) a3=Int(Left(IP$,Instr(IP$,".")-1)):IP$=Right(IP$,Len(IP$)-Instr(IP$,".")) a4=Int(IP$) Return (a1 Shl 24) + (a2 Shl 16) + (a3 Shl 8 ) +a4 End Function
Mfg,
N0X
P.S.: Einloggen etc. funzt, auch registrieren. Nur eben mit den Übertragungen das ein neuer Spieler da ist etc.
|