From 5fd866623e211890115cd4c6dfc9927ee3f66ad0 Mon Sep 17 00:00:00 2001 From: TwelveNights Date: Thu, 6 Oct 2016 12:02:35 -0700 Subject: [PATCH 01/10] Refactor Riichi point calculations --- imports/api/Constants.js | 9 +- imports/api/NewGameUtils.js | 58 ++- imports/api/PointCalculationUtils.js | 118 ++++++ imports/ui/HongKongNewGame.html | 24 +- imports/ui/HongKongNewGame.js | 128 +++--- imports/ui/JapaneseNewGame.html | 24 +- imports/ui/JapaneseNewGame.js | 589 ++++----------------------- 7 files changed, 324 insertions(+), 626 deletions(-) create mode 100644 imports/api/PointCalculationUtils.js diff --git a/imports/api/Constants.js b/imports/api/Constants.js index 3e1bc10..77c0cb0 100644 --- a/imports/api/Constants.js +++ b/imports/api/Constants.js @@ -43,7 +43,14 @@ export const Constants = { SELF_DRAW: "selfdraw", NO_WIN: "nowin", RESTART: "restart", - MISTAKE: "fuckup" + MISTAKE: "fuckup", + + EAST: "east", + SOUTH: "south", + WEST: "west", + NORTH: "north", + + MANGAN: 2000 }; Object.keys(Constants).forEach((k) => { Template.registerHelper(k, () => Constants[k] )}); diff --git a/imports/api/NewGameUtils.js b/imports/api/NewGameUtils.js index 690c24f..fc1c64c 100644 --- a/imports/api/NewGameUtils.js +++ b/imports/api/NewGameUtils.js @@ -47,24 +47,22 @@ export var NewGameUtils = { // This code could be streamlined, but let's leave it explicit switch (gameType) { - case Constants.GAME_TYPE.HONG_KONG: - if (round <= 4) - return "東"; - if (round > 4 && round <= 8) - return "南"; - if (round > 8 && round <= 12) - return "西"; - else //if (round > 12) - return "北"; - break; - case Constants.GAME_TYPE.JAPANESE: - if (round <= 4) - return "東"; - if (round > 4 && round <= 8) - return "南"; - else //if (round > 8) - return "西";; - break; + case Constants.GAME_TYPE.HONG_KONG: + if (round <= 4) + return "東"; + if (round > 4 && round <= 8) + return "南"; + if (round > 8 && round <= 12) + return "西"; + else //if (round > 12) + return "北"; + case Constants.GAME_TYPE.JAPANESE: + if (round <= 4) + return "東"; + if (round > 4 && round <= 8) + return "南"; + else //if (round > 8) + return "西"; }; }, @@ -229,29 +227,29 @@ export var NewGameUtils = { getDirectionScore(direction) { switch (direction) { - case "east": + case Constants.EAST: return Number(Session.get("east_score")); - case "south": + case Constants.SOUTH: return Number(Session.get("south_score")); - case "west": + case Constants.WEST: return Number(Session.get("west_score")); - case "north": + case Constants.NORTH: return Number(Session.get("north_score")); } }, playerToDirection(player) { - if (player == Session.get("current_east")) return "east"; - if (player == Session.get("current_south")) return "south"; - if (player == Session.get("current_west")) return "west"; - if (player == Session.get("current_north")) return "north"; + if (player == Session.get("current_east")) return Constants.EAST; + if (player == Session.get("current_south")) return Constants.SOUTH; + if (player == Session.get("current_west")) return Constants.WEST; + if (player == Session.get("current_north")) return Constants.NORTH; }, roundToDealerDirection(round) { - if (round % 4 == 1) return "east"; - if (round % 4 == 2) return "south"; - if (round % 4 == 3) return "west"; - if (round % 4 == 0) return "north"; + if (round % 4 == 1) return Constants.EAST; + if (round % 4 == 2) return Constants.SOUTH; + if (round % 4 == 3) return Constants.WEST; + if (round % 4 == 0) return Constants.NORTH; }, }; diff --git a/imports/api/PointCalculationUtils.js b/imports/api/PointCalculationUtils.js new file mode 100644 index 0000000..bddeccf --- /dev/null +++ b/imports/api/PointCalculationUtils.js @@ -0,0 +1,118 @@ +import { Constants } from './Constants'; +import { NewGameUtils } from './NewGameUtils'; + +export var PointCalculationUtils = { + jpn: { + dealin_delta, + selfdraw_delta, + mistake_delta + } +}; + +function dealin_delta(points, fu, winnerWind, loserWind, riichiSticks) { + let winds = {}; + winds[Constants.EAST] = winds[Constants.SOUTH] = winds[Constants.WEST] = winds[Constants.NORTH] = 0; + + let basicPoints; + let multiplier = (winnerWind != NewGameUtils.roundToDealerDirection(Number(Session.get("current_round")))) ? 4 : 6; + let manganPayout = multiplier * Constants.MANGAN; + + if (points < 5) { + if (fu == 20 || (points == 1 && fu == 25)) { + return 0; // Issue Protection + } else { + basicPoints = Math.ceil((fu * Math.pow(2, 2 + points)) * multiplier /100) * 100; + basicPoints = basicPoints < manganPayout ? basicPoints : manganPayout; + } + } else { + switch (points) { + case 5: basicPoints = manganPayout; break; + case 6: + case 7: basicPoints = manganPayout * 1.5; break; + case 8: + case 9: + case 10: basicPoints = manganPayout * 2; break; + case 11: + case 12: basicPoints = manganPayout * 3; break; + case 13: basicPoints = manganPayout * 4; break; + case 26: basicPoints = manganPayout * 4 * 2; break; + case 39: basicPoints = manganPayout * 4 * 3; break; + case 52: basicPoints = manganPayout * 4 * 4; break; + case 65: basicPoints = manganPayout * 4 * 5; break; + } + } + + winds[winnerWind] = basicPoints + 300 * Number(Session.get("current_bonus")) + riichiSticks * 1000; + winds[loserWind] = -basicPoints - 300 * Number(Session.get("current_bonus")); + + Session.set("free_riichi_sticks", 0); + return winds; +}; + +function selfdraw_delta(points, fu, winnerWind, riichiSticks) { + let winds = {}; + winds[Constants.EAST] = winds[Constants.SOUTH] = winds[Constants.WEST] = winds[Constants.NORTH] = 0; + + let basicPoints; + let dealerWind = NewGameUtils.roundToDealerDirection(Number(Session.get("current_round"))); + + if (points < 5) { + if (points == 1 && (fu == 20 || fu == 25)) { + return 0; // Issue Protection + } else { + basicPoints = fu * Math.pow(2, 2 + points); + basicPoints = basicPoints < Constants.MANGAN ? basicPoints : Constants.MANGAN; + } + } else { + switch (points) { + case 5: basicPoints = Constants.MANGAN; break; + case 6: + case 7: basicPoints = Constants.MANGAN * 1.5; break; + case 8: + case 9: + case 10: basicPoints = Constants.MANGAN * 2; break; + case 11: + case 12: basicPoints = Constants.MANGAN * 3; break; + case 13: basicPoints = Constants.MANGAN * 4; break; + case 26: basicPoints = Constants.MANGAN * 4 * 2; break; + case 39: basicPoints = Constants.MANGAN * 4 * 3; break; + case 52: basicPoints = Constants.MANGAN * 4 * 4; break; + case 65: basicPoints = Constants.MANGAN * 4 * 5; break; + }; + } + + let nonDealerPays = Math.ceil(basicPoints/100) * 100; + let dealerPays = Math.ceil(basicPoints/100 * 2) * 100; + + let bonuses = Number(Session.get("current_bonus")); + + for (let w in winds) { + if (winnerWind != dealerWind) { + if (w == dealerWind) { + winds[w] = -dealerPays - 100 * bonuses; + } else if (w == winnerWind) { + winds[w] = dealerPays + 2 * nonDealerPays + 300 * bonuses + riichiSticks * 1000; + } else { + winds[w] = -nonDealerPays - 100 * bonuses; + } + } else { + if (w == winnerWind) { + winds[w] = 3 * nonDealerPays + riichiSticks * 1000; + } else { + winds[w] = -nonDealerPays; + } + } + } + + Session.set("free_riichi_sticks", 0); + return winds; +}; + +function mistake_delta(loser) { + let winds = {}; + winds[Constants.EAST] = winds[Constants.SOUTH] = winds[Constants.WEST] = winds[Constants.NORTH] = 4000; + + winds[loser] = -12000; + + return winds; +}; diff --git a/imports/ui/HongKongNewGame.html b/imports/ui/HongKongNewGame.html index f7ac8f0..847ab21 100644 --- a/imports/ui/HongKongNewGame.html +++ b/imports/ui/HongKongNewGame.html @@ -101,24 +101,24 @@

Hong Kong Mahjong Game Sheet

Transaction total: - {{get_player_delta "east"}} - {{get_player_delta "south"}} - {{get_player_delta "west"}} - {{get_player_delta "north"}} + {{get_player_delta (EAST)}} + {{get_player_delta (SOUTH)}} + {{get_player_delta (WEST)}} + {{get_player_delta (NORTH)}} Current score: - {{get_player_score "east"}} - {{get_player_score "south"}} - {{get_player_score "west"}} - {{get_player_score "north"}} + {{get_player_score (EAST)}} + {{get_player_score (SOUTH)}} + {{get_player_score (WEST)}} + {{get_player_score (NORTH)}} End score: - {{get_player_score_final "east"}} - {{get_player_score_final "south"}} - {{get_player_score_final "west"}} - {{get_player_score_final "north"}} + {{get_player_score_final (EAST)}} + {{get_player_score_final (SOUTH)}} + {{get_player_score_final (WEST)}} + {{get_player_score_final (NORTH)}} diff --git a/imports/ui/HongKongNewGame.js b/imports/ui/HongKongNewGame.js index 1e8afb6..989d3ba 100644 --- a/imports/ui/HongKongNewGame.js +++ b/imports/ui/HongKongNewGame.js @@ -473,35 +473,35 @@ function push_dealin_hand(template) { var winnerWind = NewGameUtils.playerToDirection(Session.get("round_winner")); var loserWind = NewGameUtils.playerToDirection(Session.get("round_loser")); - var eastDelta = dealin_delta(points, "east", winnerWind, loserWind); - var southDelta = dealin_delta(points, "south", winnerWind, loserWind); - var westDelta = dealin_delta(points, "west", winnerWind, loserWind); - var northDelta = dealin_delta(points, "north", winnerWind, loserWind); + var eastDelta = dealin_delta(points, Constants.EAST, winnerWind, loserWind); + var southDelta = dealin_delta(points, Constants.SOUTH, winnerWind, loserWind); + var westDelta = dealin_delta(points, Constants.WEST, winnerWind, loserWind); + var northDelta = dealin_delta(points, Constants.NORTH, winnerWind, loserWind); - if (winnerWind == "east") { + if (winnerWind == Constants.EAST) { Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) + points); } - else if (winnerWind == "south") { + else if (winnerWind == Constants.SOUTH) { Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) + 1); Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) + points); } - else if (winnerWind == "west") { + else if (winnerWind == Constants.WEST) { Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) + 1); Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); } - else if (winnerWind == "north") { + else if (winnerWind == Constants.NORTH) { Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); } - if (loserWind == "east") + if (loserWind == Constants.EAST) Session.set("eastPlayerLosses", Number(Session.get("eastPlayerLosses")) + 1); - else if (loserWind == "south") + else if (loserWind == Constants.SOUTH) Session.set("southPlayerLosses", Number(Session.get("southPlayerLosses")) + 1); - else if (loserWind == "west") + else if (loserWind == Constants.WEST) Session.set("westPlayerLosses", Number(Session.get("westPlayerLosses")) + 1); - else if (loserWind == "north") + else if (loserWind == Constants.NORTH) Session.set("northPlayerLosses", Number(Session.get("northPlayerLosses")) + 1); pushHand(template, "dealin", eastDelta, southDelta, westDelta, northDelta); @@ -518,24 +518,24 @@ function push_selfdraw_hand(template) { var points = Number(Session.get("current_points")); var winnerWind = NewGameUtils.playerToDirection(Session.get("round_winner")); - var eastDelta = selfdraw_delta(points, "east", winnerWind); - var southDelta = selfdraw_delta(points, "south", winnerWind); - var westDelta = selfdraw_delta(points, "west", winnerWind); - var northDelta = selfdraw_delta(points, "north", winnerWind); + var eastDelta = selfdraw_delta(points, Constants.EAST, winnerWind); + var southDelta = selfdraw_delta(points, Constants.SOUTH, winnerWind); + var westDelta = selfdraw_delta(points, Constants.WEST, winnerWind); + var northDelta = selfdraw_delta(points, Constants.NORTH, winnerWind); - if (winnerWind == "east") { + if (winnerWind == Constants.EAST) { Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) + points); } - else if (winnerWind == "south") { + else if (winnerWind == Constants.SOUTH) { Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) + 1); Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) + points); } - else if (winnerWind == "west") { + else if (winnerWind == Constants.WEST) { Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) + 1); Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); } - else if (winnerWind == "north") { + else if (winnerWind == Constants.NORTH) { Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); } @@ -557,50 +557,50 @@ function push_dealin_pao_hand(template) { var paoWind = NewGameUtils.playerToDirection(Session.get("round_pao_player")); var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; - if (winnerWind == "east") { + if (winnerWind == Constants.EAST) { Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); } - else if (winnerWind == "south") { + else if (winnerWind == Constants.SOUTH) { Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) + 1); Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) + points); } - else if (winnerWind == "west") { + else if (winnerWind == Constants.WEST) { Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) + 1); Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); } - else if (winnerWind == "north") { + else if (winnerWind == Constants.NORTH) { Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); } - if (loserWind == "east" || paoWind == "east") + if (loserWind == Constants.EAST || paoWind == Constants.EAST) Session.set("eastPlayerLosses", Number(Session.get("eastPlayerLosses")) + 1); - else if (loserWind == "south" || paoWind == "south") + else if (loserWind == Constants.SOUTH || paoWind == Constants.SOUTH) Session.set("southPlayerLosses", Number(Session.get("southPlayerLosses")) + 1); - else if (loserWind == "west" || paoWind == "west") + else if (loserWind == Constants.WEST || paoWind == Constants.WEST) Session.set("westPlayerLosses", Number(Session.get("westPlayerLosses")) + 1); - else if (loserWind == "north" || paoWind == "north") + else if (loserWind == Constants.NORTH || paoWind == Constants.NORTH) Session.set("northPlayerLosses", Number(Session.get("northPlayerLosses")) + 1); var value = dealin_delta(points, winnerWind, winnerWind); switch (winnerWind) { - case "east": eastDelta += value; break; - case "south": southDelta += value; break; - case "west": westDelta += value; break; - case "north": northDelta += value; break; + case Constants.EAST: eastDelta += value; break; + case Constants.SOUTH: southDelta += value; break; + case Constants.WEST: westDelta += value; break; + case Constants.NORTH: northDelta += value; break; } - if (loserWind == "east") eastDelta -= value / 2; - else if (loserWind == "south") southDelta -= value / 2; - else if (loserWind == "west") westDelta -= value / 2; - else if (loserWind == "north") northDelta -= value / 2; + if (loserWind == Constants.EAST) eastDelta -= value / 2; + else if (loserWind == Constants.SOUTH) southDelta -= value / 2; + else if (loserWind == Constants.WEST) westDelta -= value / 2; + else if (loserWind == Constants.NORTH) northDelta -= value / 2; - if (paoWind == "east") eastDelta -= value / 2; - else if (paoWind == "south") southDelta -= value / 2; - else if (paoWind == "west") westDelta -= value / 2; - else if (paoWind == "north") northDelta -= value / 2; + if (paoWind == Constants.EAST) eastDelta -= value / 2; + else if (paoWind == Constants.SOUTH) southDelta -= value / 2; + else if (paoWind == Constants.WEST) westDelta -= value / 2; + else if (paoWind == Constants.NORTH) northDelta -= value / 2; pushHand(template, "dealin", eastDelta, southDelta, westDelta, northDelta); @@ -618,43 +618,43 @@ function push_selfdraw_pao_hand(template) { var paoWind = NewGameUtils.playerToDirection(Session.get("round_pao_player")); var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; - if (winnerWind == "east") { + if (winnerWind == Constants.EAST) { Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); } - else if (winnerWind == "south") { + else if (winnerWind == Constants.SOUTH) { Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) + 1); Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) + points); } - else if (winnerWind == "west") { + else if (winnerWind == Constants.WEST) { Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) + 1); Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); } - else if (winnerWind == "north") { + else if (winnerWind == Constants.NORTH) { Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); } - if (paoWind == "east") + if (paoWind == Constants.EAST) Session.set("eastPlayerLosses", Number(Session.get("eastPlayerLosses")) + 1); - else if (paoWind == "south") + else if (paoWind == Constants.SOUTH) Session.set("southPlayerLosses", Number(Session.get("southPlayerLosses")) + 1); - else if (paoWind == "west") + else if (paoWind == Constants.WEST) Session.set("westPlayerLosses", Number(Session.get("westPlayerLosses")) + 1); - else if (paoWind == "north") + else if (paoWind == Constants.NORTH) Session.set("northPlayerLosses", Number(Session.get("northPlayerLosses")) + 1); var value = selfdraw_delta(points, winnerWind, winnerWind); - if (winnerWind == "east") eastDelta += value; - else if (winnerWind == "south") southDelta += value; - else if (winnerWind == "west") westDelta += value; - else if (winnerWind == "north") northDelta += value; + if (winnerWind == Constants.EAST) eastDelta += value; + else if (winnerWind == Constants.SOUTH) southDelta += value; + else if (winnerWind == Constants.WEST) westDelta += value; + else if (winnerWind == Constants.NORTH) northDelta += value; - if (paoWind == "east") eastDelta -= value; - else if (paoWind == "south") southDelta -= value; - else if (paoWind == "west") westDelta -= value; - else if (paoWind == "north") northDelta -= value; + if (paoWind == Constants.EAST) eastDelta -= value; + else if (paoWind == Constants.SOUTH) southDelta -= value; + else if (paoWind == Constants.WEST) westDelta -= value; + else if (paoWind == Constants.NORTH) northDelta -= value; pushHand(template, "selfdraw", eastDelta, southDelta, westDelta, northDelta); @@ -681,15 +681,15 @@ function push_restart_hand(template) { function push_mistake_hand(template) { var loserWind = NewGameUtils.playerToDirection(Session.get("round_loser")); - var eastDelta = mistake_delta("east", loserWind); - var southDelta = mistake_delta("south", loserWind); - var westDelta = mistake_delta("west", loserWind); - var northDelta = mistake_delta("north", loserWind); + var eastDelta = mistake_delta(Constants.EAST, loserWind); + var southDelta = mistake_delta(Constants.SOUTH, loserWind); + var westDelta = mistake_delta(Constants.WEST, loserWind); + var northDelta = mistake_delta(Constants.NORTH, loserWind); - if (loserWind == "east") Session.set("eastFuckupTotal", Number(Session.get("eastFuckupTotal")) + 1); - else if (loserWind == "south") Session.set("southFuckupTotal", Number(Session.get("southFuckupTotal")) + 1); - else if (loserWind == "west") Session.set("westFuckupTotal", Number(Session.get("westFuckupTotal")) + 1); - else if (loserWind == "north") Session.set("northFuckupTotal", Number(Session.get("northFuckupTotal")) + 1); + if (loserWind == Constants.EAST) Session.set("eastFuckupTotal", Number(Session.get("eastFuckupTotal")) + 1); + else if (loserWind == Constants.SOUTH) Session.set("southFuckupTotal", Number(Session.get("southFuckupTotal")) + 1); + else if (loserWind == Constants.WEST) Session.set("westFuckupTotal", Number(Session.get("westFuckupTotal")) + 1); + else if (loserWind == Constants.NORTH) Session.set("northFuckupTotal", Number(Session.get("northFuckupTotal")) + 1); pushHand(template, "fuckup", eastDelta, southDelta, westDelta, northDelta); }; diff --git a/imports/ui/JapaneseNewGame.html b/imports/ui/JapaneseNewGame.html index 9990a55..4bc9e15 100644 --- a/imports/ui/JapaneseNewGame.html +++ b/imports/ui/JapaneseNewGame.html @@ -98,24 +98,24 @@

Riichi Japanese Mahjong Game Sheet

Transaction total: - {{get_player_delta "east"}} - {{get_player_delta "south"}} - {{get_player_delta "west"}} - {{get_player_delta "north"}} + {{get_player_delta (EAST)}} + {{get_player_delta (SOUTH)}} + {{get_player_delta (WEST)}} + {{get_player_delta (NORTH)}} Current score: - {{get_player_score "east"}} - {{get_player_score "south"}} - {{get_player_score "west"}} - {{get_player_score "north"}} + {{get_player_score (EAST)}} + {{get_player_score (SOUTH)}} + {{get_player_score (WEST)}} + {{get_player_score (NORTH)}} End score: - {{get_player_score_final "east"}} - {{get_player_score_final "south"}} - {{get_player_score_final "west"}} - {{get_player_score_final "north"}} + {{get_player_score_final (EAST)}} + {{get_player_score_final (SOUTH)}} + {{get_player_score_final (WEST)}} + {{get_player_score_final (NORTH)}} diff --git a/imports/ui/JapaneseNewGame.js b/imports/ui/JapaneseNewGame.js index b429279..392aa43 100644 --- a/imports/ui/JapaneseNewGame.js +++ b/imports/ui/JapaneseNewGame.js @@ -5,6 +5,7 @@ import { JapaneseHands } from '../api/GameDatabases.js'; import { Constants } from '../api/Constants.js'; import { EloCalculator } from '../api/EloCalculator.js'; import { NewGameUtils } from '../api/NewGameUtils.js'; +import { PointCalculationUtils } from '../api/PointCalculationUtils'; // Code to be evaluated when JapaneseNewGame template is reloaded Template.JapaneseNewGame.onCreated( function() { @@ -126,16 +127,16 @@ Template.JapaneseNewGame.helpers({ Number(Session.get("north_score"))); if (winScore == Session.get("east_score")) { - if (direction == "east") + if (direction == Constants.EAST) retval += 1000 * Number(Session.get("free_riichi_sticks")); } else if (winScore == Session.get("south_score")) { - if (direction == "south") + if (direction == Constants.SOUTH) retval += 1000 * Number(Session.get("free_riichi_sticks")); } else if (winScore == Session.get("west_score")) { - if (direction == "west") + if (direction == Constants.WEST) retval += 1000 * Number(Session.get("free_riichi_sticks")); } else if (winScore == Session.get("north_score")) { - if (direction == "north") + if (direction == Constants.NORTH) retval += 1000 * Number(Session.get("free_riichi_sticks")); } @@ -150,10 +151,8 @@ Template.JapaneseNewGame.helpers({ case Constants.DEFAULT_WEST: case Constants.DEFAULT_NORTH: return "?"; - break; default: return Players.findOne({japaneseLeagueName: player}).japaneseElo.toFixed(2); - break; }; }, // Return a string of the round wind for Japanese style @@ -206,6 +205,7 @@ Template.jpn_points.helpers({ { point: 26 }, { point: 39 }, { point: 52 }, + { point: 65 } ], }); @@ -617,7 +617,7 @@ function save_game_to_database(hands_array) { }; // Initialise ELO calculator to update player ELO - var jpn_elo_calculator = new EloCalculator(2000, 5, [15000, 0, -5000, -10000], game, Constants.GAME_TYPE.JAPANESE); + var jpn_elo_calculator = new EloCalculator(2000, 5, [15000, 0, -5000, -10000], game, Constants.GAME_TYPE.JAPANESE); var east_elo_delta = jpn_elo_calculator.eloChange(east_player); var south_elo_delta = jpn_elo_calculator.eloChange(south_player); var west_elo_delta = jpn_elo_calculator.eloChange(west_player); @@ -722,7 +722,7 @@ function save_game_to_database(hands_array) { Players.update({_id: west_id}, {$inc: {japanesePositionSum: position}}); // Calculate east position quickly? - var position = 4; + position = 4; if (Number(Session.get("north_score")) > Number(Session.get("east_score"))) position--; if (Number(Session.get("north_score")) > Number(Session.get("south_score"))) position--; if (Number(Session.get("north_score")) > Number(Session.get("west_score"))) position--; @@ -742,42 +742,44 @@ function push_dealin_hand(template) { var riichiSum = Session.get("free_riichi_sticks"); var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; - if (winnerWind == "east") { + if (winnerWind == Constants.EAST) { Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) + points); Session.set("eastPlayerDoraSum", Number(Session.get("eastPlayerDoraSum")) + dora); - if (Session.get("east_riichi") == true) + if (Session.get("east_riichi") == true) { Session.set("eastPlayerRiichisWon", Number(Session.get("eastPlayerRiichisWon")) + 1); - } - else if (winnerWind == "south") { + } + } else if (winnerWind == Constants.SOUTH) { Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) + 1); Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) + points); Session.set("southPlayerDoraSum", Number(Session.get("southPlayerDoraSum")) + dora); - if (Session.get("south_riichi") == true) + if (Session.get("south_riichi") == true) { Session.set("southPlayerRiichisWon", Number(Session.get("southPlayerRiichisWon")) + 1); - } - else if (winnerWind == "west") { + } + } else if (winnerWind == Constants.WEST) { Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) + 1); Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); Session.set("westPlayerDoraSum", Number(Session.get("westPlayerDoraSum")) + dora); - if (Session.get("west_riichi") == true) + if (Session.get("west_riichi") == true) { Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) + 1); + } } - else if (winnerWind == "north") { + else if (winnerWind == Constants.NORTH) { Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); Session.set("northPlayerDoraSum", Number(Session.get("northPlayerDoraSum")) + dora); - if (Session.get("north_riichi") == true) + if (Session.get("north_riichi") == true) { Session.set("northPlayerRiichisWon", Number(Session.get("northPlayerRiichisWon")) + 1); + } } - if (loserWind == "east") + if (loserWind == Constants.EAST) Session.set("eastPlayerLosses", Number(Session.get("eastPlayerLosses")) + 1); - else if (loserWind == "south") + else if (loserWind == Constants.SOUTH) Session.set("southPlayerLosses", Number(Session.get("southPlayerLosses")) + 1); - else if (loserWind == "west") + else if (loserWind == Constants.WEST) Session.set("westPlayerLosses", Number(Session.get("westPlayerLosses")) + 1); - else if (loserWind == "north") + else if (loserWind == Constants.NORTH) Session.set("northPlayerLosses", Number(Session.get("northPlayerLosses")) + 1); if (Session.get("east_riichi") == true) { @@ -801,14 +803,12 @@ function push_dealin_hand(template) { Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); } - eastDelta += dealin_delta(points, fu, "east", winnerWind, loserWind) + - rewardRiichiSticks(riichiSum, "east", winnerWind); - southDelta += dealin_delta(points, fu, "south", winnerWind, loserWind) + - rewardRiichiSticks(riichiSum, "south", winnerWind); - westDelta += dealin_delta(points, fu, "west", winnerWind, loserWind) + - rewardRiichiSticks(riichiSum, "west", winnerWind); - northDelta += dealin_delta(points, fu, "north", winnerWind, loserWind) + - rewardRiichiSticks(riichiSum, "north", winnerWind); + allDelta = PointCalculationUtils.jpn.dealin_delta(points, fu, winnerWind, loserWind, riichiSum); + + eastDelta += allDelta[Constants.EAST]; + southDelta += allDelta[Constants.SOUTH]; + westDelta += allDelta[Constants.WEST]; + northDelta += allDelta[Constants.NORTH]; pushHand(template, "dealin", eastDelta, southDelta, westDelta, northDelta); @@ -833,28 +833,28 @@ function push_selfdraw_hand(template) { var riichiSum = Session.get("free_riichi_sticks"); var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; - if (winnerWind == "east") { + if (winnerWind == Constants.EAST) { Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) + points); Session.set("eastPlayerDoraSum", Number(Session.get("eastPlayerDoraSum")) + dora); if (Session.get("east_riichi") == true) Session.set("eastPlayerRiichisWon", Number(Session.get("eastPlayerRiichisWon")) + 1); } - else if (winnerWind == "south") { + else if (winnerWind == Constants.SOUTH) { Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) + 1); Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) + points); Session.set("southPlayerDoraSum", Number(Session.get("southPlayerDoraSum")) + dora); if (Session.get("south_riichi") == true) Session.set("southPlayerRiichisWon", Number(Session.get("southPlayerRiichisWon")) + 1); } - else if (winnerWind == "west") { + else if (winnerWind == Constants.WEST) { Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) + 1); Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); Session.set("westPlayerDoraSum", Number(Session.get("westPlayerDoraSum")) + dora); if (Session.get("west_riichi") == true) Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) + 1); } - else if (winnerWind == "north") { + else if (winnerWind == Constants.NORTH) { Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); Session.set("northPlayerDoraSum", Number(Session.get("northPlayerDoraSum")) + dora); @@ -883,14 +883,12 @@ function push_selfdraw_hand(template) { Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); } - eastDelta += selfdraw_delta(points, fu, "east", winnerWind) + - rewardRiichiSticks(riichiSum, "east", winnerWind); - southDelta += selfdraw_delta(points, fu, "south", winnerWind) + - rewardRiichiSticks(riichiSum, "south", winnerWind); - westDelta += selfdraw_delta(points, fu, "west", winnerWind) + - rewardRiichiSticks(riichiSum, "west", winnerWind); - northDelta += selfdraw_delta(points, fu, "north", winnerWind) + - rewardRiichiSticks(riichiSum, "north", winnerWind); + allDeltas = PointCalculationUtils.jpn.selfdraw_delta(points, fu, winnerWind, riichiSum); + + eastDelta += allDeltas[Constants.EAST]; + southDelta += allDeltas[Constants.SOUTH]; + westDelta += allDeltas[Constants.WEST]; + northDelta += allDeltas[Constants.NORTH]; pushHand(template, "selfdraw", eastDelta, southDelta, westDelta, northDelta); @@ -1002,15 +1000,18 @@ function push_restart_hand(template) { function push_mistake_hand(template) { var loserWind = NewGameUtils.playerToDirection(Session.get("round_loser")); - var eastDelta = mistake_delta("east", loserWind); - var southDelta = mistake_delta("south", loserWind); - var westDelta = mistake_delta("west", loserWind); - var northDelta = mistake_delta("north", loserWind); - if (loserWind == "east") Session.set("eastFuckupTotal", Number(Session.get("eastFuckupTotal")) + 1); - else if (loserWind == "south") Session.set("southFuckupTotal", Number(Session.get("southFuckupTotal")) + 1); - else if (loserWind == "west") Session.set("westFuckupTotal", Number(Session.get("westFuckupTotal")) + 1); - else if (loserWind == "north") Session.set("northFuckupTotal", Number(Session.get("northFuckupTotal")) + 1); + let allDeltas = PointCalculationUtils.jpn.mistake_delta(loserWind); + + let eastDelta = allDeltas[Constants.EAST]; + let southDelta = allDeltas[Constants.SOUTH]; + let westDelta = allDeltas[Constants.WEST]; + let northDelta = allDeltas[Constants.NORTH]; + + if (loserWind == Constants.EAST) Session.set("eastFuckupTotal", Number(Session.get("eastFuckupTotal")) + 1); + else if (loserWind == Constants.SOUTH) Session.set("southFuckupTotal", Number(Session.get("southFuckupTotal")) + 1); + else if (loserWind == Constants.WEST) Session.set("westFuckupTotal", Number(Session.get("westFuckupTotal")) + 1); + else if (loserWind == Constants.NORTH) Session.set("northFuckupTotal", Number(Session.get("northFuckupTotal")) + 1); pushHand(template, "fuckup", eastDelta, southDelta, westDelta, northDelta); @@ -1030,28 +1031,28 @@ function push_split_pao_hand(template) { var riichiSum = Session.get("free_riichi_sticks"); var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; - if (winnerWind == "east") { + if (winnerWind == Constants.EAST) { Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) + points); Session.set("eastPlayerDoraSum", Number(Session.get("eastPlayerDoraSum")) + dora); if (Session.get("east_riichi") == true) Session.set("eastPlayerRiichisWon", Number(Session.get("eastPlayerRiichisWon")) + 1); } - else if (winnerWind == "south") { + else if (winnerWind == Constants.SOUTH) { Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) + 1); Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) + points); Session.set("southPlayerDoraSum", Number(Session.get("southPlayerDoraSum")) + dora); if (Session.get("south_riichi") == true) Session.set("southPlayerRiichisWon", Number(Session.get("southPlayerRiichisWon")) + 1); } - else if (winnerWind == "west") { + else if (winnerWind == Constants.WEST) { Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) + 1); Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); Session.set("westPlayerDoraSum", Number(Session.get("westPlayerDoraSum")) + dora); if (Session.get("west_riichi") == true) Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) + 1); } - else if (winnerWind == "north") { + else if (winnerWind == Constants.NORTH) { Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); Session.set("northPlayerDoraSum", Number(Session.get("northPlayerDoraSum")) + dora); @@ -1059,13 +1060,13 @@ function push_split_pao_hand(template) { Session.set("northPlayerRiichisWon", Number(Session.get("northPlayerRiichisWon")) + 1); } - if (loserWind == "east" || paoWind == "east") + if (loserWind == Constants.EAST || paoWind == Constants.EAST) Session.set("eastPlayerLosses", Number(Session.get("eastPlayerLosses")) + 1); - else if (loserWind == "south" || paoWind == "south") + else if (loserWind == Constants.SOUTH || paoWind == Constants.SOUTH) Session.set("southPlayerLosses", Number(Session.get("southPlayerLosses")) + 1); - else if (loserWind == "west" || paoWind == "west") + else if (loserWind == Constants.WEST || paoWind == Constants.WEST) Session.set("westPlayerLosses", Number(Session.get("westPlayerLosses")) + 1); - else if (loserWind == "north" || paoWind == "north") + else if (loserWind == Constants.NORTH || paoWind == Constants.NORTH) Session.set("northPlayerLosses", Number(Session.get("northPlayerLosses")) + 1); if (Session.get("east_riichi") == true) { @@ -1089,36 +1090,37 @@ function push_split_pao_hand(template) { Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); } - var value = dealin_delta(points, fu, winnerWind, winnerWind); + var value = PointCalculationUtils.jpn.dealin_delta(points, fu, winnerWind, loserWind, 0)[loserWind]; - if (((value / 2 ) % 100) == 50) + if (((value / 2 ) % 100) == 50) { value += 100; + } switch (winnerWind) { - case "east": - eastDelta += value; - eastDelta += (riichiSum * 1000); - break; - case "south": - southDelta += value; - southDelta += (riichiSum * 1000); - break; - case "west": - westDelta += value; - westDelta += (riichiSum * 1000); - break; - case "north": - northDelta += value; - northDelta += (riichiSum * 1000); - break; + case Constants.EAST: + eastDelta += value; + eastDelta += (riichiSum * 1000); + break; + case Constants.SOUTH: + southDelta += value; + southDelta += (riichiSum * 1000); + break; + case Constants.WEST: + westDelta += value; + westDelta += (riichiSum * 1000); + break; + case Constants.NORTH: + northDelta += value; + northDelta += (riichiSum * 1000); + break; } Session.set("free_riichi_sticks", 0); - if (loserWind == "east" || paoWind == "east") eastDelta -= value / 2; - if (loserWind == "south" || paoWind == "south") southDelta -= value / 2; - if (loserWind == "west" || paoWind == "west") westDelta -= value / 2; - if (loserWind == "north" || paoWind == "north") northDelta -= value / 2; + if (loserWind == Constants.EAST || paoWind == Constants.EAST) eastDelta -= value / 2; + if (loserWind == Constants.SOUTH || paoWind == Constants.SOUTH) southDelta -= value / 2; + if (loserWind == Constants.WEST || paoWind == Constants.WEST) westDelta -= value / 2; + if (loserWind == Constants.NORTH || paoWind == Constants.NORTH) northDelta -= value / 2; pushHand(template, "selfdraw", eastDelta, southDelta, westDelta, northDelta); @@ -1156,433 +1158,6 @@ function pushHand(template, handType, eastDelta, southDelta, westDelta, northDel Session.set("north_score", Number(Session.get("north_score")) + northDelta); }; -function dealin_delta(points, fu, playerWind, winnerWind, loserWind) { - var retval; - - if (playerWind != winnerWind && playerWind != loserWind) - return 0; - - if (winnerWind != NewGameUtils.roundToDealerDirection(Number(Session.get("current_round")))) { - switch (points) { - case 1: - switch (fu) { - //Issue protection against 20 and 25 other ways but return 0 - case 20: - case 25: retval = 0; break; - case 30: retval = -1000; break; - case 40: retval = -1300; break; - case 50: retval = -1600; break; - case 60: retval = -2000; break; - case 70: retval = -2300; break; - case 80: retval = -2600; break; - case 90: retval = -2900; break; - case 100: retval = -3200; break; - case 110: retval = -3600; break; - } - break; - case 2: - switch (fu) { - case 20: retval = -1300; break; - case 25: retval = -1600; break; - case 30: retval = -2000; break; - case 40: retval = -2600; break; - case 50: retval = -3200; break; - case 60: retval = -3900; break; - case 70: retval = -4500; break; - case 80: retval = -5200; break; - case 90: retval = -5800; break; - case 100: retval = -6400; break; - case 110: retval = -7100; break; - } - break; - case 3: - switch (fu) { - case 20: retval = -2600; break; - case 25: retval = -3200; break; - case 30: retval = -3900; break; - case 40: retval = -5200; break; - case 50: retval = -6400; break; - case 60: retval = -7700; break; - default: retval = -8000; break; - } - break; - case 4: - switch (fu) { - case 20: retval = -5200; break; - case 25: retval = -6400; break; - case 30: retval = -7700; break; - default: retval = -8000; break; - } - break; - case 5: retval = -8000; break; - case 6: - case 7: retval = -12000; break; - case 8: - case 9: - case 10: retval = -16000; break; - case 11: - case 12: retval = -24000; break; - case 13: retval = -32000; break; - case 26: retval = -64000; break; - case 39: retval = -96000; break; - case 52: retval = -128000; break; - } - } else { - switch (points) { - case 1: - switch (fu) { - //Issue protection against 20 and 25 other ways - case 20: - case 25: retval = 0; break; - case 30: retval = -1500; break; - case 40: retval = -2000; break; - case 50: retval = -2400; break; - case 60: retval = -2900; break; - case 70: retval = -3400; break; - case 80: retval = -3900; break; - case 90: retval = -4400; break; - case 100: retval = -4800; break; - case 110: retval = -5300; break; - } - break; - case 2: - switch (fu) { - case 20: retval = -2000; break; - case 25: retval = -2400; break; - case 30: retval = -2900; break; - case 40: retval = -3900; break; - case 50: retval = -4800; break; - case 60: retval = -5800; break; - case 70: retval = -6800; break; - case 80: retval = -7700; break; - case 90: retval = -8700; break; - case 100: retval = -9600; break; - case 110: retval = -10600; break; - } - break; - case 3: - switch (fu) { - case 20: retval = -3900; break; - case 25: retval = -4800; break; - case 30: retval = -5800; break; - case 40: retval = -7700; break; - case 50: retval = -9600; break; - case 60: retval = -11600; break; - default: retval = -12000; break; - } - break; - case 4: - switch (fu) { - case 20: retval = -7700; break; - case 25: retval = -9600; break; - case 30: retval = -11600; break; - default: retval = -12000; break; - } - break; - case 5: retval = -12000; break; - case 6: - case 7: retval = -18000; break; - case 8: - case 9: - case 10: retval = -24000; break; - case 11: - case 12: retval = -36000; break; - case 13: retval = -48000; break; - case 26: retval = -96000; break; - case 39: retval = -144000; break; - case 52: retval = -192000; break; - } - } - - if ( playerWind == winnerWind ) - retval = -1 * retval + 300 * Number(Session.get("current_bonus")); - else - retval = retval - 300 * Number(Session.get("current_bonus")); - - return retval; -}; - -function selfdraw_delta(points, fu, playerWind, winnerWind) { - var retval; - var dealerWind = NewGameUtils.roundToDealerDirection(Number(Session.get("current_round"))); - - if (winnerWind != dealerWind) { - switch (points) { - case 1: - switch (fu) { - // Issue protection against 20 and 25 other ways - case 20: - case 25: - retval = 0; - break; - case 30: - retval = (playerWind == dealerWind ? -500 : -300); - retval = (playerWind == winnerWind ? 1100 : retval); - break; - case 40: - retval = (playerWind == dealerWind ? -700 : -400); - retval = (playerWind == winnerWind ? 1500 : retval); - break; - case 50: - retval = (playerWind == dealerWind ? -800 : -400); - retval = (playerWind == winnerWind ? 1600 : retval); - break; - case 60: - retval = (playerWind == dealerWind ? -100 : -500); - retval = (playerWind == winnerWind ? 2000 : retval); - break; - case 70: - retval = (playerWind == dealerWind ? -1200 : -600); - retval = (playerWind == winnerWind ? 2400 : retval); - break; - case 80: - retval = (playerWind == dealerWind ? -1300 : -700); - retval = (playerWind == winnerWind ? 2700 : retval); - break; - case 90: - retval = (playerWind == dealerWind ? -1500 : -800); - retval = (playerWind == winnerWind ? 3100 : retval); - break; - case 100: - retval = (playerWind == dealerWind ? -1600 : -800); - retval = (playerWind == winnerWind ? 3200 : retval); - break; - case 110: - retval = (playerWind == dealerWind ? -1800 : -900); - retval = (playerWind == winnerWind ? 3600 : retval); - break; - }; - break; - case 2: - switch (fu) { - case 20: - retval = (playerWind == dealerWind ? -700 : -400); - retval = (playerWind == winnerWind ? 1500 : retval); - break; - //Issue protection against selfdraw 2p25f other ways - case 25: - retval = 0; - break; - case 30: - retval = (playerWind == dealerWind ? -1000 : -500); - retval = (playerWind == winnerWind ? 2000 : retval); - break; - case 40: - retval = (playerWind == dealerWind ? -1300 : -700); - retval = (playerWind == winnerWind ? 2700 : retval); - break; - case 50: - retval = (playerWind == dealerWind ? -1600 : -800); - retval = (playerWind == winnerWind ? 3200 : retval); - break; - case 60: - retval = (playerWind == dealerWind ? -2000 : -1000); - retval = (playerWind == winnerWind ? 4000 : retval); - break; - case 70: - retval = (playerWind == dealerWind ? -2300 : -1200); - retval = (playerWind == winnerWind ? 4700 : retval); - break; - case 80: - retval = (playerWind == dealerWind ? -2600 : -1300); - retval = (playerWind == winnerWind ? 5200 : retval); - break; - case 90: - retval = (playerWind == dealerWind ? -2900 : -1500); - retval = (playerWind == winnerWind ? 5900 : retval); - break; - case 100: - retval = (playerWind == dealerWind ? -3200 : -1600); - retval = (playerWind == winnerWind ? 6400 : retval); - break; - case 110: - retval = (playerWind == dealerWind ? -3600 : -1800); - retval = (playerWind == winnerWind ? 7200 : retval); - break; - }; - break; - case 3: - switch (fu) { - case 20: - retval = (playerWind == dealerWind ? -1300 : -700); - retval = (playerWind == winnerWind ? 2700 : retval); - break; - case 25: - retval = (playerWind == dealerWind ? -1600 : -800); - retval = (playerWind == winnerWind ? 3200 : retval); - break; - case 30: - retval = (playerWind == dealerWind ? -2000 : -1000); - retval = (playerWind == winnerWind ? 4000 : retval); - break; - case 40: - retval = (playerWind == dealerWind ? -2600 : -1300); - retval = (playerWind == winnerWind ? 5200 : retval); - break; - case 50: - retval = (playerWind == dealerWind ? -3200 : -1600); - retval = (playerWind == winnerWind ? 6400 : retval); - break; - case 60: - retval = (playerWind == dealerWind ? -3900 : -2000); - retval = (playerWind == winnerWind ? 7900 : retval); - break; - default: - retval = (playerWind == dealerWind ? -4000 : -2000); - retval = (playerWind == winnerWind ? 8000 : retval); - break; - }; - break; - case 4: - switch (fu) { - case 20: - retval = (playerWind == dealerWind ? -2600 : -1300); - retval = (playerWind == winnerWind ? 5200 : retval); - break; - case 25: - retval = (playerWind == dealerWind ? -3200 : -1600); - retval = (playerWind == winnerWind ? 6400 : retval); - break; - case 30: - retval = (playerWind == dealerWind ? -3900 : -2000); - retval = (playerWind == winnerWind ? 7900 : retval); - break; - default: - retval = (playerWind == dealerWind ? -4000 : -2000); - retval = (playerWind == winnerWind ? 8000 : retval); - break; - }; - break; - case 5: - retval = (playerWind == dealerWind ? -4000 : -2000); - retval = (playerWind == winnerWind ? 8000 : retval); - break; - case 6: - case 7: - retval = (playerWind == dealerWind ? -6000 : -3000); - retval = (playerWind == winnerWind ? 12000 : retval); - break; - case 8: - case 9: - case 10: - retval = (playerWind == dealerWind ? -8000 : -4000); - retval = (playerWind == winnerWind ? 16000 : retval); - break; - case 11: - case 12: - retval = (playerWind == dealerWind ? -12000 : -6000); - retval = (playerWind == winnerWind ? 24000 : retval); - break; - case 13: - retval = (playerWind == dealerWind ? -16000 : -8000); - retval = (playerWind == winnerWind ? 32000 : retval); - break; - case 26: - retval = (playerWind == dealerWind ? -32000 : -16000); - retval = (playerWind == winnerWind ? 64000 : retval); - break; - case 39: - retval = (playerWind == dealerWind ? -48000 : -24000); - retval = (playerWind == winnerWind ? 96000 : retval); - break; - case 52: - retval = (playerWind == dealerWind ? -64000 : -32000); - retval = (playerWind == winnerWind ? 128000 : retval); - break; - }; - } else { - switch (points) { - case 1: - switch (fu) { - //Issue protection against 20 and 25 fu other ways but return 0 - case 20: - case 25: retval = 0; break; - case 30: retval = -500; break; - case 40: retval = -700; break; - case 50: retval = -800; break; - case 60: retval = -1000; break; - case 70: retval = -1200; break; - case 80: retval = -1300; break; - case 90: retval = -1500; break; - case 100: retval = -1600; break; - case 110: retval = -1800; break; - }; - break; - case 2: - switch (fu) { - case 20: retval = -700; break; - //Issue protection against 2p25 selfdraw other ways but return 0 - case 25: retval = 0; break; - case 30: retval = -1000; break; - case 40: retval = -1300; break; - case 50: retval = -1600; break; - case 60: retval = -2000; break; - case 70: retval = -1300; break; - case 80: retval = -2600; break; - case 90: retval = -2900; break; - case 100: retval = -3200; break; - case 110: retval = -3600; break; - }; - break; - case 3: - switch (fu) { - case 20: retval = -1300; break; - case 25: retval = -1600; break; - case 30: retval = -2000; break; - case 40: retval = -2600; break; - case 50: retval = -3200; break; - case 60: retval = -3900; break; - default: retval = -4000; break; - }; - break; - case 4: - switch (fu) { - case 20: retval = -2600; break; - case 25: retval = -3200; break; - case 30: retval = -3900; break; - default: retval = -4000; break; - } - break; - case 5: retval = -4000; break; - case 6: - case 7: retval = -6000; break; - case 8: - case 9: - case 10: retval = -8000; break; - case 11: - case 12: retval = -12000; break; - case 13: retval = -16000; break; - case 26: retval = -32000; break; - case 39: retval = -48000; break; - case 52: retval = -64000; break; - } - retval *= (playerWind == winnerWind ? -3 : 1) - } - - if ( playerWind == winnerWind ) - retval = retval + 300 * Number(Session.get("current_bonus")); - else - retval = retval - 100 * Number(Session.get("current_bonus")); - - return retval; - -}; - -function mistake_delta(player, loser) { - if (player == loser) - return -12000; - else - return 4000; -}; - -function rewardRiichiSticks(riichiSticks, playerWind, winnerWind) { - if (playerWind == winnerWind){ - Session.set("free_riichi_sticks", 0); - return (riichiSticks*1000); - } - return 0; -}; - function setAllGUIRiichisFalse() { Session.set("east_riichi", false); Session.set("south_riichi", false); From 5c07cfb7e7f0040bda8cb10759cc09a39a1b9706 Mon Sep 17 00:00:00 2001 From: TwelveNights Date: Fri, 7 Oct 2016 01:03:10 -0700 Subject: [PATCH 02/10] Match coding style --- imports/api/Constants.js | 2 +- imports/api/NewGameUtils.js | 32 +++--- imports/api/PointCalculationUtils.js | 164 +++++++++++++-------------- imports/ui/JapaneseNewGame.js | 73 ++++++------ 4 files changed, 135 insertions(+), 136 deletions(-) diff --git a/imports/api/Constants.js b/imports/api/Constants.js index 77c0cb0..4e4315d 100644 --- a/imports/api/Constants.js +++ b/imports/api/Constants.js @@ -50,7 +50,7 @@ export const Constants = { WEST: "west", NORTH: "north", - MANGAN: 2000 + MANGAN_BASIC_POINTS: 2000 }; Object.keys(Constants).forEach((k) => { Template.registerHelper(k, () => Constants[k] )}); diff --git a/imports/api/NewGameUtils.js b/imports/api/NewGameUtils.js index fc1c64c..f0865a8 100644 --- a/imports/api/NewGameUtils.js +++ b/imports/api/NewGameUtils.js @@ -47,22 +47,22 @@ export var NewGameUtils = { // This code could be streamlined, but let's leave it explicit switch (gameType) { - case Constants.GAME_TYPE.HONG_KONG: - if (round <= 4) - return "東"; - if (round > 4 && round <= 8) - return "南"; - if (round > 8 && round <= 12) - return "西"; - else //if (round > 12) - return "北"; - case Constants.GAME_TYPE.JAPANESE: - if (round <= 4) - return "東"; - if (round > 4 && round <= 8) - return "南"; - else //if (round > 8) - return "西"; + case Constants.GAME_TYPE.HONG_KONG: + if (round <= 4) + return "東"; + if (round > 4 && round <= 8) + return "南"; + if (round > 8 && round <= 12) + return "西"; + else //if (round > 12) + return "北"; + case Constants.GAME_TYPE.JAPANESE: + if (round <= 4) + return "東"; + if (round > 4 && round <= 8) + return "南"; + else //if (round > 8) + return "西"; }; }, diff --git a/imports/api/PointCalculationUtils.js b/imports/api/PointCalculationUtils.js index bddeccf..8d0a976 100644 --- a/imports/api/PointCalculationUtils.js +++ b/imports/api/PointCalculationUtils.js @@ -10,99 +10,99 @@ export var PointCalculationUtils = { }; function dealin_delta(points, fu, winnerWind, loserWind, riichiSticks) { - let winds = {}; - winds[Constants.EAST] = winds[Constants.SOUTH] = winds[Constants.WEST] = winds[Constants.NORTH] = 0; + let winds = {}; + winds[Constants.EAST] = winds[Constants.SOUTH] = winds[Constants.WEST] = winds[Constants.NORTH] = 0; let basicPoints; - let multiplier = (winnerWind != NewGameUtils.roundToDealerDirection(Number(Session.get("current_round")))) ? 4 : 6; - let manganPayout = multiplier * Constants.MANGAN; + let multiplier = (winnerWind != NewGameUtils.roundToDealerDirection(Number(Session.get("current_round")))) ? 4 : 6; + let manganPayout = multiplier * Constants.MANGAN_BASIC_POINTS; if (points < 5) { - if (fu == 20 || (points == 1 && fu == 25)) { - return 0; // Issue Protection - } else { - basicPoints = Math.ceil((fu * Math.pow(2, 2 + points)) * multiplier /100) * 100; - basicPoints = basicPoints < manganPayout ? basicPoints : manganPayout; - } - } else { - switch (points) { - case 5: basicPoints = manganPayout; break; - case 6: - case 7: basicPoints = manganPayout * 1.5; break; - case 8: - case 9: - case 10: basicPoints = manganPayout * 2; break; - case 11: - case 12: basicPoints = manganPayout * 3; break; - case 13: basicPoints = manganPayout * 4; break; - case 26: basicPoints = manganPayout * 4 * 2; break; - case 39: basicPoints = manganPayout * 4 * 3; break; - case 52: basicPoints = manganPayout * 4 * 4; break; - case 65: basicPoints = manganPayout * 4 * 5; break; - } - } - - winds[winnerWind] = basicPoints + 300 * Number(Session.get("current_bonus")) + riichiSticks * 1000; - winds[loserWind] = -basicPoints - 300 * Number(Session.get("current_bonus")); + if (fu == 20 || (points == 1 && fu == 25)) { + return 0; // Issue Protection + } else { + basicPoints = Math.ceil((fu * Math.pow(2, 2 + points)) * multiplier /100) * 100; + basicPoints = basicPoints < manganPayout ? basicPoints : manganPayout; + } + } else { + switch (points) { + case 5: basicPoints = manganPayout; break; + case 6: + case 7: basicPoints = manganPayout * 1.5; break; + case 8: + case 9: + case 10: basicPoints = manganPayout * 2; break; + case 11: + case 12: basicPoints = manganPayout * 3; break; + case 13: basicPoints = manganPayout * 4; break; + case 26: basicPoints = manganPayout * 4 * 2; break; + case 39: basicPoints = manganPayout * 4 * 3; break; + case 52: basicPoints = manganPayout * 4 * 4; break; + case 65: basicPoints = manganPayout * 4 * 5; break; + } + } + + winds[winnerWind] = basicPoints + 300 * Number(Session.get("current_bonus")) + riichiSticks * 1000; + winds[loserWind] = -basicPoints - 300 * Number(Session.get("current_bonus")); Session.set("free_riichi_sticks", 0); return winds; }; function selfdraw_delta(points, fu, winnerWind, riichiSticks) { - let winds = {}; - winds[Constants.EAST] = winds[Constants.SOUTH] = winds[Constants.WEST] = winds[Constants.NORTH] = 0; + let winds = {}; + winds[Constants.EAST] = winds[Constants.SOUTH] = winds[Constants.WEST] = winds[Constants.NORTH] = 0; - let basicPoints; + let basicPoints; let dealerWind = NewGameUtils.roundToDealerDirection(Number(Session.get("current_round"))); - if (points < 5) { - if (points == 1 && (fu == 20 || fu == 25)) { - return 0; // Issue Protection - } else { - basicPoints = fu * Math.pow(2, 2 + points); - basicPoints = basicPoints < Constants.MANGAN ? basicPoints : Constants.MANGAN; - } - } else { - switch (points) { - case 5: basicPoints = Constants.MANGAN; break; - case 6: - case 7: basicPoints = Constants.MANGAN * 1.5; break; - case 8: - case 9: - case 10: basicPoints = Constants.MANGAN * 2; break; - case 11: - case 12: basicPoints = Constants.MANGAN * 3; break; - case 13: basicPoints = Constants.MANGAN * 4; break; - case 26: basicPoints = Constants.MANGAN * 4 * 2; break; - case 39: basicPoints = Constants.MANGAN * 4 * 3; break; - case 52: basicPoints = Constants.MANGAN * 4 * 4; break; - case 65: basicPoints = Constants.MANGAN * 4 * 5; break; - }; - } - - let nonDealerPays = Math.ceil(basicPoints/100) * 100; - let dealerPays = Math.ceil(basicPoints/100 * 2) * 100; - - let bonuses = Number(Session.get("current_bonus")); - - for (let w in winds) { - if (winnerWind != dealerWind) { - if (w == dealerWind) { - winds[w] = -dealerPays - 100 * bonuses; - } else if (w == winnerWind) { - winds[w] = dealerPays + 2 * nonDealerPays + 300 * bonuses + riichiSticks * 1000; - } else { - winds[w] = -nonDealerPays - 100 * bonuses; - } - } else { - if (w == winnerWind) { - winds[w] = 3 * nonDealerPays + riichiSticks * 1000; - } else { - winds[w] = -nonDealerPays; - } - } - } + if (points < 5) { + if (points == 1 && (fu == 20 || fu == 25)) { + return 0; // Issue Protection + } else { + basicPoints = fu * Math.pow(2, 2 + points); + basicPoints = basicPoints < Constants.MANGAN_BASIC_POINTS ? basicPoints : Constants.MANGAN_BASIC_POINTS; + } + } else { + switch (points) { + case 5: basicPoints = Constants.MANGAN_BASIC_POINTS; break; + case 6: + case 7: basicPoints = Constants.MANGAN_BASIC_POINTS * 1.5; break; + case 8: + case 9: + case 10: basicPoints = Constants.MANGAN_BASIC_POINTS * 2; break; + case 11: + case 12: basicPoints = Constants.MANGAN_BASIC_POINTS * 3; break; + case 13: basicPoints = Constants.MANGAN_BASIC_POINTS * 4; break; + case 26: basicPoints = Constants.MANGAN_BASIC_POINTS * 4 * 2; break; + case 39: basicPoints = Constants.MANGAN_BASIC_POINTS * 4 * 3; break; + case 52: basicPoints = Constants.MANGAN_BASIC_POINTS * 4 * 4; break; + case 65: basicPoints = Constants.MANGAN_BASIC_POINTS * 4 * 5; break; + }; + } + + let nonDealerPays = Math.ceil(basicPoints/100 * (dealerWind == winnerWind ? 2 : 1)) * 100; + let dealerPays = Math.ceil(basicPoints/100 * 2) * 100; + + let bonuses = Number(Session.get("current_bonus")); + + for (let w in winds) { + if (winnerWind != dealerWind) { + if (w == dealerWind) { + winds[w] = -dealerPays - 100 * bonuses; + } else if (w == winnerWind) { + winds[w] = dealerPays + 2 * nonDealerPays + 300 * bonuses + riichiSticks * 1000; + } else { + winds[w] = -nonDealerPays - 100 * bonuses; + } + } else { + if (w == winnerWind) { + winds[w] = 3 * nonDealerPays + riichiSticks * 1000; + } else { + winds[w] = -nonDealerPays; + } + } + } Session.set("free_riichi_sticks", 0); return winds; @@ -112,7 +112,7 @@ function mistake_delta(loser) { let winds = {}; winds[Constants.EAST] = winds[Constants.SOUTH] = winds[Constants.WEST] = winds[Constants.NORTH] = 4000; - winds[loser] = -12000; + winds[loser] = -12000; - return winds; + return winds; }; diff --git a/imports/ui/JapaneseNewGame.js b/imports/ui/JapaneseNewGame.js index 392aa43..87a58a2 100644 --- a/imports/ui/JapaneseNewGame.js +++ b/imports/ui/JapaneseNewGame.js @@ -84,7 +84,7 @@ Template.jpn_split_pao.onCreated( function() { setAllGUIRiichisFalse(); }); -// GUI helper to show current round # +// GUI helper to show current round # // [E1,E2,E3,E4,S1,S2,S3,S4,W1,W2,W3,W4,W5,W...] Template.registerHelper("jpn_round_mod4", function(round) { if (Number(round) > 8) @@ -137,8 +137,8 @@ Template.JapaneseNewGame.helpers({ retval += 1000 * Number(Session.get("free_riichi_sticks")); } else if (winScore == Session.get("north_score")) { if (direction == Constants.NORTH) - retval += 1000 * Number(Session.get("free_riichi_sticks")); - } + retval += 1000 * Number(Session.get("free_riichi_sticks")); + } return retval; @@ -417,7 +417,7 @@ Template.JapaneseNewGame.events({ break; // Push a hand where pao was split and ensure proper information case "jpn_split_pao": - // Ensure correct input of winner, loser, and pao player + // Ensure correct input of winner, loser, and pao player if (Session.get("round_winner") != Constants.NO_PERSON && Session.get("round_loser") != Constants.NO_PERSON && Session.get("round_pao_player") != Constants.NO_PERSON && @@ -617,7 +617,7 @@ function save_game_to_database(hands_array) { }; // Initialise ELO calculator to update player ELO - var jpn_elo_calculator = new EloCalculator(2000, 5, [15000, 0, -5000, -10000], game, Constants.GAME_TYPE.JAPANESE); + var jpn_elo_calculator = new EloCalculator(2000, 5, [15000, 0, -5000, -10000], game, Constants.GAME_TYPE.JAPANESE); var east_elo_delta = jpn_elo_calculator.eloChange(east_player); var south_elo_delta = jpn_elo_calculator.eloChange(south_player); var west_elo_delta = jpn_elo_calculator.eloChange(west_player); @@ -742,7 +742,7 @@ function push_dealin_hand(template) { var riichiSum = Session.get("free_riichi_sticks"); var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; - if (winnerWind == Constants.EAST) { + if (winnerWind == Constants.EAST) { Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) + points); Session.set("eastPlayerDoraSum", Number(Session.get("eastPlayerDoraSum")) + dora); @@ -753,7 +753,7 @@ function push_dealin_hand(template) { Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) + 1); Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) + points); Session.set("southPlayerDoraSum", Number(Session.get("southPlayerDoraSum")) + dora); - if (Session.get("south_riichi") == true) { + if (Session.get("south_riichi") == true) { Session.set("southPlayerRiichisWon", Number(Session.get("southPlayerRiichisWon")) + 1); } } else if (winnerWind == Constants.WEST) { @@ -763,8 +763,7 @@ function push_dealin_hand(template) { if (Session.get("west_riichi") == true) { Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) + 1); } - } - else if (winnerWind == Constants.NORTH) { + } else if (winnerWind == Constants.NORTH) { Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); Session.set("northPlayerDoraSum", Number(Session.get("northPlayerDoraSum")) + dora); @@ -773,7 +772,7 @@ function push_dealin_hand(template) { } } - if (loserWind == Constants.EAST) + if (loserWind == Constants.EAST) Session.set("eastPlayerLosses", Number(Session.get("eastPlayerLosses")) + 1); else if (loserWind == Constants.SOUTH) Session.set("southPlayerLosses", Number(Session.get("southPlayerLosses")) + 1); @@ -833,11 +832,11 @@ function push_selfdraw_hand(template) { var riichiSum = Session.get("free_riichi_sticks"); var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; - if (winnerWind == Constants.EAST) { + if (winnerWind == Constants.EAST) { Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) + points); Session.set("eastPlayerDoraSum", Number(Session.get("eastPlayerDoraSum")) + dora); - if (Session.get("east_riichi") == true) + if (Session.get("east_riichi") == true) Session.set("eastPlayerRiichisWon", Number(Session.get("eastPlayerRiichisWon")) + 1); } else if (winnerWind == Constants.SOUTH) { @@ -851,14 +850,14 @@ function push_selfdraw_hand(template) { Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) + 1); Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); Session.set("westPlayerDoraSum", Number(Session.get("westPlayerDoraSum")) + dora); - if (Session.get("west_riichi") == true) + if (Session.get("west_riichi") == true) Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) + 1); } else if (winnerWind == Constants.NORTH) { Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); Session.set("northPlayerDoraSum", Number(Session.get("northPlayerDoraSum")) + dora); - if (Session.get("north_riichi") == true) + if (Session.get("north_riichi") == true) Session.set("northPlayerRiichisWon", Number(Session.get("northPlayerRiichisWon")) + 1); } @@ -1008,7 +1007,7 @@ function push_mistake_hand(template) { let westDelta = allDeltas[Constants.WEST]; let northDelta = allDeltas[Constants.NORTH]; - if (loserWind == Constants.EAST) Session.set("eastFuckupTotal", Number(Session.get("eastFuckupTotal")) + 1); + if (loserWind == Constants.EAST) Session.set("eastFuckupTotal", Number(Session.get("eastFuckupTotal")) + 1); else if (loserWind == Constants.SOUTH) Session.set("southFuckupTotal", Number(Session.get("southFuckupTotal")) + 1); else if (loserWind == Constants.WEST) Session.set("westFuckupTotal", Number(Session.get("westFuckupTotal")) + 1); else if (loserWind == Constants.NORTH) Session.set("northFuckupTotal", Number(Session.get("northFuckupTotal")) + 1); @@ -1031,36 +1030,36 @@ function push_split_pao_hand(template) { var riichiSum = Session.get("free_riichi_sticks"); var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; - if (winnerWind == Constants.EAST) { + if (winnerWind == Constants.EAST) { Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) + points); Session.set("eastPlayerDoraSum", Number(Session.get("eastPlayerDoraSum")) + dora); - if (Session.get("east_riichi") == true) + if (Session.get("east_riichi") == true) Session.set("eastPlayerRiichisWon", Number(Session.get("eastPlayerRiichisWon")) + 1); } else if (winnerWind == Constants.SOUTH) { Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) + 1); Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) + points); Session.set("southPlayerDoraSum", Number(Session.get("southPlayerDoraSum")) + dora); - if (Session.get("south_riichi") == true) + if (Session.get("south_riichi") == true) Session.set("southPlayerRiichisWon", Number(Session.get("southPlayerRiichisWon")) + 1); } else if (winnerWind == Constants.WEST) { Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) + 1); Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); Session.set("westPlayerDoraSum", Number(Session.get("westPlayerDoraSum")) + dora); - if (Session.get("west_riichi") == true) + if (Session.get("west_riichi") == true) Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) + 1); } else if (winnerWind == Constants.NORTH) { Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); Session.set("northPlayerDoraSum", Number(Session.get("northPlayerDoraSum")) + dora); - if (Session.get("north_riichi") == true) + if (Session.get("north_riichi") == true) Session.set("northPlayerRiichisWon", Number(Session.get("northPlayerRiichisWon")) + 1); } - if (loserWind == Constants.EAST || paoWind == Constants.EAST) + if (loserWind == Constants.EAST || paoWind == Constants.EAST) Session.set("eastPlayerLosses", Number(Session.get("eastPlayerLosses")) + 1); else if (loserWind == Constants.SOUTH || paoWind == Constants.SOUTH) Session.set("southPlayerLosses", Number(Session.get("southPlayerLosses")) + 1); @@ -1097,22 +1096,22 @@ function push_split_pao_hand(template) { } switch (winnerWind) { - case Constants.EAST: - eastDelta += value; - eastDelta += (riichiSum * 1000); - break; - case Constants.SOUTH: - southDelta += value; - southDelta += (riichiSum * 1000); - break; - case Constants.WEST: - westDelta += value; - westDelta += (riichiSum * 1000); - break; - case Constants.NORTH: - northDelta += value; - northDelta += (riichiSum * 1000); - break; + case Constants.EAST: + eastDelta += value; + eastDelta += (riichiSum * 1000); + break; + case Constants.SOUTH: + southDelta += value; + southDelta += (riichiSum * 1000); + break; + case Constants.WEST: + westDelta += value; + westDelta += (riichiSum * 1000); + break; + case Constants.NORTH: + northDelta += value; + northDelta += (riichiSum * 1000); + break; } Session.set("free_riichi_sticks", 0); From b4fd46dd91d3ed9a735db96f3808b0c319f25a39 Mon Sep 17 00:00:00 2001 From: TwelveNights Date: Sun, 20 Nov 2016 13:29:22 -0800 Subject: [PATCH 03/10] Convert tabs to spaces --- imports/api/Constants.js | 112 ++++---- imports/api/NewGameUtils.js | 506 ++++++++++++++++++------------------ 2 files changed, 312 insertions(+), 306 deletions(-) diff --git a/imports/api/Constants.js b/imports/api/Constants.js index 4e4315d..ea4b774 100644 --- a/imports/api/Constants.js +++ b/imports/api/Constants.js @@ -1,56 +1,66 @@ // A class/namespace of commonly used Constants export const Constants = { - // Title - MAHJONG_CLUB_LEAGUE: "Mahjong Club League", - - // The starting number of points in a Hong Kong game - HKG_START_POINTS: 500, - - // The starting number of points in a Japanese game - JPN_START_POINTS: 25000, - - // The number of points distributed upon a tenpai round - // from noten players to tenpai ones in a Japanese game - JPN_TENPAI_PAYOUT: 3000, - - // The required number of points of any one player to - // end a game at the end of "South" round in a Japanese - // game - JPN_END_POINTS: 30000, - - // Placeholder value to establish a player select button - // That has no player selected - NO_PERSON: "no one", - - // The default text to display for player buttons/fields - // When no player has been selected - DEFAULT_EAST: "Select East!", - DEFAULT_SOUTH: "Select South!", - DEFAULT_WEST: "Select West!", - DEFAULT_NORTH: "Select North!", - - // An enum of game types for shared yet slightly altered - // rules. Possibly add more if ever decided to - GAME_TYPE: { - // Hong Kong Old Style - HONG_KONG: "hkg", - // Japanese Riichi Style - JAPANESE: "jpn" - }, - - // Round End Conditions - DEAL_IN: "dealin", - SELF_DRAW: "selfdraw", - NO_WIN: "nowin", - RESTART: "restart", - MISTAKE: "fuckup", - - EAST: "east", - SOUTH: "south", - WEST: "west", - NORTH: "north", - - MANGAN_BASIC_POINTS: 2000 + // Title + MAHJONG_CLUB_LEAGUE: "Mahjong Club League", + + // The starting number of points in a Hong Kong game + HKG_START_POINTS: 500, + + // The starting number of points in a Japanese game + JPN_START_POINTS: 25000, + + // The number of points distributed upon a tenpai round + // from noten players to tenpai ones in a Japanese game + JPN_TENPAI_PAYOUT: 3000, + + // The required number of points of any one player to + // end a game at the end of "South" round in a Japanese + // game + JPN_END_POINTS: 30000, + + // Base points of a mangan + JPN_MANGAN_BASE_POINTS: 2000, + + // Placeholder value to establish a player select button + // That has no player selected + NO_PERSON: "no one", + + // The default text to display for player buttons/fields + // When no player has been selected + DEFAULT_EAST: "Select East!", + DEFAULT_SOUTH: "Select South!", + DEFAULT_WEST: "Select West!", + DEFAULT_NORTH: "Select North!", + + // An enum of game types for shared yet slightly altered + // rules. Possibly add more if ever decided to + GAME_TYPE: { + // Hong Kong Old Style + HONG_KONG: "hkg", + // Japanese Riichi Style + JAPANESE: "jpn" + }, + + // Round End Conditions + DEAL_IN: "dealin", + SELF_DRAW: "selfdraw", + NO_WIN: "nowin", + RESTART: "restart", + MISTAKE: "fuckup", + + EAST: "east", + SOUTH: "south", + WEST: "west", + NORTH: "north", + + WINDS: [EAST, SOUTH, WEST, NORTH], + + PRIORITY: { + east: 3, + south: 2, + north: 1, + west: 0 + } }; Object.keys(Constants).forEach((k) => { Template.registerHelper(k, () => Constants[k] )}); diff --git a/imports/api/NewGameUtils.js b/imports/api/NewGameUtils.js index f0865a8..e5f50b8 100644 --- a/imports/api/NewGameUtils.js +++ b/imports/api/NewGameUtils.js @@ -2,273 +2,269 @@ import { Constants } from '../api/Constants.js'; export var NewGameUtils = { - resetGameValues(defaultScore) { - Session.set("current_east", Constants.DEFAULT_EAST); - Session.set("current_south", Constants.DEFAULT_SOUTH); - Session.set("current_west", Constants.DEFAULT_WEST); - Session.set("current_north", Constants.DEFAULT_NORTH); - - Session.set("round_winner", Constants.NO_PERSON); - Session.set("round_loser", Constants.NO_PERSON); - Session.set("round_pao_player", Constants.NO_PERSON); - - Session.set("east_score", defaultScore); - Session.set("south_score", defaultScore); - Session.set("west_score", defaultScore); - Session.set("north_score", defaultScore); - - Session.set("current_round", 1); - Session.set("current_bonus", 0); - Session.set("current_points", 0); - - Session.set("eastPlayerWins", 0); - Session.set("southPlayerWins", 0); - Session.set("westPlayerWins", 0); - Session.set("northPlayerWins", 0); - - Session.set("eastPlayerLosses", 0); - Session.set("southPlayerLosses", 0); - Session.set("westPlayerLosses", 0); - Session.set("northPlayerLosses", 0); - - Session.set("eastPlayerPointsWon", 0); - Session.set("southPlayerPointsWon", 0); - Session.set("westPlayerPointsWon", 0); - Session.set("northPlayerPointsWon", 0); - - Session.set("eastFuckupTotal", 0); - Session.set("southFuckupTotal", 0); - Session.set("westFuckupTotal", 0); - Session.set("northFuckupTotal", 0); - }, - - // UX: Convert a round and gametype into the correct round wind - displayRoundWind(round, gameType) { - - // This code could be streamlined, but let's leave it explicit - switch (gameType) { - case Constants.GAME_TYPE.HONG_KONG: - if (round <= 4) - return "東"; - if (round > 4 && round <= 8) - return "南"; - if (round > 8 && round <= 12) - return "西"; - else //if (round > 12) - return "北"; - case Constants.GAME_TYPE.JAPANESE: - if (round <= 4) - return "東"; - if (round > 4 && round <= 8) - return "南"; - else //if (round > 8) - return "西"; - }; - }, - - // Helper function to ensure all players are selected - allPlayersSelected() { - return (Session.get("current_east") != Constants.DEFAULT_EAST && - Session.get("current_south") != Constants.DEFAULT_SOUTH && - Session.get("current_west") != Constants.DEFAULT_WEST && - Session.get("current_north") != Constants.DEFAULT_NORTH); - }, - - someoneBankrupt() { - return (Session.get("east_score") < 0 || - Session.get("south_score") < 0 || - Session.get("west_score") < 0 || - Session.get("north_score") < 0); - }, - - someoneAboveMinimum(minimum) { - return (Session.get("east_score") >= minimum || - Session.get("south_score") >= minimum || - Session.get("west_score") >= minimum || - Session.get("north_score") >= minimum); - }, - - /** - * Return the position of the player in first place - * TODO: Is this general to all versions of mahjong? - * @return {String} One of ["east", "south", "west", "north"] - */ - getFirstPlace() { - let values = []; - // For ties, prioritise players in seating order - let priority = { east: 3, - south: 2, - west: 1, - north: 0 }; - - ["east", "south", "west", "north"].forEach(k => { - values.push({ wind: k, value: this.getDirectionScore(k) }) - }); - - let winner = values.reduce((a, b) => { - if (a.value == b.value) { - return priority[a["wind"]] > priority[b["wind"]] ? a : b; - } else { - return a.value > b.value ? a : b; - } - }); - - return winner['wind']; - }, - - /** - * Determine if the game ending conditions for a Japanese mahjong game are met - * @return {Boolean} True if game is over, false if not - */ - japaneseGameOver() { - // End condition where someone has below zero points - let someoneBankrupt = this.someoneBankrupt(); - // End condition where game has reached the end of west round without at least one player above minimum - let westRoundOver = Session.get("current_round") > 12; - // End condition where game has reached the end of south round with at least one player above minimum - let someoneAboveMinimum = Session.get("current_round") > 8 && - this.someoneAboveMinimum(Constants.JPN_END_POINTS); - // End condition where north player reaches first place after winning on last round - let dealerFirstAndAboveMinimum = Session.get("current_round") == 8 && - Session.get("current_bonus") > 0 && - this.getDirectionScore("north") >= Constants.JPN_END_POINTS && - this.getFirstPlace() == "north"; - - return someoneBankrupt || westRoundOver || someoneAboveMinimum || dealerFirstAndAboveMinimum; - }, - - noIllegalSelfdrawJapaneseHands() { - var retval = this.noIllegalJapaneseHands(); - - retval = retval && !(Session.get("current_points") == 2 && Session.get("current_fu") == 25); - - return retval; - }, - - noIllegalJapaneseHands() { - var retval = true; - - retval = retval && (Session.get("current_points") != 0); - retval = retval && (Session.get("current_fu") != 0 || Session.get("current_points") > 4); - - retval = retval && !(Session.get("current_points") == 1 && Session.get("current_fu") == 20); - retval = retval && !(Session.get("current_points") == 1 && Session.get("current_fu") == 25); - - return retval; - }, - - rollbackChomboStat(lastHand) { - if (Number(lastHand.eastDelta) < 0) - Session.set("eastFuckupTotal", Number(Session.get("eastFuckupTotal")) - 1); - else if (Number(lastHand.southDelta) < 0) - Session.set("southFuckupTotal", Number(Session.get("southFuckupTotal")) - 1); - else if (Number(lastHand.westDelta) < 0) - Session.set("westFuckupTotal", Number(Session.get("westFuckupTotal")) - 1); - else if (Number(lastHand.northDelta) < 0) - Session.set("northFuckupTotal", Number(Session.get("northFuckupTotal")) - 1); - }, - - rollbackHandWinStat(lastHand) { - if (Number(lastHand.eastDelta) > 0) - Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) - 1); - else if (Number(lastHand.southDelta) > 0) - Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) - 1); - else if (Number(lastHand.westDelta) > 0) - Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) - 1); - else if (Number(lastHand.northDelta) > 0) - Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) - 1); - }, - - rollbackHandRiichiStat(lastHand, riichiHistory) { - if (Number(lastHand.eastDelta) > 0) { - if (riichiHistory.east == true) - Session.set("eastPlayerRiichisWon", Number(Session.get("eastPlayerRiichisWon")) - 1); - } - else if (Number(lastHand.southDelta) > 0) { - if (riichiHistory.south == true) - Session.set("southPlayerRiichisWon", Number(Session.get("southPlayerRiichisWon")) - 1); - } - else if (Number(lastHand.westDelta) > 0) { - if (riichiHistory.west == true) - Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) - 1); - } - else if (Number(lastHand.northDelta) > 0) { - if (riichiHistory.north == true) - Session.set("northPlayerRiichisWon", Number(Session.get("northPlayerRiichisWon")) - 1); - } - - }, - - rollbackTotalPointsStat(lastHand) { - if (Number(lastHand.eastDelta) > 0) - Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) - lastHand.points); - else if (Number(lastHand.southDelta) > 0) - Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) - lastHand.points); - else if (Number(lastHand.westDelta) > 0) - Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) - lastHand.points); - else if (Number(lastHand.northDelta) > 0) - Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) - lastHand.points); - }, - - rollbackHandDealinStat(lastHand) { - // If we hit a self draw, ensure nothing happens - if (lastHand.eastDelta == 0 || lastHand.southDelta == 0 || lastHand.westDelta == 0 || lastHand.northDelta == 0) - return -1; - - if (Number(lastHand.eastDelta) < 0) - Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) - 1); - else if (Number(lastHand.southDelta) < 0) - Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) - 1); - else if (Number(lastHand.westDelta) < 0) - Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) - 1); - else if (Number(lastHand.northDelta) < 0) - Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) - 1); - }, - - getDirectionScore(direction) { - switch (direction) { - case Constants.EAST: - return Number(Session.get("east_score")); - case Constants.SOUTH: - return Number(Session.get("south_score")); - case Constants.WEST: - return Number(Session.get("west_score")); - case Constants.NORTH: - return Number(Session.get("north_score")); - } - }, - - playerToDirection(player) { - if (player == Session.get("current_east")) return Constants.EAST; - if (player == Session.get("current_south")) return Constants.SOUTH; - if (player == Session.get("current_west")) return Constants.WEST; - if (player == Session.get("current_north")) return Constants.NORTH; - }, - - roundToDealerDirection(round) { - if (round % 4 == 1) return Constants.EAST; - if (round % 4 == 2) return Constants.SOUTH; - if (round % 4 == 3) return Constants.WEST; - if (round % 4 == 0) return Constants.NORTH; - }, + resetGameValues(defaultScore) { + Session.set("current_east", Constants.DEFAULT_EAST); + Session.set("current_south", Constants.DEFAULT_SOUTH); + Session.set("current_west", Constants.DEFAULT_WEST); + Session.set("current_north", Constants.DEFAULT_NORTH); + + Session.set("round_winner", Constants.NO_PERSON); + Session.set("round_loser", Constants.NO_PERSON); + Session.set("round_pao_player", Constants.NO_PERSON); + + Session.set("east_score", defaultScore); + Session.set("south_score", defaultScore); + Session.set("west_score", defaultScore); + Session.set("north_score", defaultScore); + + Session.set("current_round", 1); + Session.set("current_bonus", 0); + Session.set("current_points", 0); + + Session.set("eastPlayerWins", 0); + Session.set("southPlayerWins", 0); + Session.set("westPlayerWins", 0); + Session.set("northPlayerWins", 0); + + Session.set("eastPlayerLosses", 0); + Session.set("southPlayerLosses", 0); + Session.set("westPlayerLosses", 0); + Session.set("northPlayerLosses", 0); + + Session.set("eastPlayerPointsWon", 0); + Session.set("southPlayerPointsWon", 0); + Session.set("westPlayerPointsWon", 0); + Session.set("northPlayerPointsWon", 0); + + Session.set("eastFuckupTotal", 0); + Session.set("southFuckupTotal", 0); + Session.set("westFuckupTotal", 0); + Session.set("northFuckupTotal", 0); + }, + + // UX: Convert a round and gametype into the correct round wind + displayRoundWind(round, gameType) { + + // This code could be streamlined, but let's leave it explicit + switch (gameType) { + case Constants.GAME_TYPE.HONG_KONG: + if (round <= 4) + return "東"; + if (round > 4 && round <= 8) + return "南"; + if (round > 8 && round <= 12) + return "西"; + else //if (round > 12) + return "北"; + case Constants.GAME_TYPE.JAPANESE: + if (round <= 4) + return "東"; + if (round > 4 && round <= 8) + return "南"; + else //if (round > 8) + return "西"; + }; + }, + + // Helper function to ensure all players are selected + allPlayersSelected() { + return (Session.get("current_east") != Constants.DEFAULT_EAST && + Session.get("current_south") != Constants.DEFAULT_SOUTH && + Session.get("current_west") != Constants.DEFAULT_WEST && + Session.get("current_north") != Constants.DEFAULT_NORTH); + }, + + someoneBankrupt() { + return (Session.get("east_score") < 0 || + Session.get("south_score") < 0 || + Session.get("west_score") < 0 || + Session.get("north_score") < 0); + }, + + someoneAboveMinimum(minimum) { + return (Session.get("east_score") >= minimum || + Session.get("south_score") >= minimum || + Session.get("west_score") >= minimum || + Session.get("north_score") >= minimum); + }, + + /** + * Return the position of the player in first place + * TODO: Is this general to all versions of mahjong? + * @return {String} One of ["east", "south", "west", "north"] + */ + getFirstPlace() { + let values = []; + // For ties, prioritise players in seating order + + Constants.WINDS.forEach(k => { + values.push({ wind: k, value: this.getDirectionScore(k) }) + }); + + let winner = values.reduce((a, b) => { + if (a.value == b.value) { + return Constants.PRIORITY[a["wind"]] > Constants.PRIORITY[b["wind"]] ? a : b; + } else { + return a.value > b.value ? a : b; + } + }); + + return winner['wind']; + }, + + /** + * Determine if the game ending conditions for a Japanese mahjong game are met + * @return {Boolean} True if game is over, false if not + */ + japaneseGameOver() { + // End condition where someone has below zero points + let someoneBankrupt = this.someoneBankrupt(); + // End condition where game has reached the end of west round without at least one player above minimum + let westRoundOver = Session.get("current_round") > 12; + // End condition where game has reached the end of south round with at least one player above minimum + let someoneAboveMinimum = Session.get("current_round") > 8 && + this.someoneAboveMinimum(Constants.JPN_END_POINTS); + // End condition where north player reaches first place after winning on last round + let dealerFirstAndAboveMinimum = Session.get("current_round") == 8 && + Session.get("current_bonus") > 0 && + this.getDirectionScore("north") >= Constants.JPN_END_POINTS && + + this.getFirstPlace() == "north"; + return someoneBankrupt || westRoundOver || someoneAboveMinimum || dealerFirstAndAboveMinimum; + }, + + noIllegalSelfdrawJapaneseHands() { + var retval = this.noIllegalJapaneseHands(); + + retval = retval && !(Session.get("current_points") == 2 && Session.get("current_fu") == 25); + + return retval; + }, + + noIllegalJapaneseHands() { + var retval = true; + + retval = retval && (Session.get("current_points") != 0); + retval = retval && (Session.get("current_fu") != 0 || Session.get("current_points") > 4); + + retval = retval && !(Session.get("current_points") == 1 && Session.get("current_fu") == 20); + retval = retval && !(Session.get("current_points") == 1 && Session.get("current_fu") == 25); + + return retval; + }, + + rollbackChomboStat(lastHand) { + if (Number(lastHand.eastDelta) < 0) + Session.set("eastFuckupTotal", Number(Session.get("eastFuckupTotal")) - 1); + else if (Number(lastHand.southDelta) < 0) + Session.set("southFuckupTotal", Number(Session.get("southFuckupTotal")) - 1); + else if (Number(lastHand.westDelta) < 0) + Session.set("westFuckupTotal", Number(Session.get("westFuckupTotal")) - 1); + else if (Number(lastHand.northDelta) < 0) + Session.set("northFuckupTotal", Number(Session.get("northFuckupTotal")) - 1); + }, + + rollbackHandWinStat(lastHand) { + if (Number(lastHand.eastDelta) > 0) + Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) - 1); + else if (Number(lastHand.southDelta) > 0) + Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) - 1); + else if (Number(lastHand.westDelta) > 0) + Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) - 1); + else if (Number(lastHand.northDelta) > 0) + Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) - 1); + }, + + rollbackHandRiichiStat(lastHand, riichiHistory) { + if (Number(lastHand.eastDelta) > 0) { + if (riichiHistory.east == true) + Session.set("eastPlayerRiichisWon", Number(Session.get("eastPlayerRiichisWon")) - 1); + } + else if (Number(lastHand.southDelta) > 0) { + if (riichiHistory.south == true) + Session.set("southPlayerRiichisWon", Number(Session.get("southPlayerRiichisWon")) - 1); + } + else if (Number(lastHand.westDelta) > 0) { + if (riichiHistory.west == true) + Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) - 1); + } + else if (Number(lastHand.northDelta) > 0) { + if (riichiHistory.north == true) + Session.set("northPlayerRiichisWon", Number(Session.get("northPlayerRiichisWon")) - 1); + } + + }, + + rollbackTotalPointsStat(lastHand) { + if (Number(lastHand.eastDelta) > 0) + Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) - lastHand.points); + else if (Number(lastHand.southDelta) > 0) + Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) - lastHand.points); + else if (Number(lastHand.westDelta) > 0) + Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) - lastHand.points); + else if (Number(lastHand.northDelta) > 0) + Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) - lastHand.points); + }, + + rollbackHandDealinStat(lastHand) { + // If we hit a self draw, ensure nothing happens + if (lastHand.eastDelta == 0 || lastHand.southDelta == 0 || lastHand.westDelta == 0 || lastHand.northDelta == 0) + return -1; + + if (Number(lastHand.eastDelta) < 0) + Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) - 1); + else if (Number(lastHand.southDelta) < 0) + Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) - 1); + else if (Number(lastHand.westDelta) < 0) + Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) - 1); + else if (Number(lastHand.northDelta) < 0) + Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) - 1); + }, + + getDirectionScore(direction) { + switch (direction) { + case Constants.EAST: + return Number(Session.get("east_score")); + case Constants.SOUTH: + return Number(Session.get("south_score")); + case Constants.WEST: + return Number(Session.get("west_score")); + case Constants.NORTH: + return Number(Session.get("north_score")); + } + }, + + playerToDirection(player) { + if (player == Session.get("current_east")) return Constants.EAST; + if (player == Session.get("current_south")) return Constants.SOUTH; + if (player == Session.get("current_west")) return Constants.WEST; + if (player == Session.get("current_north")) return Constants.NORTH; + }, + + roundToDealerDirection(round) { + if (round % 4 == 1) return Constants.EAST; + if (round % 4 == 2) return Constants.SOUTH; + if (round % 4 == 3) return Constants.WEST; + if (round % 4 == 0) return Constants.NORTH; + }, }; Template.registerHelper("get_east", function () { - return Session.get("current_east"); + return Session.get("current_east"); }); Template.registerHelper("get_south", function () { - return Session.get("current_south"); + return Session.get("current_south"); }); Template.registerHelper("get_west", function () { - return Session.get("current_west"); + return Session.get("current_west"); }); Template.registerHelper("get_north", function () { - return Session.get("current_north"); + return Session.get("current_north"); }); Template.registerHelper("get_round", function() { - return Session.get("current_round"); + return Session.get("current_round"); }); Template.registerHelper("get_bonus", function () { - return Session.get("current_bonus"); + return Session.get("current_bonus"); }); From 25695b8ee96f89b5f3993f75619c2fcae969da5d Mon Sep 17 00:00:00 2001 From: TwelveNights Date: Sun, 20 Nov 2016 14:40:19 -0800 Subject: [PATCH 04/10] Correct and test point values --- imports/api/Constants.js | 4 +- imports/api/PointCalculationUtils.js | 84 ++++++++++++---------------- imports/ui/JapaneseNewGame.js | 2 + 3 files changed, 40 insertions(+), 50 deletions(-) diff --git a/imports/api/Constants.js b/imports/api/Constants.js index ea4b774..abfd0a6 100644 --- a/imports/api/Constants.js +++ b/imports/api/Constants.js @@ -53,8 +53,6 @@ export const Constants = { WEST: "west", NORTH: "north", - WINDS: [EAST, SOUTH, WEST, NORTH], - PRIORITY: { east: 3, south: 2, @@ -63,4 +61,6 @@ export const Constants = { } }; +Constants.WINDS = [Constants.EAST, Constants.SOUTH, Constants.WEST, Constants.NORTH]; + Object.keys(Constants).forEach((k) => { Template.registerHelper(k, () => Constants[k] )}); diff --git a/imports/api/PointCalculationUtils.js b/imports/api/PointCalculationUtils.js index 8d0a976..9f75b73 100644 --- a/imports/api/PointCalculationUtils.js +++ b/imports/api/PointCalculationUtils.js @@ -15,35 +15,20 @@ function dealin_delta(points, fu, winnerWind, loserWind, riichiSticks) { let basicPoints; let multiplier = (winnerWind != NewGameUtils.roundToDealerDirection(Number(Session.get("current_round")))) ? 4 : 6; - let manganPayout = multiplier * Constants.MANGAN_BASIC_POINTS; if (points < 5) { if (fu == 20 || (points == 1 && fu == 25)) { return 0; // Issue Protection } else { - basicPoints = Math.ceil((fu * Math.pow(2, 2 + points)) * multiplier /100) * 100; + let manganPayout = Constants.JPN_MANGAN_BASE_POINTS * multiplier; + basicPoints = Math.ceil((fu * Math.pow(2, 2 + points)) * multiplier / 100) * 100; basicPoints = basicPoints < manganPayout ? basicPoints : manganPayout; } - } else { - switch (points) { - case 5: basicPoints = manganPayout; break; - case 6: - case 7: basicPoints = manganPayout * 1.5; break; - case 8: - case 9: - case 10: basicPoints = manganPayout * 2; break; - case 11: - case 12: basicPoints = manganPayout * 3; break; - case 13: basicPoints = manganPayout * 4; break; - case 26: basicPoints = manganPayout * 4 * 2; break; - case 39: basicPoints = manganPayout * 4 * 3; break; - case 52: basicPoints = manganPayout * 4 * 4; break; - case 65: basicPoints = manganPayout * 4 * 5; break; - } - } + } else { basicPoints = manganValue(points) * multiplier }; - winds[winnerWind] = basicPoints + 300 * Number(Session.get("current_bonus")) + riichiSticks * 1000; - winds[loserWind] = -basicPoints - 300 * Number(Session.get("current_bonus")); + let currentBonus = Number(Session.get("current_bonus")) * 300; + winds[winnerWind] = basicPoints + currentBonus + (riichiSticks * 1000); + winds[loserWind] = -(basicPoints + currentBonus); Session.set("free_riichi_sticks", 0); return winds; @@ -61,47 +46,31 @@ function selfdraw_delta(points, fu, winnerWind, riichiSticks) { return 0; // Issue Protection } else { basicPoints = fu * Math.pow(2, 2 + points); - basicPoints = basicPoints < Constants.MANGAN_BASIC_POINTS ? basicPoints : Constants.MANGAN_BASIC_POINTS; + basicPoints = basicPoints < Constants.JPN_MANGAN_BASE_POINTS ? basicPoints : Constants.JPN_MANGAN_BASE_POINTS; } - } else { - switch (points) { - case 5: basicPoints = Constants.MANGAN_BASIC_POINTS; break; - case 6: - case 7: basicPoints = Constants.MANGAN_BASIC_POINTS * 1.5; break; - case 8: - case 9: - case 10: basicPoints = Constants.MANGAN_BASIC_POINTS * 2; break; - case 11: - case 12: basicPoints = Constants.MANGAN_BASIC_POINTS * 3; break; - case 13: basicPoints = Constants.MANGAN_BASIC_POINTS * 4; break; - case 26: basicPoints = Constants.MANGAN_BASIC_POINTS * 4 * 2; break; - case 39: basicPoints = Constants.MANGAN_BASIC_POINTS * 4 * 3; break; - case 52: basicPoints = Constants.MANGAN_BASIC_POINTS * 4 * 4; break; - case 65: basicPoints = Constants.MANGAN_BASIC_POINTS * 4 * 5; break; - }; - } + } else { basicPoints = manganValue(points); } - let nonDealerPays = Math.ceil(basicPoints/100 * (dealerWind == winnerWind ? 2 : 1)) * 100; - let dealerPays = Math.ceil(basicPoints/100 * 2) * 100; + let nonDealerPays = Math.ceil(basicPoints / 100 * (dealerWind == winnerWind ? 2 : 1)) * 100; + let dealerPays = Math.ceil(basicPoints / 100 * 2) * 100; - let bonuses = Number(Session.get("current_bonus")); + let currentBonus = Number(Session.get("current_bonus")); for (let w in winds) { if (winnerWind != dealerWind) { if (w == dealerWind) { - winds[w] = -dealerPays - 100 * bonuses; + winds[w] = -(dealerPays + currentBonus * 100); } else if (w == winnerWind) { - winds[w] = dealerPays + 2 * nonDealerPays + 300 * bonuses + riichiSticks * 1000; + winds[w] = dealerPays + (nonDealerPays * 2) + (currentBonus * 300) + (riichiSticks * 1000); } else { - winds[w] = -nonDealerPays - 100 * bonuses; + winds[w] = -(nonDealerPays + currentBonus * 100); } } else { if (w == winnerWind) { - winds[w] = 3 * nonDealerPays + riichiSticks * 1000; + winds[w] = (nonDealerPays * 3) + (currentBonus * 300) + (riichiSticks * 1000); } else { - winds[w] = -nonDealerPays; - } + winds[w] = -(nonDealerPays + (currentBonus * 100)); } + } } Session.set("free_riichi_sticks", 0); @@ -116,3 +85,22 @@ function mistake_delta(loser) { return winds; }; + +// Calculates the total base points from han + dora values +function manganValue(points) { + switch(points) { + case 5: return Constants.JPN_MANGAN_BASE_POINTS; + case 6: + case 7: return Constants.JPN_MANGAN_BASE_POINTS * 1.5; + case 8: + case 9: + case 10: return Constants.JPN_MANGAN_BASE_POINTS * 2; + case 11: + case 12: return Constants.JPN_MANGAN_BASE_POINTS * 3; + case 13: return Constants.JPN_MANGAN_BASE_POINTS * 4; + case 26: return Constants.JPN_MANGAN_BASE_POINTS * 4 * 2; + case 39: return Constants.JPN_MANGAN_BASE_POINTS * 4 * 3; + case 52: return Constants.JPN_MANGAN_BASE_POINTS * 4 * 4; + case 65: return Constants.JPN_MANGAN_BASE_POINTS * 4 * 5; + } +} \ No newline at end of file diff --git a/imports/ui/JapaneseNewGame.js b/imports/ui/JapaneseNewGame.js index 87a58a2..12f87d8 100644 --- a/imports/ui/JapaneseNewGame.js +++ b/imports/ui/JapaneseNewGame.js @@ -803,6 +803,7 @@ function push_dealin_hand(template) { } allDelta = PointCalculationUtils.jpn.dealin_delta(points, fu, winnerWind, loserWind, riichiSum); + Session.set("free_riichi_sticks", 0); eastDelta += allDelta[Constants.EAST]; southDelta += allDelta[Constants.SOUTH]; @@ -890,6 +891,7 @@ function push_selfdraw_hand(template) { northDelta += allDeltas[Constants.NORTH]; pushHand(template, "selfdraw", eastDelta, southDelta, westDelta, northDelta); + Session.set("free_riichi_sticks", 0); if (winnerWind == NewGameUtils.roundToDealerDirection(Session.get("current_round"))) Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); From 75df9b036bd6458165d694ec7a56df184c37142d Mon Sep 17 00:00:00 2001 From: TwelveNights Date: Sun, 20 Nov 2016 14:43:21 -0800 Subject: [PATCH 05/10] Remove trailing whitespaces and convert tabs to space --- imports/ui/JapaneseNewGame.js | 1500 ++++++++++++++++----------------- 1 file changed, 750 insertions(+), 750 deletions(-) diff --git a/imports/ui/JapaneseNewGame.js b/imports/ui/JapaneseNewGame.js index 12f87d8..4079b00 100644 --- a/imports/ui/JapaneseNewGame.js +++ b/imports/ui/JapaneseNewGame.js @@ -9,264 +9,264 @@ import { PointCalculationUtils } from '../api/PointCalculationUtils'; // Code to be evaluated when JapaneseNewGame template is reloaded Template.JapaneseNewGame.onCreated( function() { - // Meteor: Template type to show for choosing hand submission - this.hand_type = new ReactiveVar( "jpn_dealin" ); + // Meteor: Template type to show for choosing hand submission + this.hand_type = new ReactiveVar( "jpn_dealin" ); - // Meteor: List of hands submitted to display - this.hands = new ReactiveArray(); + // Meteor: List of hands submitted to display + this.hands = new ReactiveArray(); - // Save game riichi history for if a hand is deleted - this.riichi_round_history = []; - this.riichi_sum_history = []; + // Save game riichi history for if a hand is deleted + this.riichi_round_history = []; + this.riichi_sum_history = []; - // Reset shared Mahjong stats - NewGameUtils.resetGameValues(Constants.JPN_START_POINTS); + // Reset shared Mahjong stats + NewGameUtils.resetGameValues(Constants.JPN_START_POINTS); - // Reset Japanese hand specific stats - Session.set("current_fu", 0); - Session.set("current_dora", 0); + // Reset Japanese hand specific stats + Session.set("current_fu", 0); + Session.set("current_dora", 0); - // Reset GUI selection fields - setAllGUIRiichisFalse(); + // Reset GUI selection fields + setAllGUIRiichisFalse(); - // Reset Japanese game specific stats - Session.set("east_riichi_sum", 0); - Session.set("south_riichi_sum", 0); - Session.set("west_riichi_sum", 0); - Session.set("north_riichi_sum", 0); + // Reset Japanese game specific stats + Session.set("east_riichi_sum", 0); + Session.set("south_riichi_sum", 0); + Session.set("west_riichi_sum", 0); + Session.set("north_riichi_sum", 0); - Session.set("eastPlayerRiichisWon", 0); - Session.set("southPlayerRiichisWon", 0); - Session.set("westPlayerRiichisWon", 0); - Session.set("northPlayerRiichisWon", 0); + Session.set("eastPlayerRiichisWon", 0); + Session.set("southPlayerRiichisWon", 0); + Session.set("westPlayerRiichisWon", 0); + Session.set("northPlayerRiichisWon", 0); - Session.set("eastPlayerDoraSum", 0); - Session.set("southPlayerDoraSum", 0); - Session.set("westPlayerDoraSum", 0); - Session.set("northPlayerDoraSum", 0); + Session.set("eastPlayerDoraSum", 0); + Session.set("southPlayerDoraSum", 0); + Session.set("westPlayerDoraSum", 0); + Session.set("northPlayerDoraSum", 0); - // Reset number of riichi sticks stored for next player win - Session.set("free_riichi_sticks", 0); + // Reset number of riichi sticks stored for next player win + Session.set("free_riichi_sticks", 0); }); // Code to be evaluated when jpn_dealin template is reloaded Template.jpn_dealin.onCreated( function() { - // Reset GUI selection fields - setAllGUIRiichisFalse(); + // Reset GUI selection fields + setAllGUIRiichisFalse(); }); // Code to be evaluated when jpn_selfdraw template is reloaded Template.jpn_selfdraw.onCreated( function() { - // Reset GUI selection fields - setAllGUIRiichisFalse(); + // Reset GUI selection fields + setAllGUIRiichisFalse(); }); // Code to be evaluated when jpn_nowin tenplate is reloaded Template.jpn_nowin.onCreated( function() { - // Reset GUI selection fields - Session.set("east_tenpai", false); - Session.set("south_tenpai", false); - Session.set("west_tenpai", false); - Session.set("north_tenpai", false); + // Reset GUI selection fields + Session.set("east_tenpai", false); + Session.set("south_tenpai", false); + Session.set("west_tenpai", false); + Session.set("north_tenpai", false); - setAllGUIRiichisFalse(); + setAllGUIRiichisFalse(); }); // Code to be evaluated when jpn_restart tenplate is reloaded Template.jpn_restart.onCreated( function() { - // Reset GUI selection fields - setAllGUIRiichisFalse(); + // Reset GUI selection fields + setAllGUIRiichisFalse(); }); // Code to be evaluated when jpn_split_pao tenplate is reloaded Template.jpn_split_pao.onCreated( function() { - // Reset GUI selection fields - setAllGUIRiichisFalse(); + // Reset GUI selection fields + setAllGUIRiichisFalse(); }); // GUI helper to show current round # // [E1,E2,E3,E4,S1,S2,S3,S4,W1,W2,W3,W4,W5,W...] Template.registerHelper("jpn_round_mod4", function(round) { - if (Number(round) > 8) - return (Number(round) - 8); - var retval = Number(round) % 4; - if (retval == 0) - retval = 4; - return retval; + if (Number(round) > 8) + return (Number(round) - 8); + var retval = Number(round) % 4; + if (retval == 0) + retval = 4; + return retval; }) // GUI helpers for hand submission template Template.JapaneseNewGame.helpers({ - // Choose hand type for submission form - hand_type() { - return Template.instance().hand_type.get(); - }, - // Choose player to select from dropdown menus - players() { - return Players.find({}, {sort: { japaneseLeagueName: 1}}); - }, - // Return all recorded hands for a game as an array - hands() { - return Template.instance().hands.get(); - }, - // Show what a player's +/- is - get_player_delta(direction) { - return (NewGameUtils.getDirectionScore(direction) - Constants.JPN_START_POINTS); - }, - // Show what a player's current score is - get_player_score(direction) { - return NewGameUtils.getDirectionScore(direction); - }, - // Show what a player's score will look like if game is ended now - get_player_score_final(direction) { - retval = NewGameUtils.getDirectionScore(direction); - - var winScore = Math.max(Number(Session.get("east_score")), - Number(Session.get("south_score")), - Number(Session.get("west_score")), - Number(Session.get("north_score"))); - - if (winScore == Session.get("east_score")) { - if (direction == Constants.EAST) - retval += 1000 * Number(Session.get("free_riichi_sticks")); - } else if (winScore == Session.get("south_score")) { - if (direction == Constants.SOUTH) - retval += 1000 * Number(Session.get("free_riichi_sticks")); - } else if (winScore == Session.get("west_score")) { - if (direction == Constants.WEST) - retval += 1000 * Number(Session.get("free_riichi_sticks")); - } else if (winScore == Session.get("north_score")) { - if (direction == Constants.NORTH) - retval += 1000 * Number(Session.get("free_riichi_sticks")); - } - - - return retval; - }, - // Show a player's ELO - get_jpn_elo(player) { - switch (player) { - case Constants.DEFAULT_EAST: - case Constants.DEFAULT_SOUTH: - case Constants.DEFAULT_WEST: - case Constants.DEFAULT_NORTH: - return "?"; - default: - return Players.findOne({japaneseLeagueName: player}).japaneseElo.toFixed(2); - }; - }, - // Return a string of the round wind for Japanese style - displayRoundWind(round) { - return NewGameUtils.displayRoundWind(round, Constants.GAME_TYPE.JAPANESE); - }, + // Choose hand type for submission form + hand_type() { + return Template.instance().hand_type.get(); + }, + // Choose player to select from dropdown menus + players() { + return Players.find({}, {sort: { japaneseLeagueName: 1}}); + }, + // Return all recorded hands for a game as an array + hands() { + return Template.instance().hands.get(); + }, + // Show what a player's +/- is + get_player_delta(direction) { + return (NewGameUtils.getDirectionScore(direction) - Constants.JPN_START_POINTS); + }, + // Show what a player's current score is + get_player_score(direction) { + return NewGameUtils.getDirectionScore(direction); + }, + // Show what a player's score will look like if game is ended now + get_player_score_final(direction) { + retval = NewGameUtils.getDirectionScore(direction); + + var winScore = Math.max(Number(Session.get("east_score")), + Number(Session.get("south_score")), + Number(Session.get("west_score")), + Number(Session.get("north_score"))); + + if (winScore == Session.get("east_score")) { + if (direction == Constants.EAST) + retval += 1000 * Number(Session.get("free_riichi_sticks")); + } else if (winScore == Session.get("south_score")) { + if (direction == Constants.SOUTH) + retval += 1000 * Number(Session.get("free_riichi_sticks")); + } else if (winScore == Session.get("west_score")) { + if (direction == Constants.WEST) + retval += 1000 * Number(Session.get("free_riichi_sticks")); + } else if (winScore == Session.get("north_score")) { + if (direction == Constants.NORTH) + retval += 1000 * Number(Session.get("free_riichi_sticks")); + } + + + return retval; + }, + // Show a player's ELO + get_jpn_elo(player) { + switch (player) { + case Constants.DEFAULT_EAST: + case Constants.DEFAULT_SOUTH: + case Constants.DEFAULT_WEST: + case Constants.DEFAULT_NORTH: + return "?"; + default: + return Players.findOne({japaneseLeagueName: player}).japaneseElo.toFixed(2); + }; + }, + // Return a string of the round wind for Japanese style + displayRoundWind(round) { + return NewGameUtils.displayRoundWind(round, Constants.GAME_TYPE.JAPANESE); + }, }); // GUI helpers for rendering hands Template.jpn_render_hand.helpers({ - // Boolean expressions to help with rendering hands - is_dealin(hand_type) { - return hand_type == Constants.DEAL_IN; - }, - is_selfdraw(hand_type) { - return hand_type == Constants.SELF_DRAW; - }, - is_nowin(hand_type) { - return hand_type == Constants.NO_WIN; - }, - is_restart(hand_type) { - return hand_type == Constants.RESTART; - }, - is_mistake(hand_type) { - return hand_type == Constants.MISTAKE; - }, - // Return a string of the round wind for Japanese style - displayRoundWind(round) { - return NewGameUtils.displayRoundWind(round, Constants.GAME_TYPE.JAPANESE); - }, + // Boolean expressions to help with rendering hands + is_dealin(hand_type) { + return hand_type == Constants.DEAL_IN; + }, + is_selfdraw(hand_type) { + return hand_type == Constants.SELF_DRAW; + }, + is_nowin(hand_type) { + return hand_type == Constants.NO_WIN; + }, + is_restart(hand_type) { + return hand_type == Constants.RESTART; + }, + is_mistake(hand_type) { + return hand_type == Constants.MISTAKE; + }, + // Return a string of the round wind for Japanese style + displayRoundWind(round) { + return NewGameUtils.displayRoundWind(round, Constants.GAME_TYPE.JAPANESE); + }, }) // Helper for point selection dropdown--All allowable points // Assume that 13, 26, 39, and 52 are single->quadruple yakuman Template.jpn_points.helpers({ - possible_points: [ - { point: 1 }, - { point: 2 }, - { point: 3 }, - { point: 4 }, - { point: 5 }, - { point: 6 }, - { point: 7 }, - { point: 8 }, - { point: 9 }, - { point: 10 }, - { point: 11 }, - { point: 12 }, - { point: 13 }, - { point: 26 }, - { point: 39 }, - { point: 52 }, - { point: 65 } - ], + possible_points: [ + { point: 1 }, + { point: 2 }, + { point: 3 }, + { point: 4 }, + { point: 5 }, + { point: 6 }, + { point: 7 }, + { point: 8 }, + { point: 9 }, + { point: 10 }, + { point: 11 }, + { point: 12 }, + { point: 13 }, + { point: 26 }, + { point: 39 }, + { point: 52 }, + { point: 65 } + ], }); // Helper for fu selection dropdown--All allowable fu // Must do checks to ensure a valid point/fu selection is made Template.jpn_fu.helpers({ - possible_fu: [ - { fu: 20 }, - { fu: 25 }, - { fu: 30 }, - { fu: 40 }, - { fu: 50 }, - { fu: 60 }, - { fu: 70 }, - { fu: 80 }, - { fu: 90 }, - { fu: 100 }, - { fu: 110 }, - ], + possible_fu: [ + { fu: 20 }, + { fu: 25 }, + { fu: 30 }, + { fu: 40 }, + { fu: 50 }, + { fu: 60 }, + { fu: 70 }, + { fu: 80 }, + { fu: 90 }, + { fu: 100 }, + { fu: 110 }, + ], }); // Helper for dora selection dropdown--All allowable dora Template.jpn_dora.helpers({ - possible_dora: [ - { dora: 0 }, - { dora: 1 }, - { dora: 2 }, - { dora: 3 }, - { dora: 4 }, - { dora: 5 }, - { dora: 6 }, - { dora: 7 }, - { dora: 8 }, - { dora: 9 }, - { dora: 10 }, - { dora: 11 }, - { dora: 12 }, - { dora: 13 }, - { dora: 14 }, - { dora: 15 }, - { dora: 16 }, - { dora: 17 }, - { dora: 18 }, - { dora: 19 }, - { dora: 20 }, - { dora: 21 }, - { dora: 22 }, - { dora: 23 }, - { dora: 24 }, - { dora: 25 }, - { dora: 26 }, - { dora: 27 }, - { dora: 28 }, - { dora: 29 }, - { dora: 30 }, - { dora: 31 }, - { dora: 32 }, - { dora: 33 }, - { dora: 34 }, - { dora: 35 }, - ], + possible_dora: [ + { dora: 0 }, + { dora: 1 }, + { dora: 2 }, + { dora: 3 }, + { dora: 4 }, + { dora: 5 }, + { dora: 6 }, + { dora: 7 }, + { dora: 8 }, + { dora: 9 }, + { dora: 10 }, + { dora: 11 }, + { dora: 12 }, + { dora: 13 }, + { dora: 14 }, + { dora: 15 }, + { dora: 16 }, + { dora: 17 }, + { dora: 18 }, + { dora: 19 }, + { dora: 20 }, + { dora: 21 }, + { dora: 22 }, + { dora: 23 }, + { dora: 24 }, + { dora: 25 }, + { dora: 26 }, + { dora: 27 }, + { dora: 28 }, + { dora: 29 }, + { dora: 30 }, + { dora: 31 }, + { dora: 32 }, + { dora: 33 }, + { dora: 34 }, + { dora: 35 }, + ], }); // Functions for browser events @@ -595,591 +595,591 @@ Template.JapaneseNewGame.events({ // Save the currently recorded game to database and update player statistics function save_game_to_database(hands_array) { - var position; - - var east_player = Session.get("current_east"); - var south_player= Session.get("current_south"); - var west_player = Session.get("current_west"); - var north_player= Session.get("current_north"); - - // Initialise game to be saved - var game = { - timestamp: Date.now(), - east_player: east_player, - south_player: south_player, - west_player: west_player, - north_player: north_player, - east_score: (Number(Session.get("east_score"))), - south_score: (Number(Session.get("south_score"))), - west_score: (Number(Session.get("west_score"))), - north_score: (Number(Session.get("north_score"))), - all_hands: hands_array, - }; - - // Initialise ELO calculator to update player ELO - var jpn_elo_calculator = new EloCalculator(2000, 5, [15000, 0, -5000, -10000], game, Constants.GAME_TYPE.JAPANESE); - var east_elo_delta = jpn_elo_calculator.eloChange(east_player); - var south_elo_delta = jpn_elo_calculator.eloChange(south_player); - var west_elo_delta = jpn_elo_calculator.eloChange(west_player); - var north_elo_delta = jpn_elo_calculator.eloChange(north_player); - - var east_id = Players.findOne({japaneseLeagueName: east_player}, {})._id; - var south_id = Players.findOne({japaneseLeagueName: south_player}, {})._id; - var west_id = Players.findOne({japaneseLeagueName: west_player}, {})._id; - var north_id = Players.findOne({japaneseLeagueName: north_player}, {})._id; - - if (east_elo_delta != NaN && south_elo_delta != NaN && west_elo_delta != NaN && north_elo_delta != NaN) { - // Save ELO - Players.update({_id: east_id}, {$inc: {japaneseElo: Number(east_elo_delta)}}); - Players.update({_id: south_id}, {$inc: {japaneseElo: Number(south_elo_delta)}}); - Players.update({_id: west_id}, {$inc: {japaneseElo: Number(west_elo_delta)}}); - Players.update({_id: north_id}, {$inc: {japaneseElo: Number(north_elo_delta)}}); - - // Update number of games - Players.update({_id: east_id}, {$inc: {japaneseGamesPlayed: 1}}); - Players.update({_id: south_id}, {$inc: {japaneseGamesPlayed: 1}}); - Players.update({_id: west_id}, {$inc: {japaneseGamesPlayed: 1}}); - Players.update({_id: north_id}, {$inc: {japaneseGamesPlayed: 1}}); - - // Update bankruptcy count - if (Number(Session.get("east_score")) < 0) - Players.update({_id: east_id}, {$inc: {japaneseBankruptTotal: 1}}); - if (Number(Session.get("south_score")) < 0) - Players.update({_id: south_id}, {$inc: {japaneseBankruptTotal: 1}}); - if (Number(Session.get("west_score")) < 0) - Players.update({_id: west_id}, {$inc: {japaneseBankruptTotal: 1}}); - if (Number(Session.get("north_score")) < 0) - Players.update({_id: north_id}, {$inc: {japaneseBankruptTotal: 1}}); - - // Save chombo counts - Players.update({_id: east_id}, {$inc: {japaneseChomboTotal: Number(Session.get("eastFuckupTotal"))}}); - Players.update({_id: south_id}, {$inc: {japaneseChomboTotal: Number(Session.get("southFuckupTotal"))}}); - Players.update({_id: west_id}, {$inc: {japaneseChomboTotal: Number(Session.get("westFuckupTotal"))}}); - Players.update({_id: north_id}, {$inc: {japaneseChomboTotal: Number(Session.get("northFuckupTotal"))}}); - - // Update riichi count - Players.update({_id: east_id}, {$inc: {japaneseRiichiTotal: Number(Session.get("east_riichi_sum"))}}); - Players.update({_id: south_id}, {$inc: {japaneseRiichiTotal: Number(Session.get("south_riichi_sum"))}}); - Players.update({_id: west_id}, {$inc: {japaneseRiichiTotal: Number(Session.get("west_riichi_sum"))}}); - Players.update({_id: north_id}, {$inc: {japaneseRiichiTotal: Number(Session.get("north_riichi_sum"))}}); - - // Update hands count (Includes chombos, do we want this?) - Players.update({_id: east_id}, {$inc: {japaneseHandsTotal: hands_array.length}}); - Players.update({_id: south_id}, {$inc: {japaneseHandsTotal: hands_array.length}}); - Players.update({_id: west_id}, {$inc: {japaneseHandsTotal: hands_array.length}}); - Players.update({_id: north_id}, {$inc: {japaneseHandsTotal: hands_array.length}}); - - // Save number of hands won - Players.update({_id: east_id}, {$inc: {japaneseHandsWin: Number(Session.get("eastPlayerWins"))}}); - Players.update({_id: south_id}, {$inc: {japaneseHandsWin: Number(Session.get("southPlayerWins"))}}); - Players.update({_id: west_id}, {$inc: {japaneseHandsWin: Number(Session.get("westPlayerWins"))}}); - Players.update({_id: north_id}, {$inc: {japaneseHandsWin: Number(Session.get("northPlayerWins"))}}); - - // Save number of points won - Players.update({_id: east_id}, {$inc: {japaneseWinPointsTotal: Number(Session.get("eastPlayerPointsWon"))}}); - Players.update({_id: south_id}, {$inc: {japaneseWinPointsTotal: Number(Session.get("southPlayerPointsWon"))}}); - Players.update({_id: west_id}, {$inc: {japaneseWinPointsTotal: Number(Session.get("westPlayerPointsWon"))}}); - Players.update({_id: north_id}, {$inc: {japaneseWinPointsTotal: Number(Session.get("northPlayerPointsWon"))}}); - - // Update total dora - Players.update({_id: east_id}, {$inc: {japaneseWinDoraTotal: Number(Session.get("eastPlayerDoraSum"))}}); - Players.update({_id: south_id}, {$inc: {japaneseWinDoraTotal: Number(Session.get("southPlayerDoraSum"))}}); - Players.update({_id: west_id}, {$inc: {japaneseWinDoraTotal: Number(Session.get("westPlayerDoraSum"))}}); - Players.update({_id: north_id}, {$inc: {japaneseWinDoraTotal: Number(Session.get("northPlayerDoraSum"))}}); - - // Save number of riichied hands won - Players.update({_id: east_id}, {$inc: {japaneseWinRiichiTotal: Number(Session.get("eastPlayerRiichisWon"))}}); - Players.update({_id: south_id}, {$inc: {japaneseWinRiichiTotal: Number(Session.get("southPlayerRiichisWon"))}}); - Players.update({_id: west_id}, {$inc: {japaneseWinRiichiTotal: Number(Session.get("westPlayerRiichisWon"))}}); - Players.update({_id: north_id}, {$inc: {japaneseWinRiichiTotal: Number(Session.get("northPlayerRiichisWon"))}}); - - // Save number of hands lost - Players.update({_id: east_id}, {$inc: {japaneseHandsLose: Number(Session.get("eastPlayerLosses"))}}); - Players.update({_id: south_id}, {$inc: {japaneseHandsLose: Number(Session.get("southPlayerLosses"))}}); - Players.update({_id: west_id}, {$inc: {japaneseHandsLose: Number(Session.get("westPlayerLosses"))}}); - Players.update({_id: north_id}, {$inc: {japaneseHandsLose: Number(Session.get("northPlayerLosses"))}}); - - // Calculate positions - // Calculate east position quickly? - position = 4; - if (Number(Session.get("east_score")) >= Number(Session.get("south_score"))) position--; - if (Number(Session.get("east_score")) >= Number(Session.get("west_score"))) position--; - if (Number(Session.get("east_score")) >= Number(Session.get("north_score"))) position--; - Players.update({_id: east_id}, {$inc: {japanesePositionSum: position}}); - - // Calculate east position quickly? - position = 4; - if (Number(Session.get("south_score")) > Number(Session.get("east_score"))) position--; - if (Number(Session.get("south_score")) >= Number(Session.get("west_score"))) position--; - if (Number(Session.get("south_score")) >= Number(Session.get("north_score"))) position--; - Players.update({_id: south_id}, {$inc: {japanesePositionSum: position}}); - - // Calculate east position quickly? - position = 4; - if (Number(Session.get("west_score")) > Number(Session.get("east_score"))) position--; - if (Number(Session.get("west_score")) > Number(Session.get("south_score"))) position--; - if (Number(Session.get("west_score")) >= Number(Session.get("north_score"))) position--; - Players.update({_id: west_id}, {$inc: {japanesePositionSum: position}}); - - // Calculate east position quickly? - position = 4; - if (Number(Session.get("north_score")) > Number(Session.get("east_score"))) position--; - if (Number(Session.get("north_score")) > Number(Session.get("south_score"))) position--; - if (Number(Session.get("north_score")) > Number(Session.get("west_score"))) position--; - Players.update({_id: north_id}, {$inc: {japanesePositionSum: position}}); - - //Save game to database - JapaneseHands.insert(game); - } + var position; + + var east_player = Session.get("current_east"); + var south_player= Session.get("current_south"); + var west_player = Session.get("current_west"); + var north_player= Session.get("current_north"); + + // Initialise game to be saved + var game = { + timestamp: Date.now(), + east_player: east_player, + south_player: south_player, + west_player: west_player, + north_player: north_player, + east_score: (Number(Session.get("east_score"))), + south_score: (Number(Session.get("south_score"))), + west_score: (Number(Session.get("west_score"))), + north_score: (Number(Session.get("north_score"))), + all_hands: hands_array, + }; + + // Initialise ELO calculator to update player ELO + var jpn_elo_calculator = new EloCalculator(2000, 5, [15000, 0, -5000, -10000], game, Constants.GAME_TYPE.JAPANESE); + var east_elo_delta = jpn_elo_calculator.eloChange(east_player); + var south_elo_delta = jpn_elo_calculator.eloChange(south_player); + var west_elo_delta = jpn_elo_calculator.eloChange(west_player); + var north_elo_delta = jpn_elo_calculator.eloChange(north_player); + + var east_id = Players.findOne({japaneseLeagueName: east_player}, {})._id; + var south_id = Players.findOne({japaneseLeagueName: south_player}, {})._id; + var west_id = Players.findOne({japaneseLeagueName: west_player}, {})._id; + var north_id = Players.findOne({japaneseLeagueName: north_player}, {})._id; + + if (east_elo_delta != NaN && south_elo_delta != NaN && west_elo_delta != NaN && north_elo_delta != NaN) { + // Save ELO + Players.update({_id: east_id}, {$inc: {japaneseElo: Number(east_elo_delta)}}); + Players.update({_id: south_id}, {$inc: {japaneseElo: Number(south_elo_delta)}}); + Players.update({_id: west_id}, {$inc: {japaneseElo: Number(west_elo_delta)}}); + Players.update({_id: north_id}, {$inc: {japaneseElo: Number(north_elo_delta)}}); + + // Update number of games + Players.update({_id: east_id}, {$inc: {japaneseGamesPlayed: 1}}); + Players.update({_id: south_id}, {$inc: {japaneseGamesPlayed: 1}}); + Players.update({_id: west_id}, {$inc: {japaneseGamesPlayed: 1}}); + Players.update({_id: north_id}, {$inc: {japaneseGamesPlayed: 1}}); + + // Update bankruptcy count + if (Number(Session.get("east_score")) < 0) + Players.update({_id: east_id}, {$inc: {japaneseBankruptTotal: 1}}); + if (Number(Session.get("south_score")) < 0) + Players.update({_id: south_id}, {$inc: {japaneseBankruptTotal: 1}}); + if (Number(Session.get("west_score")) < 0) + Players.update({_id: west_id}, {$inc: {japaneseBankruptTotal: 1}}); + if (Number(Session.get("north_score")) < 0) + Players.update({_id: north_id}, {$inc: {japaneseBankruptTotal: 1}}); + + // Save chombo counts + Players.update({_id: east_id}, {$inc: {japaneseChomboTotal: Number(Session.get("eastFuckupTotal"))}}); + Players.update({_id: south_id}, {$inc: {japaneseChomboTotal: Number(Session.get("southFuckupTotal"))}}); + Players.update({_id: west_id}, {$inc: {japaneseChomboTotal: Number(Session.get("westFuckupTotal"))}}); + Players.update({_id: north_id}, {$inc: {japaneseChomboTotal: Number(Session.get("northFuckupTotal"))}}); + + // Update riichi count + Players.update({_id: east_id}, {$inc: {japaneseRiichiTotal: Number(Session.get("east_riichi_sum"))}}); + Players.update({_id: south_id}, {$inc: {japaneseRiichiTotal: Number(Session.get("south_riichi_sum"))}}); + Players.update({_id: west_id}, {$inc: {japaneseRiichiTotal: Number(Session.get("west_riichi_sum"))}}); + Players.update({_id: north_id}, {$inc: {japaneseRiichiTotal: Number(Session.get("north_riichi_sum"))}}); + + // Update hands count (Includes chombos, do we want this?) + Players.update({_id: east_id}, {$inc: {japaneseHandsTotal: hands_array.length}}); + Players.update({_id: south_id}, {$inc: {japaneseHandsTotal: hands_array.length}}); + Players.update({_id: west_id}, {$inc: {japaneseHandsTotal: hands_array.length}}); + Players.update({_id: north_id}, {$inc: {japaneseHandsTotal: hands_array.length}}); + + // Save number of hands won + Players.update({_id: east_id}, {$inc: {japaneseHandsWin: Number(Session.get("eastPlayerWins"))}}); + Players.update({_id: south_id}, {$inc: {japaneseHandsWin: Number(Session.get("southPlayerWins"))}}); + Players.update({_id: west_id}, {$inc: {japaneseHandsWin: Number(Session.get("westPlayerWins"))}}); + Players.update({_id: north_id}, {$inc: {japaneseHandsWin: Number(Session.get("northPlayerWins"))}}); + + // Save number of points won + Players.update({_id: east_id}, {$inc: {japaneseWinPointsTotal: Number(Session.get("eastPlayerPointsWon"))}}); + Players.update({_id: south_id}, {$inc: {japaneseWinPointsTotal: Number(Session.get("southPlayerPointsWon"))}}); + Players.update({_id: west_id}, {$inc: {japaneseWinPointsTotal: Number(Session.get("westPlayerPointsWon"))}}); + Players.update({_id: north_id}, {$inc: {japaneseWinPointsTotal: Number(Session.get("northPlayerPointsWon"))}}); + + // Update total dora + Players.update({_id: east_id}, {$inc: {japaneseWinDoraTotal: Number(Session.get("eastPlayerDoraSum"))}}); + Players.update({_id: south_id}, {$inc: {japaneseWinDoraTotal: Number(Session.get("southPlayerDoraSum"))}}); + Players.update({_id: west_id}, {$inc: {japaneseWinDoraTotal: Number(Session.get("westPlayerDoraSum"))}}); + Players.update({_id: north_id}, {$inc: {japaneseWinDoraTotal: Number(Session.get("northPlayerDoraSum"))}}); + + // Save number of riichied hands won + Players.update({_id: east_id}, {$inc: {japaneseWinRiichiTotal: Number(Session.get("eastPlayerRiichisWon"))}}); + Players.update({_id: south_id}, {$inc: {japaneseWinRiichiTotal: Number(Session.get("southPlayerRiichisWon"))}}); + Players.update({_id: west_id}, {$inc: {japaneseWinRiichiTotal: Number(Session.get("westPlayerRiichisWon"))}}); + Players.update({_id: north_id}, {$inc: {japaneseWinRiichiTotal: Number(Session.get("northPlayerRiichisWon"))}}); + + // Save number of hands lost + Players.update({_id: east_id}, {$inc: {japaneseHandsLose: Number(Session.get("eastPlayerLosses"))}}); + Players.update({_id: south_id}, {$inc: {japaneseHandsLose: Number(Session.get("southPlayerLosses"))}}); + Players.update({_id: west_id}, {$inc: {japaneseHandsLose: Number(Session.get("westPlayerLosses"))}}); + Players.update({_id: north_id}, {$inc: {japaneseHandsLose: Number(Session.get("northPlayerLosses"))}}); + + // Calculate positions + // Calculate east position quickly? + position = 4; + if (Number(Session.get("east_score")) >= Number(Session.get("south_score"))) position--; + if (Number(Session.get("east_score")) >= Number(Session.get("west_score"))) position--; + if (Number(Session.get("east_score")) >= Number(Session.get("north_score"))) position--; + Players.update({_id: east_id}, {$inc: {japanesePositionSum: position}}); + + // Calculate east position quickly? + position = 4; + if (Number(Session.get("south_score")) > Number(Session.get("east_score"))) position--; + if (Number(Session.get("south_score")) >= Number(Session.get("west_score"))) position--; + if (Number(Session.get("south_score")) >= Number(Session.get("north_score"))) position--; + Players.update({_id: south_id}, {$inc: {japanesePositionSum: position}}); + + // Calculate east position quickly? + position = 4; + if (Number(Session.get("west_score")) > Number(Session.get("east_score"))) position--; + if (Number(Session.get("west_score")) > Number(Session.get("south_score"))) position--; + if (Number(Session.get("west_score")) >= Number(Session.get("north_score"))) position--; + Players.update({_id: west_id}, {$inc: {japanesePositionSum: position}}); + + // Calculate east position quickly? + position = 4; + if (Number(Session.get("north_score")) > Number(Session.get("east_score"))) position--; + if (Number(Session.get("north_score")) > Number(Session.get("south_score"))) position--; + if (Number(Session.get("north_score")) > Number(Session.get("west_score"))) position--; + Players.update({_id: north_id}, {$inc: {japanesePositionSum: position}}); + + //Save game to database + JapaneseHands.insert(game); + } }; function push_dealin_hand(template) { - var points = Number(Session.get("current_points")); - var fu = Number(Session.get("current_fu")); - var dora = Number(Session.get("current_dora")); - var winnerWind = NewGameUtils.playerToDirection(Session.get("round_winner")); - var loserWind = NewGameUtils.playerToDirection(Session.get("round_loser")); - var riichiSum = Session.get("free_riichi_sticks"); - var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; - - if (winnerWind == Constants.EAST) { - Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); - Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) + points); - Session.set("eastPlayerDoraSum", Number(Session.get("eastPlayerDoraSum")) + dora); - if (Session.get("east_riichi") == true) { - Session.set("eastPlayerRiichisWon", Number(Session.get("eastPlayerRiichisWon")) + 1); - } - } else if (winnerWind == Constants.SOUTH) { - Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) + 1); - Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) + points); - Session.set("southPlayerDoraSum", Number(Session.get("southPlayerDoraSum")) + dora); - if (Session.get("south_riichi") == true) { - Session.set("southPlayerRiichisWon", Number(Session.get("southPlayerRiichisWon")) + 1); - } - } else if (winnerWind == Constants.WEST) { - Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) + 1); - Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); - Session.set("westPlayerDoraSum", Number(Session.get("westPlayerDoraSum")) + dora); - if (Session.get("west_riichi") == true) { - Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) + 1); - } - } else if (winnerWind == Constants.NORTH) { - Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); - Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); - Session.set("northPlayerDoraSum", Number(Session.get("northPlayerDoraSum")) + dora); - if (Session.get("north_riichi") == true) { - Session.set("northPlayerRiichisWon", Number(Session.get("northPlayerRiichisWon")) + 1); - } - } - - if (loserWind == Constants.EAST) - Session.set("eastPlayerLosses", Number(Session.get("eastPlayerLosses")) + 1); - else if (loserWind == Constants.SOUTH) - Session.set("southPlayerLosses", Number(Session.get("southPlayerLosses")) + 1); - else if (loserWind == Constants.WEST) - Session.set("westPlayerLosses", Number(Session.get("westPlayerLosses")) + 1); - else if (loserWind == Constants.NORTH) - Session.set("northPlayerLosses", Number(Session.get("northPlayerLosses")) + 1); - - if (Session.get("east_riichi") == true) { - eastDelta -= 1000; - riichiSum++; - Session.set("east_riichi_sum", Number(Session.get("east_riichi_sum")) + 1); - } - if (Session.get("south_riichi") == true) { - southDelta -= 1000; - riichiSum++; - Session.set("south_riichi_sum", Number(Session.get("south_riichi_sum")) + 1); - } - if (Session.get("west_riichi") == true) { - westDelta -= 1000; - riichiSum++; - Session.set("west_riichi_sum", Number(Session.get("west_riichi_sum")) + 1); - } - if (Session.get("north_riichi") == true) { - northDelta -= 1000; - riichiSum++; - Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); - } - - allDelta = PointCalculationUtils.jpn.dealin_delta(points, fu, winnerWind, loserWind, riichiSum); - Session.set("free_riichi_sticks", 0); - - eastDelta += allDelta[Constants.EAST]; - southDelta += allDelta[Constants.SOUTH]; - westDelta += allDelta[Constants.WEST]; - northDelta += allDelta[Constants.NORTH]; - - pushHand(template, "dealin", eastDelta, southDelta, westDelta, northDelta); - - if (winnerWind == NewGameUtils.roundToDealerDirection(Session.get("current_round"))) - Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); - else { - Session.set("current_bonus", 0); - Session.set("current_round", Number(Session.get("current_round")) + 1) - } - - template.riichi_round_history.push({east: Session.get("east_riichi"), - south: Session.get("south_riichi"), - west: Session.get("west_riichi"), - north: Session.get("north_riichi")}); + var points = Number(Session.get("current_points")); + var fu = Number(Session.get("current_fu")); + var dora = Number(Session.get("current_dora")); + var winnerWind = NewGameUtils.playerToDirection(Session.get("round_winner")); + var loserWind = NewGameUtils.playerToDirection(Session.get("round_loser")); + var riichiSum = Session.get("free_riichi_sticks"); + var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; + + if (winnerWind == Constants.EAST) { + Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); + Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) + points); + Session.set("eastPlayerDoraSum", Number(Session.get("eastPlayerDoraSum")) + dora); + if (Session.get("east_riichi") == true) { + Session.set("eastPlayerRiichisWon", Number(Session.get("eastPlayerRiichisWon")) + 1); + } + } else if (winnerWind == Constants.SOUTH) { + Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) + 1); + Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) + points); + Session.set("southPlayerDoraSum", Number(Session.get("southPlayerDoraSum")) + dora); + if (Session.get("south_riichi") == true) { + Session.set("southPlayerRiichisWon", Number(Session.get("southPlayerRiichisWon")) + 1); + } + } else if (winnerWind == Constants.WEST) { + Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) + 1); + Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); + Session.set("westPlayerDoraSum", Number(Session.get("westPlayerDoraSum")) + dora); + if (Session.get("west_riichi") == true) { + Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) + 1); + } + } else if (winnerWind == Constants.NORTH) { + Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); + Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); + Session.set("northPlayerDoraSum", Number(Session.get("northPlayerDoraSum")) + dora); + if (Session.get("north_riichi") == true) { + Session.set("northPlayerRiichisWon", Number(Session.get("northPlayerRiichisWon")) + 1); + } + } + + if (loserWind == Constants.EAST) + Session.set("eastPlayerLosses", Number(Session.get("eastPlayerLosses")) + 1); + else if (loserWind == Constants.SOUTH) + Session.set("southPlayerLosses", Number(Session.get("southPlayerLosses")) + 1); + else if (loserWind == Constants.WEST) + Session.set("westPlayerLosses", Number(Session.get("westPlayerLosses")) + 1); + else if (loserWind == Constants.NORTH) + Session.set("northPlayerLosses", Number(Session.get("northPlayerLosses")) + 1); + + if (Session.get("east_riichi") == true) { + eastDelta -= 1000; + riichiSum++; + Session.set("east_riichi_sum", Number(Session.get("east_riichi_sum")) + 1); + } + if (Session.get("south_riichi") == true) { + southDelta -= 1000; + riichiSum++; + Session.set("south_riichi_sum", Number(Session.get("south_riichi_sum")) + 1); + } + if (Session.get("west_riichi") == true) { + westDelta -= 1000; + riichiSum++; + Session.set("west_riichi_sum", Number(Session.get("west_riichi_sum")) + 1); + } + if (Session.get("north_riichi") == true) { + northDelta -= 1000; + riichiSum++; + Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); + } + + allDelta = PointCalculationUtils.jpn.dealin_delta(points, fu, winnerWind, loserWind, riichiSum); + Session.set("free_riichi_sticks", 0); + + eastDelta += allDelta[Constants.EAST]; + southDelta += allDelta[Constants.SOUTH]; + westDelta += allDelta[Constants.WEST]; + northDelta += allDelta[Constants.NORTH]; + + pushHand(template, "dealin", eastDelta, southDelta, westDelta, northDelta); + + if (winnerWind == NewGameUtils.roundToDealerDirection(Session.get("current_round"))) + Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); + else { + Session.set("current_bonus", 0); + Session.set("current_round", Number(Session.get("current_round")) + 1) + } + + template.riichi_round_history.push({east: Session.get("east_riichi"), + south: Session.get("south_riichi"), + west: Session.get("west_riichi"), + north: Session.get("north_riichi")}); }; function push_selfdraw_hand(template) { - var points = Number(Session.get("current_points")); - var fu = Number(Session.get("current_fu")); - var dora = Number(Session.get("current_dora")); - var winnerWind = NewGameUtils.playerToDirection(Session.get("round_winner")); - var riichiSum = Session.get("free_riichi_sticks"); - var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; - - if (winnerWind == Constants.EAST) { - Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); - Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) + points); - Session.set("eastPlayerDoraSum", Number(Session.get("eastPlayerDoraSum")) + dora); - if (Session.get("east_riichi") == true) - Session.set("eastPlayerRiichisWon", Number(Session.get("eastPlayerRiichisWon")) + 1); - } - else if (winnerWind == Constants.SOUTH) { - Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) + 1); - Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) + points); - Session.set("southPlayerDoraSum", Number(Session.get("southPlayerDoraSum")) + dora); - if (Session.get("south_riichi") == true) - Session.set("southPlayerRiichisWon", Number(Session.get("southPlayerRiichisWon")) + 1); - } - else if (winnerWind == Constants.WEST) { - Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) + 1); - Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); - Session.set("westPlayerDoraSum", Number(Session.get("westPlayerDoraSum")) + dora); - if (Session.get("west_riichi") == true) - Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) + 1); - } - else if (winnerWind == Constants.NORTH) { - Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); - Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); - Session.set("northPlayerDoraSum", Number(Session.get("northPlayerDoraSum")) + dora); - if (Session.get("north_riichi") == true) - Session.set("northPlayerRiichisWon", Number(Session.get("northPlayerRiichisWon")) + 1); - } - - if (Session.get("east_riichi") == true) { - eastDelta -= 1000; - riichiSum++; - Session.set("east_riichi_sum", Number(Session.get("east_riichi_sum")) + 1); - } - if (Session.get("south_riichi") == true) { - southDelta -= 1000; - riichiSum++; - Session.set("south_riichi_sum", Number(Session.get("south_riichi_sum")) + 1); - } - if (Session.get("west_riichi") == true) { - westDelta -= 1000; - riichiSum++; - Session.set("west_riichi_sum", Number(Session.get("west_riichi_sum")) + 1); - } - if (Session.get("north_riichi") == true) { - northDelta -= 1000; - riichiSum++; - Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); - } - - allDeltas = PointCalculationUtils.jpn.selfdraw_delta(points, fu, winnerWind, riichiSum); - - eastDelta += allDeltas[Constants.EAST]; - southDelta += allDeltas[Constants.SOUTH]; - westDelta += allDeltas[Constants.WEST]; - northDelta += allDeltas[Constants.NORTH]; - - pushHand(template, "selfdraw", eastDelta, southDelta, westDelta, northDelta); - Session.set("free_riichi_sticks", 0); - - if (winnerWind == NewGameUtils.roundToDealerDirection(Session.get("current_round"))) - Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); - else { - Session.set("current_bonus", 0); - Session.set("current_round", Number(Session.get("current_round")) + 1); - } - - template.riichi_round_history.push({east: Session.get("east_riichi"), - south: Session.get("south_riichi"), - west: Session.get("west_riichi"), - north: Session.get("north_riichi")}); + var points = Number(Session.get("current_points")); + var fu = Number(Session.get("current_fu")); + var dora = Number(Session.get("current_dora")); + var winnerWind = NewGameUtils.playerToDirection(Session.get("round_winner")); + var riichiSum = Session.get("free_riichi_sticks"); + var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; + + if (winnerWind == Constants.EAST) { + Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); + Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) + points); + Session.set("eastPlayerDoraSum", Number(Session.get("eastPlayerDoraSum")) + dora); + if (Session.get("east_riichi") == true) + Session.set("eastPlayerRiichisWon", Number(Session.get("eastPlayerRiichisWon")) + 1); + } + else if (winnerWind == Constants.SOUTH) { + Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) + 1); + Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) + points); + Session.set("southPlayerDoraSum", Number(Session.get("southPlayerDoraSum")) + dora); + if (Session.get("south_riichi") == true) + Session.set("southPlayerRiichisWon", Number(Session.get("southPlayerRiichisWon")) + 1); + } + else if (winnerWind == Constants.WEST) { + Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) + 1); + Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); + Session.set("westPlayerDoraSum", Number(Session.get("westPlayerDoraSum")) + dora); + if (Session.get("west_riichi") == true) + Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) + 1); + } + else if (winnerWind == Constants.NORTH) { + Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); + Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); + Session.set("northPlayerDoraSum", Number(Session.get("northPlayerDoraSum")) + dora); + if (Session.get("north_riichi") == true) + Session.set("northPlayerRiichisWon", Number(Session.get("northPlayerRiichisWon")) + 1); + } + + if (Session.get("east_riichi") == true) { + eastDelta -= 1000; + riichiSum++; + Session.set("east_riichi_sum", Number(Session.get("east_riichi_sum")) + 1); + } + if (Session.get("south_riichi") == true) { + southDelta -= 1000; + riichiSum++; + Session.set("south_riichi_sum", Number(Session.get("south_riichi_sum")) + 1); + } + if (Session.get("west_riichi") == true) { + westDelta -= 1000; + riichiSum++; + Session.set("west_riichi_sum", Number(Session.get("west_riichi_sum")) + 1); + } + if (Session.get("north_riichi") == true) { + northDelta -= 1000; + riichiSum++; + Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); + } + + allDeltas = PointCalculationUtils.jpn.selfdraw_delta(points, fu, winnerWind, riichiSum); + + eastDelta += allDeltas[Constants.EAST]; + southDelta += allDeltas[Constants.SOUTH]; + westDelta += allDeltas[Constants.WEST]; + northDelta += allDeltas[Constants.NORTH]; + + pushHand(template, "selfdraw", eastDelta, southDelta, westDelta, northDelta); + Session.set("free_riichi_sticks", 0); + + if (winnerWind == NewGameUtils.roundToDealerDirection(Session.get("current_round"))) + Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); + else { + Session.set("current_bonus", 0); + Session.set("current_round", Number(Session.get("current_round")) + 1); + } + + template.riichi_round_history.push({east: Session.get("east_riichi"), + south: Session.get("south_riichi"), + west: Session.get("west_riichi"), + north: Session.get("north_riichi")}); }; function push_nowin_hand(template) { - var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; - var tenpaiSum = 0, tenpaiWin, tenpaiLose, riichiSum = 0; - - if (Session.get("east_tenpai") == true) tenpaiSum++; - if (Session.get("south_tenpai") == true) tenpaiSum++; - if (Session.get("west_tenpai") == true) tenpaiSum++; - if (Session.get("north_tenpai") == true) tenpaiSum++; - - tenpaiWin = Constants.JPN_TENPAI_PAYOUT / tenpaiSum; - tenpaiLose = -Constants.JPN_TENPAI_PAYOUT / (4 - tenpaiSum); - if (tenpaiSum != 4 && tenpaiSum != 0) { - eastDelta += Session.get("east_tenpai") == true ? tenpaiWin : tenpaiLose; - southDelta += Session.get("south_tenpai") == true ? tenpaiWin : tenpaiLose; - westDelta += Session.get("west_tenpai") == true ? tenpaiWin : tenpaiLose; - northDelta += Session.get("north_tenpai") == true ? tenpaiWin : tenpaiLose; - } - - if (Session.get("east_riichi") == true) { - eastDelta -= 1000; - riichiSum++; - Session.set("east_riichi_sum", Number(Session.get("east_riichi_sum")) + 1); - } - if (Session.get("south_riichi") == true) { - southDelta -= 1000; - riichiSum++; - Session.set("south_riichi_sum", Number(Session.get("south_riichi_sum")) + 1); - } - if (Session.get("west_riichi") == true) { - westDelta -= 1000; - riichiSum++; - Session.set("west_riichi_sum", Number(Session.get("west_riichi_sum")) + 1); - } - if (Session.get("north_riichi") == true) { - northDelta -= 1000; - riichiSum++; - Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); - } - - Session.set("free_riichi_sticks", Number(Session.get("free_riichi_sticks")) + riichiSum); - - pushHand(template, "nowin", eastDelta, southDelta, westDelta, northDelta); - - if (Session.get(NewGameUtils.roundToDealerDirection(Session.get("current_round")) + "_tenpai") == true) - Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); - else { - Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); - Session.set("current_round", Number(Session.get("current_round")) + 1); - } - - template.riichi_round_history.push({east: Session.get("east_riichi"), - south: Session.get("south_riichi"), - west: Session.get("west_riichi"), - north: Session.get("north_riichi")}); + var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; + var tenpaiSum = 0, tenpaiWin, tenpaiLose, riichiSum = 0; + + if (Session.get("east_tenpai") == true) tenpaiSum++; + if (Session.get("south_tenpai") == true) tenpaiSum++; + if (Session.get("west_tenpai") == true) tenpaiSum++; + if (Session.get("north_tenpai") == true) tenpaiSum++; + + tenpaiWin = Constants.JPN_TENPAI_PAYOUT / tenpaiSum; + tenpaiLose = -Constants.JPN_TENPAI_PAYOUT / (4 - tenpaiSum); + if (tenpaiSum != 4 && tenpaiSum != 0) { + eastDelta += Session.get("east_tenpai") == true ? tenpaiWin : tenpaiLose; + southDelta += Session.get("south_tenpai") == true ? tenpaiWin : tenpaiLose; + westDelta += Session.get("west_tenpai") == true ? tenpaiWin : tenpaiLose; + northDelta += Session.get("north_tenpai") == true ? tenpaiWin : tenpaiLose; + } + + if (Session.get("east_riichi") == true) { + eastDelta -= 1000; + riichiSum++; + Session.set("east_riichi_sum", Number(Session.get("east_riichi_sum")) + 1); + } + if (Session.get("south_riichi") == true) { + southDelta -= 1000; + riichiSum++; + Session.set("south_riichi_sum", Number(Session.get("south_riichi_sum")) + 1); + } + if (Session.get("west_riichi") == true) { + westDelta -= 1000; + riichiSum++; + Session.set("west_riichi_sum", Number(Session.get("west_riichi_sum")) + 1); + } + if (Session.get("north_riichi") == true) { + northDelta -= 1000; + riichiSum++; + Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); + } + + Session.set("free_riichi_sticks", Number(Session.get("free_riichi_sticks")) + riichiSum); + + pushHand(template, "nowin", eastDelta, southDelta, westDelta, northDelta); + + if (Session.get(NewGameUtils.roundToDealerDirection(Session.get("current_round")) + "_tenpai") == true) + Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); + else { + Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); + Session.set("current_round", Number(Session.get("current_round")) + 1); + } + + template.riichi_round_history.push({east: Session.get("east_riichi"), + south: Session.get("south_riichi"), + west: Session.get("west_riichi"), + north: Session.get("north_riichi")}); }; function push_restart_hand(template) { - var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; - var riichiSum = 0; - - if (Session.get("east_riichi") == true) { - eastDelta -= 1000; - riichiSum++; - Session.set("east_riichi_sum", Number(Session.get("east_riichi_sum")) + 1); - } - if (Session.get("south_riichi") == true) { - southDelta -= 1000; - riichiSum++; - Session.set("south_riichi_sum", Number(Session.get("south_riichi_sum")) + 1); - } - if (Session.get("west_riichi") == true) { - westDelta -= 1000; - riichiSum++; - Session.set("west_riichi_sum", Number(Session.get("west_riichi_sum")) + 1); - } - if (Session.get("north_riichi") == true) { - northDelta -= 1000; - riichiSum++; - Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); - } - - Session.set("free_riichi_sticks", Number(Session.get("free_riichi_sticks")) + riichiSum); - - pushHand(template, "restart", eastDelta, southDelta, westDelta, northDelta); - - Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); - - template.riichi_round_history.push({east: Session.get("east_riichi"), - south: Session.get("south_riichi"), - west: Session.get("west_riichi"), - north: Session.get("north_riichi")}); + var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; + var riichiSum = 0; + + if (Session.get("east_riichi") == true) { + eastDelta -= 1000; + riichiSum++; + Session.set("east_riichi_sum", Number(Session.get("east_riichi_sum")) + 1); + } + if (Session.get("south_riichi") == true) { + southDelta -= 1000; + riichiSum++; + Session.set("south_riichi_sum", Number(Session.get("south_riichi_sum")) + 1); + } + if (Session.get("west_riichi") == true) { + westDelta -= 1000; + riichiSum++; + Session.set("west_riichi_sum", Number(Session.get("west_riichi_sum")) + 1); + } + if (Session.get("north_riichi") == true) { + northDelta -= 1000; + riichiSum++; + Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); + } + + Session.set("free_riichi_sticks", Number(Session.get("free_riichi_sticks")) + riichiSum); + + pushHand(template, "restart", eastDelta, southDelta, westDelta, northDelta); + + Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); + + template.riichi_round_history.push({east: Session.get("east_riichi"), + south: Session.get("south_riichi"), + west: Session.get("west_riichi"), + north: Session.get("north_riichi")}); }; function push_mistake_hand(template) { - var loserWind = NewGameUtils.playerToDirection(Session.get("round_loser")); + var loserWind = NewGameUtils.playerToDirection(Session.get("round_loser")); - let allDeltas = PointCalculationUtils.jpn.mistake_delta(loserWind); + let allDeltas = PointCalculationUtils.jpn.mistake_delta(loserWind); - let eastDelta = allDeltas[Constants.EAST]; - let southDelta = allDeltas[Constants.SOUTH]; - let westDelta = allDeltas[Constants.WEST]; - let northDelta = allDeltas[Constants.NORTH]; + let eastDelta = allDeltas[Constants.EAST]; + let southDelta = allDeltas[Constants.SOUTH]; + let westDelta = allDeltas[Constants.WEST]; + let northDelta = allDeltas[Constants.NORTH]; - if (loserWind == Constants.EAST) Session.set("eastFuckupTotal", Number(Session.get("eastFuckupTotal")) + 1); - else if (loserWind == Constants.SOUTH) Session.set("southFuckupTotal", Number(Session.get("southFuckupTotal")) + 1); - else if (loserWind == Constants.WEST) Session.set("westFuckupTotal", Number(Session.get("westFuckupTotal")) + 1); - else if (loserWind == Constants.NORTH) Session.set("northFuckupTotal", Number(Session.get("northFuckupTotal")) + 1); + if (loserWind == Constants.EAST) Session.set("eastFuckupTotal", Number(Session.get("eastFuckupTotal")) + 1); + else if (loserWind == Constants.SOUTH) Session.set("southFuckupTotal", Number(Session.get("southFuckupTotal")) + 1); + else if (loserWind == Constants.WEST) Session.set("westFuckupTotal", Number(Session.get("westFuckupTotal")) + 1); + else if (loserWind == Constants.NORTH) Session.set("northFuckupTotal", Number(Session.get("northFuckupTotal")) + 1); - pushHand(template, "fuckup", eastDelta, southDelta, westDelta, northDelta); + pushHand(template, "fuckup", eastDelta, southDelta, westDelta, northDelta); - template.riichi_round_history.push({east: false, - south: false, - west: false, - north: false}); + template.riichi_round_history.push({east: false, + south: false, + west: false, + north: false}); }; function push_split_pao_hand(template) { - var points = Number(Session.get("current_points")); - var fu = Number(Session.get("current_fu")); - var dora = Number(Session.get("current_dora")); - var winnerWind = NewGameUtils.playerToDirection(Session.get("round_winner")); - var loserWind = NewGameUtils.playerToDirection(Session.get("round_loser")); - var paoWind = NewGameUtils.playerToDirection(Session.get("round_pao_player")); - var riichiSum = Session.get("free_riichi_sticks"); - var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; - - if (winnerWind == Constants.EAST) { - Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); - Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) + points); - Session.set("eastPlayerDoraSum", Number(Session.get("eastPlayerDoraSum")) + dora); - if (Session.get("east_riichi") == true) - Session.set("eastPlayerRiichisWon", Number(Session.get("eastPlayerRiichisWon")) + 1); - } - else if (winnerWind == Constants.SOUTH) { - Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) + 1); - Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) + points); - Session.set("southPlayerDoraSum", Number(Session.get("southPlayerDoraSum")) + dora); - if (Session.get("south_riichi") == true) - Session.set("southPlayerRiichisWon", Number(Session.get("southPlayerRiichisWon")) + 1); - } - else if (winnerWind == Constants.WEST) { - Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) + 1); - Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); - Session.set("westPlayerDoraSum", Number(Session.get("westPlayerDoraSum")) + dora); - if (Session.get("west_riichi") == true) - Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) + 1); - } - else if (winnerWind == Constants.NORTH) { - Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); - Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); - Session.set("northPlayerDoraSum", Number(Session.get("northPlayerDoraSum")) + dora); - if (Session.get("north_riichi") == true) - Session.set("northPlayerRiichisWon", Number(Session.get("northPlayerRiichisWon")) + 1); - } - - if (loserWind == Constants.EAST || paoWind == Constants.EAST) - Session.set("eastPlayerLosses", Number(Session.get("eastPlayerLosses")) + 1); - else if (loserWind == Constants.SOUTH || paoWind == Constants.SOUTH) - Session.set("southPlayerLosses", Number(Session.get("southPlayerLosses")) + 1); - else if (loserWind == Constants.WEST || paoWind == Constants.WEST) - Session.set("westPlayerLosses", Number(Session.get("westPlayerLosses")) + 1); - else if (loserWind == Constants.NORTH || paoWind == Constants.NORTH) - Session.set("northPlayerLosses", Number(Session.get("northPlayerLosses")) + 1); - - if (Session.get("east_riichi") == true) { - eastDelta -= 1000; - riichiSum++; - Session.set("east_riichi_sum", Number(Session.get("east_riichi_sum")) + 1); - } - if (Session.get("south_riichi") == true) { - southDelta -= 1000; - riichiSum++; - Session.set("south_riichi_sum", Number(Session.get("south_riichi_sum")) + 1); - } - if (Session.get("west_riichi") == true) { - westDelta -= 1000; - riichiSum++; - Session.set("west_riichi_sum", Number(Session.get("west_riichi_sum")) + 1); - } - if (Session.get("north_riichi") == true) { - northDelta -= 1000; - riichiSum++; - Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); - } - - var value = PointCalculationUtils.jpn.dealin_delta(points, fu, winnerWind, loserWind, 0)[loserWind]; - - if (((value / 2 ) % 100) == 50) { - value += 100; - } - - switch (winnerWind) { - case Constants.EAST: - eastDelta += value; - eastDelta += (riichiSum * 1000); - break; - case Constants.SOUTH: - southDelta += value; - southDelta += (riichiSum * 1000); - break; - case Constants.WEST: - westDelta += value; - westDelta += (riichiSum * 1000); - break; - case Constants.NORTH: - northDelta += value; - northDelta += (riichiSum * 1000); - break; - } - - Session.set("free_riichi_sticks", 0); - - if (loserWind == Constants.EAST || paoWind == Constants.EAST) eastDelta -= value / 2; - if (loserWind == Constants.SOUTH || paoWind == Constants.SOUTH) southDelta -= value / 2; - if (loserWind == Constants.WEST || paoWind == Constants.WEST) westDelta -= value / 2; - if (loserWind == Constants.NORTH || paoWind == Constants.NORTH) northDelta -= value / 2; - - pushHand(template, "selfdraw", eastDelta, southDelta, westDelta, northDelta); - - if (winnerWind == NewGameUtils.roundToDealerDirection(Session.get("current_round"))) - Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); - else { - Session.set("current_bonus", 0); - Session.set("current_round", Number(Session.get("current_round")) + 1) - } - - template.riichi_round_history.push({east: Session.get("east_riichi"), - south: Session.get("south_riichi"), - west: Session.get("west_riichi"), - north: Session.get("north_riichi")}); + var points = Number(Session.get("current_points")); + var fu = Number(Session.get("current_fu")); + var dora = Number(Session.get("current_dora")); + var winnerWind = NewGameUtils.playerToDirection(Session.get("round_winner")); + var loserWind = NewGameUtils.playerToDirection(Session.get("round_loser")); + var paoWind = NewGameUtils.playerToDirection(Session.get("round_pao_player")); + var riichiSum = Session.get("free_riichi_sticks"); + var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; + + if (winnerWind == Constants.EAST) { + Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); + Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) + points); + Session.set("eastPlayerDoraSum", Number(Session.get("eastPlayerDoraSum")) + dora); + if (Session.get("east_riichi") == true) + Session.set("eastPlayerRiichisWon", Number(Session.get("eastPlayerRiichisWon")) + 1); + } + else if (winnerWind == Constants.SOUTH) { + Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) + 1); + Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) + points); + Session.set("southPlayerDoraSum", Number(Session.get("southPlayerDoraSum")) + dora); + if (Session.get("south_riichi") == true) + Session.set("southPlayerRiichisWon", Number(Session.get("southPlayerRiichisWon")) + 1); + } + else if (winnerWind == Constants.WEST) { + Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) + 1); + Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); + Session.set("westPlayerDoraSum", Number(Session.get("westPlayerDoraSum")) + dora); + if (Session.get("west_riichi") == true) + Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) + 1); + } + else if (winnerWind == Constants.NORTH) { + Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); + Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); + Session.set("northPlayerDoraSum", Number(Session.get("northPlayerDoraSum")) + dora); + if (Session.get("north_riichi") == true) + Session.set("northPlayerRiichisWon", Number(Session.get("northPlayerRiichisWon")) + 1); + } + + if (loserWind == Constants.EAST || paoWind == Constants.EAST) + Session.set("eastPlayerLosses", Number(Session.get("eastPlayerLosses")) + 1); + else if (loserWind == Constants.SOUTH || paoWind == Constants.SOUTH) + Session.set("southPlayerLosses", Number(Session.get("southPlayerLosses")) + 1); + else if (loserWind == Constants.WEST || paoWind == Constants.WEST) + Session.set("westPlayerLosses", Number(Session.get("westPlayerLosses")) + 1); + else if (loserWind == Constants.NORTH || paoWind == Constants.NORTH) + Session.set("northPlayerLosses", Number(Session.get("northPlayerLosses")) + 1); + + if (Session.get("east_riichi") == true) { + eastDelta -= 1000; + riichiSum++; + Session.set("east_riichi_sum", Number(Session.get("east_riichi_sum")) + 1); + } + if (Session.get("south_riichi") == true) { + southDelta -= 1000; + riichiSum++; + Session.set("south_riichi_sum", Number(Session.get("south_riichi_sum")) + 1); + } + if (Session.get("west_riichi") == true) { + westDelta -= 1000; + riichiSum++; + Session.set("west_riichi_sum", Number(Session.get("west_riichi_sum")) + 1); + } + if (Session.get("north_riichi") == true) { + northDelta -= 1000; + riichiSum++; + Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); + } + + var value = PointCalculationUtils.jpn.dealin_delta(points, fu, winnerWind, loserWind, 0)[loserWind]; + + if (((value / 2 ) % 100) == 50) { + value += 100; + } + + switch (winnerWind) { + case Constants.EAST: + eastDelta += value; + eastDelta += (riichiSum * 1000); + break; + case Constants.SOUTH: + southDelta += value; + southDelta += (riichiSum * 1000); + break; + case Constants.WEST: + westDelta += value; + westDelta += (riichiSum * 1000); + break; + case Constants.NORTH: + northDelta += value; + northDelta += (riichiSum * 1000); + break; + } + + Session.set("free_riichi_sticks", 0); + + if (loserWind == Constants.EAST || paoWind == Constants.EAST) eastDelta -= value / 2; + if (loserWind == Constants.SOUTH || paoWind == Constants.SOUTH) southDelta -= value / 2; + if (loserWind == Constants.WEST || paoWind == Constants.WEST) westDelta -= value / 2; + if (loserWind == Constants.NORTH || paoWind == Constants.NORTH) northDelta -= value / 2; + + pushHand(template, "selfdraw", eastDelta, southDelta, westDelta, northDelta); + + if (winnerWind == NewGameUtils.roundToDealerDirection(Session.get("current_round"))) + Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); + else { + Session.set("current_bonus", 0); + Session.set("current_round", Number(Session.get("current_round")) + 1) + } + + template.riichi_round_history.push({east: Session.get("east_riichi"), + south: Session.get("south_riichi"), + west: Session.get("west_riichi"), + north: Session.get("north_riichi")}); }; function pushHand(template, handType, eastDelta, southDelta, westDelta, northDelta) { - template.hands.push( - { - handType: handType, - round: Session.get("current_round"), - bonus: Session.get("current_bonus"), - points: Session.get("current_points"), - fu: Session.get("current_fu"), - dora: Session.get("current_dora"), - eastDelta: eastDelta, - southDelta: southDelta, - westDelta: westDelta, - northDelta: northDelta, - }); - - Session.set("east_score", Number(Session.get("east_score")) + eastDelta); - Session.set("south_score", Number(Session.get("south_score")) + southDelta); - Session.set("west_score", Number(Session.get("west_score")) + westDelta); - Session.set("north_score", Number(Session.get("north_score")) + northDelta); + template.hands.push( + { + handType: handType, + round: Session.get("current_round"), + bonus: Session.get("current_bonus"), + points: Session.get("current_points"), + fu: Session.get("current_fu"), + dora: Session.get("current_dora"), + eastDelta: eastDelta, + southDelta: southDelta, + westDelta: westDelta, + northDelta: northDelta, + }); + + Session.set("east_score", Number(Session.get("east_score")) + eastDelta); + Session.set("south_score", Number(Session.get("south_score")) + southDelta); + Session.set("west_score", Number(Session.get("west_score")) + westDelta); + Session.set("north_score", Number(Session.get("north_score")) + northDelta); }; function setAllGUIRiichisFalse() { - Session.set("east_riichi", false); - Session.set("south_riichi", false); - Session.set("west_riichi", false); - Session.set("north_riichi", false); + Session.set("east_riichi", false); + Session.set("south_riichi", false); + Session.set("west_riichi", false); + Session.set("north_riichi", false); }; Template.jpn_points.events({ - 'change select[name="points"]'(event) { - Session.set("current_points", event.target.value); - } + 'change select[name="points"]'(event) { + Session.set("current_points", event.target.value); + } }); Template.jpn_fu.events({ - 'change select[name="fu"]'(event) { - Session.set("current_fu", event.target.value); - } + 'change select[name="fu"]'(event) { + Session.set("current_fu", event.target.value); + } }); Template.jpn_dora.events({ - 'change select[name="dora"]'(event) { - Session.set("current_dora", event.target.value); - } + 'change select[name="dora"]'(event) { + Session.set("current_dora", event.target.value); + } }); From 7f008682fb99929bbe1818c34cd3406af0bc628b Mon Sep 17 00:00:00 2001 From: TwelveNights Date: Sun, 20 Nov 2016 22:18:05 -0800 Subject: [PATCH 06/10] Reconvert spaces to tabs --- imports/api/Constants.js | 96 +- imports/api/NewGameUtils.js | 502 ++++----- imports/api/PointCalculationUtils.js | 14 +- imports/ui/JapaneseNewGame.js | 1500 +++++++++++++------------- 4 files changed, 1056 insertions(+), 1056 deletions(-) diff --git a/imports/api/Constants.js b/imports/api/Constants.js index abfd0a6..0cb9fa2 100644 --- a/imports/api/Constants.js +++ b/imports/api/Constants.js @@ -1,64 +1,64 @@ // A class/namespace of commonly used Constants export const Constants = { - // Title - MAHJONG_CLUB_LEAGUE: "Mahjong Club League", + // Title + MAHJONG_CLUB_LEAGUE: "Mahjong Club League", - // The starting number of points in a Hong Kong game - HKG_START_POINTS: 500, + // The starting number of points in a Hong Kong game + HKG_START_POINTS: 500, - // The starting number of points in a Japanese game - JPN_START_POINTS: 25000, + // The starting number of points in a Japanese game + JPN_START_POINTS: 25000, - // The number of points distributed upon a tenpai round - // from noten players to tenpai ones in a Japanese game - JPN_TENPAI_PAYOUT: 3000, + // The number of points distributed upon a tenpai round + // from noten players to tenpai ones in a Japanese game + JPN_TENPAI_PAYOUT: 3000, - // The required number of points of any one player to - // end a game at the end of "South" round in a Japanese - // game - JPN_END_POINTS: 30000, + // The required number of points of any one player to + // end a game at the end of "South" round in a Japanese + // game + JPN_END_POINTS: 30000, - // Base points of a mangan - JPN_MANGAN_BASE_POINTS: 2000, + // Base points of a mangan + JPN_MANGAN_BASE_POINTS: 2000, - // Placeholder value to establish a player select button - // That has no player selected - NO_PERSON: "no one", + // Placeholder value to establish a player select button + // That has no player selected + NO_PERSON: "no one", - // The default text to display for player buttons/fields - // When no player has been selected - DEFAULT_EAST: "Select East!", - DEFAULT_SOUTH: "Select South!", - DEFAULT_WEST: "Select West!", - DEFAULT_NORTH: "Select North!", + // The default text to display for player buttons/fields + // When no player has been selected + DEFAULT_EAST: "Select East!", + DEFAULT_SOUTH: "Select South!", + DEFAULT_WEST: "Select West!", + DEFAULT_NORTH: "Select North!", - // An enum of game types for shared yet slightly altered - // rules. Possibly add more if ever decided to - GAME_TYPE: { - // Hong Kong Old Style - HONG_KONG: "hkg", - // Japanese Riichi Style - JAPANESE: "jpn" - }, + // An enum of game types for shared yet slightly altered + // rules. Possibly add more if ever decided to + GAME_TYPE: { + // Hong Kong Old Style + HONG_KONG: "hkg", + // Japanese Riichi Style + JAPANESE: "jpn" + }, - // Round End Conditions - DEAL_IN: "dealin", - SELF_DRAW: "selfdraw", - NO_WIN: "nowin", - RESTART: "restart", - MISTAKE: "fuckup", + // Round End Conditions + DEAL_IN: "dealin", + SELF_DRAW: "selfdraw", + NO_WIN: "nowin", + RESTART: "restart", + MISTAKE: "fuckup", - EAST: "east", - SOUTH: "south", - WEST: "west", - NORTH: "north", + EAST: "east", + SOUTH: "south", + WEST: "west", + NORTH: "north", - PRIORITY: { - east: 3, - south: 2, - north: 1, - west: 0 - } + PRIORITY: { + east: 3, + south: 2, + north: 1, + west: 0 + } }; Constants.WINDS = [Constants.EAST, Constants.SOUTH, Constants.WEST, Constants.NORTH]; diff --git a/imports/api/NewGameUtils.js b/imports/api/NewGameUtils.js index e5f50b8..1e625ae 100644 --- a/imports/api/NewGameUtils.js +++ b/imports/api/NewGameUtils.js @@ -2,269 +2,269 @@ import { Constants } from '../api/Constants.js'; export var NewGameUtils = { - resetGameValues(defaultScore) { - Session.set("current_east", Constants.DEFAULT_EAST); - Session.set("current_south", Constants.DEFAULT_SOUTH); - Session.set("current_west", Constants.DEFAULT_WEST); - Session.set("current_north", Constants.DEFAULT_NORTH); - - Session.set("round_winner", Constants.NO_PERSON); - Session.set("round_loser", Constants.NO_PERSON); - Session.set("round_pao_player", Constants.NO_PERSON); - - Session.set("east_score", defaultScore); - Session.set("south_score", defaultScore); - Session.set("west_score", defaultScore); - Session.set("north_score", defaultScore); - - Session.set("current_round", 1); - Session.set("current_bonus", 0); - Session.set("current_points", 0); - - Session.set("eastPlayerWins", 0); - Session.set("southPlayerWins", 0); - Session.set("westPlayerWins", 0); - Session.set("northPlayerWins", 0); - - Session.set("eastPlayerLosses", 0); - Session.set("southPlayerLosses", 0); - Session.set("westPlayerLosses", 0); - Session.set("northPlayerLosses", 0); - - Session.set("eastPlayerPointsWon", 0); - Session.set("southPlayerPointsWon", 0); - Session.set("westPlayerPointsWon", 0); - Session.set("northPlayerPointsWon", 0); - - Session.set("eastFuckupTotal", 0); - Session.set("southFuckupTotal", 0); - Session.set("westFuckupTotal", 0); - Session.set("northFuckupTotal", 0); - }, - - // UX: Convert a round and gametype into the correct round wind - displayRoundWind(round, gameType) { - - // This code could be streamlined, but let's leave it explicit - switch (gameType) { - case Constants.GAME_TYPE.HONG_KONG: - if (round <= 4) - return "東"; - if (round > 4 && round <= 8) - return "南"; - if (round > 8 && round <= 12) - return "西"; - else //if (round > 12) - return "北"; - case Constants.GAME_TYPE.JAPANESE: - if (round <= 4) - return "東"; - if (round > 4 && round <= 8) - return "南"; - else //if (round > 8) - return "西"; - }; - }, - - // Helper function to ensure all players are selected - allPlayersSelected() { - return (Session.get("current_east") != Constants.DEFAULT_EAST && - Session.get("current_south") != Constants.DEFAULT_SOUTH && - Session.get("current_west") != Constants.DEFAULT_WEST && - Session.get("current_north") != Constants.DEFAULT_NORTH); - }, - - someoneBankrupt() { - return (Session.get("east_score") < 0 || - Session.get("south_score") < 0 || - Session.get("west_score") < 0 || - Session.get("north_score") < 0); - }, - - someoneAboveMinimum(minimum) { - return (Session.get("east_score") >= minimum || - Session.get("south_score") >= minimum || - Session.get("west_score") >= minimum || - Session.get("north_score") >= minimum); - }, - - /** - * Return the position of the player in first place - * TODO: Is this general to all versions of mahjong? - * @return {String} One of ["east", "south", "west", "north"] - */ - getFirstPlace() { - let values = []; - // For ties, prioritise players in seating order - - Constants.WINDS.forEach(k => { - values.push({ wind: k, value: this.getDirectionScore(k) }) - }); - - let winner = values.reduce((a, b) => { - if (a.value == b.value) { - return Constants.PRIORITY[a["wind"]] > Constants.PRIORITY[b["wind"]] ? a : b; - } else { - return a.value > b.value ? a : b; - } - }); - - return winner['wind']; - }, - - /** - * Determine if the game ending conditions for a Japanese mahjong game are met - * @return {Boolean} True if game is over, false if not - */ - japaneseGameOver() { - // End condition where someone has below zero points - let someoneBankrupt = this.someoneBankrupt(); - // End condition where game has reached the end of west round without at least one player above minimum - let westRoundOver = Session.get("current_round") > 12; - // End condition where game has reached the end of south round with at least one player above minimum - let someoneAboveMinimum = Session.get("current_round") > 8 && - this.someoneAboveMinimum(Constants.JPN_END_POINTS); - // End condition where north player reaches first place after winning on last round - let dealerFirstAndAboveMinimum = Session.get("current_round") == 8 && - Session.get("current_bonus") > 0 && - this.getDirectionScore("north") >= Constants.JPN_END_POINTS && - - this.getFirstPlace() == "north"; - return someoneBankrupt || westRoundOver || someoneAboveMinimum || dealerFirstAndAboveMinimum; - }, - - noIllegalSelfdrawJapaneseHands() { - var retval = this.noIllegalJapaneseHands(); - - retval = retval && !(Session.get("current_points") == 2 && Session.get("current_fu") == 25); - - return retval; - }, - - noIllegalJapaneseHands() { - var retval = true; - - retval = retval && (Session.get("current_points") != 0); - retval = retval && (Session.get("current_fu") != 0 || Session.get("current_points") > 4); - - retval = retval && !(Session.get("current_points") == 1 && Session.get("current_fu") == 20); - retval = retval && !(Session.get("current_points") == 1 && Session.get("current_fu") == 25); - - return retval; - }, - - rollbackChomboStat(lastHand) { - if (Number(lastHand.eastDelta) < 0) - Session.set("eastFuckupTotal", Number(Session.get("eastFuckupTotal")) - 1); - else if (Number(lastHand.southDelta) < 0) - Session.set("southFuckupTotal", Number(Session.get("southFuckupTotal")) - 1); - else if (Number(lastHand.westDelta) < 0) - Session.set("westFuckupTotal", Number(Session.get("westFuckupTotal")) - 1); - else if (Number(lastHand.northDelta) < 0) - Session.set("northFuckupTotal", Number(Session.get("northFuckupTotal")) - 1); - }, - - rollbackHandWinStat(lastHand) { - if (Number(lastHand.eastDelta) > 0) - Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) - 1); - else if (Number(lastHand.southDelta) > 0) - Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) - 1); - else if (Number(lastHand.westDelta) > 0) - Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) - 1); - else if (Number(lastHand.northDelta) > 0) - Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) - 1); - }, - - rollbackHandRiichiStat(lastHand, riichiHistory) { - if (Number(lastHand.eastDelta) > 0) { - if (riichiHistory.east == true) - Session.set("eastPlayerRiichisWon", Number(Session.get("eastPlayerRiichisWon")) - 1); - } - else if (Number(lastHand.southDelta) > 0) { - if (riichiHistory.south == true) - Session.set("southPlayerRiichisWon", Number(Session.get("southPlayerRiichisWon")) - 1); - } - else if (Number(lastHand.westDelta) > 0) { - if (riichiHistory.west == true) - Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) - 1); - } - else if (Number(lastHand.northDelta) > 0) { - if (riichiHistory.north == true) - Session.set("northPlayerRiichisWon", Number(Session.get("northPlayerRiichisWon")) - 1); - } - - }, - - rollbackTotalPointsStat(lastHand) { - if (Number(lastHand.eastDelta) > 0) - Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) - lastHand.points); - else if (Number(lastHand.southDelta) > 0) - Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) - lastHand.points); - else if (Number(lastHand.westDelta) > 0) - Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) - lastHand.points); - else if (Number(lastHand.northDelta) > 0) - Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) - lastHand.points); - }, - - rollbackHandDealinStat(lastHand) { - // If we hit a self draw, ensure nothing happens - if (lastHand.eastDelta == 0 || lastHand.southDelta == 0 || lastHand.westDelta == 0 || lastHand.northDelta == 0) - return -1; - - if (Number(lastHand.eastDelta) < 0) - Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) - 1); - else if (Number(lastHand.southDelta) < 0) - Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) - 1); - else if (Number(lastHand.westDelta) < 0) - Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) - 1); - else if (Number(lastHand.northDelta) < 0) - Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) - 1); - }, - - getDirectionScore(direction) { - switch (direction) { - case Constants.EAST: - return Number(Session.get("east_score")); - case Constants.SOUTH: - return Number(Session.get("south_score")); - case Constants.WEST: - return Number(Session.get("west_score")); - case Constants.NORTH: - return Number(Session.get("north_score")); - } - }, - - playerToDirection(player) { - if (player == Session.get("current_east")) return Constants.EAST; - if (player == Session.get("current_south")) return Constants.SOUTH; - if (player == Session.get("current_west")) return Constants.WEST; - if (player == Session.get("current_north")) return Constants.NORTH; - }, - - roundToDealerDirection(round) { - if (round % 4 == 1) return Constants.EAST; - if (round % 4 == 2) return Constants.SOUTH; - if (round % 4 == 3) return Constants.WEST; - if (round % 4 == 0) return Constants.NORTH; - }, + resetGameValues(defaultScore) { + Session.set("current_east", Constants.DEFAULT_EAST); + Session.set("current_south", Constants.DEFAULT_SOUTH); + Session.set("current_west", Constants.DEFAULT_WEST); + Session.set("current_north", Constants.DEFAULT_NORTH); + + Session.set("round_winner", Constants.NO_PERSON); + Session.set("round_loser", Constants.NO_PERSON); + Session.set("round_pao_player", Constants.NO_PERSON); + + Session.set("east_score", defaultScore); + Session.set("south_score", defaultScore); + Session.set("west_score", defaultScore); + Session.set("north_score", defaultScore); + + Session.set("current_round", 1); + Session.set("current_bonus", 0); + Session.set("current_points", 0); + + Session.set("eastPlayerWins", 0); + Session.set("southPlayerWins", 0); + Session.set("westPlayerWins", 0); + Session.set("northPlayerWins", 0); + + Session.set("eastPlayerLosses", 0); + Session.set("southPlayerLosses", 0); + Session.set("westPlayerLosses", 0); + Session.set("northPlayerLosses", 0); + + Session.set("eastPlayerPointsWon", 0); + Session.set("southPlayerPointsWon", 0); + Session.set("westPlayerPointsWon", 0); + Session.set("northPlayerPointsWon", 0); + + Session.set("eastFuckupTotal", 0); + Session.set("southFuckupTotal", 0); + Session.set("westFuckupTotal", 0); + Session.set("northFuckupTotal", 0); + }, + + // UX: Convert a round and gametype into the correct round wind + displayRoundWind(round, gameType) { + + // This code could be streamlined, but let's leave it explicit + switch (gameType) { + case Constants.GAME_TYPE.HONG_KONG: + if (round <= 4) + return "東"; + if (round > 4 && round <= 8) + return "南"; + if (round > 8 && round <= 12) + return "西"; + else //if (round > 12) + return "北"; + case Constants.GAME_TYPE.JAPANESE: + if (round <= 4) + return "東"; + if (round > 4 && round <= 8) + return "南"; + else //if (round > 8) + return "西"; + }; + }, + + // Helper function to ensure all players are selected + allPlayersSelected() { + return (Session.get("current_east") != Constants.DEFAULT_EAST && + Session.get("current_south") != Constants.DEFAULT_SOUTH && + Session.get("current_west") != Constants.DEFAULT_WEST && + Session.get("current_north") != Constants.DEFAULT_NORTH); + }, + + someoneBankrupt() { + return (Session.get("east_score") < 0 || + Session.get("south_score") < 0 || + Session.get("west_score") < 0 || + Session.get("north_score") < 0); + }, + + someoneAboveMinimum(minimum) { + return (Session.get("east_score") >= minimum || + Session.get("south_score") >= minimum || + Session.get("west_score") >= minimum || + Session.get("north_score") >= minimum); + }, + + /** + * Return the position of the player in first place + * TODO: Is this general to all versions of mahjong? + * @return {String} One of ["east", "south", "west", "north"] + */ + getFirstPlace() { + let values = []; + // For ties, prioritise players in seating order + + Constants.WINDS.forEach(k => { + values.push({ wind: k, value: this.getDirectionScore(k) }) + }); + + let winner = values.reduce((a, b) => { + if (a.value == b.value) { + return Constants.PRIORITY[a["wind"]] > Constants.PRIORITY[b["wind"]] ? a : b; + } else { + return a.value > b.value ? a : b; + } + }); + + return winner['wind']; + }, + + /** + * Determine if the game ending conditions for a Japanese mahjong game are met + * @return {Boolean} True if game is over, false if not + */ + japaneseGameOver() { + // End condition where someone has below zero points + let someoneBankrupt = this.someoneBankrupt(); + // End condition where game has reached the end of west round without at least one player above minimum + let westRoundOver = Session.get("current_round") > 12; + // End condition where game has reached the end of south round with at least one player above minimum + let someoneAboveMinimum = Session.get("current_round") > 8 && + this.someoneAboveMinimum(Constants.JPN_END_POINTS); + // End condition where north player reaches first place after winning on last round + let dealerFirstAndAboveMinimum = Session.get("current_round") == 8 && + Session.get("current_bonus") > 0 && + this.getDirectionScore("north") >= Constants.JPN_END_POINTS && + + this.getFirstPlace() == "north"; + return someoneBankrupt || westRoundOver || someoneAboveMinimum || dealerFirstAndAboveMinimum; + }, + + noIllegalSelfdrawJapaneseHands() { + var retval = this.noIllegalJapaneseHands(); + + retval = retval && !(Session.get("current_points") == 2 && Session.get("current_fu") == 25); + + return retval; + }, + + noIllegalJapaneseHands() { + var retval = true; + + retval = retval && (Session.get("current_points") != 0); + retval = retval && (Session.get("current_fu") != 0 || Session.get("current_points") > 4); + + retval = retval && !(Session.get("current_points") == 1 && Session.get("current_fu") == 20); + retval = retval && !(Session.get("current_points") == 1 && Session.get("current_fu") == 25); + + return retval; + }, + + rollbackChomboStat(lastHand) { + if (Number(lastHand.eastDelta) < 0) + Session.set("eastFuckupTotal", Number(Session.get("eastFuckupTotal")) - 1); + else if (Number(lastHand.southDelta) < 0) + Session.set("southFuckupTotal", Number(Session.get("southFuckupTotal")) - 1); + else if (Number(lastHand.westDelta) < 0) + Session.set("westFuckupTotal", Number(Session.get("westFuckupTotal")) - 1); + else if (Number(lastHand.northDelta) < 0) + Session.set("northFuckupTotal", Number(Session.get("northFuckupTotal")) - 1); + }, + + rollbackHandWinStat(lastHand) { + if (Number(lastHand.eastDelta) > 0) + Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) - 1); + else if (Number(lastHand.southDelta) > 0) + Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) - 1); + else if (Number(lastHand.westDelta) > 0) + Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) - 1); + else if (Number(lastHand.northDelta) > 0) + Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) - 1); + }, + + rollbackHandRiichiStat(lastHand, riichiHistory) { + if (Number(lastHand.eastDelta) > 0) { + if (riichiHistory.east == true) + Session.set("eastPlayerRiichisWon", Number(Session.get("eastPlayerRiichisWon")) - 1); + } + else if (Number(lastHand.southDelta) > 0) { + if (riichiHistory.south == true) + Session.set("southPlayerRiichisWon", Number(Session.get("southPlayerRiichisWon")) - 1); + } + else if (Number(lastHand.westDelta) > 0) { + if (riichiHistory.west == true) + Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) - 1); + } + else if (Number(lastHand.northDelta) > 0) { + if (riichiHistory.north == true) + Session.set("northPlayerRiichisWon", Number(Session.get("northPlayerRiichisWon")) - 1); + } + + }, + + rollbackTotalPointsStat(lastHand) { + if (Number(lastHand.eastDelta) > 0) + Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) - lastHand.points); + else if (Number(lastHand.southDelta) > 0) + Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) - lastHand.points); + else if (Number(lastHand.westDelta) > 0) + Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) - lastHand.points); + else if (Number(lastHand.northDelta) > 0) + Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) - lastHand.points); + }, + + rollbackHandDealinStat(lastHand) { + // If we hit a self draw, ensure nothing happens + if (lastHand.eastDelta == 0 || lastHand.southDelta == 0 || lastHand.westDelta == 0 || lastHand.northDelta == 0) + return -1; + + if (Number(lastHand.eastDelta) < 0) + Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) - 1); + else if (Number(lastHand.southDelta) < 0) + Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) - 1); + else if (Number(lastHand.westDelta) < 0) + Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) - 1); + else if (Number(lastHand.northDelta) < 0) + Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) - 1); + }, + + getDirectionScore(direction) { + switch (direction) { + case Constants.EAST: + return Number(Session.get("east_score")); + case Constants.SOUTH: + return Number(Session.get("south_score")); + case Constants.WEST: + return Number(Session.get("west_score")); + case Constants.NORTH: + return Number(Session.get("north_score")); + } + }, + + playerToDirection(player) { + if (player == Session.get("current_east")) return Constants.EAST; + if (player == Session.get("current_south")) return Constants.SOUTH; + if (player == Session.get("current_west")) return Constants.WEST; + if (player == Session.get("current_north")) return Constants.NORTH; + }, + + roundToDealerDirection(round) { + if (round % 4 == 1) return Constants.EAST; + if (round % 4 == 2) return Constants.SOUTH; + if (round % 4 == 3) return Constants.WEST; + if (round % 4 == 0) return Constants.NORTH; + }, }; Template.registerHelper("get_east", function () { - return Session.get("current_east"); + return Session.get("current_east"); }); Template.registerHelper("get_south", function () { - return Session.get("current_south"); + return Session.get("current_south"); }); Template.registerHelper("get_west", function () { - return Session.get("current_west"); + return Session.get("current_west"); }); Template.registerHelper("get_north", function () { - return Session.get("current_north"); + return Session.get("current_north"); }); Template.registerHelper("get_round", function() { - return Session.get("current_round"); + return Session.get("current_round"); }); Template.registerHelper("get_bonus", function () { - return Session.get("current_bonus"); + return Session.get("current_bonus"); }); diff --git a/imports/api/PointCalculationUtils.js b/imports/api/PointCalculationUtils.js index 9f75b73..66af4db 100644 --- a/imports/api/PointCalculationUtils.js +++ b/imports/api/PointCalculationUtils.js @@ -2,11 +2,11 @@ import { Constants } from './Constants'; import { NewGameUtils } from './NewGameUtils'; export var PointCalculationUtils = { - jpn: { - dealin_delta, - selfdraw_delta, - mistake_delta - } + jpn: { + dealin_delta, + selfdraw_delta, + mistake_delta + } }; function dealin_delta(points, fu, winnerWind, loserWind, riichiSticks) { @@ -78,8 +78,8 @@ function selfdraw_delta(points, fu, winnerWind, riichiSticks) { }; function mistake_delta(loser) { - let winds = {}; - winds[Constants.EAST] = winds[Constants.SOUTH] = winds[Constants.WEST] = winds[Constants.NORTH] = 4000; + let winds = {}; + winds[Constants.EAST] = winds[Constants.SOUTH] = winds[Constants.WEST] = winds[Constants.NORTH] = 4000; winds[loser] = -12000; diff --git a/imports/ui/JapaneseNewGame.js b/imports/ui/JapaneseNewGame.js index 4079b00..ab9a059 100644 --- a/imports/ui/JapaneseNewGame.js +++ b/imports/ui/JapaneseNewGame.js @@ -9,264 +9,264 @@ import { PointCalculationUtils } from '../api/PointCalculationUtils'; // Code to be evaluated when JapaneseNewGame template is reloaded Template.JapaneseNewGame.onCreated( function() { - // Meteor: Template type to show for choosing hand submission - this.hand_type = new ReactiveVar( "jpn_dealin" ); + // Meteor: Template type to show for choosing hand submission + this.hand_type = new ReactiveVar( "jpn_dealin" ); - // Meteor: List of hands submitted to display - this.hands = new ReactiveArray(); + // Meteor: List of hands submitted to display + this.hands = new ReactiveArray(); - // Save game riichi history for if a hand is deleted - this.riichi_round_history = []; - this.riichi_sum_history = []; + // Save game riichi history for if a hand is deleted + this.riichi_round_history = []; + this.riichi_sum_history = []; - // Reset shared Mahjong stats - NewGameUtils.resetGameValues(Constants.JPN_START_POINTS); + // Reset shared Mahjong stats + NewGameUtils.resetGameValues(Constants.JPN_START_POINTS); - // Reset Japanese hand specific stats - Session.set("current_fu", 0); - Session.set("current_dora", 0); + // Reset Japanese hand specific stats + Session.set("current_fu", 0); + Session.set("current_dora", 0); - // Reset GUI selection fields - setAllGUIRiichisFalse(); + // Reset GUI selection fields + setAllGUIRiichisFalse(); - // Reset Japanese game specific stats - Session.set("east_riichi_sum", 0); - Session.set("south_riichi_sum", 0); - Session.set("west_riichi_sum", 0); - Session.set("north_riichi_sum", 0); + // Reset Japanese game specific stats + Session.set("east_riichi_sum", 0); + Session.set("south_riichi_sum", 0); + Session.set("west_riichi_sum", 0); + Session.set("north_riichi_sum", 0); - Session.set("eastPlayerRiichisWon", 0); - Session.set("southPlayerRiichisWon", 0); - Session.set("westPlayerRiichisWon", 0); - Session.set("northPlayerRiichisWon", 0); + Session.set("eastPlayerRiichisWon", 0); + Session.set("southPlayerRiichisWon", 0); + Session.set("westPlayerRiichisWon", 0); + Session.set("northPlayerRiichisWon", 0); - Session.set("eastPlayerDoraSum", 0); - Session.set("southPlayerDoraSum", 0); - Session.set("westPlayerDoraSum", 0); - Session.set("northPlayerDoraSum", 0); + Session.set("eastPlayerDoraSum", 0); + Session.set("southPlayerDoraSum", 0); + Session.set("westPlayerDoraSum", 0); + Session.set("northPlayerDoraSum", 0); - // Reset number of riichi sticks stored for next player win - Session.set("free_riichi_sticks", 0); + // Reset number of riichi sticks stored for next player win + Session.set("free_riichi_sticks", 0); }); // Code to be evaluated when jpn_dealin template is reloaded Template.jpn_dealin.onCreated( function() { - // Reset GUI selection fields - setAllGUIRiichisFalse(); + // Reset GUI selection fields + setAllGUIRiichisFalse(); }); // Code to be evaluated when jpn_selfdraw template is reloaded Template.jpn_selfdraw.onCreated( function() { - // Reset GUI selection fields - setAllGUIRiichisFalse(); + // Reset GUI selection fields + setAllGUIRiichisFalse(); }); // Code to be evaluated when jpn_nowin tenplate is reloaded Template.jpn_nowin.onCreated( function() { - // Reset GUI selection fields - Session.set("east_tenpai", false); - Session.set("south_tenpai", false); - Session.set("west_tenpai", false); - Session.set("north_tenpai", false); + // Reset GUI selection fields + Session.set("east_tenpai", false); + Session.set("south_tenpai", false); + Session.set("west_tenpai", false); + Session.set("north_tenpai", false); - setAllGUIRiichisFalse(); + setAllGUIRiichisFalse(); }); // Code to be evaluated when jpn_restart tenplate is reloaded Template.jpn_restart.onCreated( function() { - // Reset GUI selection fields - setAllGUIRiichisFalse(); + // Reset GUI selection fields + setAllGUIRiichisFalse(); }); // Code to be evaluated when jpn_split_pao tenplate is reloaded Template.jpn_split_pao.onCreated( function() { - // Reset GUI selection fields - setAllGUIRiichisFalse(); + // Reset GUI selection fields + setAllGUIRiichisFalse(); }); // GUI helper to show current round # // [E1,E2,E3,E4,S1,S2,S3,S4,W1,W2,W3,W4,W5,W...] Template.registerHelper("jpn_round_mod4", function(round) { - if (Number(round) > 8) - return (Number(round) - 8); - var retval = Number(round) % 4; - if (retval == 0) - retval = 4; - return retval; + if (Number(round) > 8) + return (Number(round) - 8); + var retval = Number(round) % 4; + if (retval == 0) + retval = 4; + return retval; }) // GUI helpers for hand submission template Template.JapaneseNewGame.helpers({ - // Choose hand type for submission form - hand_type() { - return Template.instance().hand_type.get(); - }, - // Choose player to select from dropdown menus - players() { - return Players.find({}, {sort: { japaneseLeagueName: 1}}); - }, - // Return all recorded hands for a game as an array - hands() { - return Template.instance().hands.get(); - }, - // Show what a player's +/- is - get_player_delta(direction) { - return (NewGameUtils.getDirectionScore(direction) - Constants.JPN_START_POINTS); - }, - // Show what a player's current score is - get_player_score(direction) { - return NewGameUtils.getDirectionScore(direction); - }, - // Show what a player's score will look like if game is ended now - get_player_score_final(direction) { - retval = NewGameUtils.getDirectionScore(direction); - - var winScore = Math.max(Number(Session.get("east_score")), - Number(Session.get("south_score")), - Number(Session.get("west_score")), - Number(Session.get("north_score"))); - - if (winScore == Session.get("east_score")) { - if (direction == Constants.EAST) - retval += 1000 * Number(Session.get("free_riichi_sticks")); - } else if (winScore == Session.get("south_score")) { - if (direction == Constants.SOUTH) - retval += 1000 * Number(Session.get("free_riichi_sticks")); - } else if (winScore == Session.get("west_score")) { - if (direction == Constants.WEST) - retval += 1000 * Number(Session.get("free_riichi_sticks")); - } else if (winScore == Session.get("north_score")) { - if (direction == Constants.NORTH) - retval += 1000 * Number(Session.get("free_riichi_sticks")); - } - - - return retval; - }, - // Show a player's ELO - get_jpn_elo(player) { - switch (player) { - case Constants.DEFAULT_EAST: - case Constants.DEFAULT_SOUTH: - case Constants.DEFAULT_WEST: - case Constants.DEFAULT_NORTH: - return "?"; - default: - return Players.findOne({japaneseLeagueName: player}).japaneseElo.toFixed(2); - }; - }, - // Return a string of the round wind for Japanese style - displayRoundWind(round) { - return NewGameUtils.displayRoundWind(round, Constants.GAME_TYPE.JAPANESE); - }, + // Choose hand type for submission form + hand_type() { + return Template.instance().hand_type.get(); + }, + // Choose player to select from dropdown menus + players() { + return Players.find({}, {sort: { japaneseLeagueName: 1}}); + }, + // Return all recorded hands for a game as an array + hands() { + return Template.instance().hands.get(); + }, + // Show what a player's +/- is + get_player_delta(direction) { + return (NewGameUtils.getDirectionScore(direction) - Constants.JPN_START_POINTS); + }, + // Show what a player's current score is + get_player_score(direction) { + return NewGameUtils.getDirectionScore(direction); + }, + // Show what a player's score will look like if game is ended now + get_player_score_final(direction) { + retval = NewGameUtils.getDirectionScore(direction); + + var winScore = Math.max(Number(Session.get("east_score")), + Number(Session.get("south_score")), + Number(Session.get("west_score")), + Number(Session.get("north_score"))); + + if (winScore == Session.get("east_score")) { + if (direction == Constants.EAST) + retval += 1000 * Number(Session.get("free_riichi_sticks")); + } else if (winScore == Session.get("south_score")) { + if (direction == Constants.SOUTH) + retval += 1000 * Number(Session.get("free_riichi_sticks")); + } else if (winScore == Session.get("west_score")) { + if (direction == Constants.WEST) + retval += 1000 * Number(Session.get("free_riichi_sticks")); + } else if (winScore == Session.get("north_score")) { + if (direction == Constants.NORTH) + retval += 1000 * Number(Session.get("free_riichi_sticks")); + } + + + return retval; + }, + // Show a player's ELO + get_jpn_elo(player) { + switch (player) { + case Constants.DEFAULT_EAST: + case Constants.DEFAULT_SOUTH: + case Constants.DEFAULT_WEST: + case Constants.DEFAULT_NORTH: + return "?"; + default: + return Players.findOne({japaneseLeagueName: player}).japaneseElo.toFixed(2); + }; + }, + // Return a string of the round wind for Japanese style + displayRoundWind(round) { + return NewGameUtils.displayRoundWind(round, Constants.GAME_TYPE.JAPANESE); + }, }); // GUI helpers for rendering hands Template.jpn_render_hand.helpers({ - // Boolean expressions to help with rendering hands - is_dealin(hand_type) { - return hand_type == Constants.DEAL_IN; - }, - is_selfdraw(hand_type) { - return hand_type == Constants.SELF_DRAW; - }, - is_nowin(hand_type) { - return hand_type == Constants.NO_WIN; - }, - is_restart(hand_type) { - return hand_type == Constants.RESTART; - }, - is_mistake(hand_type) { - return hand_type == Constants.MISTAKE; - }, - // Return a string of the round wind for Japanese style - displayRoundWind(round) { - return NewGameUtils.displayRoundWind(round, Constants.GAME_TYPE.JAPANESE); - }, + // Boolean expressions to help with rendering hands + is_dealin(hand_type) { + return hand_type == Constants.DEAL_IN; + }, + is_selfdraw(hand_type) { + return hand_type == Constants.SELF_DRAW; + }, + is_nowin(hand_type) { + return hand_type == Constants.NO_WIN; + }, + is_restart(hand_type) { + return hand_type == Constants.RESTART; + }, + is_mistake(hand_type) { + return hand_type == Constants.MISTAKE; + }, + // Return a string of the round wind for Japanese style + displayRoundWind(round) { + return NewGameUtils.displayRoundWind(round, Constants.GAME_TYPE.JAPANESE); + }, }) // Helper for point selection dropdown--All allowable points // Assume that 13, 26, 39, and 52 are single->quadruple yakuman Template.jpn_points.helpers({ - possible_points: [ - { point: 1 }, - { point: 2 }, - { point: 3 }, - { point: 4 }, - { point: 5 }, - { point: 6 }, - { point: 7 }, - { point: 8 }, - { point: 9 }, - { point: 10 }, - { point: 11 }, - { point: 12 }, - { point: 13 }, - { point: 26 }, - { point: 39 }, - { point: 52 }, - { point: 65 } - ], + possible_points: [ + { point: 1 }, + { point: 2 }, + { point: 3 }, + { point: 4 }, + { point: 5 }, + { point: 6 }, + { point: 7 }, + { point: 8 }, + { point: 9 }, + { point: 10 }, + { point: 11 }, + { point: 12 }, + { point: 13 }, + { point: 26 }, + { point: 39 }, + { point: 52 }, + { point: 65 } + ], }); // Helper for fu selection dropdown--All allowable fu // Must do checks to ensure a valid point/fu selection is made Template.jpn_fu.helpers({ - possible_fu: [ - { fu: 20 }, - { fu: 25 }, - { fu: 30 }, - { fu: 40 }, - { fu: 50 }, - { fu: 60 }, - { fu: 70 }, - { fu: 80 }, - { fu: 90 }, - { fu: 100 }, - { fu: 110 }, - ], + possible_fu: [ + { fu: 20 }, + { fu: 25 }, + { fu: 30 }, + { fu: 40 }, + { fu: 50 }, + { fu: 60 }, + { fu: 70 }, + { fu: 80 }, + { fu: 90 }, + { fu: 100 }, + { fu: 110 }, + ], }); // Helper for dora selection dropdown--All allowable dora Template.jpn_dora.helpers({ - possible_dora: [ - { dora: 0 }, - { dora: 1 }, - { dora: 2 }, - { dora: 3 }, - { dora: 4 }, - { dora: 5 }, - { dora: 6 }, - { dora: 7 }, - { dora: 8 }, - { dora: 9 }, - { dora: 10 }, - { dora: 11 }, - { dora: 12 }, - { dora: 13 }, - { dora: 14 }, - { dora: 15 }, - { dora: 16 }, - { dora: 17 }, - { dora: 18 }, - { dora: 19 }, - { dora: 20 }, - { dora: 21 }, - { dora: 22 }, - { dora: 23 }, - { dora: 24 }, - { dora: 25 }, - { dora: 26 }, - { dora: 27 }, - { dora: 28 }, - { dora: 29 }, - { dora: 30 }, - { dora: 31 }, - { dora: 32 }, - { dora: 33 }, - { dora: 34 }, - { dora: 35 }, - ], + possible_dora: [ + { dora: 0 }, + { dora: 1 }, + { dora: 2 }, + { dora: 3 }, + { dora: 4 }, + { dora: 5 }, + { dora: 6 }, + { dora: 7 }, + { dora: 8 }, + { dora: 9 }, + { dora: 10 }, + { dora: 11 }, + { dora: 12 }, + { dora: 13 }, + { dora: 14 }, + { dora: 15 }, + { dora: 16 }, + { dora: 17 }, + { dora: 18 }, + { dora: 19 }, + { dora: 20 }, + { dora: 21 }, + { dora: 22 }, + { dora: 23 }, + { dora: 24 }, + { dora: 25 }, + { dora: 26 }, + { dora: 27 }, + { dora: 28 }, + { dora: 29 }, + { dora: 30 }, + { dora: 31 }, + { dora: 32 }, + { dora: 33 }, + { dora: 34 }, + { dora: 35 }, + ], }); // Functions for browser events @@ -595,591 +595,591 @@ Template.JapaneseNewGame.events({ // Save the currently recorded game to database and update player statistics function save_game_to_database(hands_array) { - var position; - - var east_player = Session.get("current_east"); - var south_player= Session.get("current_south"); - var west_player = Session.get("current_west"); - var north_player= Session.get("current_north"); - - // Initialise game to be saved - var game = { - timestamp: Date.now(), - east_player: east_player, - south_player: south_player, - west_player: west_player, - north_player: north_player, - east_score: (Number(Session.get("east_score"))), - south_score: (Number(Session.get("south_score"))), - west_score: (Number(Session.get("west_score"))), - north_score: (Number(Session.get("north_score"))), - all_hands: hands_array, - }; - - // Initialise ELO calculator to update player ELO - var jpn_elo_calculator = new EloCalculator(2000, 5, [15000, 0, -5000, -10000], game, Constants.GAME_TYPE.JAPANESE); - var east_elo_delta = jpn_elo_calculator.eloChange(east_player); - var south_elo_delta = jpn_elo_calculator.eloChange(south_player); - var west_elo_delta = jpn_elo_calculator.eloChange(west_player); - var north_elo_delta = jpn_elo_calculator.eloChange(north_player); - - var east_id = Players.findOne({japaneseLeagueName: east_player}, {})._id; - var south_id = Players.findOne({japaneseLeagueName: south_player}, {})._id; - var west_id = Players.findOne({japaneseLeagueName: west_player}, {})._id; - var north_id = Players.findOne({japaneseLeagueName: north_player}, {})._id; - - if (east_elo_delta != NaN && south_elo_delta != NaN && west_elo_delta != NaN && north_elo_delta != NaN) { - // Save ELO - Players.update({_id: east_id}, {$inc: {japaneseElo: Number(east_elo_delta)}}); - Players.update({_id: south_id}, {$inc: {japaneseElo: Number(south_elo_delta)}}); - Players.update({_id: west_id}, {$inc: {japaneseElo: Number(west_elo_delta)}}); - Players.update({_id: north_id}, {$inc: {japaneseElo: Number(north_elo_delta)}}); - - // Update number of games - Players.update({_id: east_id}, {$inc: {japaneseGamesPlayed: 1}}); - Players.update({_id: south_id}, {$inc: {japaneseGamesPlayed: 1}}); - Players.update({_id: west_id}, {$inc: {japaneseGamesPlayed: 1}}); - Players.update({_id: north_id}, {$inc: {japaneseGamesPlayed: 1}}); - - // Update bankruptcy count - if (Number(Session.get("east_score")) < 0) - Players.update({_id: east_id}, {$inc: {japaneseBankruptTotal: 1}}); - if (Number(Session.get("south_score")) < 0) - Players.update({_id: south_id}, {$inc: {japaneseBankruptTotal: 1}}); - if (Number(Session.get("west_score")) < 0) - Players.update({_id: west_id}, {$inc: {japaneseBankruptTotal: 1}}); - if (Number(Session.get("north_score")) < 0) - Players.update({_id: north_id}, {$inc: {japaneseBankruptTotal: 1}}); - - // Save chombo counts - Players.update({_id: east_id}, {$inc: {japaneseChomboTotal: Number(Session.get("eastFuckupTotal"))}}); - Players.update({_id: south_id}, {$inc: {japaneseChomboTotal: Number(Session.get("southFuckupTotal"))}}); - Players.update({_id: west_id}, {$inc: {japaneseChomboTotal: Number(Session.get("westFuckupTotal"))}}); - Players.update({_id: north_id}, {$inc: {japaneseChomboTotal: Number(Session.get("northFuckupTotal"))}}); - - // Update riichi count - Players.update({_id: east_id}, {$inc: {japaneseRiichiTotal: Number(Session.get("east_riichi_sum"))}}); - Players.update({_id: south_id}, {$inc: {japaneseRiichiTotal: Number(Session.get("south_riichi_sum"))}}); - Players.update({_id: west_id}, {$inc: {japaneseRiichiTotal: Number(Session.get("west_riichi_sum"))}}); - Players.update({_id: north_id}, {$inc: {japaneseRiichiTotal: Number(Session.get("north_riichi_sum"))}}); - - // Update hands count (Includes chombos, do we want this?) - Players.update({_id: east_id}, {$inc: {japaneseHandsTotal: hands_array.length}}); - Players.update({_id: south_id}, {$inc: {japaneseHandsTotal: hands_array.length}}); - Players.update({_id: west_id}, {$inc: {japaneseHandsTotal: hands_array.length}}); - Players.update({_id: north_id}, {$inc: {japaneseHandsTotal: hands_array.length}}); - - // Save number of hands won - Players.update({_id: east_id}, {$inc: {japaneseHandsWin: Number(Session.get("eastPlayerWins"))}}); - Players.update({_id: south_id}, {$inc: {japaneseHandsWin: Number(Session.get("southPlayerWins"))}}); - Players.update({_id: west_id}, {$inc: {japaneseHandsWin: Number(Session.get("westPlayerWins"))}}); - Players.update({_id: north_id}, {$inc: {japaneseHandsWin: Number(Session.get("northPlayerWins"))}}); - - // Save number of points won - Players.update({_id: east_id}, {$inc: {japaneseWinPointsTotal: Number(Session.get("eastPlayerPointsWon"))}}); - Players.update({_id: south_id}, {$inc: {japaneseWinPointsTotal: Number(Session.get("southPlayerPointsWon"))}}); - Players.update({_id: west_id}, {$inc: {japaneseWinPointsTotal: Number(Session.get("westPlayerPointsWon"))}}); - Players.update({_id: north_id}, {$inc: {japaneseWinPointsTotal: Number(Session.get("northPlayerPointsWon"))}}); - - // Update total dora - Players.update({_id: east_id}, {$inc: {japaneseWinDoraTotal: Number(Session.get("eastPlayerDoraSum"))}}); - Players.update({_id: south_id}, {$inc: {japaneseWinDoraTotal: Number(Session.get("southPlayerDoraSum"))}}); - Players.update({_id: west_id}, {$inc: {japaneseWinDoraTotal: Number(Session.get("westPlayerDoraSum"))}}); - Players.update({_id: north_id}, {$inc: {japaneseWinDoraTotal: Number(Session.get("northPlayerDoraSum"))}}); - - // Save number of riichied hands won - Players.update({_id: east_id}, {$inc: {japaneseWinRiichiTotal: Number(Session.get("eastPlayerRiichisWon"))}}); - Players.update({_id: south_id}, {$inc: {japaneseWinRiichiTotal: Number(Session.get("southPlayerRiichisWon"))}}); - Players.update({_id: west_id}, {$inc: {japaneseWinRiichiTotal: Number(Session.get("westPlayerRiichisWon"))}}); - Players.update({_id: north_id}, {$inc: {japaneseWinRiichiTotal: Number(Session.get("northPlayerRiichisWon"))}}); - - // Save number of hands lost - Players.update({_id: east_id}, {$inc: {japaneseHandsLose: Number(Session.get("eastPlayerLosses"))}}); - Players.update({_id: south_id}, {$inc: {japaneseHandsLose: Number(Session.get("southPlayerLosses"))}}); - Players.update({_id: west_id}, {$inc: {japaneseHandsLose: Number(Session.get("westPlayerLosses"))}}); - Players.update({_id: north_id}, {$inc: {japaneseHandsLose: Number(Session.get("northPlayerLosses"))}}); - - // Calculate positions - // Calculate east position quickly? - position = 4; - if (Number(Session.get("east_score")) >= Number(Session.get("south_score"))) position--; - if (Number(Session.get("east_score")) >= Number(Session.get("west_score"))) position--; - if (Number(Session.get("east_score")) >= Number(Session.get("north_score"))) position--; - Players.update({_id: east_id}, {$inc: {japanesePositionSum: position}}); - - // Calculate east position quickly? - position = 4; - if (Number(Session.get("south_score")) > Number(Session.get("east_score"))) position--; - if (Number(Session.get("south_score")) >= Number(Session.get("west_score"))) position--; - if (Number(Session.get("south_score")) >= Number(Session.get("north_score"))) position--; - Players.update({_id: south_id}, {$inc: {japanesePositionSum: position}}); - - // Calculate east position quickly? - position = 4; - if (Number(Session.get("west_score")) > Number(Session.get("east_score"))) position--; - if (Number(Session.get("west_score")) > Number(Session.get("south_score"))) position--; - if (Number(Session.get("west_score")) >= Number(Session.get("north_score"))) position--; - Players.update({_id: west_id}, {$inc: {japanesePositionSum: position}}); - - // Calculate east position quickly? - position = 4; - if (Number(Session.get("north_score")) > Number(Session.get("east_score"))) position--; - if (Number(Session.get("north_score")) > Number(Session.get("south_score"))) position--; - if (Number(Session.get("north_score")) > Number(Session.get("west_score"))) position--; - Players.update({_id: north_id}, {$inc: {japanesePositionSum: position}}); - - //Save game to database - JapaneseHands.insert(game); - } + var position; + + var east_player = Session.get("current_east"); + var south_player= Session.get("current_south"); + var west_player = Session.get("current_west"); + var north_player= Session.get("current_north"); + + // Initialise game to be saved + var game = { + timestamp: Date.now(), + east_player: east_player, + south_player: south_player, + west_player: west_player, + north_player: north_player, + east_score: (Number(Session.get("east_score"))), + south_score: (Number(Session.get("south_score"))), + west_score: (Number(Session.get("west_score"))), + north_score: (Number(Session.get("north_score"))), + all_hands: hands_array, + }; + + // Initialise ELO calculator to update player ELO + var jpn_elo_calculator = new EloCalculator(2000, 5, [15000, 0, -5000, -10000], game, Constants.GAME_TYPE.JAPANESE); + var east_elo_delta = jpn_elo_calculator.eloChange(east_player); + var south_elo_delta = jpn_elo_calculator.eloChange(south_player); + var west_elo_delta = jpn_elo_calculator.eloChange(west_player); + var north_elo_delta = jpn_elo_calculator.eloChange(north_player); + + var east_id = Players.findOne({japaneseLeagueName: east_player}, {})._id; + var south_id = Players.findOne({japaneseLeagueName: south_player}, {})._id; + var west_id = Players.findOne({japaneseLeagueName: west_player}, {})._id; + var north_id = Players.findOne({japaneseLeagueName: north_player}, {})._id; + + if (east_elo_delta != NaN && south_elo_delta != NaN && west_elo_delta != NaN && north_elo_delta != NaN) { + // Save ELO + Players.update({_id: east_id}, {$inc: {japaneseElo: Number(east_elo_delta)}}); + Players.update({_id: south_id}, {$inc: {japaneseElo: Number(south_elo_delta)}}); + Players.update({_id: west_id}, {$inc: {japaneseElo: Number(west_elo_delta)}}); + Players.update({_id: north_id}, {$inc: {japaneseElo: Number(north_elo_delta)}}); + + // Update number of games + Players.update({_id: east_id}, {$inc: {japaneseGamesPlayed: 1}}); + Players.update({_id: south_id}, {$inc: {japaneseGamesPlayed: 1}}); + Players.update({_id: west_id}, {$inc: {japaneseGamesPlayed: 1}}); + Players.update({_id: north_id}, {$inc: {japaneseGamesPlayed: 1}}); + + // Update bankruptcy count + if (Number(Session.get("east_score")) < 0) + Players.update({_id: east_id}, {$inc: {japaneseBankruptTotal: 1}}); + if (Number(Session.get("south_score")) < 0) + Players.update({_id: south_id}, {$inc: {japaneseBankruptTotal: 1}}); + if (Number(Session.get("west_score")) < 0) + Players.update({_id: west_id}, {$inc: {japaneseBankruptTotal: 1}}); + if (Number(Session.get("north_score")) < 0) + Players.update({_id: north_id}, {$inc: {japaneseBankruptTotal: 1}}); + + // Save chombo counts + Players.update({_id: east_id}, {$inc: {japaneseChomboTotal: Number(Session.get("eastFuckupTotal"))}}); + Players.update({_id: south_id}, {$inc: {japaneseChomboTotal: Number(Session.get("southFuckupTotal"))}}); + Players.update({_id: west_id}, {$inc: {japaneseChomboTotal: Number(Session.get("westFuckupTotal"))}}); + Players.update({_id: north_id}, {$inc: {japaneseChomboTotal: Number(Session.get("northFuckupTotal"))}}); + + // Update riichi count + Players.update({_id: east_id}, {$inc: {japaneseRiichiTotal: Number(Session.get("east_riichi_sum"))}}); + Players.update({_id: south_id}, {$inc: {japaneseRiichiTotal: Number(Session.get("south_riichi_sum"))}}); + Players.update({_id: west_id}, {$inc: {japaneseRiichiTotal: Number(Session.get("west_riichi_sum"))}}); + Players.update({_id: north_id}, {$inc: {japaneseRiichiTotal: Number(Session.get("north_riichi_sum"))}}); + + // Update hands count (Includes chombos, do we want this?) + Players.update({_id: east_id}, {$inc: {japaneseHandsTotal: hands_array.length}}); + Players.update({_id: south_id}, {$inc: {japaneseHandsTotal: hands_array.length}}); + Players.update({_id: west_id}, {$inc: {japaneseHandsTotal: hands_array.length}}); + Players.update({_id: north_id}, {$inc: {japaneseHandsTotal: hands_array.length}}); + + // Save number of hands won + Players.update({_id: east_id}, {$inc: {japaneseHandsWin: Number(Session.get("eastPlayerWins"))}}); + Players.update({_id: south_id}, {$inc: {japaneseHandsWin: Number(Session.get("southPlayerWins"))}}); + Players.update({_id: west_id}, {$inc: {japaneseHandsWin: Number(Session.get("westPlayerWins"))}}); + Players.update({_id: north_id}, {$inc: {japaneseHandsWin: Number(Session.get("northPlayerWins"))}}); + + // Save number of points won + Players.update({_id: east_id}, {$inc: {japaneseWinPointsTotal: Number(Session.get("eastPlayerPointsWon"))}}); + Players.update({_id: south_id}, {$inc: {japaneseWinPointsTotal: Number(Session.get("southPlayerPointsWon"))}}); + Players.update({_id: west_id}, {$inc: {japaneseWinPointsTotal: Number(Session.get("westPlayerPointsWon"))}}); + Players.update({_id: north_id}, {$inc: {japaneseWinPointsTotal: Number(Session.get("northPlayerPointsWon"))}}); + + // Update total dora + Players.update({_id: east_id}, {$inc: {japaneseWinDoraTotal: Number(Session.get("eastPlayerDoraSum"))}}); + Players.update({_id: south_id}, {$inc: {japaneseWinDoraTotal: Number(Session.get("southPlayerDoraSum"))}}); + Players.update({_id: west_id}, {$inc: {japaneseWinDoraTotal: Number(Session.get("westPlayerDoraSum"))}}); + Players.update({_id: north_id}, {$inc: {japaneseWinDoraTotal: Number(Session.get("northPlayerDoraSum"))}}); + + // Save number of riichied hands won + Players.update({_id: east_id}, {$inc: {japaneseWinRiichiTotal: Number(Session.get("eastPlayerRiichisWon"))}}); + Players.update({_id: south_id}, {$inc: {japaneseWinRiichiTotal: Number(Session.get("southPlayerRiichisWon"))}}); + Players.update({_id: west_id}, {$inc: {japaneseWinRiichiTotal: Number(Session.get("westPlayerRiichisWon"))}}); + Players.update({_id: north_id}, {$inc: {japaneseWinRiichiTotal: Number(Session.get("northPlayerRiichisWon"))}}); + + // Save number of hands lost + Players.update({_id: east_id}, {$inc: {japaneseHandsLose: Number(Session.get("eastPlayerLosses"))}}); + Players.update({_id: south_id}, {$inc: {japaneseHandsLose: Number(Session.get("southPlayerLosses"))}}); + Players.update({_id: west_id}, {$inc: {japaneseHandsLose: Number(Session.get("westPlayerLosses"))}}); + Players.update({_id: north_id}, {$inc: {japaneseHandsLose: Number(Session.get("northPlayerLosses"))}}); + + // Calculate positions + // Calculate east position quickly? + position = 4; + if (Number(Session.get("east_score")) >= Number(Session.get("south_score"))) position--; + if (Number(Session.get("east_score")) >= Number(Session.get("west_score"))) position--; + if (Number(Session.get("east_score")) >= Number(Session.get("north_score"))) position--; + Players.update({_id: east_id}, {$inc: {japanesePositionSum: position}}); + + // Calculate east position quickly? + position = 4; + if (Number(Session.get("south_score")) > Number(Session.get("east_score"))) position--; + if (Number(Session.get("south_score")) >= Number(Session.get("west_score"))) position--; + if (Number(Session.get("south_score")) >= Number(Session.get("north_score"))) position--; + Players.update({_id: south_id}, {$inc: {japanesePositionSum: position}}); + + // Calculate east position quickly? + position = 4; + if (Number(Session.get("west_score")) > Number(Session.get("east_score"))) position--; + if (Number(Session.get("west_score")) > Number(Session.get("south_score"))) position--; + if (Number(Session.get("west_score")) >= Number(Session.get("north_score"))) position--; + Players.update({_id: west_id}, {$inc: {japanesePositionSum: position}}); + + // Calculate east position quickly? + position = 4; + if (Number(Session.get("north_score")) > Number(Session.get("east_score"))) position--; + if (Number(Session.get("north_score")) > Number(Session.get("south_score"))) position--; + if (Number(Session.get("north_score")) > Number(Session.get("west_score"))) position--; + Players.update({_id: north_id}, {$inc: {japanesePositionSum: position}}); + + //Save game to database + JapaneseHands.insert(game); + } }; function push_dealin_hand(template) { - var points = Number(Session.get("current_points")); - var fu = Number(Session.get("current_fu")); - var dora = Number(Session.get("current_dora")); - var winnerWind = NewGameUtils.playerToDirection(Session.get("round_winner")); - var loserWind = NewGameUtils.playerToDirection(Session.get("round_loser")); - var riichiSum = Session.get("free_riichi_sticks"); - var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; - - if (winnerWind == Constants.EAST) { - Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); - Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) + points); - Session.set("eastPlayerDoraSum", Number(Session.get("eastPlayerDoraSum")) + dora); - if (Session.get("east_riichi") == true) { - Session.set("eastPlayerRiichisWon", Number(Session.get("eastPlayerRiichisWon")) + 1); - } - } else if (winnerWind == Constants.SOUTH) { - Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) + 1); - Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) + points); - Session.set("southPlayerDoraSum", Number(Session.get("southPlayerDoraSum")) + dora); - if (Session.get("south_riichi") == true) { - Session.set("southPlayerRiichisWon", Number(Session.get("southPlayerRiichisWon")) + 1); - } - } else if (winnerWind == Constants.WEST) { - Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) + 1); - Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); - Session.set("westPlayerDoraSum", Number(Session.get("westPlayerDoraSum")) + dora); - if (Session.get("west_riichi") == true) { - Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) + 1); - } - } else if (winnerWind == Constants.NORTH) { - Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); - Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); - Session.set("northPlayerDoraSum", Number(Session.get("northPlayerDoraSum")) + dora); - if (Session.get("north_riichi") == true) { - Session.set("northPlayerRiichisWon", Number(Session.get("northPlayerRiichisWon")) + 1); - } - } - - if (loserWind == Constants.EAST) - Session.set("eastPlayerLosses", Number(Session.get("eastPlayerLosses")) + 1); - else if (loserWind == Constants.SOUTH) - Session.set("southPlayerLosses", Number(Session.get("southPlayerLosses")) + 1); - else if (loserWind == Constants.WEST) - Session.set("westPlayerLosses", Number(Session.get("westPlayerLosses")) + 1); - else if (loserWind == Constants.NORTH) - Session.set("northPlayerLosses", Number(Session.get("northPlayerLosses")) + 1); - - if (Session.get("east_riichi") == true) { - eastDelta -= 1000; - riichiSum++; - Session.set("east_riichi_sum", Number(Session.get("east_riichi_sum")) + 1); - } - if (Session.get("south_riichi") == true) { - southDelta -= 1000; - riichiSum++; - Session.set("south_riichi_sum", Number(Session.get("south_riichi_sum")) + 1); - } - if (Session.get("west_riichi") == true) { - westDelta -= 1000; - riichiSum++; - Session.set("west_riichi_sum", Number(Session.get("west_riichi_sum")) + 1); - } - if (Session.get("north_riichi") == true) { - northDelta -= 1000; - riichiSum++; - Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); - } - - allDelta = PointCalculationUtils.jpn.dealin_delta(points, fu, winnerWind, loserWind, riichiSum); - Session.set("free_riichi_sticks", 0); - - eastDelta += allDelta[Constants.EAST]; - southDelta += allDelta[Constants.SOUTH]; - westDelta += allDelta[Constants.WEST]; - northDelta += allDelta[Constants.NORTH]; - - pushHand(template, "dealin", eastDelta, southDelta, westDelta, northDelta); - - if (winnerWind == NewGameUtils.roundToDealerDirection(Session.get("current_round"))) - Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); - else { - Session.set("current_bonus", 0); - Session.set("current_round", Number(Session.get("current_round")) + 1) - } - - template.riichi_round_history.push({east: Session.get("east_riichi"), - south: Session.get("south_riichi"), - west: Session.get("west_riichi"), - north: Session.get("north_riichi")}); + var points = Number(Session.get("current_points")); + var fu = Number(Session.get("current_fu")); + var dora = Number(Session.get("current_dora")); + var winnerWind = NewGameUtils.playerToDirection(Session.get("round_winner")); + var loserWind = NewGameUtils.playerToDirection(Session.get("round_loser")); + var riichiSum = Session.get("free_riichi_sticks"); + var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; + + if (winnerWind == Constants.EAST) { + Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); + Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) + points); + Session.set("eastPlayerDoraSum", Number(Session.get("eastPlayerDoraSum")) + dora); + if (Session.get("east_riichi") == true) { + Session.set("eastPlayerRiichisWon", Number(Session.get("eastPlayerRiichisWon")) + 1); + } + } else if (winnerWind == Constants.SOUTH) { + Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) + 1); + Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) + points); + Session.set("southPlayerDoraSum", Number(Session.get("southPlayerDoraSum")) + dora); + if (Session.get("south_riichi") == true) { + Session.set("southPlayerRiichisWon", Number(Session.get("southPlayerRiichisWon")) + 1); + } + } else if (winnerWind == Constants.WEST) { + Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) + 1); + Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); + Session.set("westPlayerDoraSum", Number(Session.get("westPlayerDoraSum")) + dora); + if (Session.get("west_riichi") == true) { + Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) + 1); + } + } else if (winnerWind == Constants.NORTH) { + Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); + Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); + Session.set("northPlayerDoraSum", Number(Session.get("northPlayerDoraSum")) + dora); + if (Session.get("north_riichi") == true) { + Session.set("northPlayerRiichisWon", Number(Session.get("northPlayerRiichisWon")) + 1); + } + } + + if (loserWind == Constants.EAST) + Session.set("eastPlayerLosses", Number(Session.get("eastPlayerLosses")) + 1); + else if (loserWind == Constants.SOUTH) + Session.set("southPlayerLosses", Number(Session.get("southPlayerLosses")) + 1); + else if (loserWind == Constants.WEST) + Session.set("westPlayerLosses", Number(Session.get("westPlayerLosses")) + 1); + else if (loserWind == Constants.NORTH) + Session.set("northPlayerLosses", Number(Session.get("northPlayerLosses")) + 1); + + if (Session.get("east_riichi") == true) { + eastDelta -= 1000; + riichiSum++; + Session.set("east_riichi_sum", Number(Session.get("east_riichi_sum")) + 1); + } + if (Session.get("south_riichi") == true) { + southDelta -= 1000; + riichiSum++; + Session.set("south_riichi_sum", Number(Session.get("south_riichi_sum")) + 1); + } + if (Session.get("west_riichi") == true) { + westDelta -= 1000; + riichiSum++; + Session.set("west_riichi_sum", Number(Session.get("west_riichi_sum")) + 1); + } + if (Session.get("north_riichi") == true) { + northDelta -= 1000; + riichiSum++; + Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); + } + + allDelta = PointCalculationUtils.jpn.dealin_delta(points, fu, winnerWind, loserWind, riichiSum); + Session.set("free_riichi_sticks", 0); + + eastDelta += allDelta[Constants.EAST]; + southDelta += allDelta[Constants.SOUTH]; + westDelta += allDelta[Constants.WEST]; + northDelta += allDelta[Constants.NORTH]; + + pushHand(template, "dealin", eastDelta, southDelta, westDelta, northDelta); + + if (winnerWind == NewGameUtils.roundToDealerDirection(Session.get("current_round"))) + Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); + else { + Session.set("current_bonus", 0); + Session.set("current_round", Number(Session.get("current_round")) + 1) + } + + template.riichi_round_history.push({east: Session.get("east_riichi"), + south: Session.get("south_riichi"), + west: Session.get("west_riichi"), + north: Session.get("north_riichi")}); }; function push_selfdraw_hand(template) { - var points = Number(Session.get("current_points")); - var fu = Number(Session.get("current_fu")); - var dora = Number(Session.get("current_dora")); - var winnerWind = NewGameUtils.playerToDirection(Session.get("round_winner")); - var riichiSum = Session.get("free_riichi_sticks"); - var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; - - if (winnerWind == Constants.EAST) { - Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); - Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) + points); - Session.set("eastPlayerDoraSum", Number(Session.get("eastPlayerDoraSum")) + dora); - if (Session.get("east_riichi") == true) - Session.set("eastPlayerRiichisWon", Number(Session.get("eastPlayerRiichisWon")) + 1); - } - else if (winnerWind == Constants.SOUTH) { - Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) + 1); - Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) + points); - Session.set("southPlayerDoraSum", Number(Session.get("southPlayerDoraSum")) + dora); - if (Session.get("south_riichi") == true) - Session.set("southPlayerRiichisWon", Number(Session.get("southPlayerRiichisWon")) + 1); - } - else if (winnerWind == Constants.WEST) { - Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) + 1); - Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); - Session.set("westPlayerDoraSum", Number(Session.get("westPlayerDoraSum")) + dora); - if (Session.get("west_riichi") == true) - Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) + 1); - } - else if (winnerWind == Constants.NORTH) { - Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); - Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); - Session.set("northPlayerDoraSum", Number(Session.get("northPlayerDoraSum")) + dora); - if (Session.get("north_riichi") == true) - Session.set("northPlayerRiichisWon", Number(Session.get("northPlayerRiichisWon")) + 1); - } - - if (Session.get("east_riichi") == true) { - eastDelta -= 1000; - riichiSum++; - Session.set("east_riichi_sum", Number(Session.get("east_riichi_sum")) + 1); - } - if (Session.get("south_riichi") == true) { - southDelta -= 1000; - riichiSum++; - Session.set("south_riichi_sum", Number(Session.get("south_riichi_sum")) + 1); - } - if (Session.get("west_riichi") == true) { - westDelta -= 1000; - riichiSum++; - Session.set("west_riichi_sum", Number(Session.get("west_riichi_sum")) + 1); - } - if (Session.get("north_riichi") == true) { - northDelta -= 1000; - riichiSum++; - Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); - } - - allDeltas = PointCalculationUtils.jpn.selfdraw_delta(points, fu, winnerWind, riichiSum); - - eastDelta += allDeltas[Constants.EAST]; - southDelta += allDeltas[Constants.SOUTH]; - westDelta += allDeltas[Constants.WEST]; - northDelta += allDeltas[Constants.NORTH]; - - pushHand(template, "selfdraw", eastDelta, southDelta, westDelta, northDelta); - Session.set("free_riichi_sticks", 0); - - if (winnerWind == NewGameUtils.roundToDealerDirection(Session.get("current_round"))) - Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); - else { - Session.set("current_bonus", 0); - Session.set("current_round", Number(Session.get("current_round")) + 1); - } - - template.riichi_round_history.push({east: Session.get("east_riichi"), - south: Session.get("south_riichi"), - west: Session.get("west_riichi"), - north: Session.get("north_riichi")}); + var points = Number(Session.get("current_points")); + var fu = Number(Session.get("current_fu")); + var dora = Number(Session.get("current_dora")); + var winnerWind = NewGameUtils.playerToDirection(Session.get("round_winner")); + var riichiSum = Session.get("free_riichi_sticks"); + var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; + + if (winnerWind == Constants.EAST) { + Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); + Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) + points); + Session.set("eastPlayerDoraSum", Number(Session.get("eastPlayerDoraSum")) + dora); + if (Session.get("east_riichi") == true) + Session.set("eastPlayerRiichisWon", Number(Session.get("eastPlayerRiichisWon")) + 1); + } + else if (winnerWind == Constants.SOUTH) { + Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) + 1); + Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) + points); + Session.set("southPlayerDoraSum", Number(Session.get("southPlayerDoraSum")) + dora); + if (Session.get("south_riichi") == true) + Session.set("southPlayerRiichisWon", Number(Session.get("southPlayerRiichisWon")) + 1); + } + else if (winnerWind == Constants.WEST) { + Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) + 1); + Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); + Session.set("westPlayerDoraSum", Number(Session.get("westPlayerDoraSum")) + dora); + if (Session.get("west_riichi") == true) + Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) + 1); + } + else if (winnerWind == Constants.NORTH) { + Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); + Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); + Session.set("northPlayerDoraSum", Number(Session.get("northPlayerDoraSum")) + dora); + if (Session.get("north_riichi") == true) + Session.set("northPlayerRiichisWon", Number(Session.get("northPlayerRiichisWon")) + 1); + } + + if (Session.get("east_riichi") == true) { + eastDelta -= 1000; + riichiSum++; + Session.set("east_riichi_sum", Number(Session.get("east_riichi_sum")) + 1); + } + if (Session.get("south_riichi") == true) { + southDelta -= 1000; + riichiSum++; + Session.set("south_riichi_sum", Number(Session.get("south_riichi_sum")) + 1); + } + if (Session.get("west_riichi") == true) { + westDelta -= 1000; + riichiSum++; + Session.set("west_riichi_sum", Number(Session.get("west_riichi_sum")) + 1); + } + if (Session.get("north_riichi") == true) { + northDelta -= 1000; + riichiSum++; + Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); + } + + allDeltas = PointCalculationUtils.jpn.selfdraw_delta(points, fu, winnerWind, riichiSum); + + eastDelta += allDeltas[Constants.EAST]; + southDelta += allDeltas[Constants.SOUTH]; + westDelta += allDeltas[Constants.WEST]; + northDelta += allDeltas[Constants.NORTH]; + + pushHand(template, "selfdraw", eastDelta, southDelta, westDelta, northDelta); + Session.set("free_riichi_sticks", 0); + + if (winnerWind == NewGameUtils.roundToDealerDirection(Session.get("current_round"))) + Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); + else { + Session.set("current_bonus", 0); + Session.set("current_round", Number(Session.get("current_round")) + 1); + } + + template.riichi_round_history.push({east: Session.get("east_riichi"), + south: Session.get("south_riichi"), + west: Session.get("west_riichi"), + north: Session.get("north_riichi")}); }; function push_nowin_hand(template) { - var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; - var tenpaiSum = 0, tenpaiWin, tenpaiLose, riichiSum = 0; - - if (Session.get("east_tenpai") == true) tenpaiSum++; - if (Session.get("south_tenpai") == true) tenpaiSum++; - if (Session.get("west_tenpai") == true) tenpaiSum++; - if (Session.get("north_tenpai") == true) tenpaiSum++; - - tenpaiWin = Constants.JPN_TENPAI_PAYOUT / tenpaiSum; - tenpaiLose = -Constants.JPN_TENPAI_PAYOUT / (4 - tenpaiSum); - if (tenpaiSum != 4 && tenpaiSum != 0) { - eastDelta += Session.get("east_tenpai") == true ? tenpaiWin : tenpaiLose; - southDelta += Session.get("south_tenpai") == true ? tenpaiWin : tenpaiLose; - westDelta += Session.get("west_tenpai") == true ? tenpaiWin : tenpaiLose; - northDelta += Session.get("north_tenpai") == true ? tenpaiWin : tenpaiLose; - } - - if (Session.get("east_riichi") == true) { - eastDelta -= 1000; - riichiSum++; - Session.set("east_riichi_sum", Number(Session.get("east_riichi_sum")) + 1); - } - if (Session.get("south_riichi") == true) { - southDelta -= 1000; - riichiSum++; - Session.set("south_riichi_sum", Number(Session.get("south_riichi_sum")) + 1); - } - if (Session.get("west_riichi") == true) { - westDelta -= 1000; - riichiSum++; - Session.set("west_riichi_sum", Number(Session.get("west_riichi_sum")) + 1); - } - if (Session.get("north_riichi") == true) { - northDelta -= 1000; - riichiSum++; - Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); - } - - Session.set("free_riichi_sticks", Number(Session.get("free_riichi_sticks")) + riichiSum); - - pushHand(template, "nowin", eastDelta, southDelta, westDelta, northDelta); - - if (Session.get(NewGameUtils.roundToDealerDirection(Session.get("current_round")) + "_tenpai") == true) - Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); - else { - Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); - Session.set("current_round", Number(Session.get("current_round")) + 1); - } - - template.riichi_round_history.push({east: Session.get("east_riichi"), - south: Session.get("south_riichi"), - west: Session.get("west_riichi"), - north: Session.get("north_riichi")}); + var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; + var tenpaiSum = 0, tenpaiWin, tenpaiLose, riichiSum = 0; + + if (Session.get("east_tenpai") == true) tenpaiSum++; + if (Session.get("south_tenpai") == true) tenpaiSum++; + if (Session.get("west_tenpai") == true) tenpaiSum++; + if (Session.get("north_tenpai") == true) tenpaiSum++; + + tenpaiWin = Constants.JPN_TENPAI_PAYOUT / tenpaiSum; + tenpaiLose = -Constants.JPN_TENPAI_PAYOUT / (4 - tenpaiSum); + if (tenpaiSum != 4 && tenpaiSum != 0) { + eastDelta += Session.get("east_tenpai") == true ? tenpaiWin : tenpaiLose; + southDelta += Session.get("south_tenpai") == true ? tenpaiWin : tenpaiLose; + westDelta += Session.get("west_tenpai") == true ? tenpaiWin : tenpaiLose; + northDelta += Session.get("north_tenpai") == true ? tenpaiWin : tenpaiLose; + } + + if (Session.get("east_riichi") == true) { + eastDelta -= 1000; + riichiSum++; + Session.set("east_riichi_sum", Number(Session.get("east_riichi_sum")) + 1); + } + if (Session.get("south_riichi") == true) { + southDelta -= 1000; + riichiSum++; + Session.set("south_riichi_sum", Number(Session.get("south_riichi_sum")) + 1); + } + if (Session.get("west_riichi") == true) { + westDelta -= 1000; + riichiSum++; + Session.set("west_riichi_sum", Number(Session.get("west_riichi_sum")) + 1); + } + if (Session.get("north_riichi") == true) { + northDelta -= 1000; + riichiSum++; + Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); + } + + Session.set("free_riichi_sticks", Number(Session.get("free_riichi_sticks")) + riichiSum); + + pushHand(template, "nowin", eastDelta, southDelta, westDelta, northDelta); + + if (Session.get(NewGameUtils.roundToDealerDirection(Session.get("current_round")) + "_tenpai") == true) + Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); + else { + Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); + Session.set("current_round", Number(Session.get("current_round")) + 1); + } + + template.riichi_round_history.push({east: Session.get("east_riichi"), + south: Session.get("south_riichi"), + west: Session.get("west_riichi"), + north: Session.get("north_riichi")}); }; function push_restart_hand(template) { - var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; - var riichiSum = 0; - - if (Session.get("east_riichi") == true) { - eastDelta -= 1000; - riichiSum++; - Session.set("east_riichi_sum", Number(Session.get("east_riichi_sum")) + 1); - } - if (Session.get("south_riichi") == true) { - southDelta -= 1000; - riichiSum++; - Session.set("south_riichi_sum", Number(Session.get("south_riichi_sum")) + 1); - } - if (Session.get("west_riichi") == true) { - westDelta -= 1000; - riichiSum++; - Session.set("west_riichi_sum", Number(Session.get("west_riichi_sum")) + 1); - } - if (Session.get("north_riichi") == true) { - northDelta -= 1000; - riichiSum++; - Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); - } - - Session.set("free_riichi_sticks", Number(Session.get("free_riichi_sticks")) + riichiSum); - - pushHand(template, "restart", eastDelta, southDelta, westDelta, northDelta); - - Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); - - template.riichi_round_history.push({east: Session.get("east_riichi"), - south: Session.get("south_riichi"), - west: Session.get("west_riichi"), - north: Session.get("north_riichi")}); + var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; + var riichiSum = 0; + + if (Session.get("east_riichi") == true) { + eastDelta -= 1000; + riichiSum++; + Session.set("east_riichi_sum", Number(Session.get("east_riichi_sum")) + 1); + } + if (Session.get("south_riichi") == true) { + southDelta -= 1000; + riichiSum++; + Session.set("south_riichi_sum", Number(Session.get("south_riichi_sum")) + 1); + } + if (Session.get("west_riichi") == true) { + westDelta -= 1000; + riichiSum++; + Session.set("west_riichi_sum", Number(Session.get("west_riichi_sum")) + 1); + } + if (Session.get("north_riichi") == true) { + northDelta -= 1000; + riichiSum++; + Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); + } + + Session.set("free_riichi_sticks", Number(Session.get("free_riichi_sticks")) + riichiSum); + + pushHand(template, "restart", eastDelta, southDelta, westDelta, northDelta); + + Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); + + template.riichi_round_history.push({east: Session.get("east_riichi"), + south: Session.get("south_riichi"), + west: Session.get("west_riichi"), + north: Session.get("north_riichi")}); }; function push_mistake_hand(template) { - var loserWind = NewGameUtils.playerToDirection(Session.get("round_loser")); + var loserWind = NewGameUtils.playerToDirection(Session.get("round_loser")); - let allDeltas = PointCalculationUtils.jpn.mistake_delta(loserWind); + let allDeltas = PointCalculationUtils.jpn.mistake_delta(loserWind); - let eastDelta = allDeltas[Constants.EAST]; - let southDelta = allDeltas[Constants.SOUTH]; - let westDelta = allDeltas[Constants.WEST]; - let northDelta = allDeltas[Constants.NORTH]; + let eastDelta = allDeltas[Constants.EAST]; + let southDelta = allDeltas[Constants.SOUTH]; + let westDelta = allDeltas[Constants.WEST]; + let northDelta = allDeltas[Constants.NORTH]; - if (loserWind == Constants.EAST) Session.set("eastFuckupTotal", Number(Session.get("eastFuckupTotal")) + 1); - else if (loserWind == Constants.SOUTH) Session.set("southFuckupTotal", Number(Session.get("southFuckupTotal")) + 1); - else if (loserWind == Constants.WEST) Session.set("westFuckupTotal", Number(Session.get("westFuckupTotal")) + 1); - else if (loserWind == Constants.NORTH) Session.set("northFuckupTotal", Number(Session.get("northFuckupTotal")) + 1); + if (loserWind == Constants.EAST) Session.set("eastFuckupTotal", Number(Session.get("eastFuckupTotal")) + 1); + else if (loserWind == Constants.SOUTH) Session.set("southFuckupTotal", Number(Session.get("southFuckupTotal")) + 1); + else if (loserWind == Constants.WEST) Session.set("westFuckupTotal", Number(Session.get("westFuckupTotal")) + 1); + else if (loserWind == Constants.NORTH) Session.set("northFuckupTotal", Number(Session.get("northFuckupTotal")) + 1); - pushHand(template, "fuckup", eastDelta, southDelta, westDelta, northDelta); + pushHand(template, "fuckup", eastDelta, southDelta, westDelta, northDelta); - template.riichi_round_history.push({east: false, - south: false, - west: false, - north: false}); + template.riichi_round_history.push({east: false, + south: false, + west: false, + north: false}); }; function push_split_pao_hand(template) { - var points = Number(Session.get("current_points")); - var fu = Number(Session.get("current_fu")); - var dora = Number(Session.get("current_dora")); - var winnerWind = NewGameUtils.playerToDirection(Session.get("round_winner")); - var loserWind = NewGameUtils.playerToDirection(Session.get("round_loser")); - var paoWind = NewGameUtils.playerToDirection(Session.get("round_pao_player")); - var riichiSum = Session.get("free_riichi_sticks"); - var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; - - if (winnerWind == Constants.EAST) { - Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); - Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) + points); - Session.set("eastPlayerDoraSum", Number(Session.get("eastPlayerDoraSum")) + dora); - if (Session.get("east_riichi") == true) - Session.set("eastPlayerRiichisWon", Number(Session.get("eastPlayerRiichisWon")) + 1); - } - else if (winnerWind == Constants.SOUTH) { - Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) + 1); - Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) + points); - Session.set("southPlayerDoraSum", Number(Session.get("southPlayerDoraSum")) + dora); - if (Session.get("south_riichi") == true) - Session.set("southPlayerRiichisWon", Number(Session.get("southPlayerRiichisWon")) + 1); - } - else if (winnerWind == Constants.WEST) { - Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) + 1); - Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); - Session.set("westPlayerDoraSum", Number(Session.get("westPlayerDoraSum")) + dora); - if (Session.get("west_riichi") == true) - Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) + 1); - } - else if (winnerWind == Constants.NORTH) { - Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); - Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); - Session.set("northPlayerDoraSum", Number(Session.get("northPlayerDoraSum")) + dora); - if (Session.get("north_riichi") == true) - Session.set("northPlayerRiichisWon", Number(Session.get("northPlayerRiichisWon")) + 1); - } - - if (loserWind == Constants.EAST || paoWind == Constants.EAST) - Session.set("eastPlayerLosses", Number(Session.get("eastPlayerLosses")) + 1); - else if (loserWind == Constants.SOUTH || paoWind == Constants.SOUTH) - Session.set("southPlayerLosses", Number(Session.get("southPlayerLosses")) + 1); - else if (loserWind == Constants.WEST || paoWind == Constants.WEST) - Session.set("westPlayerLosses", Number(Session.get("westPlayerLosses")) + 1); - else if (loserWind == Constants.NORTH || paoWind == Constants.NORTH) - Session.set("northPlayerLosses", Number(Session.get("northPlayerLosses")) + 1); - - if (Session.get("east_riichi") == true) { - eastDelta -= 1000; - riichiSum++; - Session.set("east_riichi_sum", Number(Session.get("east_riichi_sum")) + 1); - } - if (Session.get("south_riichi") == true) { - southDelta -= 1000; - riichiSum++; - Session.set("south_riichi_sum", Number(Session.get("south_riichi_sum")) + 1); - } - if (Session.get("west_riichi") == true) { - westDelta -= 1000; - riichiSum++; - Session.set("west_riichi_sum", Number(Session.get("west_riichi_sum")) + 1); - } - if (Session.get("north_riichi") == true) { - northDelta -= 1000; - riichiSum++; - Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); - } - - var value = PointCalculationUtils.jpn.dealin_delta(points, fu, winnerWind, loserWind, 0)[loserWind]; - - if (((value / 2 ) % 100) == 50) { - value += 100; - } - - switch (winnerWind) { - case Constants.EAST: - eastDelta += value; - eastDelta += (riichiSum * 1000); - break; - case Constants.SOUTH: - southDelta += value; - southDelta += (riichiSum * 1000); - break; - case Constants.WEST: - westDelta += value; - westDelta += (riichiSum * 1000); - break; - case Constants.NORTH: - northDelta += value; - northDelta += (riichiSum * 1000); - break; - } - - Session.set("free_riichi_sticks", 0); - - if (loserWind == Constants.EAST || paoWind == Constants.EAST) eastDelta -= value / 2; - if (loserWind == Constants.SOUTH || paoWind == Constants.SOUTH) southDelta -= value / 2; - if (loserWind == Constants.WEST || paoWind == Constants.WEST) westDelta -= value / 2; - if (loserWind == Constants.NORTH || paoWind == Constants.NORTH) northDelta -= value / 2; - - pushHand(template, "selfdraw", eastDelta, southDelta, westDelta, northDelta); - - if (winnerWind == NewGameUtils.roundToDealerDirection(Session.get("current_round"))) - Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); - else { - Session.set("current_bonus", 0); - Session.set("current_round", Number(Session.get("current_round")) + 1) - } - - template.riichi_round_history.push({east: Session.get("east_riichi"), - south: Session.get("south_riichi"), - west: Session.get("west_riichi"), - north: Session.get("north_riichi")}); + var points = Number(Session.get("current_points")); + var fu = Number(Session.get("current_fu")); + var dora = Number(Session.get("current_dora")); + var winnerWind = NewGameUtils.playerToDirection(Session.get("round_winner")); + var loserWind = NewGameUtils.playerToDirection(Session.get("round_loser")); + var paoWind = NewGameUtils.playerToDirection(Session.get("round_pao_player")); + var riichiSum = Session.get("free_riichi_sticks"); + var eastDelta = 0, southDelta = 0, westDelta = 0, northDelta = 0; + + if (winnerWind == Constants.EAST) { + Session.set("eastPlayerWins", Number(Session.get("eastPlayerWins")) + 1); + Session.set("eastPlayerPointsWon", Number(Session.get("eastPlayerPointsWon")) + points); + Session.set("eastPlayerDoraSum", Number(Session.get("eastPlayerDoraSum")) + dora); + if (Session.get("east_riichi") == true) + Session.set("eastPlayerRiichisWon", Number(Session.get("eastPlayerRiichisWon")) + 1); + } + else if (winnerWind == Constants.SOUTH) { + Session.set("southPlayerWins", Number(Session.get("southPlayerWins")) + 1); + Session.set("southPlayerPointsWon", Number(Session.get("southPlayerPointsWon")) + points); + Session.set("southPlayerDoraSum", Number(Session.get("southPlayerDoraSum")) + dora); + if (Session.get("south_riichi") == true) + Session.set("southPlayerRiichisWon", Number(Session.get("southPlayerRiichisWon")) + 1); + } + else if (winnerWind == Constants.WEST) { + Session.set("westPlayerWins", Number(Session.get("westPlayerWins")) + 1); + Session.set("westPlayerPointsWon", Number(Session.get("westPlayerPointsWon")) + points); + Session.set("westPlayerDoraSum", Number(Session.get("westPlayerDoraSum")) + dora); + if (Session.get("west_riichi") == true) + Session.set("westPlayerRiichisWon", Number(Session.get("westPlayerRiichisWon")) + 1); + } + else if (winnerWind == Constants.NORTH) { + Session.set("northPlayerWins", Number(Session.get("northPlayerWins")) + 1); + Session.set("northPlayerPointsWon", Number(Session.get("northPlayerPointsWon")) + points); + Session.set("northPlayerDoraSum", Number(Session.get("northPlayerDoraSum")) + dora); + if (Session.get("north_riichi") == true) + Session.set("northPlayerRiichisWon", Number(Session.get("northPlayerRiichisWon")) + 1); + } + + if (loserWind == Constants.EAST || paoWind == Constants.EAST) + Session.set("eastPlayerLosses", Number(Session.get("eastPlayerLosses")) + 1); + else if (loserWind == Constants.SOUTH || paoWind == Constants.SOUTH) + Session.set("southPlayerLosses", Number(Session.get("southPlayerLosses")) + 1); + else if (loserWind == Constants.WEST || paoWind == Constants.WEST) + Session.set("westPlayerLosses", Number(Session.get("westPlayerLosses")) + 1); + else if (loserWind == Constants.NORTH || paoWind == Constants.NORTH) + Session.set("northPlayerLosses", Number(Session.get("northPlayerLosses")) + 1); + + if (Session.get("east_riichi") == true) { + eastDelta -= 1000; + riichiSum++; + Session.set("east_riichi_sum", Number(Session.get("east_riichi_sum")) + 1); + } + if (Session.get("south_riichi") == true) { + southDelta -= 1000; + riichiSum++; + Session.set("south_riichi_sum", Number(Session.get("south_riichi_sum")) + 1); + } + if (Session.get("west_riichi") == true) { + westDelta -= 1000; + riichiSum++; + Session.set("west_riichi_sum", Number(Session.get("west_riichi_sum")) + 1); + } + if (Session.get("north_riichi") == true) { + northDelta -= 1000; + riichiSum++; + Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); + } + + var value = PointCalculationUtils.jpn.dealin_delta(points, fu, winnerWind, loserWind, 0)[loserWind]; + + if (((value / 2 ) % 100) == 50) { + value += 100; + } + + switch (winnerWind) { + case Constants.EAST: + eastDelta += value; + eastDelta += (riichiSum * 1000); + break; + case Constants.SOUTH: + southDelta += value; + southDelta += (riichiSum * 1000); + break; + case Constants.WEST: + westDelta += value; + westDelta += (riichiSum * 1000); + break; + case Constants.NORTH: + northDelta += value; + northDelta += (riichiSum * 1000); + break; + } + + Session.set("free_riichi_sticks", 0); + + if (loserWind == Constants.EAST || paoWind == Constants.EAST) eastDelta -= value / 2; + if (loserWind == Constants.SOUTH || paoWind == Constants.SOUTH) southDelta -= value / 2; + if (loserWind == Constants.WEST || paoWind == Constants.WEST) westDelta -= value / 2; + if (loserWind == Constants.NORTH || paoWind == Constants.NORTH) northDelta -= value / 2; + + pushHand(template, "selfdraw", eastDelta, southDelta, westDelta, northDelta); + + if (winnerWind == NewGameUtils.roundToDealerDirection(Session.get("current_round"))) + Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); + else { + Session.set("current_bonus", 0); + Session.set("current_round", Number(Session.get("current_round")) + 1) + } + + template.riichi_round_history.push({east: Session.get("east_riichi"), + south: Session.get("south_riichi"), + west: Session.get("west_riichi"), + north: Session.get("north_riichi")}); }; function pushHand(template, handType, eastDelta, southDelta, westDelta, northDelta) { - template.hands.push( - { - handType: handType, - round: Session.get("current_round"), - bonus: Session.get("current_bonus"), - points: Session.get("current_points"), - fu: Session.get("current_fu"), - dora: Session.get("current_dora"), - eastDelta: eastDelta, - southDelta: southDelta, - westDelta: westDelta, - northDelta: northDelta, - }); - - Session.set("east_score", Number(Session.get("east_score")) + eastDelta); - Session.set("south_score", Number(Session.get("south_score")) + southDelta); - Session.set("west_score", Number(Session.get("west_score")) + westDelta); - Session.set("north_score", Number(Session.get("north_score")) + northDelta); + template.hands.push( + { + handType: handType, + round: Session.get("current_round"), + bonus: Session.get("current_bonus"), + points: Session.get("current_points"), + fu: Session.get("current_fu"), + dora: Session.get("current_dora"), + eastDelta: eastDelta, + southDelta: southDelta, + westDelta: westDelta, + northDelta: northDelta, + }); + + Session.set("east_score", Number(Session.get("east_score")) + eastDelta); + Session.set("south_score", Number(Session.get("south_score")) + southDelta); + Session.set("west_score", Number(Session.get("west_score")) + westDelta); + Session.set("north_score", Number(Session.get("north_score")) + northDelta); }; function setAllGUIRiichisFalse() { - Session.set("east_riichi", false); - Session.set("south_riichi", false); - Session.set("west_riichi", false); - Session.set("north_riichi", false); + Session.set("east_riichi", false); + Session.set("south_riichi", false); + Session.set("west_riichi", false); + Session.set("north_riichi", false); }; Template.jpn_points.events({ - 'change select[name="points"]'(event) { - Session.set("current_points", event.target.value); - } + 'change select[name="points"]'(event) { + Session.set("current_points", event.target.value); + } }); Template.jpn_fu.events({ - 'change select[name="fu"]'(event) { - Session.set("current_fu", event.target.value); - } + 'change select[name="fu"]'(event) { + Session.set("current_fu", event.target.value); + } }); Template.jpn_dora.events({ - 'change select[name="dora"]'(event) { - Session.set("current_dora", event.target.value); - } + 'change select[name="dora"]'(event) { + Session.set("current_dora", event.target.value); + } }); From 5aff7f1a670b09e2726bdacfcd12645492177282 Mon Sep 17 00:00:00 2001 From: TwelveNights Date: Sun, 20 Nov 2016 23:12:02 -0800 Subject: [PATCH 07/10] Update code based on review --- imports/api/Constants.js | 9 ++++ imports/api/NewGameUtils.js | 27 ++++++----- imports/api/PointCalculationUtils.js | 70 +++++++++++++++++++--------- imports/ui/JapaneseNewGame.js | 8 ++-- 4 files changed, 74 insertions(+), 40 deletions(-) diff --git a/imports/api/Constants.js b/imports/api/Constants.js index 0cb9fa2..40c68b9 100644 --- a/imports/api/Constants.js +++ b/imports/api/Constants.js @@ -21,6 +21,15 @@ export const Constants = { // Base points of a mangan JPN_MANGAN_BASE_POINTS: 2000, + // Points for every bonus round + BONUS_POINTS: 300, + + // Points for every riichi stick + RIICHI_POINTS: 1000, + + // Points paid for a mistake + MISTAKE_POINTS: 12000, + // Placeholder value to establish a player select button // That has no player selected NO_PERSON: "no one", diff --git a/imports/api/NewGameUtils.js b/imports/api/NewGameUtils.js index 1e625ae..4dfb943 100644 --- a/imports/api/NewGameUtils.js +++ b/imports/api/NewGameUtils.js @@ -69,23 +69,23 @@ export var NewGameUtils = { // Helper function to ensure all players are selected allPlayersSelected() { return (Session.get("current_east") != Constants.DEFAULT_EAST && - Session.get("current_south") != Constants.DEFAULT_SOUTH && - Session.get("current_west") != Constants.DEFAULT_WEST && - Session.get("current_north") != Constants.DEFAULT_NORTH); + Session.get("current_south") != Constants.DEFAULT_SOUTH && + Session.get("current_west") != Constants.DEFAULT_WEST && + Session.get("current_north") != Constants.DEFAULT_NORTH); }, someoneBankrupt() { return (Session.get("east_score") < 0 || - Session.get("south_score") < 0 || - Session.get("west_score") < 0 || - Session.get("north_score") < 0); + Session.get("south_score") < 0 || + Session.get("west_score") < 0 || + Session.get("north_score") < 0); }, someoneAboveMinimum(minimum) { return (Session.get("east_score") >= minimum || - Session.get("south_score") >= minimum || - Session.get("west_score") >= minimum || - Session.get("north_score") >= minimum); + Session.get("south_score") >= minimum || + Session.get("west_score") >= minimum || + Session.get("north_score") >= minimum); }, /** @@ -123,13 +123,12 @@ export var NewGameUtils = { let westRoundOver = Session.get("current_round") > 12; // End condition where game has reached the end of south round with at least one player above minimum let someoneAboveMinimum = Session.get("current_round") > 8 && - this.someoneAboveMinimum(Constants.JPN_END_POINTS); + this.someoneAboveMinimum(Constants.JPN_END_POINTS); // End condition where north player reaches first place after winning on last round let dealerFirstAndAboveMinimum = Session.get("current_round") == 8 && - Session.get("current_bonus") > 0 && - this.getDirectionScore("north") >= Constants.JPN_END_POINTS && - - this.getFirstPlace() == "north"; + Session.get("current_bonus") > 0 && + this.getDirectionScore("north") >= Constants.JPN_END_POINTS && + this.getFirstPlace() == "north"; return someoneBankrupt || westRoundOver || someoneAboveMinimum || dealerFirstAndAboveMinimum; }, diff --git a/imports/api/PointCalculationUtils.js b/imports/api/PointCalculationUtils.js index 66af4db..fcce845 100644 --- a/imports/api/PointCalculationUtils.js +++ b/imports/api/PointCalculationUtils.js @@ -2,20 +2,28 @@ import { Constants } from './Constants'; import { NewGameUtils } from './NewGameUtils'; export var PointCalculationUtils = { - jpn: { - dealin_delta, - selfdraw_delta, - mistake_delta - } + jpn_dealin_delta, + jpn_selfdraw_delta, + jpn_mistake_delta }; +/** + * Calculates the change in points as a result of a deal-in hand + * Uses the formula from https://en.wikipedia.org/wiki/Japanese_Mahjong_scoring_rules + * + * @return {Object} containing the point difference for each seat + */ function dealin_delta(points, fu, winnerWind, loserWind, riichiSticks) { let winds = {}; winds[Constants.EAST] = winds[Constants.SOUTH] = winds[Constants.WEST] = winds[Constants.NORTH] = 0; let basicPoints; + let currentBonus = Number(Session.get("current_bonus")) * Constants.BONUS_POINTS; + + // The multiplier is for whether or not it's a dealer victory let multiplier = (winnerWind != NewGameUtils.roundToDealerDirection(Number(Session.get("current_round")))) ? 4 : 6; + // Check to see if you have to count basic points if (points < 5) { if (fu == 20 || (points == 1 && fu == 25)) { return 0; // Issue Protection @@ -26,21 +34,34 @@ function dealin_delta(points, fu, winnerWind, loserWind, riichiSticks) { } } else { basicPoints = manganValue(points) * multiplier }; - let currentBonus = Number(Session.get("current_bonus")) * 300; - winds[winnerWind] = basicPoints + currentBonus + (riichiSticks * 1000); + winds[winnerWind] = basicPoints + currentBonus + (riichiSticks * Constants.RIICHI_POINTS); winds[loserWind] = -(basicPoints + currentBonus); Session.set("free_riichi_sticks", 0); return winds; }; + +/** + * Calculates the change in points as a result of a self-drawn hand + * Uses the formula from https://en.wikipedia.org/wiki/Japanese_Mahjong_scoring_rules + * + * @return {Object} containing the point difference for each seat + */ function selfdraw_delta(points, fu, winnerWind, riichiSticks) { let winds = {}; - winds[Constants.EAST] = winds[Constants.SOUTH] = winds[Constants.WEST] = winds[Constants.NORTH] = 0; + let dealerWind = NewGameUtils.roundToDealerDirection(Number(Session.get("current_round"))); let basicPoints; - let dealerWind = NewGameUtils.roundToDealerDirection(Number(Session.get("current_round"))); + let nonDealerPays; + let dealerPays; + let currentBonus = Number(Session.get("currentBonus")); + let individualBonusPayout = Constants.BONUS_POINTS / 3; + + winds[Constants.EAST] = winds[Constants.SOUTH] = winds[Constants.WEST] = winds[Constants.NORTH] = 0; + + // Check to see if you have to count basic points if (points < 5) { if (points == 1 && (fu == 20 || fu == 25)) { return 0; // Issue Protection @@ -50,43 +71,48 @@ function selfdraw_delta(points, fu, winnerWind, riichiSticks) { } } else { basicPoints = manganValue(points); } - let nonDealerPays = Math.ceil(basicPoints / 100 * (dealerWind == winnerWind ? 2 : 1)) * 100; - let dealerPays = Math.ceil(basicPoints / 100 * 2) * 100; - - let currentBonus = Number(Session.get("current_bonus")); + nonDealerPays = Math.ceil(basicPoints / 100 * (dealerWind == winnerWind ? 2 : 1)) * 100; + dealerPays = Math.ceil(basicPoints / 100 * 2) * 100; for (let w in winds) { if (winnerWind != dealerWind) { if (w == dealerWind) { - winds[w] = -(dealerPays + currentBonus * 100); + winds[w] = -(dealerPays + currentBonus * individualBonusPayout); } else if (w == winnerWind) { - winds[w] = dealerPays + (nonDealerPays * 2) + (currentBonus * 300) + (riichiSticks * 1000); + winds[w] = dealerPays + (nonDealerPays * 2) + (currentBonus * Constants.BONUS_POINTS) + (riichiSticks * Constants.RIICHI_POINTS); } else { - winds[w] = -(nonDealerPays + currentBonus * 100); + winds[w] = -(nonDealerPays + currentBonus * individualBonusPayout); } } else { if (w == winnerWind) { - winds[w] = (nonDealerPays * 3) + (currentBonus * 300) + (riichiSticks * 1000); + winds[w] = (nonDealerPays * 3) + (currentBonus * Constants.BONUS_POINTS) + (riichiSticks * Constants.RIICHI_POINTS); } else { - winds[w] = -(nonDealerPays + (currentBonus * 100)); - } + winds[w] = -(nonDealerPays + (currentBonus * individualBonusPayout)); } + } } Session.set("free_riichi_sticks", 0); return winds; }; +/** + * Calculates the point difference as the result of a mistaken hand + * @return {Object} containing the point difference for each seat + */ function mistake_delta(loser) { let winds = {}; - winds[Constants.EAST] = winds[Constants.SOUTH] = winds[Constants.WEST] = winds[Constants.NORTH] = 4000; + winds[Constants.EAST] = winds[Constants.SOUTH] = winds[Constants.WEST] = winds[Constants.NORTH] = Constants.MISTAKE_POINTS / 3; - winds[loser] = -12000; + winds[loser] = -Constants.MISTAKE_POINTS; return winds; }; -// Calculates the total base points from han + dora values +/** + * Calculates the total base points from han + dora values for high value hands + * @returns {Number} representing number of base points as the result of a certain point threshold + */ function manganValue(points) { switch(points) { case 5: return Constants.JPN_MANGAN_BASE_POINTS; diff --git a/imports/ui/JapaneseNewGame.js b/imports/ui/JapaneseNewGame.js index ab9a059..36581b4 100644 --- a/imports/ui/JapaneseNewGame.js +++ b/imports/ui/JapaneseNewGame.js @@ -802,7 +802,7 @@ function push_dealin_hand(template) { Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); } - allDelta = PointCalculationUtils.jpn.dealin_delta(points, fu, winnerWind, loserWind, riichiSum); + allDelta = PointCalculationUtils.dealin_delta(points, fu, winnerWind, loserWind, riichiSum); Session.set("free_riichi_sticks", 0); eastDelta += allDelta[Constants.EAST]; @@ -883,7 +883,7 @@ function push_selfdraw_hand(template) { Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); } - allDeltas = PointCalculationUtils.jpn.selfdraw_delta(points, fu, winnerWind, riichiSum); + allDeltas = PointCalculationUtils.jpn_selfdraw_delta(points, fu, winnerWind, riichiSum); eastDelta += allDeltas[Constants.EAST]; southDelta += allDeltas[Constants.SOUTH]; @@ -1002,7 +1002,7 @@ function push_restart_hand(template) { function push_mistake_hand(template) { var loserWind = NewGameUtils.playerToDirection(Session.get("round_loser")); - let allDeltas = PointCalculationUtils.jpn.mistake_delta(loserWind); + let allDeltas = PointCalculationUtils.jpn_mistake_delta(loserWind); let eastDelta = allDeltas[Constants.EAST]; let southDelta = allDeltas[Constants.SOUTH]; @@ -1091,7 +1091,7 @@ function push_split_pao_hand(template) { Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); } - var value = PointCalculationUtils.jpn.dealin_delta(points, fu, winnerWind, loserWind, 0)[loserWind]; + var value = PointCalculationUtils.jpn_dealin_delta(points, fu, winnerWind, loserWind, 0)[loserWind]; if (((value / 2 ) % 100) == 50) { value += 100; From 149c1b17afc37c6f7aa0e2b87498cc383ee606e8 Mon Sep 17 00:00:00 2001 From: TwelveNights Date: Sun, 20 Nov 2016 23:15:36 -0800 Subject: [PATCH 08/10] Rename functions --- imports/api/PointCalculationUtils.js | 6 +++--- imports/ui/JapaneseNewGame.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/imports/api/PointCalculationUtils.js b/imports/api/PointCalculationUtils.js index fcce845..54bac54 100644 --- a/imports/api/PointCalculationUtils.js +++ b/imports/api/PointCalculationUtils.js @@ -13,7 +13,7 @@ export var PointCalculationUtils = { * * @return {Object} containing the point difference for each seat */ -function dealin_delta(points, fu, winnerWind, loserWind, riichiSticks) { +function jpn_dealin_delta(points, fu, winnerWind, loserWind, riichiSticks) { let winds = {}; winds[Constants.EAST] = winds[Constants.SOUTH] = winds[Constants.WEST] = winds[Constants.NORTH] = 0; @@ -48,7 +48,7 @@ function dealin_delta(points, fu, winnerWind, loserWind, riichiSticks) { * * @return {Object} containing the point difference for each seat */ -function selfdraw_delta(points, fu, winnerWind, riichiSticks) { +function jpn_selfdraw_delta(points, fu, winnerWind, riichiSticks) { let winds = {}; let dealerWind = NewGameUtils.roundToDealerDirection(Number(Session.get("current_round"))); @@ -100,7 +100,7 @@ function selfdraw_delta(points, fu, winnerWind, riichiSticks) { * Calculates the point difference as the result of a mistaken hand * @return {Object} containing the point difference for each seat */ -function mistake_delta(loser) { +function jpn_mistake_delta(loser) { let winds = {}; winds[Constants.EAST] = winds[Constants.SOUTH] = winds[Constants.WEST] = winds[Constants.NORTH] = Constants.MISTAKE_POINTS / 3; diff --git a/imports/ui/JapaneseNewGame.js b/imports/ui/JapaneseNewGame.js index 36581b4..e267cdd 100644 --- a/imports/ui/JapaneseNewGame.js +++ b/imports/ui/JapaneseNewGame.js @@ -802,7 +802,7 @@ function push_dealin_hand(template) { Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); } - allDelta = PointCalculationUtils.dealin_delta(points, fu, winnerWind, loserWind, riichiSum); + allDelta = PointCalculationUtils.jpn_dealin_delta(points, fu, winnerWind, loserWind, riichiSum); Session.set("free_riichi_sticks", 0); eastDelta += allDelta[Constants.EAST]; From c2a13da74dafbc75c2e0bd65f30d5bfff0090d65 Mon Sep 17 00:00:00 2001 From: TwelveNights Date: Sun, 20 Nov 2016 23:54:15 -0800 Subject: [PATCH 09/10] Submit one more set of changes based off of review --- imports/api/Constants.js | 6 ++-- imports/api/NewGameUtils.js | 8 ++--- imports/api/PointCalculationUtils.js | 49 ++++++++++++++++++---------- imports/ui/JapaneseNewGame.js | 10 +++--- 4 files changed, 44 insertions(+), 29 deletions(-) diff --git a/imports/api/Constants.js b/imports/api/Constants.js index 40c68b9..ea84d53 100644 --- a/imports/api/Constants.js +++ b/imports/api/Constants.js @@ -22,13 +22,13 @@ export const Constants = { JPN_MANGAN_BASE_POINTS: 2000, // Points for every bonus round - BONUS_POINTS: 300, + JPN_BONUS_POINTS: 300, // Points for every riichi stick - RIICHI_POINTS: 1000, + JPN_RIICHI_POINTS: 1000, // Points paid for a mistake - MISTAKE_POINTS: 12000, + JPN_MISTAKE_POINTS: 12000, // Placeholder value to establish a player select button // That has no player selected diff --git a/imports/api/NewGameUtils.js b/imports/api/NewGameUtils.js index 4dfb943..a9f8a21 100644 --- a/imports/api/NewGameUtils.js +++ b/imports/api/NewGameUtils.js @@ -123,12 +123,12 @@ export var NewGameUtils = { let westRoundOver = Session.get("current_round") > 12; // End condition where game has reached the end of south round with at least one player above minimum let someoneAboveMinimum = Session.get("current_round") > 8 && - this.someoneAboveMinimum(Constants.JPN_END_POINTS); + this.someoneAboveMinimum(Constants.JPN_END_POINTS); // End condition where north player reaches first place after winning on last round let dealerFirstAndAboveMinimum = Session.get("current_round") == 8 && - Session.get("current_bonus") > 0 && - this.getDirectionScore("north") >= Constants.JPN_END_POINTS && - this.getFirstPlace() == "north"; + Session.get("current_bonus") > 0 && + this.getDirectionScore("north") >= Constants.JPN_END_POINTS && + this.getFirstPlace() == "north"; return someoneBankrupt || westRoundOver || someoneAboveMinimum || dealerFirstAndAboveMinimum; }, diff --git a/imports/api/PointCalculationUtils.js b/imports/api/PointCalculationUtils.js index 54bac54..e6dbad3 100644 --- a/imports/api/PointCalculationUtils.js +++ b/imports/api/PointCalculationUtils.js @@ -2,15 +2,22 @@ import { Constants } from './Constants'; import { NewGameUtils } from './NewGameUtils'; export var PointCalculationUtils = { - jpn_dealin_delta, - jpn_selfdraw_delta, - jpn_mistake_delta + jpn: { + dealin_delta: jpn_dealin_delta, + selfdraw_delta: jpn_selfdraw_delta, + mistake_delta: jpn_mistake_delta + } }; /** * Calculates the change in points as a result of a deal-in hand * Uses the formula from https://en.wikipedia.org/wiki/Japanese_Mahjong_scoring_rules - * + * + * @param {number} points - the total number of han + dora + * @param {number} fu - the fu value of the hand + * @param {string} winnerWind - the winning seat wind + * @param {string} loserWind - the losing seat wind + * @param {number} riichiSticks - the total number of thrown riichi sticks * @return {Object} containing the point difference for each seat */ function jpn_dealin_delta(points, fu, winnerWind, loserWind, riichiSticks) { @@ -18,7 +25,7 @@ function jpn_dealin_delta(points, fu, winnerWind, loserWind, riichiSticks) { winds[Constants.EAST] = winds[Constants.SOUTH] = winds[Constants.WEST] = winds[Constants.NORTH] = 0; let basicPoints; - let currentBonus = Number(Session.get("current_bonus")) * Constants.BONUS_POINTS; + let currentBonus = Number(Session.get("current_bonus")) * Constants.JPN_BONUS_POINTS; // The multiplier is for whether or not it's a dealer victory let multiplier = (winnerWind != NewGameUtils.roundToDealerDirection(Number(Session.get("current_round")))) ? 4 : 6; @@ -32,12 +39,13 @@ function jpn_dealin_delta(points, fu, winnerWind, loserWind, riichiSticks) { basicPoints = Math.ceil((fu * Math.pow(2, 2 + points)) * multiplier / 100) * 100; basicPoints = basicPoints < manganPayout ? basicPoints : manganPayout; } - } else { basicPoints = manganValue(points) * multiplier }; + } else { + basicPoints = manganValue(points) * multiplier; + } - winds[winnerWind] = basicPoints + currentBonus + (riichiSticks * Constants.RIICHI_POINTS); + winds[winnerWind] = basicPoints + currentBonus + (riichiSticks * Constants.JPN_RIICHI_POINTS); winds[loserWind] = -(basicPoints + currentBonus); - Session.set("free_riichi_sticks", 0); return winds; }; @@ -45,7 +53,11 @@ function jpn_dealin_delta(points, fu, winnerWind, loserWind, riichiSticks) { /** * Calculates the change in points as a result of a self-drawn hand * Uses the formula from https://en.wikipedia.org/wiki/Japanese_Mahjong_scoring_rules - * + * + * @param {number} points - the total number of han + dora + * @param {number} fu - the fu value of the hand + * @param {string} winnerWind - the winning seat wind + * @param {number} riichiSticks - the total number of thrown riichi sticks * @return {Object} containing the point difference for each seat */ function jpn_selfdraw_delta(points, fu, winnerWind, riichiSticks) { @@ -57,7 +69,7 @@ function jpn_selfdraw_delta(points, fu, winnerWind, riichiSticks) { let dealerPays; let currentBonus = Number(Session.get("currentBonus")); - let individualBonusPayout = Constants.BONUS_POINTS / 3; + let individualBonusPayout = Constants.JPN_BONUS_POINTS / 3; winds[Constants.EAST] = winds[Constants.SOUTH] = winds[Constants.WEST] = winds[Constants.NORTH] = 0; @@ -69,7 +81,9 @@ function jpn_selfdraw_delta(points, fu, winnerWind, riichiSticks) { basicPoints = fu * Math.pow(2, 2 + points); basicPoints = basicPoints < Constants.JPN_MANGAN_BASE_POINTS ? basicPoints : Constants.JPN_MANGAN_BASE_POINTS; } - } else { basicPoints = manganValue(points); } + } else { + basicPoints = manganValue(points); + } nonDealerPays = Math.ceil(basicPoints / 100 * (dealerWind == winnerWind ? 2 : 1)) * 100; dealerPays = Math.ceil(basicPoints / 100 * 2) * 100; @@ -79,39 +93,40 @@ function jpn_selfdraw_delta(points, fu, winnerWind, riichiSticks) { if (w == dealerWind) { winds[w] = -(dealerPays + currentBonus * individualBonusPayout); } else if (w == winnerWind) { - winds[w] = dealerPays + (nonDealerPays * 2) + (currentBonus * Constants.BONUS_POINTS) + (riichiSticks * Constants.RIICHI_POINTS); + winds[w] = dealerPays + (nonDealerPays * 2) + (currentBonus * Constants.JPN_BONUS_POINTS) + (riichiSticks * Constants.JPN_RIICHI_POINTS); } else { winds[w] = -(nonDealerPays + currentBonus * individualBonusPayout); } } else { if (w == winnerWind) { - winds[w] = (nonDealerPays * 3) + (currentBonus * Constants.BONUS_POINTS) + (riichiSticks * Constants.RIICHI_POINTS); + winds[w] = (nonDealerPays * 3) + (currentBonus * Constants.JPN_BONUS_POINTS) + (riichiSticks * Constants.JPN_RIICHI_POINTS); } else { winds[w] = -(nonDealerPays + (currentBonus * individualBonusPayout)); } } } - Session.set("free_riichi_sticks", 0); return winds; }; /** * Calculates the point difference as the result of a mistaken hand + * @param {string} loser - the losing seat wind * @return {Object} containing the point difference for each seat */ function jpn_mistake_delta(loser) { let winds = {}; - winds[Constants.EAST] = winds[Constants.SOUTH] = winds[Constants.WEST] = winds[Constants.NORTH] = Constants.MISTAKE_POINTS / 3; + winds[Constants.EAST] = winds[Constants.SOUTH] = winds[Constants.WEST] = winds[Constants.NORTH] = Constants.JPN_MISTAKE_POINTS / 3; - winds[loser] = -Constants.MISTAKE_POINTS; + winds[loser] = -Constants.JPN_MISTAKE_POINTS; return winds; }; /** * Calculates the total base points from han + dora values for high value hands - * @returns {Number} representing number of base points as the result of a certain point threshold + * @param {number} points - total points from han + dora + * @returns {number} representing number of base points as the result of a certain point threshold */ function manganValue(points) { switch(points) { diff --git a/imports/ui/JapaneseNewGame.js b/imports/ui/JapaneseNewGame.js index e267cdd..38b7955 100644 --- a/imports/ui/JapaneseNewGame.js +++ b/imports/ui/JapaneseNewGame.js @@ -802,7 +802,7 @@ function push_dealin_hand(template) { Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); } - allDelta = PointCalculationUtils.jpn_dealin_delta(points, fu, winnerWind, loserWind, riichiSum); + allDelta = PointCalculationUtils.jpn.dealin_delta(points, fu, winnerWind, loserWind, riichiSum); Session.set("free_riichi_sticks", 0); eastDelta += allDelta[Constants.EAST]; @@ -883,7 +883,8 @@ function push_selfdraw_hand(template) { Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); } - allDeltas = PointCalculationUtils.jpn_selfdraw_delta(points, fu, winnerWind, riichiSum); + allDeltas = PointCalculationUtils.jpn.selfdraw_delta(points, fu, winnerWind, riichiSum); + Session.set("free_riichi_sticks", 0); eastDelta += allDeltas[Constants.EAST]; southDelta += allDeltas[Constants.SOUTH]; @@ -891,7 +892,6 @@ function push_selfdraw_hand(template) { northDelta += allDeltas[Constants.NORTH]; pushHand(template, "selfdraw", eastDelta, southDelta, westDelta, northDelta); - Session.set("free_riichi_sticks", 0); if (winnerWind == NewGameUtils.roundToDealerDirection(Session.get("current_round"))) Session.set("current_bonus", Number(Session.get("current_bonus")) + 1); @@ -1002,7 +1002,7 @@ function push_restart_hand(template) { function push_mistake_hand(template) { var loserWind = NewGameUtils.playerToDirection(Session.get("round_loser")); - let allDeltas = PointCalculationUtils.jpn_mistake_delta(loserWind); + let allDeltas = PointCalculationUtils.jpn.mistake_delta(loserWind); let eastDelta = allDeltas[Constants.EAST]; let southDelta = allDeltas[Constants.SOUTH]; @@ -1091,7 +1091,7 @@ function push_split_pao_hand(template) { Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); } - var value = PointCalculationUtils.jpn_dealin_delta(points, fu, winnerWind, loserWind, 0)[loserWind]; + var value = PointCalculationUtils.jpn.dealin_delta(points, fu, winnerWind, loserWind, 0)[loserWind]; if (((value / 2 ) % 100) == 50) { value += 100; From 7f254dd42948ca0c3c31da373751afb197ba9262 Mon Sep 17 00:00:00 2001 From: TwelveNights Date: Wed, 23 Nov 2016 10:31:42 -0800 Subject: [PATCH 10/10] Capitalize number and modify pao points to use correct wind --- imports/api/PointCalculationUtils.js | 16 ++++++++-------- imports/ui/JapaneseNewGame.js | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/imports/api/PointCalculationUtils.js b/imports/api/PointCalculationUtils.js index e6dbad3..84b920d 100644 --- a/imports/api/PointCalculationUtils.js +++ b/imports/api/PointCalculationUtils.js @@ -13,11 +13,11 @@ export var PointCalculationUtils = { * Calculates the change in points as a result of a deal-in hand * Uses the formula from https://en.wikipedia.org/wiki/Japanese_Mahjong_scoring_rules * - * @param {number} points - the total number of han + dora - * @param {number} fu - the fu value of the hand + * @param {Number} points - the total number of han + dora + * @param {Number} fu - the fu value of the hand * @param {string} winnerWind - the winning seat wind * @param {string} loserWind - the losing seat wind - * @param {number} riichiSticks - the total number of thrown riichi sticks + * @param {Number} riichiSticks - the total number of thrown riichi sticks * @return {Object} containing the point difference for each seat */ function jpn_dealin_delta(points, fu, winnerWind, loserWind, riichiSticks) { @@ -54,10 +54,10 @@ function jpn_dealin_delta(points, fu, winnerWind, loserWind, riichiSticks) { * Calculates the change in points as a result of a self-drawn hand * Uses the formula from https://en.wikipedia.org/wiki/Japanese_Mahjong_scoring_rules * - * @param {number} points - the total number of han + dora - * @param {number} fu - the fu value of the hand + * @param {Number} points - the total number of han + dora + * @param {Number} fu - the fu value of the hand * @param {string} winnerWind - the winning seat wind - * @param {number} riichiSticks - the total number of thrown riichi sticks + * @param {Number} riichiSticks - the total number of thrown riichi sticks * @return {Object} containing the point difference for each seat */ function jpn_selfdraw_delta(points, fu, winnerWind, riichiSticks) { @@ -125,8 +125,8 @@ function jpn_mistake_delta(loser) { /** * Calculates the total base points from han + dora values for high value hands - * @param {number} points - total points from han + dora - * @returns {number} representing number of base points as the result of a certain point threshold + * @param {Number} points - total points from han + dora + * @returns {Number} representing number of base points as the result of a certain point threshold */ function manganValue(points) { switch(points) { diff --git a/imports/ui/JapaneseNewGame.js b/imports/ui/JapaneseNewGame.js index 38b7955..5fb68f8 100644 --- a/imports/ui/JapaneseNewGame.js +++ b/imports/ui/JapaneseNewGame.js @@ -1091,7 +1091,7 @@ function push_split_pao_hand(template) { Session.set("north_riichi_sum", Number(Session.get("north_riichi_sum")) + 1); } - var value = PointCalculationUtils.jpn.dealin_delta(points, fu, winnerWind, loserWind, 0)[loserWind]; + var value = PointCalculationUtils.jpn.dealin_delta(points, fu, winnerWind, loserWind, 0)[winnerWind]; if (((value / 2 ) % 100) == 50) { value += 100;