5621
|
1 |
/*
|
|
2 |
* Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
|
|
3 |
* Copyright (c) 2011 Richard Deurwaarder <xeli@xelification.com>
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or modify
|
|
6 |
* it under the terms of the GNU General Public License as published by
|
|
7 |
* the Free Software Foundation; version 2 of the License
|
|
8 |
*
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
* GNU General Public License for more details.
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU General Public License
|
|
15 |
* along with this program; if not, write to the Free Software
|
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
17 |
*/
|
|
18 |
|
|
19 |
|
5475
|
20 |
package org.hedgewars.mobile.EngineProtocol;
|
5433
|
21 |
|
|
22 |
import java.io.IOException;
|
|
23 |
import java.io.InputStream;
|
|
24 |
import java.io.OutputStream;
|
|
25 |
import java.net.ServerSocket;
|
|
26 |
import java.net.Socket;
|
|
27 |
import java.net.UnknownHostException;
|
|
28 |
|
|
29 |
public class EngineProtocolNetwork implements Runnable{
|
|
30 |
|
|
31 |
public static final String GAMEMODE_LOCAL = "TL";
|
|
32 |
public static final String GAMEMODE_DEMO = "TD";
|
|
33 |
public static final String GAMEMODE_NET = "TN";
|
|
34 |
public static final String GAMEMODE_SAVE = "TS";
|
|
35 |
|
|
36 |
public static final int BUFFER_SIZE = 255; //From iOS code which got it from the origional frontend
|
|
37 |
|
|
38 |
public static final int MODE_GENLANDPREVIEW = 0;
|
|
39 |
public static final int MODE_GAME = 1;
|
|
40 |
|
|
41 |
private ServerSocket serverSocket;
|
|
42 |
private InputStream input;
|
|
43 |
private OutputStream output;
|
|
44 |
public int port;
|
5475
|
45 |
private final GameConfig config;
|
5433
|
46 |
|
5475
|
47 |
public EngineProtocolNetwork(GameConfig _config){
|
|
48 |
config = _config;
|
5433
|
49 |
try {
|
|
50 |
serverSocket = new ServerSocket(0);
|
|
51 |
port = serverSocket.getLocalPort();
|
|
52 |
Thread ipcThread = new Thread(this, "IPC - Thread");
|
|
53 |
ipcThread.start();
|
|
54 |
} catch (UnknownHostException e) {
|
|
55 |
e.printStackTrace();
|
|
56 |
} catch (IOException e) {
|
|
57 |
e.printStackTrace();
|
|
58 |
}
|
|
59 |
}
|
|
60 |
|
|
61 |
public void run(){
|
5475
|
62 |
//if(mode == MODE_GENLANDPREVIEW) genLandPreviewIPC();
|
|
63 |
/*else if (mode == MODE_GAME)*/ gameIPC();
|
5433
|
64 |
}
|
|
65 |
|
|
66 |
private void gameIPC(){
|
|
67 |
try{
|
|
68 |
Socket sock = serverSocket.accept();
|
|
69 |
input = sock.getInputStream();
|
|
70 |
output = sock.getOutputStream();
|
|
71 |
|
|
72 |
boolean clientQuit = false;
|
|
73 |
int msgSize = 0;
|
|
74 |
byte[] buffer = new byte[BUFFER_SIZE];
|
|
75 |
|
|
76 |
while(!clientQuit){
|
|
77 |
msgSize = 0;
|
|
78 |
|
|
79 |
input.read(buffer, 0, 1);
|
|
80 |
msgSize = buffer[0];
|
|
81 |
|
|
82 |
input.read(buffer, 0, msgSize);
|
|
83 |
|
|
84 |
switch(buffer[0]){
|
|
85 |
case 'C'://game init
|
5475
|
86 |
config.sendToEngine(this);
|
5433
|
87 |
break;
|
|
88 |
case '?'://ping - pong
|
|
89 |
sendToEngine("!");
|
|
90 |
break;
|
|
91 |
case 'E'://error - quits game
|
|
92 |
|
|
93 |
break;
|
|
94 |
case 'e':
|
|
95 |
|
|
96 |
break;
|
|
97 |
case 'i'://game statistics
|
|
98 |
switch(buffer[1]){
|
|
99 |
case 'r'://winning team
|
|
100 |
break;
|
|
101 |
case 'D'://best shot
|
|
102 |
break;
|
|
103 |
case 'k'://best hedgehog
|
|
104 |
break;
|
|
105 |
case 'K'://# hogs killed
|
|
106 |
break;
|
|
107 |
case 'H'://team health graph
|
|
108 |
break;
|
|
109 |
case 'T':// local team stats
|
|
110 |
break;
|
|
111 |
case 'P'://teams ranking
|
|
112 |
break;
|
|
113 |
case 's'://self damage
|
|
114 |
break;
|
|
115 |
case 'S'://friendly fire
|
|
116 |
break;
|
|
117 |
case 'B'://turn skipped
|
|
118 |
break;
|
|
119 |
default:
|
|
120 |
|
|
121 |
}
|
|
122 |
break;
|
|
123 |
case 'q'://game ended remove save file
|
|
124 |
|
|
125 |
break;
|
|
126 |
case 'Q'://game ended but not finished
|
|
127 |
|
|
128 |
break;
|
|
129 |
}
|
|
130 |
|
|
131 |
}
|
|
132 |
|
|
133 |
}catch(IOException e){
|
|
134 |
e.printStackTrace();
|
|
135 |
}
|
|
136 |
}
|
|
137 |
|
5475
|
138 |
public void sendToEngine(String s){
|
5433
|
139 |
int length = s.length();
|
|
140 |
|
|
141 |
try {
|
|
142 |
output.write(length);
|
|
143 |
output.write(s.getBytes(), 0, length);
|
|
144 |
} catch (IOException e) {
|
|
145 |
e.printStackTrace();
|
|
146 |
}
|
|
147 |
|
|
148 |
|
|
149 |
}
|
|
150 |
|
|
151 |
}
|