示例#1
0
        public void HandleMessage(ISession session, string message, Action<string> sendAction, ref bool commitTransaction)
        {
            var msg = JsonConvert.DeserializeObject<LoginMessage>(message);
            commitTransaction = true;

            if (msg != null) {

                var player = session.QueryOver<Player>().Where(x => x.Name == msg.Username && x.Password == msg.Password).SingleOrDefault();
                if (player == null) {
                    sendAction(JsonConvert.SerializeObject(new ResponseMessage {
                        Success = false,
                        Error = "Incorrect Username or Password"
                    }));
                    return;
                }

                var loggedInUser = session.QueryOver<LoggedInUser>().Where(x => x.Player.Id == player.Id).SingleOrDefault();

                if (loggedInUser != null) {
                    sendAction(JsonConvert.SerializeObject(new ResponseMessage {
                        Success = false,
                        Error = "Already logged in"
                    }));
                    return;
                }

                loggedInUser = new LoggedInUser {
                    Player = player,
                    Token = Guid.NewGuid().ToString(),
                    Expiration = DateTime.UtcNow.AddMinutes(10),
                    LastAction = null
                };
                session.Save(loggedInUser);

                sendAction(JsonConvert.SerializeObject(new ResponseMessage {
                    Success = true,
                    Token = loggedInUser.Token
                }));

            } else {
                sendAction(JsonConvert.SerializeObject(new ResponseMessage {
                    Success = false,
                    Error = "Incorrect message."
                }));
            }
        }
示例#2
0
        public void HandleMessage(ISession session, string message, Action<string> sendAction, ref bool commitTransaction)
        {
            var msg = JsonConvert.DeserializeObject<RegisterMessage>(message);
            commitTransaction = true;

            if (msg != null) {

                var player = session.QueryOver<Player>().Where(x => x.Name == msg.Username).SingleOrDefault();

                if (player != null) {
                    sendAction(JsonConvert.SerializeObject(new ResponseMessage {
                        Success = false,
                        Error = "Username already exists."
                    }));
                }

                // TODO check if password is bcrypt or some shit

                player = new Player { Name = msg.Username, Password = msg.Password };
                session.Save(player);

                var loggedInUser = new LoggedInUser {
                    Player = player,
                    Token = Guid.NewGuid().ToString(),
                    Expiration = DateTime.UtcNow.AddHours(1),
                    LastAction = null
                };
                session.Save(loggedInUser);

                sendAction(JsonConvert.SerializeObject(new ResponseMessage {
                    Success = true,
                    Token = loggedInUser.Token
                }));

            } else {
                sendAction(JsonConvert.SerializeObject(new ResponseMessage {
                    Success = false,
                    Error = "Incorrect message."
                }));
            }
        }