/
LoginSession.java
78 lines (57 loc) · 2.21 KB
/
LoginSession.java
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
package com.gie.core.login;
import com.gie.core.channel.SessionHandler;
import com.gie.core.login.game.GameDecoder;
import com.gie.game.World;
import com.gie.game.entity.player.Player;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.socket.SocketChannel;
import java.util.concurrent.ExecutionException;
/**
* Created by Adam on 09/10/2016.
*/
public class LoginSession extends SessionHandler {
private Player player;
public LoginSession(Channel channel) {
super(channel);
this.player = new Player(this);
}
public Player getPlayer() {
return player;
}
private World world = new World();
private LoginResponses responses = LoginResponses.LOGIN_SUCCESSFUL;
@Override
public void receieveMessage(Object o) throws ExecutionException, InterruptedException {
if (!(o instanceof LoginHandler)) {
return;
}
LoginHandler details = (LoginHandler) o;
SocketChannel channel = (SocketChannel) details.getChannel().channel();
String username = details.getUsername();
String password = details.getPassword();
String address = channel.remoteAddress().getAddress().getHostAddress();
player.setUsername(username);
player.setPassword(password);
player.setAddress(address);
if (world.isUpdating()) {
responses = LoginResponses.SERVER_IS_BEING_UPDATED;
}
if (world.getPlayers().isFull()) {
responses = LoginResponses.WORLD_IS_FULL;
}
if (password != player.getPassword()) {
responses = LoginResponses.FALSE_CREDENTIALS;
}
ChannelFuture future = channel.writeAndFlush(new LoginResponseHandler(responses, 0, false));
if (responses != LoginResponses.LOGIN_SUCCESSFUL) {
future.addListener(ChannelFutureListener.CLOSE);
return;
}
future.awaitUninterruptibly();
channel.pipeline().replace("login-decoder", "incoming-decoder", new GameDecoder(details.getOut()));
world.addPlayer(player);
System.out.println("Registered " + player.getUsername());
}
}