RuneHive-Game
Loading...
Searching...
No Matches
com.runehive.content.ai.LazyAIManager Class Reference
Collaboration diagram for com.runehive.content.ai.LazyAIManager:

Static Public Member Functions

static void clearPlayerConsent (String username)
static OpenAIService getOpenAIService ()
static boolean handleGandalfAIClick (Player player, Npc npc)
static boolean isInitialized ()
static void markInstructionsReceived (String username)
static void shutdown ()

Static Private Member Functions

static void cancelInactivityTimer (String username)
static void initializeOpenAIAndProcess (Player player, Npc npc)
static synchronized boolean initializeOpenAIServices ()
static void showGptInstructions (Player player, Npc npc)
static void showGptInstructionsWithGreeting (Player player, Npc npc)
static void showOpenAIConnectionOffer (Player player, Npc npc)
static void startInactivityTimer (Player player, Npc npc)

Static Private Attributes

static final int GANDALF_AI_ID = 2108
static final Map< String, TaskinactivityTimers = new ConcurrentHashMap<>()
static final Logger logger = LoggerFactory.getLogger(LazyAIManager.class)
static final AtomicBoolean openAIInitialized = new AtomicBoolean(false)
static volatile OpenAIService openAIService
static final Set< String > sessionConsents = ConcurrentHashMap.newKeySet()

Detailed Description

Definition at line 17 of file LazyAIManager.java.

Member Function Documentation

◆ cancelInactivityTimer()

void com.runehive.content.ai.LazyAIManager.cancelInactivityTimer ( String username)
staticprivate

Definition at line 209 of file LazyAIManager.java.

209 {
210 Task timer = inactivityTimers.remove(username);
211 if (timer != null) {
212 timer.cancel();
213 }
214 }

References com.runehive.game.task.Task.cancel(), and inactivityTimers.

Referenced by clearPlayerConsent(), markInstructionsReceived(), and startInactivityTimer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ clearPlayerConsent()

void com.runehive.content.ai.LazyAIManager.clearPlayerConsent ( String username)
static

Definition at line 216 of file LazyAIManager.java.

216 {
217 if (sessionConsents.remove(username)) {
218 logger.info("Cleared OpenAI consent for player: {}", username);
219
220 // Send blue chat message to player about service termination
221 World.getPlayerByName(username).ifPresent(player -> {
222 String timestamp = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
223 player.message("<col=0000FF>[OpenAI Service] Terminated at " + timestamp + ". Conversation History flushed.</col>");
224 });
225 }
226
227 cancelInactivityTimer(username);
228
229 if (openAIService != null) {
230 openAIService.clearConversationHistory(username);
231 }
232 }

References cancelInactivityTimer(), com.runehive.game.world.World.getPlayerByName(), logger, openAIService, and sessionConsents.

Referenced by startInactivityTimer(), and com.runehive.content.ai.AIDialogueHandler.streamGandalfResponse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getOpenAIService()

OpenAIService com.runehive.content.ai.LazyAIManager.getOpenAIService ( )
static

Definition at line 133 of file LazyAIManager.java.

133 {
134 return openAIService;
135 }

References openAIService.

Referenced by com.runehive.game.world.cronjobs.impl.OpenAIRelayJob.execute(), and com.runehive.content.ai.AIDialogueHandler.processGandalfAIMessage().

Here is the caller graph for this function:

◆ handleGandalfAIClick()

boolean com.runehive.content.ai.LazyAIManager.handleGandalfAIClick ( Player player,
Npc npc )
static

Definition at line 28 of file LazyAIManager.java.

28 {
29 if (npc.id != GANDALF_AI_ID) {
30 return false;
31 }
32
33 // Position player 1 tile west of NPC and setup camera
34 Position npcPos = npc.getPosition();
35 Position playerTarget = new Position(npcPos.getX() - 1, npcPos.getY(), npcPos.getHeight());
36 player.move(playerTarget);
37 player.face(npc);
38
39 // Initialize dialogue camera - player speaks first
40 player.dialogueCamNpc = npc;
41 player.dialogueCamMode = com.runehive.game.world.entity.mob.player.camera.DialogueCameraDirector.Mode.BEHIND_PLAYER;
42
43 if (sessionConsents.contains(player.getUsername())) {
44 if (!openAIInitialized.get()) {
45 initializeOpenAIServices();
46 }
47
48 // Always show instructions when NPC is clicked (connection is idle)
49 showGptInstructions(player, npc);
50 } else {
51 showOpenAIConnectionOffer(player, npc);
52 }
53 return true;
54 }

