172 líneas
6.1 KiB
JavaScript
172 líneas
6.1 KiB
JavaScript
(function () {
|
|
$(document).ready(function () {
|
|
|
|
class MWCallbackI extends MW.MWCallback {
|
|
message(data) {
|
|
var username = data.split(" ")[0];
|
|
if (data.split(" ")[1] == "left") {
|
|
if (map.players[username]) {
|
|
map.players[username].deleted = true;
|
|
$("#chatOutput").html($("#chatOutput").html() + "<b>[" + [username] + "]</b>" + " left the game.<br>");
|
|
$("#chatOutput").scrollTop($("#chatOutput").get(0).scrollHeight);
|
|
}
|
|
} else if (data.split(" ")[1] == "chat") {
|
|
$("#chatOutput").html($("#chatOutput").html() + "<b>[" + [username] + "]</b>" + ": " + data.replace(username + " chat ", "") + "<br>");
|
|
$("#chatOutput").scrollTop($("#chatOutput").get(0).scrollHeight);
|
|
} else {
|
|
if (username != userName) {
|
|
if (!map.players[username]) {
|
|
map.players[username] = {};
|
|
}
|
|
let jsonData = JSON.parse(data.replace(username + " ", ""))
|
|
if (!map.players[username].item) {
|
|
map.players[username].deleted = false;
|
|
map.players[username].landscapeTexture = new PIXI.Texture.fromImage(map.player.url);
|
|
map.players[username].texture = new PIXI.Texture(map.players[username].landscapeTexture);
|
|
map.players[username].item = new PIXI.Sprite(map.players[username].texture);
|
|
map.players[username].nameStyle = new PIXI.TextStyle({
|
|
fontFamily: 'komtit',
|
|
fontSize: 24,
|
|
fill: ['#FFF1E1', '#FFF1E1'], // gradient
|
|
stroke: '#35130B',
|
|
strokeThickness: 5,
|
|
dropShadow: true,
|
|
dropShadowColor: '#724833',
|
|
dropShadowBlur: 4,
|
|
dropShadowAngle: Math.PI / 2,
|
|
dropShadowDistance: 1,
|
|
wordWrap: false,
|
|
wordWrapWidth: 440,
|
|
padding: 20
|
|
});
|
|
map.players[username].username = username;
|
|
map.players[username].name = new PIXI.Text([username], map.players[username].nameStyle);
|
|
$("#chatOutput").html($("#chatOutput").html() + "<b>[" + [username] + "]</b>" + " join the game.<br>");
|
|
$("#chatOutput").scrollTop($("#chatOutput").get(0).scrollHeight);
|
|
}
|
|
map.players[username].px = jsonData.px;
|
|
map.players[username].py = jsonData.py;
|
|
map.players[username].direction = jsonData.direction;
|
|
map.players[username].state = jsonData.state;
|
|
map.players[username].scale = jsonData.scale;
|
|
map.players[username].score = jsonData.score;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
const State = {
|
|
Disconnected: 0,
|
|
Connecting: 1,
|
|
Connected: 2
|
|
};
|
|
|
|
let state = State.Disconnected;
|
|
|
|
async function runWithSession(router, session) {
|
|
try {
|
|
|
|
const [timeout, category, adapter] = await Promise.all(
|
|
[
|
|
router.getACMTimeout(),
|
|
router.getCategoryForClient(),
|
|
router.ice_getCommunicator().createObjectAdapterWithRouter("", router)
|
|
]);
|
|
|
|
const connection = router.ice_getCachedConnection();
|
|
if (timeout > 0) {
|
|
connection.setACM(timeout, undefined, Ice.ACMHeartbeat.HeartbeatAlways);
|
|
}
|
|
|
|
connection.setCloseCallback(() => error("Connection lost"));
|
|
|
|
const callback = MW.MWCallbackPrx.uncheckedCast(
|
|
adapter.add(new MWCallbackI(), new Ice.Identity("callback", category)));
|
|
|
|
await session.setCallback(callback);
|
|
|
|
state = State.Connected;
|
|
|
|
await new Promise(
|
|
(resolve, reject) => {
|
|
setTimeout(function () {
|
|
run(session);
|
|
}, 2000);
|
|
$("#chatInput").keypress(
|
|
e => {
|
|
if (e.which === 13) {
|
|
(async function () {
|
|
const message = $(e.currentTarget).val();
|
|
$(e.currentTarget).val("");
|
|
try {
|
|
await session.sendPosition("chat " + message);
|
|
} catch (ex) {
|
|
reject(ex);
|
|
}
|
|
|
|
}());
|
|
return false;
|
|
}
|
|
});
|
|
|
|
$("#signout").click(() => {
|
|
connection.setCloseCallback(null);
|
|
resolve();
|
|
});
|
|
});
|
|
await router.destroySession();
|
|
} finally {
|
|
state = State.Disconnected;
|
|
}
|
|
}
|
|
|
|
async function signin() {
|
|
let communicator;
|
|
try {
|
|
state = State.Connecting;
|
|
|
|
const port = "4064";
|
|
if (window.location.hostname == "172.100.0.100") {
|
|
var hostname = "172.100.0.102";
|
|
} else if (window.location.hostname == "192.168.1.51") {
|
|
var hostname = "192.168.1.51";
|
|
} else {
|
|
var hostname = "83.57.124.192";
|
|
}
|
|
|
|
const proxy = "MWServer/router:ws -p " + port + " -h " + hostname;
|
|
|
|
const initData = new Ice.InitializationData();
|
|
initData.properties = Ice.createProperties();
|
|
initData.properties.setProperty("Ice.Default.Router", proxy);
|
|
communicator = Ice.initialize(initData);
|
|
|
|
const router = await Glacier2.RouterPrx.checkedCast(communicator.getDefaultRouter());
|
|
const session = await router.createSession(userName, "pass");
|
|
await runWithSession(router, MW.MWSessionPrx.uncheckedCast(session));
|
|
} catch (ex) {
|
|
|
|
if (ex instanceof Glacier2.PermissionDeniedException) {
|
|
await error("permission denied:\n" + ex.reason);
|
|
} else if (ex instanceof Glacier2.CannotCreateSessionException) {
|
|
await error("cannot create session:\n" + ex.reason);
|
|
} else if (ex instanceof Ice.ConnectFailedException) {
|
|
await error("connection to server failed");
|
|
} else {
|
|
await error(ex.toString());
|
|
}
|
|
} finally {
|
|
if (communicator) {
|
|
await communicator.destroy();
|
|
}
|
|
}
|
|
}
|
|
|
|
async function error(message) {
|
|
console.log(message);
|
|
state = State.Disconnected;
|
|
}
|
|
|
|
signin();
|
|
});
|
|
}()); |