5433
|
1 |
package org.hedgewars.mobile;
|
|
2 |
|
|
3 |
import java.io.IOException;
|
|
4 |
import java.io.InputStream;
|
|
5 |
import java.io.OutputStream;
|
|
6 |
import java.net.ServerSocket;
|
|
7 |
import java.net.Socket;
|
|
8 |
import java.net.UnknownHostException;
|
|
9 |
|
|
10 |
import android.util.Log;
|
|
11 |
|
|
12 |
public class EngineProtocolNetwork implements Runnable{
|
|
13 |
|
|
14 |
public static final String GAMEMODE_LOCAL = "TL";
|
|
15 |
public static final String GAMEMODE_DEMO = "TD";
|
|
16 |
public static final String GAMEMODE_NET = "TN";
|
|
17 |
public static final String GAMEMODE_SAVE = "TS";
|
|
18 |
|
|
19 |
public static final int BUFFER_SIZE = 255; //From iOS code which got it from the origional frontend
|
|
20 |
|
|
21 |
public static final int MODE_GENLANDPREVIEW = 0;
|
|
22 |
public static final int MODE_GAME = 1;
|
|
23 |
|
|
24 |
private int mode = -1;
|
|
25 |
private ServerSocket serverSocket;
|
|
26 |
private InputStream input;
|
|
27 |
private OutputStream output;
|
|
28 |
public int port;
|
|
29 |
|
|
30 |
public EngineProtocolNetwork(int _mode){
|
|
31 |
try {
|
|
32 |
mode = _mode;
|
|
33 |
|
|
34 |
serverSocket = new ServerSocket(0);
|
|
35 |
port = serverSocket.getLocalPort();
|
|
36 |
Thread ipcThread = new Thread(this, "IPC - Thread");
|
|
37 |
ipcThread.start();
|
|
38 |
} catch (UnknownHostException e) {
|
|
39 |
e.printStackTrace();
|
|
40 |
} catch (IOException e) {
|
|
41 |
e.printStackTrace();
|
|
42 |
}
|
|
43 |
}
|
|
44 |
public EngineProtocolNetwork(String uuid){
|
|
45 |
|
|
46 |
}
|
|
47 |
|
|
48 |
public void run(){
|
|
49 |
if(mode == MODE_GENLANDPREVIEW) genLandPreviewIPC();
|
|
50 |
else if (mode == MODE_GAME) gameIPC();
|
|
51 |
}
|
|
52 |
|
|
53 |
private void genLandPreviewIPC(){
|
|
54 |
|
|
55 |
}
|
|
56 |
|
|
57 |
private void gameIPC(){
|
|
58 |
try{
|
|
59 |
Socket sock = serverSocket.accept();
|
|
60 |
input = sock.getInputStream();
|
|
61 |
output = sock.getOutputStream();
|
|
62 |
|
|
63 |
boolean clientQuit = false;
|
|
64 |
int msgSize = 0;
|
|
65 |
byte[] buffer = new byte[BUFFER_SIZE];
|
|
66 |
|
|
67 |
while(!clientQuit){
|
|
68 |
msgSize = 0;
|
|
69 |
|
|
70 |
input.read(buffer, 0, 1);
|
|
71 |
msgSize = buffer[0];
|
|
72 |
Log.e("bla", "bla" + msgSize + " + " + buffer[0] + " + " + buffer[1]);
|
|
73 |
|
|
74 |
input.read(buffer, 0, msgSize);
|
|
75 |
|
|
76 |
switch(buffer[0]){
|
|
77 |
case 'C'://game init
|
|
78 |
Log.e("bla", "send init");
|
|
79 |
|
|
80 |
sendToEngine(GAMEMODE_LOCAL);//Start localgame
|
|
81 |
|
|
82 |
//seed info
|
|
83 |
|
|
84 |
|
|
85 |
break;
|
|
86 |
case '?'://ping - pong
|
|
87 |
sendToEngine("!");
|
|
88 |
break;
|
|
89 |
case 'E'://error - quits game
|
|
90 |
|
|
91 |
break;
|
|
92 |
case 'e':
|
|
93 |
|
|
94 |
break;
|
|
95 |
case 'i'://game statistics
|
|
96 |
switch(buffer[1]){
|
|
97 |
case 'r'://winning team
|
|
98 |
break;
|
|
99 |
case 'D'://best shot
|
|
100 |
break;
|
|
101 |
case 'k'://best hedgehog
|
|
102 |
break;
|
|
103 |
case 'K'://# hogs killed
|
|
104 |
break;
|
|
105 |
case 'H'://team health graph
|
|
106 |
break;
|
|
107 |
case 'T':// local team stats
|
|
108 |
break;
|
|
109 |
case 'P'://teams ranking
|
|
110 |
break;
|
|
111 |
case 's'://self damage
|
|
112 |
break;
|
|
113 |
case 'S'://friendly fire
|
|
114 |
break;
|
|
115 |
case 'B'://turn skipped
|
|
116 |
break;
|
|
117 |
default:
|
|
118 |
|
|
119 |
}
|
|
120 |
break;
|
|
121 |
case 'q'://game ended remove save file
|
|
122 |
|
|
123 |
break;
|
|
124 |
case 'Q'://game ended but not finished
|
|
125 |
|
|
126 |
break;
|
|
127 |
}
|
|
128 |
|
|
129 |
}
|
|
130 |
|
|
131 |
}catch(IOException e){
|
|
132 |
e.printStackTrace();
|
|
133 |
}
|
|
134 |
}
|
|
135 |
|
|
136 |
private void sendToEngine(String s){
|
|
137 |
int length = s.length();
|
|
138 |
|
|
139 |
try {
|
|
140 |
output.write(length);
|
|
141 |
output.write(s.getBytes(), 0, length);
|
|
142 |
} catch (IOException e) {
|
|
143 |
e.printStackTrace();
|
|
144 |
}
|
|
145 |
|
|
146 |
|
|
147 |
}
|
|
148 |
|
|
149 |
}
|