private void MaintainGameSession(double delta) { _lastAliveMsg += delta; if (_lastAliveMsg >= GConfig.D.GameSessionLoopAliveMessageAfterSeconds * GConfig.D.GameSimulationTime) { TStuffLog.Info("Game loop of Session: " + _session.Id + " is alive!"); _lastAliveMsg = 0; } }
public void RemoveUser(Connection connection) { var user = GetUserByConnection(connection); if (user != null) { TStuffLog.Info("Remove user: " + user.Name); _users.Remove(user); } }
private void CreateGameWithSettings(User user, GameSettings incomingObject) { //If a user has allready a GameSession if (_gr.GameSessions.GetSessionByUserId(user) != null) { user.Send(RequestNames.Message, new ClientMessage { Message = "There exists a game which you have created!", MessageType = MessageType.Error, Title = "Create Game Error" }); return; } ; _gr.GameSessions.CreateGameSession(user, incomingObject); TStuffLog.Info("Game Startet!"); //Join Connection Player; }
public void Process(Connection con, Login login) { TStuffLog.Debug("Get Login"); if (_userHandler.Login(con, login)) { TStuffLog.Info("Login for: " + login.Name + " Successfull"); //Update Game data var user = _userHandler.GetUserByConnection(con); user.Send(RequestNames.GetObjectDb, new RaceModels { TowerRaces = _gr.TowerRaces.ToArray(), MobRaces = _gr.MobRaces.ToArray(), Towers = _gr.Towers.ToArray(), Mobs = _gr.Mobs.ToArray() }); } else { TStuffLog.Warning("Login Error!"); } }
private void ProcessGameNotifications(GameNotifications incomingObject, Connection connection) { var user = _gr.User.GetUserByConnection(connection); var session = _gr.GameSessions.GetSessionByUserId(user); var player = session.Players.Single(p => p.User.Id == user.Id); switch (incomingObject.Notification) { case GameNotificationType.GameCanceled: case GameNotificationType.LeaveGame: _gr.GameSessions.LeaveGame(connection); return; case GameNotificationType.Ready: session.Players.Single(p => p.User.Id == user.Id).State = incomingObject.IntValue == 1 ? PlayerState.Ready : PlayerState.Joined; break; case GameNotificationType.ChangeTeam: session.Teams.Single(p => p.Value.Any(x => x.User.Id == user.Id)).Value.Remove(player); session.Teams[incomingObject.IntValue].Add(player); player.TeamId = incomingObject.IntValue; break; case GameNotificationType.StartGame: TStuffLog.Info("Start Game"); //BotAdd StartupGame(session); break; } var joinInfo = _gr.GameSessions.GetGameJoinInfo(session); session.Players.ForEach(p => { p.User.Send(RequestNames.GameJoinInfo, joinInfo); }); }
public GameLoop(GlobalRegister gr, GameSession session) { TStuffLog.Info("Start Gameloop"); _gr = gr; _session = session; InitializeGameLoopData(); _startTime = new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds; _lastTime = _startTime; _gameTimer = new Timer(state => { var delta = new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds - _lastTime; var total = new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds - _startTime; MaintainGameSession(delta); ProcessGame(delta, total); _lastTime = new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds; }, null, 0, GConfig.D.LogicUpdateEachMillisecond); _startSendTime = new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds; _lastSendTime = _startTime; _sendUpdateTimer = new Timer(state => { var delta = new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds - _lastSendTime; var total = new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds - _startSendTime; try { SendDataState(delta); } catch (Exception ex) { TStuffLog.Error(ex.ToString()); } _lastSendTime = new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds; }, null, 0, GConfig.D.SendLoopMilliseconds); }
public void Dispose() { TStuffLog.Info("Game Session is disposed"); GameLoop?.StopLoop(); }
static void Main(string[] args) { TStuffLog.LogLevel = LogLevel.Info; TStuffLog.LogFileLevel = LogLevel.Info; TStuffLog.LogActions.Add((message, serializedObject, level, member, filepat, line) => { if (TStuffLog.LogLevel > level) { return; } switch (level) { case LogLevel.Trace: Console.ForegroundColor = ConsoleColor.Green; break; case LogLevel.Debug: Console.ForegroundColor = ConsoleColor.Cyan; break; case LogLevel.Info: break; case LogLevel.Warning: Console.ForegroundColor = ConsoleColor.Yellow; break; case LogLevel.Error: case LogLevel.Fatal: Console.ForegroundColor = ConsoleColor.Red; break; default: throw new ArgumentOutOfRangeException(nameof(level), level, null); } Console.WriteLine("[{5},{0}/{1}:{2}, Level:{3}] Message: {4}", filepat.Split('\\').Last(), member, line, level.ToString(), message, DateTime.Now.ToString("T")); Console.ResetColor(); }); NetworkComms.DisableLogging(); if (File.Exists("config.json")) { GConfig.D = JsonConvert.DeserializeObject <GameServerConfiguration>(File.ReadAllText("config.json")); } else { GConfig.D = new GameServerConfiguration(); File.WriteAllText("config.json", JsonConvert.SerializeObject(GConfig.D, Formatting.Indented)); } TStuffLog.Info("Server Started"); TStuffLog.LogLevel = GConfig.D.ServerLogLevel; TStuffLog.LogFileLevel = GConfig.D.LogFileLevel; DataSerializer ds = DPSManager.GetDataSerializer <ProtobufSerializer>(); NetworkComms.DefaultSendReceiveOptions = new SendReceiveOptions(ds, new List <DataProcessor>(), new Dictionary <string, string>()); var gamecontext = SetupGameRegister(); SetupGlobalNetworkHandler(gamecontext); GConfig.D.GameSimulationTime = 400; var cont = new LoginService(gamecontext); var map = new CreateGameService(gamecontext); Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Any, GConfig.D.ServerPort)); //Connectc fake user gamecontext.User.AddSystemUser(GConfig.D.MainBotName + " 0", 0); gamecontext.User.AddSystemUser(GConfig.D.MainBotName + " 1", 1); gamecontext.User.AddSystemUser(GConfig.D.MainBotName + " 2", 2); gamecontext.User.AddSystemUser(GConfig.D.MainBotName + " 3", 3); gamecontext.User.AddSystemUser(GConfig.D.MainBotName + " 4", 4); //File.WriteAllText("mobs.json",JsonConvert.SerializeObject(gamecontext.Mobs)); //File.WriteAllText("tower.json", JsonConvert.SerializeObject(gamecontext.Towers)); //File.WriteAllText("race_mob.json", JsonConvert.SerializeObject(gamecontext.MobRaces)); //File.WriteAllText("race_tower.json", JsonConvert.SerializeObject(gamecontext.TowerRaces)); Console.ReadLine(); }