forked from noxious/server
aids
This commit is contained in:
parent
c49a2563f8
commit
2f4ae43996
@ -8,29 +8,16 @@ interface ILoadedZone {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class ZoneManager {
|
class ZoneManager {
|
||||||
private static instance: ZoneManager;
|
|
||||||
private loadedZones: ILoadedZone[] = [];
|
private loadedZones: ILoadedZone[] = [];
|
||||||
|
|
||||||
private constructor() {}
|
|
||||||
|
|
||||||
// Singleton instance getter
|
|
||||||
public static getInstance(): ZoneManager {
|
|
||||||
if (!ZoneManager.instance) {
|
|
||||||
ZoneManager.instance = new ZoneManager();
|
|
||||||
}
|
|
||||||
return ZoneManager.instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Method to initialize zone loading
|
// Method to initialize zone loading
|
||||||
public async boot() {
|
public async boot() {
|
||||||
const zoneRepository = new ZoneRepository();
|
if (!await ZoneRepository.getById(1)) {
|
||||||
|
|
||||||
if (!await zoneRepository.getById(1)) {
|
|
||||||
const zoneService = new ZoneService();
|
const zoneService = new ZoneService();
|
||||||
await zoneService.createDemoZone();
|
await zoneService.createDemoZone();
|
||||||
}
|
}
|
||||||
|
|
||||||
const zones = await zoneRepository.getAll();
|
const zones = await ZoneRepository.getAll();
|
||||||
|
|
||||||
for (const zone of zones) {
|
for (const zone of zones) {
|
||||||
this.loadZone(zone);
|
this.loadZone(zone);
|
||||||
@ -62,6 +49,22 @@ class ZoneManager {
|
|||||||
return loadedZone.zone;
|
return loadedZone.zone;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public addCharacterToZone(zoneId: number, character: Character) {
|
||||||
|
const loadedZone = this.loadedZones.find((loadedZone) => {
|
||||||
|
return loadedZone.zone.id === zoneId;
|
||||||
|
});
|
||||||
|
if (loadedZone) {
|
||||||
|
loadedZone.characters.push(character);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ZoneManager;
|
public getCharactersInZone(zoneId: number): Character[] {
|
||||||
|
const loadedZone = this.loadedZones.find((loadedZone) => {
|
||||||
|
return loadedZone.zone.id === zoneId;
|
||||||
|
});
|
||||||
|
return loadedZone ? loadedZone.characters : [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default new ZoneManager;
|
||||||
|
@ -1,7 +1,29 @@
|
|||||||
import { Socket, Server } from "socket.io";
|
import { Socket, Server } from "socket.io";
|
||||||
|
import ZoneRepository from "../repositories/zone.repository";
|
||||||
|
import ZoneManager from "../ZoneManager";
|
||||||
|
|
||||||
|
interface IZoneLoad {
|
||||||
|
zoneId?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @TODO: Implement zone loading, check if zone id is given or else load current character zone
|
||||||
|
*
|
||||||
|
* Handle character zone load event
|
||||||
|
* @param socket
|
||||||
|
* @param io
|
||||||
|
*/
|
||||||
export default function characterZoneLoad(socket: Socket, io: Server) {
|
export default function characterZoneLoad(socket: Socket, io: Server) {
|
||||||
socket.on('character:map:load', (data) => {
|
socket.on('character:zone:load', async (data: IZoneLoad) => {
|
||||||
console.log(`---User ${socket.id} has requested map.`);
|
console.log(`---User ${socket.id} has requested zone.`);
|
||||||
|
|
||||||
|
const zone = await ZoneRepository.getById(1);
|
||||||
|
|
||||||
|
// zoneManager.addCharacterToZone(1, socket.user.id)
|
||||||
|
|
||||||
|
io.to(socket.id).emit('character:zone:load', {
|
||||||
|
success: true,
|
||||||
|
data: zone
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
25
src/app/events/character.zone.load_players.ts
Normal file
25
src/app/events/character.zone.load_players.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import { Socket, Server } from "socket.io";
|
||||||
|
import ZoneRepository from "../repositories/zone.repository";
|
||||||
|
import ZoneManager from "../ZoneManager";
|
||||||
|
|
||||||
|
interface IZoneLoadPlayers {
|
||||||
|
zoneId: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @TODO: Implement zone loading, check if zone id is given or else load current character zone
|
||||||
|
*
|
||||||
|
* Handle character zone load event
|
||||||
|
* @param socket
|
||||||
|
* @param io
|
||||||
|
*/
|
||||||
|
export default function characterZoneLoad(socket: Socket, io: Server) {
|
||||||
|
socket.on('character:zone:load:players', async (data: IZoneLoadPlayers) => {
|
||||||
|
console.log(`---User ${socket.id} has requested zone players for zone.` + data.zoneId);
|
||||||
|
|
||||||
|
io.to(socket.id).emit('character:zone:load:players', {
|
||||||
|
success: true,
|
||||||
|
data: ZoneManager.getCharactersInZone(data.zoneId)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
@ -15,6 +15,24 @@ class CharacterRepository {
|
|||||||
throw new Error(`Failed to get character by user ID: ${error.message}`);
|
throw new Error(`Failed to get character by user ID: ${error.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async create(userId: number, name: string): Promise<Character | null> {
|
||||||
|
try {
|
||||||
|
return await prisma.character.create({
|
||||||
|
data: {
|
||||||
|
userId,
|
||||||
|
name,
|
||||||
|
position_x: 0,
|
||||||
|
position_y: 0,
|
||||||
|
rotation: 0,
|
||||||
|
zoneId: 1,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (error: any) {
|
||||||
|
// Handle error
|
||||||
|
throw new Error(`Failed to create character: ${error.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new CharacterService;
|
export default new CharacterRepository;
|
@ -50,4 +50,4 @@ class ZoneRepository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default ZoneRepository;
|
export default new ZoneRepository;
|
@ -1,5 +1,6 @@
|
|||||||
import bcrypt from "bcryptjs";
|
import bcrypt from "bcryptjs";
|
||||||
import UserRepository from "../repositories/user.repository";
|
import UserRepository from "../repositories/user.repository";
|
||||||
|
import CharacterRepository from "../repositories/character.repository";
|
||||||
|
|
||||||
class UserService {
|
class UserService {
|
||||||
async login(username: string, password: string): Promise<boolean | any> {
|
async login(username: string, password: string): Promise<boolean | any> {
|
||||||
@ -23,7 +24,12 @@ class UserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const hashedPassword = await bcrypt.hash(password, 10);
|
const hashedPassword = await bcrypt.hash(password, 10);
|
||||||
return await UserRepository.create(username, hashedPassword);
|
const newUser = await UserRepository.create(username, hashedPassword);
|
||||||
|
|
||||||
|
// @TODO: Create a new character for the user
|
||||||
|
const newCharacter = await CharacterRepository.create(newUser.id, newUser.username);
|
||||||
|
|
||||||
|
return newUser
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,8 +5,7 @@ class ZoneService
|
|||||||
{
|
{
|
||||||
async createDemoZone(): Promise<boolean>
|
async createDemoZone(): Promise<boolean>
|
||||||
{
|
{
|
||||||
const zoneRepo = new ZoneRepository();
|
await ZoneRepository.create("Demo Zone", 10, 10, [
|
||||||
await zoneRepo.create("Demo Zone", 10, 10, [
|
|
||||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||||
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
|
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
|
||||||
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
|
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
|
||||||
|
@ -55,8 +55,7 @@ export class Server
|
|||||||
await api.addAuthRoutes(this.app);
|
await api.addAuthRoutes(this.app);
|
||||||
|
|
||||||
// Load zone manager
|
// Load zone manager
|
||||||
const zoneManager = ZoneManager.getInstance();
|
await ZoneManager.boot();
|
||||||
await zoneManager.boot();
|
|
||||||
|
|
||||||
// Listen for socket connections
|
// Listen for socket connections
|
||||||
this.io.on('connection', this.handleConnection.bind(this));
|
this.io.on('connection', this.handleConnection.bind(this));
|
||||||
@ -68,7 +67,7 @@ export class Server
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
private async handleConnection(socket: Socket) {
|
private async handleConnection(socket: Socket) {
|
||||||
const eventsPath = path.join(__dirname, 'events');
|
const eventsPath = path.join(__dirname, 'app', 'events');
|
||||||
try {
|
try {
|
||||||
const files: string[] = await fs.promises.readdir(eventsPath);
|
const files: string[] = await fs.promises.readdir(eventsPath);
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user