• Login

Dear visitor, welcome to RPG Studio - Make your World real. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

Warkurus

Ankömmling

  • "Warkurus" started this thread

Motto: Shit happens.

  • Send private message

1

Tuesday, August 20th 2013, 2:15am

Kommunikation zwischen Java und Ruby

Hiho,

ich habe mal versucht einen java server zu schreiben mit welchem man übers internet pokemon zu zweit spielen kann. nun kann ich ihn leider nicht alleine testen, weil ich tcp/ip benutze und wollte desshalb fragen, ob ihr irgendwelche logischen fehler im code finden könnt oder lust auf ein paar testläufe habt.
(kleines Update)Der Java-Server:
Spoiler

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.io.*;
import java.net.*;
import org.apache.commons.io.*;
 
public class simple_server {
 
	static Socket[] client;
	static int n=0;
	static OutputStream[] os;
	static InputStream[] is;
	static StringWriter writer=new StringWriter();
	static String encoding="UTF-8";
	static String stringstream=new String();
	static String[] buffer;
	static Boolean end=false;
 
	public static void main(String argv[]) throws Exception 
	{
		ServerSocket server = new ServerSocket(7654);
		client=new Socket[2];
		os=new OutputStream[2];
		is=new InputStream[2];
		for (n=0;n<2;n++)
		{
			client[n]=server.accept();
			System.out.print("connected \n");
			os[n]= client[n].getOutputStream();
			is[n]= client[n].getInputStream();
		}
		buffer =new String[2];
 
		while (end=false)
		{
			//exchange streams
			os[0].write(is[1].read());
			os[1].write(is[0].read());
			//check streams for break-off condition
			for (n=0;n<2;n++){
				IOUtils.copy(is[n], writer, encoding);
				stringstream=writer.toString();
				if (buffer[n] !="end")
				{
					if (stringstream==",")
					{
						System.out.print("buffer"+ n +"="+ buffer[n]+"\n");
						buffer[n]="";
					}else{
						buffer[n]=buffer[n]+stringstream;
					}
				}else{
					end=true;
				}
			}
		}
		client[0].close();
		client[1].close();
		server.close();
	}
}

highlight to read

Der Teil aus dem Menü:
Spoiler

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
 elsif cmdHostGame>=0 && command==cmdHostGame
