messages refactoring
This commit is contained in:
80
src/client/message.ts
Normal file
80
src/client/message.ts
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
|
||||||
|
import * as c from "./constants.js"
|
||||||
|
import { gc } from "./global.js"
|
||||||
|
import * as en from "../shared_js/enums.js"
|
||||||
|
|
||||||
|
/*
|
||||||
|
before game
|
||||||
|
*/
|
||||||
|
export function matchmaking()
|
||||||
|
{
|
||||||
|
const text = "searching...";
|
||||||
|
console.log(text);
|
||||||
|
gc.text1.clear();
|
||||||
|
gc.text1.pos.assign(c.w*0.2, c.h*0.5);
|
||||||
|
gc.text1.text = text;
|
||||||
|
gc.text1.update();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function matchmakingComplete()
|
||||||
|
{
|
||||||
|
const text = "match found !";
|
||||||
|
console.log(text);
|
||||||
|
gc.text1.clear();
|
||||||
|
gc.text1.pos.assign(c.w*0.15, c.h*0.5);
|
||||||
|
gc.text1.text = text;
|
||||||
|
gc.text1.update();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function matchAbort()
|
||||||
|
{
|
||||||
|
const text = "match abort";
|
||||||
|
console.log(text);
|
||||||
|
gc.text1.clear();
|
||||||
|
gc.text1.pos.assign(c.w*0.15, c.h*0.5);
|
||||||
|
gc.text1.text = text;
|
||||||
|
gc.text1.update();
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
gc.text2.pos.assign(c.w*0.44, c.h*0.6);
|
||||||
|
gc.text2.text = "pardon =(";
|
||||||
|
const oriSize = gc.text2.size;
|
||||||
|
gc.text2.size = c.w*0.025;
|
||||||
|
gc.text2.update();
|
||||||
|
gc.text2.size = oriSize;
|
||||||
|
}, 2500);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
in game
|
||||||
|
*/
|
||||||
|
export function win()
|
||||||
|
{
|
||||||
|
gc.text1.pos.assign(c.w*0.415, c.h*0.5);
|
||||||
|
gc.text1.text = "WIN";
|
||||||
|
}
|
||||||
|
|
||||||
|
export function lose()
|
||||||
|
{
|
||||||
|
gc.text1.pos.assign(c.w*0.383, c.h*0.5);
|
||||||
|
gc.text1.text = "LOSE";
|
||||||
|
}
|
||||||
|
|
||||||
|
export function forfeit(playerSide: en.PlayerSide)
|
||||||
|
{
|
||||||
|
if (playerSide === en.PlayerSide.left) {
|
||||||
|
gc.text2.pos.assign(c.w*0.65, c.h*0.42);
|
||||||
|
gc.text3.pos.assign(c.w*0.65, c.h*0.52);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
gc.text2.pos.assign(c.w*0.09, c.h*0.42);
|
||||||
|
gc.text3.pos.assign(c.w*0.09, c.h*0.52);
|
||||||
|
}
|
||||||
|
setTimeout(() => {
|
||||||
|
gc.text2.text = "par forfait";
|
||||||
|
}, 1500);
|
||||||
|
setTimeout(() => {
|
||||||
|
gc.text3.text = "calme ta joie";
|
||||||
|
}, 3500);
|
||||||
|
}
|
||||||
@@ -49,56 +49,17 @@ function init()
|
|||||||
initWebSocket(matchOptions);
|
initWebSocket(matchOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
function matchmaking()
|
export function start()
|
||||||
{
|
|
||||||
const text = "searching...";
|
|
||||||
console.log(text);
|
|
||||||
gc.text1.clear();
|
|
||||||
gc.text1.pos.assign(c.w*0.2, c.h*0.5);
|
|
||||||
gc.text1.text = text;
|
|
||||||
gc.text1.update();
|
|
||||||
}
|
|
||||||
|
|
||||||
function matchmakingComplete()
|
|
||||||
{
|
|
||||||
const text = "match found !";
|
|
||||||
console.log(text);
|
|
||||||
gc.text1.clear();
|
|
||||||
gc.text1.pos.assign(c.w*0.15, c.h*0.5);
|
|
||||||
gc.text1.text = text;
|
|
||||||
gc.text1.update();
|
|
||||||
}
|
|
||||||
|
|
||||||
function matchAbort()
|
|
||||||
{
|
|
||||||
const text = "match abort";
|
|
||||||
console.log(text);
|
|
||||||
gc.text1.clear();
|
|
||||||
gc.text1.pos.assign(c.w*0.15, c.h*0.5);
|
|
||||||
gc.text1.text = text;
|
|
||||||
gc.text1.update();
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
gc.text2.pos.assign(c.w*0.44, c.h*0.6);
|
|
||||||
gc.text2.text = "pardon =(";
|
|
||||||
const oriSize = gc.text2.size;
|
|
||||||
gc.text2.size = c.w*0.025;
|
|
||||||
gc.text2.update();
|
|
||||||
gc.text2.size = oriSize;
|
|
||||||
}, 3000);
|
|
||||||
}
|
|
||||||
|
|
||||||
function matchStart()
|
|
||||||
{
|
{
|
||||||
gc.text1.pos.assign(c.w*0.5, c.h*0.75);
|
gc.text1.pos.assign(c.w*0.5, c.h*0.75);
|
||||||
countdown(c.matchStartDelay/1000, (count: number) => {
|
countdown(c.matchStartDelay/1000, (count: number) => {
|
||||||
gc.text1.clear();
|
gc.text1.clear();
|
||||||
gc.text1.text = `${count}`;
|
gc.text1.text = `${count}`;
|
||||||
gc.text1.update();
|
gc.text1.update();
|
||||||
}, matchResume);
|
}, resume);
|
||||||
}
|
}
|
||||||
|
|
||||||
function matchResume()
|
function resume()
|
||||||
{
|
{
|
||||||
gc.text1.text = "";
|
gc.text1.text = "";
|
||||||
window.addEventListener('keydown', function (e) {
|
window.addEventListener('keydown', function (e) {
|
||||||
@@ -112,6 +73,3 @@ function matchResume()
|
|||||||
pong.gameLoopInterval = window.setInterval(gameLoop, c.gameLoopIntervalMS);
|
pong.gameLoopInterval = window.setInterval(gameLoop, c.gameLoopIntervalMS);
|
||||||
pong.drawLoopInterval = window.setInterval(drawLoop, c.drawLoopIntervalMS);
|
pong.drawLoopInterval = window.setInterval(drawLoop, c.drawLoopIntervalMS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export {matchmaking, matchmakingComplete, matchAbort, matchStart}
|
|
||||||
|
|||||||
@@ -3,7 +3,8 @@ import * as c from "./constants.js"
|
|||||||
import { gc, matchOptions } from "./global.js"
|
import { gc, matchOptions } from "./global.js"
|
||||||
import * as ev from "../shared_js/class/Event.js"
|
import * as ev from "../shared_js/class/Event.js"
|
||||||
import * as en from "../shared_js/enums.js"
|
import * as en from "../shared_js/enums.js"
|
||||||
import { matchmaking, matchmakingComplete, matchAbort, matchStart } from "./pong.js";
|
import * as msg from "./message.js";
|
||||||
|
import { start } from "./pong.js";
|
||||||
import { RacketClient } from "./class/RectangleClient.js";
|
import { RacketClient } from "./class/RectangleClient.js";
|
||||||
import { repeatInput } from "./handleInput.js";
|
import { repeatInput } from "./handleInput.js";
|
||||||
import { soundRoblox } from "./audio.js"
|
import { soundRoblox } from "./audio.js"
|
||||||
@@ -45,7 +46,7 @@ function preMatchListener(this: WebSocket, event: MessageEvent)
|
|||||||
clientInfo.id = (<ev.EventAssignId>data).id;
|
clientInfo.id = (<ev.EventAssignId>data).id;
|
||||||
break;
|
break;
|
||||||
case en.EventTypes.matchmakingInProgress:
|
case en.EventTypes.matchmakingInProgress:
|
||||||
matchmaking();
|
msg.matchmaking();
|
||||||
break;
|
break;
|
||||||
case en.EventTypes.matchmakingComplete:
|
case en.EventTypes.matchmakingComplete:
|
||||||
clientInfo.side = (<ev.EventMatchmakingComplete>data).side;
|
clientInfo.side = (<ev.EventMatchmakingComplete>data).side;
|
||||||
@@ -62,16 +63,16 @@ function preMatchListener(this: WebSocket, event: MessageEvent)
|
|||||||
clientInfo.opponentNextPos = new VectorInteger(clientInfo.opponent.pos.x, clientInfo.opponent.pos.y);
|
clientInfo.opponentNextPos = new VectorInteger(clientInfo.opponent.pos.x, clientInfo.opponent.pos.y);
|
||||||
clientInfo.racket.color = "darkgreen"; // for testing purpose
|
clientInfo.racket.color = "darkgreen"; // for testing purpose
|
||||||
socket.send(JSON.stringify( new ev.ClientEvent(en.EventTypes.clientPlayerReady) )); // TODO: set an interval/timeout to resend until matchStart response (in case of network problem)
|
socket.send(JSON.stringify( new ev.ClientEvent(en.EventTypes.clientPlayerReady) )); // TODO: set an interval/timeout to resend until matchStart response (in case of network problem)
|
||||||
matchmakingComplete();
|
msg.matchmakingComplete();
|
||||||
break;
|
break;
|
||||||
case en.EventTypes.matchStart:
|
case en.EventTypes.matchStart:
|
||||||
socket.removeEventListener("message", preMatchListener);
|
socket.removeEventListener("message", preMatchListener);
|
||||||
socket.addEventListener("message", inGameListener);
|
socket.addEventListener("message", inGameListener);
|
||||||
matchStart();
|
start();
|
||||||
break;
|
break;
|
||||||
case en.EventTypes.matchAbort:
|
case en.EventTypes.matchAbort:
|
||||||
socket.removeEventListener("message", preMatchListener);
|
socket.removeEventListener("message", preMatchListener);
|
||||||
matchAbort();
|
msg.matchAbort();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -173,28 +174,13 @@ function scoreUpdate(data: ev.EventScoreUpdate)
|
|||||||
function matchEnd(data: ev.EventMatchEnd)
|
function matchEnd(data: ev.EventMatchEnd)
|
||||||
{
|
{
|
||||||
if (data.winner === clientInfo.side) {
|
if (data.winner === clientInfo.side) {
|
||||||
gc.text1.pos.assign(c.w*0.415, c.h*0.5);
|
msg.win();
|
||||||
gc.text1.text = "WIN";
|
|
||||||
if (data.forfeit) {
|
if (data.forfeit) {
|
||||||
if (clientInfo.side === en.PlayerSide.left) {
|
msg.forfeit(clientInfo.side);
|
||||||
gc.text2.pos.assign(c.w*0.65, c.h*0.42);
|
|
||||||
gc.text3.pos.assign(c.w*0.65, c.h*0.52);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
gc.text2.pos.assign(c.w*0.09, c.h*0.42);
|
|
||||||
gc.text3.pos.assign(c.w*0.09, c.h*0.52);
|
|
||||||
}
|
|
||||||
setTimeout(() => {
|
|
||||||
gc.text2.text = "par forfait";
|
|
||||||
}, 2500);
|
|
||||||
setTimeout(() => {
|
|
||||||
gc.text3.text = "calme ta joie";
|
|
||||||
}, 5000);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
gc.text1.pos.assign(c.w*0.383, c.h*0.5);
|
msg.lose();
|
||||||
gc.text1.text = "LOSE";
|
|
||||||
}
|
}
|
||||||
// matchEnded = true; // unused
|
// matchEnded = true; // unused
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user