References com.runehive.game.world.entity.mob.Mob.face(), GANDALF_AI_ID, com.runehive.game.world.position.Position.getHeight(), com.runehive.game.world.entity.Entity.getPosition(), com.runehive.game.world.entity.mob.player.Player.getUsername(), com.runehive.game.world.position.Position.getX(), com.runehive.game.world.position.Position.getY(), com.runehive.game.world.entity.mob.Mob.id, initializeOpenAIServices(), com.runehive.game.world.entity.mob.Mob.move(), openAIInitialized, sessionConsents, showGptInstructions(), and showOpenAIConnectionOffer().

Here is the call graph for this function:

◆ initializeOpenAIAndProcess()

void com.runehive.content.ai.LazyAIManager.initializeOpenAIAndProcess ( Player player,
Npc npc )
staticprivate

Definition at line 90 of file LazyAIManager.java.

90 {
91 if (!openAIInitialized.get()) {
92 if (initializeOpenAIServices()) {
93 showGptInstructionsWithGreeting(player, npc);
94 } else {
95 player.dialogueFactory
96 .sendNpcChat(npc.id, Expression.SAD,
97 "Alas! The mystical networks are unreachable.",
98 "Please ensure the OpenAI key scroll is present",
99 "and try again later.")
100 .execute();
101 return;
102 }
103 } else {
104 showGptInstructionsWithGreeting(player, npc);
105 }
106 }

References com.runehive.game.world.entity.mob.Mob.id, initializeOpenAIServices(), openAIInitialized, com.runehive.content.dialogue.Expression.SAD, and showGptInstructionsWithGreeting().

Referenced by showOpenAIConnectionOffer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initializeOpenAIServices()

synchronized boolean com.runehive.content.ai.LazyAIManager.initializeOpenAIServices ( )
staticprivate

Definition at line 108 of file LazyAIManager.java.

108 {
109 if (openAIInitialized.compareAndSet(false, true)) {
110 logger.info("Player accepted OpenAI connection - initializing OpenAI services...");
111
112 try {
113 openAIService = OpenAIService.getInstance();
114 openAIService.initialize();
115 logger.info("Official OpenAI Service initialized with GPT-4o-mini model");
116
117 logger.info("OpenAI services fully initialized and ready for interactions!");
118 return true;
119
120 } catch (Exception e) {
121 logger.error("Failed to initialize OpenAI services", e);
122 openAIInitialized.set(false);
123 return false;
124 }
125 }
126 return true;
127 }

References com.runehive.content.ai.OpenAIService.getInstance(), logger, openAIInitialized, and openAIService.

Referenced by handleGandalfAIClick(), and initializeOpenAIAndProcess().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isInitialized()

boolean com.runehive.content.ai.LazyAIManager.isInitialized ( )
static

Definition at line 129 of file LazyAIManager.java.

129 {
130 return openAIInitialized.get();
131 }

References openAIInitialized.

Referenced by com.runehive.game.world.cronjobs.impl.OpenAIRelayJob.execute(), and com.runehive.content.ai.AIDialogueHandler.handleAICommand().

Here is the caller graph for this function:

◆ markInstructionsReceived()

void com.runehive.content.ai.LazyAIManager.markInstructionsReceived ( String username)
static

Definition at line 168 of file LazyAIManager.java.

168 {
169 cancelInactivityTimer(username);
170 }

References cancelInactivityTimer().

Referenced by com.runehive.content.ai.AIDialogueHandler.handleAICommand().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ showGptInstructions()

void com.runehive.content.ai.LazyAIManager.showGptInstructions ( Player player,
Npc npc )
staticprivate

Definition at line 154 of file LazyAIManager.java.

