Added logics for saving zone updates
This commit is contained in:
parent
aef82affc5
commit
890a4bdb88
@ -3,3 +3,8 @@ ENV=development
|
|||||||
PORT=4000
|
PORT=4000
|
||||||
DATABASE_URL="mysql://root@localhost:3306/nq"
|
DATABASE_URL="mysql://root@localhost:3306/nq"
|
||||||
JWT_SECRET="secret"
|
JWT_SECRET="secret"
|
||||||
|
|
||||||
|
# Default character create values
|
||||||
|
DEFAULT_CHARACTER_ZONE="0"
|
||||||
|
DEFAULT_CHARACTER_POS_X="0"
|
||||||
|
DEFAULT_CHARACTER_POS_Y="0"
|
@ -9,13 +9,13 @@ interface IZoneLoad {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle character zone request event
|
* Handle game master zone request event
|
||||||
* @param socket
|
* @param socket
|
||||||
* @param io
|
* @param io
|
||||||
*/
|
*/
|
||||||
export default function (socket: TSocket, io: Server) {
|
export default function (socket: TSocket, io: Server) {
|
||||||
socket.on('gm:zone_editor:zone:request', async (data: IZoneLoad) => {
|
socket.on('gm:zone_editor:zone:request', async (data: IZoneLoad) => {
|
||||||
console.log(`---GM ${socket.character?.id} has requested zone in zone editor.`);
|
console.log(`---GM ${socket.character?.id} has requested zone via zone editor.`);
|
||||||
|
|
||||||
if (!data.zoneId) {
|
if (!data.zoneId) {
|
||||||
console.log(`---Zone id not provided.`);
|
console.log(`---Zone id not provided.`);
|
||||||
@ -29,11 +29,7 @@ export default function (socket: TSocket, io: Server) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.join(zone.id.toString());
|
// send over zone to game master
|
||||||
|
socket.emit('gm:zone_editor:zone:load', zone);
|
||||||
// send over zone and characters to socket
|
|
||||||
socket.emit('gm:zone_editor:zone:load', {
|
|
||||||
zone: zone
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
49
src/app/events/GmZoneEditorZoneSave.ts
Normal file
49
src/app/events/GmZoneEditorZoneSave.ts
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import { Server } from "socket.io";
|
||||||
|
import {TSocket} from "../utilities/Types";
|
||||||
|
import ZoneRepository from "../repositories/ZoneRepository";
|
||||||
|
import ZoneManager from "../ZoneManager";
|
||||||
|
import {Character, Zone} from "@prisma/client";
|
||||||
|
|
||||||
|
interface IZoneLoad {
|
||||||
|
zoneId: number;
|
||||||
|
name: string;
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
tiles: number[][];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle game master zone save event
|
||||||
|
* @param socket
|
||||||
|
* @param io
|
||||||
|
*/
|
||||||
|
export default function (socket: TSocket, io: Server) {
|
||||||
|
socket.on('gm:zone_editor:zone:save', async (data: IZoneLoad) => {
|
||||||
|
console.log(`---GM ${socket.character?.id} has saved zone via zone editor.`);
|
||||||
|
|
||||||
|
if (!data.zoneId) {
|
||||||
|
console.log(`---Zone id not provided.`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let zone = await ZoneRepository.getById(data.zoneId);
|
||||||
|
|
||||||
|
if (!zone) {
|
||||||
|
console.log(`---Zone not found.`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await ZoneRepository.update(
|
||||||
|
data.zoneId,
|
||||||
|
data.name,
|
||||||
|
data.width,
|
||||||
|
data.height,
|
||||||
|
data.tiles
|
||||||
|
);
|
||||||
|
|
||||||
|
zone = await ZoneRepository.getById(data.zoneId);
|
||||||
|
|
||||||
|
// send over zone and characters to socket
|
||||||
|
socket.emit('gm:zone_editor:zone:load', zone);
|
||||||
|
});
|
||||||
|
}
|
@ -48,6 +48,25 @@ class ZoneRepository {
|
|||||||
throw new Error(`Failed to create zone: ${error.message}`);
|
throw new Error(`Failed to create zone: ${error.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async update(id: number, name: string, width: number, height: number, tiles: any): Promise<Zone> {
|
||||||
|
try {
|
||||||
|
return await prisma.zone.update({
|
||||||
|
where: {
|
||||||
|
id: id
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
name: name,
|
||||||
|
width: width,
|
||||||
|
height: height,
|
||||||
|
tiles: tiles
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (error: any) {
|
||||||
|
// Handle error
|
||||||
|
throw new Error(`Failed to update zone: ${error.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new ZoneRepository;
|
export default new ZoneRepository;
|
@ -8,6 +8,10 @@ class config
|
|||||||
static HOST: string = process.env.HOST || "0.0.0.0";
|
static HOST: string = process.env.HOST || "0.0.0.0";
|
||||||
static PORT: number = process.env.PORT ? parseInt(process.env.PORT) : 6969;
|
static PORT: number = process.env.PORT ? parseInt(process.env.PORT) : 6969;
|
||||||
static JWT_SECRET: string = process.env.JWT_SECRET || "secret";
|
static JWT_SECRET: string = process.env.JWT_SECRET || "secret";
|
||||||
|
|
||||||
|
static DEFAULT_CHARACTER_ZONE: number = parseInt(process.env.DEFAULT_CHARACTER_ZONE || "1");
|
||||||
|
static DEFAULT_CHARACTER_X: number = parseInt(process.env.DEFAULT_CHARACTER_POS_X || "0");
|
||||||
|
static DEFAULT_CHARACTER_Y: number = parseInt(process.env.DEFAULT_CHARACTER_POS_Y || "0");
|
||||||
}
|
}
|
||||||
|
|
||||||
export default config;
|
export default config;
|
Loading…
x
Reference in New Issue
Block a user