$skip=true
#system 'start Ruby193/bin/ruby.exe multiplayer_server.rb'
$socket=TCPSocket.new('127.0.0.1', 7654)
$host=true
pbSendPlayerInformation
pbGetPlayerInformation
scene=pbNewBattleScene
battle=PokeBattle_Battle.new(scene,@party1,@party2,$Trainer,trainer[0])
Events.onStartBattle.trigger(nil,nil)
battle.internalbattle=true
pbPrepareBattle(battle)
decision=0
trainerbgm=pbGetTrainerBattleBGM(trainer[0])
pbBattleAnimation(trainerbgm) {
pbSceneStandby {
decision=battle.pbStartBattle
}
if $PokemonGlobal.partner
for i in $Trainer.party; i.heal; end
end
if decision==2 || decision==5
if canlose
for i in $Trainer.party; i.heal; end
for i in 0...10
Graphics.update
end
else
$game_system.bgm_unpause
$game_system.bgs_unpause
Kernel.pbStartOver
end
end
Events.onEndBattle.trigger(nil,decision)
}
$socket.close
$skip=nil
$host=nil
elsif cmdConnecttoGame>=0 && command==cmdConnecttoGame
$skip=true
@scene.pbSetMultiplayerInformation("Enter IP")
Graphics.update
target_ip=""
ip_finished = false
while ip_finished==false do
Keyboard.update
if Keyboard.trigger(Keys::N_0) or Keyboard.trigger(Keys::NUM_0)
target_ip +="0"
elsif Keyboard.trigger(Keys::N_1) or Keyboard.trigger(Keys::NUM_1)
target_ip +="1"
elsif Keyboard.trigger(Keys::N_2) or Keyboard.trigger(Keys::NUM_2)
target_ip +="2"
elsif Keyboard.trigger(Keys::N_3) or Keyboard.trigger(Keys::NUM_3)
target_ip +="3"
elsif Keyboard.trigger(Keys::N_4) or Keyboard.trigger(Keys::NUM_4)
target_ip +="4"
elsif Keyboard.trigger(Keys::N_5) or Keyboard.trigger(Keys::NUM_5)
target_ip +="5"
elsif Keyboard.trigger(Keys::N_6) or Keyboard.trigger(Keys::NUM_6)
target_ip +="6"
elsif Keyboard.trigger(Keys::N_7) or Keyboard.trigger(Keys::NUM_7)
target_ip +="7"
elsif Keyboard.trigger(Keys::N_8) or Keyboard.trigger(Keys::NUM_8)
target_ip +="8"
elsif Keyboard.trigger(Keys::N_9) or Keyboard.trigger(Keys::NUM_9)
target_ip +="9"
elsif Keyboard.trigger(Keys::PERIOD)
target_ip +="."
elsif Kboard.keyb($R_Key_DELETE)==1 or Kboard.keyb($R_Key_BACK)==1
target_ip =""
elsif Kboard.keyb($R_Key_RETURN)==1
check=target_ip.split('.')
k=0
if check.length==4
for i in 0..3
check[i]=check[i].to_i
k+=1 if check[i]>=0 && check[i]<=255
end
end
if k != 4 and target_ip !=""
Kernel.pbMessage(_INTL("IP address is incorrect."))
target_ip=""
end
elsif Kboard.keyb($R_Key_Y)==1 && k==4
ip_finished= true
break
elsif Kboard.keyb($R_Key_N)==1 && k==4
target_ip =""
k=0
end
if target_ip ==""
@scene.pbSetMultiplayerInformation("Enter IP")
else
@scene.pbSetMultiplayerInformation(target_ip)
end
@scene.pbSetAuxiliaryWindow("Is "+target_ip+" correct? (Y/N)") if k==4
Graphics.update
end
$socket= TCPSocket.new(target_ip, 7654)
$socket.write('connected')
$host=false
pbSendPlayerInformation
pbGetPlayerInformation
print("Client initialised.")
scene=pbNewBattleScene
@scene=scene
battle=PokeBattle_Battle.new(scene,@party1,@party2,$Trainer,trainer[0])
Events.onStartBattle.trigger(nil,nil)
battle.internalbattle=true
pbPrepareBattle(battle)
decision=0
trainerbgm=pbGetTrainerBattleBGM(trainer[0])
pbBattleAnimation(trainerbgm) {
pbSceneStandby {
decision=battle.pbStartBattle
}
if $PokemonGlobal.partner
for i in $Trainer.party; i.heal; end
end
if decision==2 || decision==5
if canlose
for i in $Trainer.party; i.heal; end
for i in 0...10
Graphics.update
end
else
$game_system.bgm_unpause
$game_system.bgs_unpause
Kernel.pbStartOver
end
end
Events.onEndBattle.trigger(nil,decision)
}
$socket.close
$skip=nil
$host=nil

highlight to read

Die Methoden pbGetPlayerInformation und pbSendPlayerInformation:
Spoiler

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
 def pbSendPlayerInformation