154 {
155 player.dialogueFactory
156 .sendPlayerChat("Hello again, Wise Old Man!")
157 .onAction(() -> player.dialogueCamMode = com.runehive.game.world.entity.mob.player.camera.DialogueCameraDirector.Mode.BEHIND_NPC)
158 .sendNpcChat(npc.id, Expression.HAPPY,
159 "To send a message, type:",
160 "<col=0000FF>::gpt <your message></col>")
161 .onAction(() -> {
162 player.interfaceManager.close();
163 com.runehive.game.world.entity.mob.player.camera.DialogueCameraDirector.requestReset(player);
164 })
165 .execute();
166 }

References com.runehive.game.world.entity.mob.player.InterfaceManager.close(), com.runehive.game.world.entity.mob.player.Player.dialogueCamMode, com.runehive.content.dialogue.Expression.HAPPY, com.runehive.game.world.entity.mob.Mob.id, and com.runehive.game.world.entity.mob.player.Player.interfaceManager.

Referenced by handleGandalfAIClick().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ showGptInstructionsWithGreeting()

void com.runehive.content.ai.LazyAIManager.showGptInstructionsWithGreeting ( Player player,
Npc npc )
staticprivate

Definition at line 137 of file LazyAIManager.java.

137 {
138 npc.animate(new com.runehive.game.Animation(858));
139 npc.speak("GPT 4o at your service!");
140 player.dialogueFactory
141 .sendPlayerChat("Hello again, Wise Old Man!")
142 .onAction(() -> player.dialogueCamMode = com.runehive.game.world.entity.mob.player.camera.DialogueCameraDirector.Mode.BEHIND_NPC)
143 .sendNpcChat(npc.id, Expression.HAPPY,
144 "To send a message, type:",
145 "<col=0000FF>::gpt <your message></col>")
146 .onAction(() -> {
147 player.interfaceManager.close();
148 com.runehive.game.world.entity.mob.player.camera.DialogueCameraDirector.requestReset(player);
149 startInactivityTimer(player, npc);
150 })
151 .execute();
152 }

References com.runehive.game.world.entity.mob.Mob.animate(), com.runehive.game.world.entity.mob.player.InterfaceManager.close(), com.runehive.game.world.entity.mob.player.Player.dialogueCamMode, com.runehive.content.dialogue.Expression.HAPPY, com.runehive.game.world.entity.mob.Mob.id, com.runehive.game.world.entity.mob.player.Player.interfaceManager, com.runehive.game.world.entity.mob.Mob.speak(), and startInactivityTimer().

Referenced by initializeOpenAIAndProcess().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ showOpenAIConnectionOffer()

void com.runehive.content.ai.LazyAIManager.showOpenAIConnectionOffer ( Player player,
Npc npc )
staticprivate

Definition at line 56 of file LazyAIManager.java.

56 {
57 player.dialogueFactory
58 .sendPlayerChat("Hey, Wise Old Man!")
59 .onAction(() -> player.dialogueCamMode = com.runehive.game.world.entity.mob.player.camera.DialogueCameraDirector.Mode.BEHIND_NPC)
60 .sendNpcChat(npc.id, Expression.HAPPY,
61 "Greetings, " + player.getUsername() + "!",
62 "I am Gandalf GPT4o. I can connect to the OpenAI",
63 "mystical networks to provide you with advanced",
64 "AI assistance.")
65 .sendNpcChat(npc.id, Expression.HAPPY,
66 "Would you like me to establish this connection?")
67 .sendOption(
68 "Yes, connect to OpenAI", () -> {
69 sessionConsents.add(player.getUsername());
70 logger.info("Player {} consented to OpenAI connection for this session", player.getUsername());
71
72 // Send blue chat message to player about service initialization
73 String timestamp = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
74 player.message("<col=0000FF>[OpenAI Service] Initialized at " + timestamp + ". Conversation History = 10.</col>");
75
76 initializeOpenAIAndProcess(player, npc);
77 },
78 "No, maybe later", () -> {
79 player.dialogueFactory
80 .sendNpcChat(npc.id, Expression.HAPPY,
81 "Very well. I shall remain here if you change",
82 "your mind. The mystical networks await when",
83 "you are ready.")
84 .execute();
85 }
86 )
87 .execute();
88 }

