Skip to content

Commit

Permalink
Converted drawoffers.js to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
Naviary3 authored Jan 8, 2025
1 parent f75bd77 commit 103e07e
Showing 1 changed file with 45 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@

// Import Start
import guidrawoffer from '../gui/guidrawoffer.js';
import statustext from '../gui/statustext.js';
import websocket from '../websocket.js';
import guipause from '../gui/guipause.js';
import sound from './sound.js';
import moveutil from '../../chess/util/moveutil.js';
import onlinegame from './onlinegame/onlinegame.js';
import gameslot from '../chess/gameslot.js';
import gameloader from '../chess/gameloader.js';
// Import End

'use strict';

/**
* This script stores the logic surrounding draw extending and acceptance
Expand All @@ -21,28 +8,51 @@ import gameloader from '../chess/gameloader.js';
* if we have done so, in the current online game.
*/

import onlinegame from './onlinegame/onlinegame.js';
import gameslot from '../chess/gameslot.js';

// @ts-ignore
import guidrawoffer from '../gui/guidrawoffer.js';
// @ts-ignore
import statustext from '../gui/statustext.js';
// @ts-ignore
import websocket from '../websocket.js';
// @ts-ignore
import guipause from '../gui/guipause.js';
// @ts-ignore
import sound from './sound.js';
// @ts-ignore
import moveutil from '../../chess/util/moveutil.js';
import { DrawOfferInfo } from './onlinegamerouter.js';


// Variables ---------------------------------------------------


/**
* Minimum number of plies (half-moves) that
* must span between 2 consecutive draw offers
* by the same player!
*
* THIS MUST ALWAYS MATCH THE SERVER-SIDE!!!!
*/
const movesBetweenDrawOffers = 2;
const movesBetweenDrawOffers: number = 2;

/** The last move we extended a draw, if we have, otherwise undefined. */
let plyOfLastOfferedDraw;
let plyOfLastOfferedDraw: number | undefined;

/** Whether we have an open draw offer FROM OUR OPPONENT */
let isAcceptingDraw = false;
let isAcceptingDraw: boolean = false;


// Functions ---------------------------------------------------


/**
* Returns true if us extending a dropper to our opponent is legal.
* @returns {boolean}
* Returns true if us extending a draw offer to our opponent is legal.
*/
function isOfferingDrawLegal() {
const gamefile = gameslot.getGamefile();
function isOfferingDrawLegal(): boolean {
const gamefile = gameslot.getGamefile()!;
if (!onlinegame.areInOnlineGame()) return false; // Can't offer draws in local games
if (!moveutil.isGameResignable(gamefile)) return false; // Not atleast 2+ moves
if (onlinegame.hasServerConcludedGame()) return false; // Can't offer draws after the game has ended
Expand All @@ -53,10 +63,9 @@ function isOfferingDrawLegal() {
/**
* Returns true if it's been too soon since our last draw offer extension
* for us to extend another one. We cannot extend them too rapidly.
* @returns {boolean}
*/
function isTooSoonToOfferDraw() {
const gamefile = gameslot.getGamefile();
function isTooSoonToOfferDraw(): boolean {
const gamefile = gameslot.getGamefile()!;
if (plyOfLastOfferedDraw === undefined) return false; // We have made zero offers so far this game

const movesSinceLastOffer = gamefile.moves.length - plyOfLastOfferedDraw;
Expand All @@ -66,9 +75,10 @@ function isTooSoonToOfferDraw() {

/**
* Returns *true* if we have an open draw offer from our OPPONENT.
* @returns {boolean}
*/
function areWeAcceptingDraw() { return isAcceptingDraw; }
function areWeAcceptingDraw(): boolean {
return isAcceptingDraw;
}

/** Is called when we receive a draw offer from our opponent */
function onOpponentExtendedOffer() {
Expand All @@ -89,7 +99,7 @@ function onOpponentDeclinedOffer() {
*/
function extendOffer() {
websocket.sendmessage('game', 'offerdraw');
const gamefile = gameslot.getGamefile();
const gamefile = gameslot.getGamefile()!;
plyOfLastOfferedDraw = gamefile.moves.length;
statustext.showStatus(`Waiting for opponent to accept...`);
guipause.updateDrawOfferButton();
Expand All @@ -111,11 +121,11 @@ function callback_AcceptDraw() {
* the draw offer UI on the bottom navigation bar,
* or when we click "Accept Draw" in the pause menu,
* OR when we make a move while there's an open offer!
* @param {Object} [options] - Optional settings.
* @param {boolean} [options.informServer=true] - If true, the server will be informed that the draw offer has been declined.
* We'll want to set this to false if we call this after making a move, because the server auto-declines it.
*/
function callback_declineDraw({ informServer = true } = {}) {
* @param [options] - Optional settings.
* @param [options.informServer=true] - If true, the server will be informed that the draw offer has been declined.
* We'll want to set this to false if we call this after making a move, because the server auto-declines it.
*/
function callback_declineDraw({ informServer = true }: { informServer?: boolean } = {}) {
if (!isAcceptingDraw) return; // No open draw offer from our opponent

if (informServer) {
Expand All @@ -129,17 +139,15 @@ function callback_declineDraw({ informServer = true } = {}) {
/**
* Set the current draw offer values according to the information provided.
* This is called after a page refresh when we're in a game.
* @param {Object} drawOffer - An object that looks like: `{ unconfirmed, lastOfferPly }`, where `unconfirmed` is
* a boolean that's true if the opponent has an open draw offer we have not yet confirmed/rejected,
* and `lastOfferPly` is the last move ply WE EXTENDED an offer, if we have, otherwise undefined.
*/
function set(drawOffer) {
function set(drawOffer: DrawOfferInfo) {
plyOfLastOfferedDraw = drawOffer.lastOfferPly;
if (!drawOffer.unconfirmed) return; // No open draw offer
// Open draw offer!!
onOpponentExtendedOffer();
}

/** Called whenever a move is played in an online game */
function onMovePlayed({ isOpponents }) {
// Declines any open draw offer from our opponent. We don't need to inform
// the server because the server auto declines when we submit our move.
Expand All @@ -157,6 +165,8 @@ function onGameClose() {
guipause.updateDrawOfferButton();
}



export default {
isOfferingDrawLegal,
areWeAcceptingDraw,
Expand Down

0 comments on commit 103e07e

Please sign in to comment.