information="$Trainer.name="+$Trainer.name.to_s+",$Trainer.trainertype="+$Trainer.trainertype.to_s+","
$socket.write(information)
for i in 1..$Trainer.party.length do
$socket.write("index="+(i-1).to_s+",")
$socket.write("totalhp="+$Trainer.party[i-1].totalhp.to_s+",")
$socket.write("attack="+$Trainer.party[i-1].attack.to_s+",")
$socket.write("defense="+$Trainer.party[i-1].defense.to_s+",")
$socket.write("speed="+$Trainer.party[i-1].speed.to_s+",")
$socket.write("spatk="+$Trainer.party[i-1].spatk.to_s+",")
$socket.write("spdef="+$Trainer.party[i-1].spdef.to_s+",")
for j in 0..5 do
$socket.write("iv["+j.to_s+"]="+$Trainer.party[i-1].iv[j].to_s+",")
$socket.write("ev["+j.to_s+"]="+$Trainer.party[i-1].ev[j].to_s+",")
end
$socket.write("species="+$Trainer.party[i-1].species.to_s+",")
$socket.write("item="+$Trainer.party[i-1].item.to_s+",")
$socket.write("name="+$Trainer.party[i-1].name+",")
$socket.write("status="+$Trainer.party[i-1].status.to_s+",")
$socket.write("statusCount="+$Trainer.party[i-1].statusCount.to_s+",")
for k in 0..3 do
$socket.write("id="+$Trainer.party[i-1].moves[k].id.to_s+",")
# $socket.write("name="+$Trainer.party[i-1].moves[k].name+",")
# $socket.write("battle="+$Trainer.party[i-1].moves[k].battle+",")
# $socket.write("accuracy="+$Trainer.party[i-1].moves[k].accuracy.to_s+",")
# $socket.write("basedmg="+$Trainer.party[i-1].moves[k].basedamage.to_s+",")
# $socket.write("add1Effekt="+$Trainer.party[i-1].moves[k].add1Effekt+",")
$socket.write("type="+$Trainer.party[i-1].moves[k].type.to_s+",")
# $socket.write("function="+$Trainer.party[i-1].moves[k].function+",")
# $socket.write("target="+$Trainer.party[i-1].moves[k].target+",")
# $socket.write("flags="+$Trainer.party[i-1].moves[k].flags.to_s+",")
# $socket.write("thismove="+$Trainer.party[i-1].moves[k].thismove+",")
$socket.write("pp="+$Trainer.party[i-1].moves[k].pp.to_s+",")
$socket.write("totalpp="+$Trainer.party[i-1].moves[k].totalpp.to_s+",")
# $socket.write("priority="+$Trainer.party[i-1].moves[k].priority.to_s+",")
end
$socket.write("abilityflag="+$Trainer.party[i-1].abilityflag.to_s+",")
end
$socket.write("end,")
end
 