References com.runehive.game.world.entity.mob.player.Player.dialogueCamMode, com.runehive.game.world.entity.mob.player.Player.getUsername(), com.runehive.content.dialogue.Expression.HAPPY, com.runehive.game.world.entity.mob.Mob.id, initializeOpenAIAndProcess(), logger, com.runehive.game.world.entity.mob.player.Player.message(), and sessionConsents.

Referenced by handleGandalfAIClick().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ shutdown()

void com.runehive.content.ai.LazyAIManager.shutdown ( )
static

Definition at line 234 of file LazyAIManager.java.

234 {
235 if (openAIInitialized.get() && openAIService != null) {
236 logger.info("Shutting down OpenAI services...");
237 openAIService.shutdown();
238 }
239 sessionConsents.clear();
240 inactivityTimers.values().forEach(Task::cancel);
241 inactivityTimers.clear();
242 }

References com.runehive.game.task.Task.cancel(), inactivityTimers, logger, openAIInitialized, openAIService, and sessionConsents.

Here is the call graph for this function:

◆ startInactivityTimer()

void com.runehive.content.ai.LazyAIManager.startInactivityTimer ( Player player,
Npc npc )
staticprivate

Definition at line 172 of file LazyAIManager.java.

172 {
173 cancelInactivityTimer(player.getUsername());
174
175 Task timer = new Task(100) {
176 int warningsSent = 0;
177
178 @Override
179 public void execute() {
180 if (!player.isRegistered() || !sessionConsents.contains(player.getUsername())) {
181 cancel();
182 return;
183 }
184
185 if (warningsSent == 0) {
186 player.dialogueFactory
187 .sendNpcChat(npc.id, Expression.HAPPY,
188 "Hey " + player.getUsername() + ", I'm still here!",
189 "If I don't hear back from you within 1 minute,",
190 "I shall close the OpenAI realms. Your message",
191 "history would be reset in this event.")
192 .execute();
193 warningsSent++;
194 } else {
195 player.dialogueFactory
196 .sendNpcChat(npc.id, Expression.HAPPY,
197 "Farewell, " + player.getUsername() + "!")
198 .execute();
199 clearPlayerConsent(player.getUsername());
200 cancel();
201 }
202 }
203 };
204
205 World.schedule(timer);
206 inactivityTimers.put(player.getUsername(), timer);
207 }

References cancelInactivityTimer(), clearPlayerConsent(), com.runehive.game.world.entity.mob.player.Player.getUsername(), com.runehive.content.dialogue.Expression.HAPPY, com.runehive.game.world.entity.mob.Mob.id, inactivityTimers, com.runehive.game.world.entity.Entity.isRegistered(), com.runehive.game.world.World.schedule(), and sessionConsents.

Referenced by showGptInstructionsWithGreeting().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ GANDALF_AI_ID

final int com.runehive.content.ai.LazyAIManager.GANDALF_AI_ID = 2108
staticprivate

Definition at line 21 of file LazyAIManager.java.

Referenced by handleGandalfAIClick().

◆ inactivityTimers

final Map<String, Task> com.runehive.content.ai.LazyAIManager.inactivityTimers = new ConcurrentHashMap<>()
staticprivate

Definition at line 24 of file LazyAIManager.java.

Referenced by cancelInactivityTimer(), shutdown(), and startInactivityTimer().

◆ logger

final Logger com.runehive.content.ai.LazyAIManager.logger = LoggerFactory.getLogger(LazyAIManager.class)
staticprivate

◆ openAIInitialized

final AtomicBoolean com.runehive.content.ai.LazyAIManager.openAIInitialized = new AtomicBoolean(false)
staticprivate

◆ openAIService

volatile OpenAIService com.runehive.content.ai.LazyAIManager.openAIService
staticprivate

◆ sessionConsents

final Set<String> com.runehive.content.ai.LazyAIManager.sessionConsents = ConcurrentHashMap.newKeySet()
staticprivate

The documentation for this class was generated from the following file: