-
Notifications
You must be signed in to change notification settings - Fork 3
/
SFSHandler.hx
199 lines (175 loc) · 5.09 KB
/
SFSHandler.hx
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package;
import com.smartfoxserver.v2.SmartFox;
import com.smartfoxserver.v2.core.SFSEvent;
import Commands;
import Move;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.requests.ExtensionRequest;
import com.smartfoxserver.v2.requests.LeaveRoomRequest;
import com.smartfoxserver.v2.requests.LoginRequest;
import tools.SFSObjectTool;
/**
* ...
* @author vincent blanchet
*/
class SFSHandler
{
var sfs:SmartFox;
public var onMove:Move->Void;
public var onTurn:Int->String->Void;
public var onReady:StartState->Void;
//public var onEnd:EndResult->Void;
public var currentTurn:String;
public var currentTurnId:Int;
public var me(get, null):com.smartfoxserver.v2.entities.SFSUser;
public var log:String->Void;
public function new()
{
}
private function onUserExit(e:SFSEvent):Void
{
#if html5
log("User disconnected :" + e.user.name);
#else
log("User disconnected :" + e.params.user.name);
#end
}
private function run(e:SFSEvent):Void
{
trace("on extension response");
var extParams:SFSObject = #if html5 e.params #else e.params.params #end;
#if html5
switch(e.cmd)
#else
switch(e.params.cmd)
#end
{
case Commands.TURN :
currentTurn = extParams.getUtfString("name");
currentTurnId = extParams.getInt("id");
trace('it is $currentTurn\'s turn');
if (onTurn != null)
onTurn(currentTurnId,currentTurn);
case Commands.MOVE :
var move : Move = SFSObjectTool.sfsObjectToInstance(extParams);
if (onMove != null)
onMove(move);
case Commands.READY :
//server is ready
log("Game started");
var startState:StartState = SFSObjectTool.sfsObjectToInstance(extParams);
if (onReady != null)
onReady(startState);
case Commands.WAIT_OPPONENT:
log("Game joined, waiting for opponent...");
}
}
public function move(move:Move):Void{
sfs.send(new ExtensionRequest(Commands.MOVE, SFSObjectTool.instanceToSFSObject(move)));
}
/**
* player is ready
*/
public function ready():Void{
sfs.send(new ExtensionRequest(Commands.READY));
}
public function leaveGame():Void
{
sfs.send(new LeaveRoomRequest());
}
public function play():Void
{
log("Sending request for joining a game.");
sfs.send(new ExtensionRequest(Commands.PLAY));
}
public function connect():Void
{
#if !html5
var config:com.smartfoxserver.v2.util.ConfigData = new com.smartfoxserver.v2.util.ConfigData();
config.httpPort = 8080;
config.useBlueBox = false;
#else
var config:com.smartfoxserver.v2.SmartFox.ConfigObj = {host:"",port:0,useSSL:false,zone:"",debug:true};
#end
config.debug = true;
config.host = "127.0.0.1";
config.port = #if web 8080 #else 9933 #end;
config.zone = "Haxe";
#if html5
sfs = new com.smartfoxserver.v2.SmartFox(config);
sfs.logger.level = 0;
#else
sfs = new com.smartfoxserver.v2.SmartFox(true);
#end
sfs.addEventListener(SFSEvent.CONNECTION, onConnection #if html5 ,this #end);
sfs.addEventListener(SFSEvent.SOCKET_ERROR, onSocketError #if html5 ,this #end);
sfs.addEventListener(SFSEvent.LOGIN_ERROR, onSocketError #if html5 ,this #end);
sfs.addEventListener(SFSEvent.ROOM_CREATION_ERROR, onSocketError #if html5 ,this #end);
sfs.addEventListener(SFSEvent.ROOM_JOIN_ERROR, onSocketError #if html5 , this #end);
sfs.addEventListener(SFSEvent.USER_EXIT_ROOM, onUserExit #if html5 , this #end);
sfs.addEventListener(SFSEvent.EXTENSION_RESPONSE, run #if html5 , this #end);
sfs.addEventListener(SFSEvent.CONNECTION_LOST, onConnectionLost #if html5 , this #end);
trace("config:" + config);
try{
#if html5
sfs.connect();
#else
sfs.connectWithConfig(config);
#end
}catch (e:Dynamic){
trace(e+" " + haxe.CallStack.toString( haxe.CallStack.exceptionStack()));
}
}
private function onConnectionLost(e:SFSEvent):Void
{
log("Connection lost!!!");
}
private function onLogin(e:SFSEvent):Void
{
log("Logged.");
play();
}
private function onConnection(e:SFSEvent):Void
{
log("onConnection");
#if html5
if (e.success)
#else
if (e.params.success)
#end
{
log("Connected");
sfs.addEventListener(SFSEvent.LOGIN, onLogin #if html5 ,this #end);
sfs.send(new LoginRequest(null,null,null, "Haxe"));
}else{
log("Not connected to internet");
}
}
private function onSocketError(e:SFSEvent):Void
{
log("socket error:" + e.params);
}
public function isAvailable():Bool
{
#if html5
return sfs.isConnected();
#else
return sfs.isConnected;
#end
}
public function destroy():Void
{
sfs.removeEventListener(SFSEvent.CONNECTION, onConnection);
sfs.removeEventListener(SFSEvent.SOCKET_ERROR, onSocketError);
sfs.removeEventListener(SFSEvent.LOGIN_ERROR, onSocketError);
sfs.removeEventListener(SFSEvent.ROOM_CREATION_ERROR, onSocketError);
sfs.removeEventListener(SFSEvent.ROOM_JOIN_ERROR, onSocketError);
sfs.removeEventListener(SFSEvent.USER_EXIT_ROOM, onUserExit);
sfs.removeEventListener(SFSEvent.EXTENSION_RESPONSE, run);
sfs.removeEventListener(SFSEvent.CONNECTION_LOST, onConnectionLost);
}
function get_me():com.smartfoxserver.v2.entities.SFSUser
{
return cast sfs.mySelf;
}
}