def pbGetPlayerInformation
buffer=""
information=""
theend=false
while theend==false do
buffer+=$socket.gets
pbMultiplayerInformation(buffer)
Graphics.update
if (buffer.split(',').length>1)
buffer_new=buffer.split(',')
for i in 0..buffer_new.length-2
information+=buffer_new[i]+","
pbMultiplayerInformation(buffer_new[i])
Graphics.update
end
buffer=buffer_new[buffer_new.length-1]
end
theend=true if buffer=="end" or buffer=="end,"
end
information=information.split(',')
information=information.split('=')
for i in 0..information.length
if information[i]=="$Trainer.name"
# Player2_name=information[i+1]
elsif information[i]=='$Trainer.trainertype'
# Player2_type=information[i+1]
end
information.delete_at(i)
information.delete_at(i+1)
break if Player2_name !=nil && Player2_taype != nil
i+=1
end
$Player2=PokeBattle_Trainer.new(Player2_name, Player2_type)
length=information.length
for i in 0..length-1
if information[i]='index'
index=information[i+1].to_i
elsif information[i]='totalhp' && 0<=index<=5
$Player2.party[index].totalhp=information[i+1].to_i
elsif information[i]='attack'
$Player2.party[index].attack=information[i+1].to_i
elsif information[i]='defense'
$Player2.party[index].defense=information[i+1].to_i
elsif information[i]='speed'
$Player2.party[index].speed=information[i+1].to_i
elsif information[i]='spatk'
$Player2.party[index].spatk=information[i+1].to_i
elsif information[i]='spdef'
$Player2.party[index].spdef=information[i+1].to_i
elsif information[i]='species'
$Player2.party[index].species=information[i+1]
elsif information[i]='item'
$Player2.party[index].item=information[i+1].to_i
elsif information[i]='name'
$Player2.party[index].name=information[i+1]
elsif information[i]='status'
$Player2.party[index].status=information[i+1].to_i
elsif information[i]='statusCount'
$Player2.party[index].statusCount=information[i+1].to_i
elsif information[i]='abilityflag'
$Player2.party[index].abilityflag=information[i+1].to_i
else
for j in 0..5
iv='iv['+j.to_s+']'
ev='ev['+j.to_s+']'
if information[i]==iv
$Player2.party[index].iv[j]=information[i+1].to_i
elsif information[i]==ev
$Player2.party[index].ev[j]=information[i+1].to_i
elsif information[i]=='id' && j<4
$Player2.party[index].moves[j].id=information[i+1].to_i
# elsif information[i]=='name' && j<4
# $Player2.party[index].moves[j].name=information[i+1]
# elsif information[i]=='battle' && j<4
# $Player2.party[index].moves[j].battle=information[i+1].to_i
# elsif information[i]=='accuracy' && j<4
# $Player2.party[index].moves[j].accuracy=information[i+1].to_i
# elsif information[i]=='basedmg' && j<4
# $Player2.party[index].moves[j].basedamage=information[i+1].to_i
# elsif information[i]=='add1Effect' && j<4
# $Player2.party[index].moves[j].add1Effect=information[i+1].to_i
elsif information[i]=='type' && j<4
$Player2.party[index].moves[j].type=information[i+1].to_i
# elsif information[i]=='function' && j<4
# $Player2.party[index].moves[j].function=information[i+1].to_i
# elsif information[i]=='target' && j<4
# $Player2.party[index].moves[j].target=information[i+1].to_i
# elsif information[i]=='flags' && j<4
# $Player2.party[index].moves[j].flags=information[i+1].to_i
# elsif information[i]=='thismove' && j<4
# $Player2.party[index].moves[j].thismove=information[i+1].to_i
elsif information[i]=='pp' && j<4
$Player2.party[index].moves[j].pp=information[i+1].to_i
elsif information[i]=='totalpp' && j<4
$Player2.party[index].moves[j].totalpp=information[i+1].to_i
elsif information[i]=='priority' && j<4
$Player2.party[index].moves[j].priority=information[i+1].to_i
end
end
end
information.delete_at(i)
information.delete_at(i+1)
i+=1
end
end

highlight to read

der restliche teil für die übertragung der kampfdaten ist kreuz und quer in pokemon essentials nach dem motto

Source code

1
$socket.write(damage.to_s) if $socket != nil
verteilt. wenn nötig, reiche ich die jeweilugen stellen nach.

This post has been edited 1 times, last edit by "Warkurus" (Aug 20th 2013, 1:56pm)


2

Tuesday, August 20th 2013, 11:51pm

Du kannst mit tcp/ip auch lokale Nachrichten schicken, ist durchaus auch üblich für IPC. Natürlich machts Sinn das auch übers Netz zu testen wenn es am Ende da laufen soll, aber das klingt so als hättest du es noch gar nicht getestet in welchem Falle ich dir rate es erstmal lokal zu testen.

Warkurus

Ankömmling

  • "Warkurus" started this thread

Motto: Shit happens.

  • Send private message

3

Wednesday, August 21st 2013, 12:50am

ich hatte es erst lokal getestet, daraus aber dann die falschen schlüsse gezogen. dann nochmal mit jemand anderen und komme jetzt zu dem fehler, dass der letzte verbundene client einen timeout error gibt. hatte heute 4 möglichkeiten ausprobiert den inputstream in einen outputstream zu schreiben, aber das änderte nichts. wenn die umwandlung von inputstream zu string richtig ist, dann kommt vom ruby ende gar nichts bei java an, weil ich in eine zeit lang nur echo senden ließ. ich habe keine ahnung was ich noch machen könnte, das einzige was funktioniert ist server.accept...

Similar threads

Social bookmarks