-
Notifications
You must be signed in to change notification settings - Fork 0
/
player_handler.erl
178 lines (139 loc) · 4.21 KB
/
player_handler.erl
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
-module(player_handler).
-compile(export_all). % TA BORT SEN!
-export([]).
-include("config.hrl").
init(Socket, FSM_Pid) ->
try
recv(Socket, FSM_Pid)
catch
error:X ->
case X of % Socket closed
{badmatch,{error,closed}} ->
?SERVER:rm_player({socket, Socket}),
gen_tcp:close(Socket),
ok; %Remove playaah
_ ->
?SERVER:rm_player({socket, Socket}),
gen_tcp:close(Socket),
io:format("Caught error in player_handler: ~p\n", [X])
end
end.
recv(Socket, FSM) ->
Header = ?TCP:readHeader(Socket),
RestOfMsg = case Header of
2 -> % Hello World
PV = ?TCP:readInteger(Socket),
PN = ?TCP:readString(Socket),
[PV,PN];
5 -> % List game request
[];
7 -> % Host request
[];
8 -> % Join request
GameName = ?TCP:readString(Socket),
[GameName];
11 -> % Change civilization request
NewCiv = ?TCP:readString(Socket),
[NewCiv];
12 -> % Lock game request
LockFlag = ?TCP:readBoolean(Socket),
[LockFlag];
13 -> % Start game request
Width = ?TCP:readInteger(Socket),
Height = ?TCP:readInteger(Socket),
[Width, Height];
15 -> %Move request
MovePath = ?TCP:readList(Socket, "Position"),
[MovePath];
16 -> % End of turn
[];
18 -> %Combat request
AttPos = ?TCP:readElement(Socket, "Position"),
DefPos = ?TCP:readElement(Socket, "Position"),
[AttPos, DefPos];
23 -> %Spawned
Pos = ?TCP:readElement(Socket, "Position"),
Unit = ?TCP:readElement(Socket, "Unit"),
[Pos, Unit];
21 -> %Built city
Pos = ?TCP:readElement(Socket, "Position"),
CityName = ?TCP:readString(Socket),
[Pos, CityName];
24 -> %Exit game
[];
26 ->
FromPos = ?TCP:readElement(Socket, "Position"),
ToPos = ?TCP:readElement(Socket, "Position"),
[FromPos, ToPos];
28 ->
ContainerPos = ?TCP:readElement(Socket, "Position"),
UnitType = ?TCP:readString(Socket),
ManPower = ?TCP:readInteger(Socket),
ToPlacePos = ?TCP:readElement(Socket, "Position"),
[ContainerPos, UnitType, ManPower, ToPlacePos];
29 -> %Disband unit
Pos = ?TCP:readElement(Socket, "Position"),
[Pos];
31 -> %Fortify
Pos = ?TCP:readElement(Socket, "Position"),
[Pos];
32 -> %UnFortify
Pos = ?TCP:readElement(Socket, "Position"),
[Pos];
_ ->
[]
end,
Packet = {Header, RestOfMsg},
send_to_fsm(FSM, Packet),
recv(Socket, FSM).
sendFailMsg(Socket, ID, Header) ->
?TCP:sendFailPacket(Socket, ID, Header).
sendMsg(Socket, {Header, List}) ->
case Header of
1 -> %Confirm'd
[Head] = List,
?TCP:sendHeader(Socket, Header),
?TCP:sendHeader(Socket, Head);
3 -> %Welcome to the Real world
?TCP:sendHeader(Socket, Header);
4 -> %Ping, implement later
ok;
6 -> %List game answer
[Games] = List,
?TCP:sendHeader(Socket, Header),
?TCP:sendList(Socket, "String", Games);
9 -> %Join answer
[HostName] = List,
?TCP:sendHeader(Socket, Header),
?TCP:sendString(Socket, HostName);
10 -> %Game session information
[PList, Locked] = List,
?TCP:sendHeader(Socket, 10), %Game session information
?TCP:sendList(Socket, "Player", PList),
?TCP:sendBoolean(Socket, Locked);
14 -> %Start game answer, implement later
[Map, TL] = List,
TileList = lists:flatten(?GAMEPLAN:tuplemap_to_listmap(TL)), %Gör om tuplemappen till en lista och skicka
?TCP:sendHeader(Socket, Header),
?TCP:sendList(Socket, "Column", Map),
?TCP:sendList(Socket, "Tile", TileList);
17 -> % It's your turn
[TL] = List,
TileList = lists:flatten(?GAMEPLAN:tuplemap_to_listmap(TL)), %Gör om tuplemappen till en lista och skicka
?TCP:sendHeader(Socket, 17), %It's your turn
?TCP:sendList(Socket, "Tile", TileList);
19 -> %Combat result
[RemAttMp, RemDefMp] = List,
?TCP:sendHeader(Socket, 19),
?TCP:sendInteger(Socket, RemAttMp),
?TCP:sendInteger(Socket, RemDefMp);
25 -> % Game Closed
?TCP:sendHeader(Socket, 25);
30 -> % Casualty report
[{X, Y}, DPU] = List,
?TCP:sendHeader(Socket, 30),
?TCP:sendPosition(Socket, {X, Y}),
?TCP:sendInteger(Socket, DPU)
end.
send_to_fsm(To, Packet) ->
?P_FSM:send_packet(To, Packet).