const { marketlist, shopdata } = require("./server"); const NewsItemServer =…
- User
const { market_list, shop_data } = require("./server"); const NewsItemServer = require("./NewsItemServer.js"); const newsArray = [ new NewsItemServer("2023-11-11", "Новость 1"), new NewsItemServer("2023-11-11", "Новость 2"), new NewsItemServer("2023-11-11", "Новость 3"), new NewsItemServer("2023-11-11", "Новость 4"), new NewsItemServer("2023-11-11", "Новость 5"), new NewsItemServer("2023-11-11", "Новость 6") ]; const Area = require("./Area.js"), { playerJson, battle_items, send } = require("./server"); var CTFBattle, TDMBattle, DMBattle, DOMBattle, ASLBattle, effect_model = { effects: [] };
class Lobby extends Area {
codeconstructor() { super(); this.battles = []; this.chat = require("./LobbyChat"); this.battle_count = 0; this.anticheat = 0; this.newsShown = false; // Флаг, указывающий, были ли новости показаны игроку } createBattles() { if (this.battles.length === 0) { var battle = new DMBattle(true, "For newbies", "map_sandbox", 0, 8, 1, 5, 10, false, false); this.battles.push(battle); this.broadcast("create_battle;" + JSON.stringify(battle.toObject())); } }//battle;init_mines;{"mines":[]}
code - Assistant
To make the news shown to all players, you can modify the
Lobbyclass as follows:- Add a static property
newsShownto keep track of whether the news has been shown or not:
javascriptclass Lobby extends Area { static newsShown = false; // ... }- Modify the
sendNewsmethod to check thenewsShownproperty before sending the news to a player:
javascriptsendNews(socket) { if (!Lobby.newsShown) { const newsJSON = JSON.stringify(newsArray); this.send(socket, "show_news;" + newsJSON); Lobby.newsShown = true; } }- Inside the
onDatamethod, call thesendNewsmethod at the beginning to ensure the news is shown to all players:
javascript - Add a static property
- User
ты мне показал одно и тоже что у меня и так есть
- Assistant
Приношу извинения за это. Если вы уже имеете переменную
newsShownв классеLobby, то вам просто нужно изменить методsendNews, чтобы вызывать его только один раз для всех игроков. Вот как может выглядеть измененный методsendNews:javascriptsendNews() { if (!Lobby.newsShown) { const newsJSON = JSON.stringify(newsArray); this.broadcast("show_news;" + newsJSON); Lobby.newsShown = true; } }Затем, в методе
onData, вы можете вызвать методsendNewsодин раз при подключении первого игрока:javascriptonData(socket, args) { // ... if (this.players.length === 1) { this.sendNews(); } // ... }Теперь новости будут отправлены только один раз при подключении первого игрока, и все игроки увидят их.
- User
вообще перестали показывать новости
- Assistant
Если новости перестали показываться, вам следует проверить несколько вещей:
-
Убедитесь, что у вас есть новости в массиве
newsArray. Проверьте, что массив содержит объектыNewsItemServerс корректной датой и текстом новости. -
Проверьте, что переменная
newsShownв классеLobbyустановлена в значениеfalse. Это позволяет показывать новости при первом вызовеsendNews. -
Убедитесь, что в методе
onDataклассаLobbyвызывается методsendNewsтолько один раз при подключении первого игрока. Вы можете проверить количество игроков в лобби и вызватьsendNewsтолько если игроков ровно один.
javascriptonData(socket, args) { // ... if (this.players.length === 1) { this.sendNews(); } // ... } -
- User
мне надо чтобы новости могли увидеть все игрока, а не только один когда он зашел
- Assistant
If you want the news to be visible to all players, not just one when they first log in, you can update the
Lobbyclass as follows:-
Remove the
newsShownproperty from theLobbyclass. We won't need it anymore. -
Update the
sendNewsmethod to send the news to all players in the lobby:
javascriptsendNews() { const newsJSON = JSON.stringify(newsArray); this.broadcast("show_news;" + newsJSON); }- In the
onDatamethod, call thesendNewsmethod when a player logs in or connects to the lobby:
javascriptonData(socket, args) { // ... if (args[0] === "user_inited") { // Player log in or connect to the lobby this.sendNews(); } // ... }With these changes, the
sendNewsmethod will be called whenever a player logs in or connects to the lobby, ensuring that all players can see the news. -