-- ===============================
--                          Set variables
-- ===============================

function var_init()
var_rm = 0           -- var_rallies_me
var_gm = 0          -- var_games_me
var_sm = 0          -- var_sets_me
var_ro = 0            -- var_rallies_opponent
var_go = 0           -- var_games_opponent
var_so = 0           -- var_sets_opponent
var_adv_o = 0     -- var_advantage_opponent
var_adv_m = 0    -- var_advantage_me
var_break = 0
var_deuce = 0

tiebreak = false
var_super = 1
tb=7              -- tiebreak counts to 7
var_sets = 2
var_service = 1
var_reset = 3
var_start = true
var_settings = false
var_setpoint = false
var_matchpoint = false
var_showbreak = false

wm_action('sw_reset')
end

var_init()



-- ===============================
--          Rally-, game- and set counter
-- ===============================

-- game counter opponent

function var_game_opp()
set_old()
var_ro = var_ro + 1
if tiebreak == false then
   if var_ro == 1 then var_ro = 15 end
   if var_ro == 16 then var_ro = 30 end
   if var_ro == 31 then var_ro = 40 end
   if var_ro == 41 then
      if var_rm < 40 then var_set_opp() end
      if var_rm == 40 then var_adv_o = 1 end
      if var_rm == 41 then
         var_rm = 40
         var_ro = 40
         var_adv_m = 0
         var_deuce = var_deuce + 1
      end
   end
   if var_ro == 42 then var_set_opp() end
 else
   if var_ro > tb-1 and var_ro - var_rm > 1 then var_tb_opp() end
   if (var_ro + var_rm)%2 == 1 then switch_service() end
   end
   check_point()
   check_break()
end


-- set counter opponent

function var_set_opp()
var_rm = 0
var_ro = 0
var_adv_m = 0
var_adv_o = 0
var_go = var_go + 1
if var_go > 5 and var_go - var_gm > 1 then
   var_go = 0
   var_gm = 0
   var_so = var_so + 1
   if var_so == var_sets then wm_action('sw_start_stop') end
 else
   if var_go==6 and var_gm==6 then
      tiebreak = true
      if var_super==2 and var_so==var_sets-1 and var_sm==var_sets-1 then tb=10 else tb=7 end
      service = var_service
   end
end
switch_service()
end


-- tiebreak won by opponent

function var_tb_opp()
var_rm = 0
var_ro = 0
var_adv_m = 0
var_adv_o = 0
var_go = 0
var_gm = 0
var_so = var_so + 1
if var_so == var_sets then wm_action('sw_start_stop') end
tiebreak = false
if var_service ~= service then switch_service() end 
end


-- game counter me

function var_game_me()
set_old()
var_rm = var_rm + 1
if tiebreak == false then
   if var_rm == 1 then var_rm = 15 end
   if var_rm == 16 then var_rm = 30 end
   if var_rm == 31 then var_rm = 40 end
   if var_rm == 41 then
      if var_ro < 40 then var_set_me() end
      if var_ro == 40 then var_adv_m = 1 end
      if var_ro == 41 then
         var_ro= 40
         var_rm = 40
         var_adv_o = 0
         var_deuce = var_deuce + 1
      end
   end
   if var_rm == 42 then var_set_me() end
 else
   if var_rm > tb-1 and var_rm - var_ro > 1 then var_tb_me() end
   if (var_ro + var_rm)%2 == 1 then switch_service() end
   end
   check_point()
   check_break()
end


-- set counter me

function var_set_me()
var_rm = 0
var_ro = 0
var_adv_m = 0
var_adv_o = 0
var_gm = var_gm + 1
if var_gm > 5 and var_gm - var_go > 1 then
   var_go = 0
   var_gm = 0
   var_sm = var_sm + 1
   if var_sm == var_sets then wm_action('sw_start_stop') end
 else
   if var_gm==6 and var_go==6 then
      tiebreak = true
      if var_super==2 and var_so==var_sets-1 and var_sm==var_sets-1 then tb=10 else tb=7 end
      service = var_service
   end
end
switch_service()
end


-- tiebreak won by me

function var_tb_me()
var_rm = 0
var_ro = 0
var_adv_m = 0
var_adv_o = 0
var_go = 0
var_gm = 0
var_sm = var_sm + 1
if var_sm == var_sets then wm_action('sw_start_stop') end
tiebreak = false
if var_service ~= service then switch_service() end
end


-- check whether set- or matchpoint

function check_point()
var_setpoint = false
var_matchpoint = false
if (var_ro == 40 and var_rm < 40) or var_ro == 41 then
   if var_go > 4 and var_go - var_gm > 0 then
      if var_so == var_sets - 1 then var_matchpoint = true else var_setpoint = true end
   end
end
if (var_rm == 40 and var_ro < 40) or var_rm == 41 then
   if var_gm > 4 and var_gm - var_go > 0 then
      if var_sm == var_sets - 1 then var_matchpoint = true else var_setpoint = true end
   end
end
if tiebreak == true then
   if var_rm > tb-2 and var_rm - var_ro > 0 then
     if (var_sm == var_sets - 1) then var_matchpoint = true else var_setpoint = true end
end
   if var_ro > tb-2 and var_ro - var_rm > 0 then
      if var_so == var_sets - 1 then var_matchpoint = true else var_setpoint = true end
   end
end
end


function check_break()
if var_setpoint == false and var_matchpoint == false then
   if var_service == 1 then
      if var_rm == 41 or (var_rm == 40 and var_ro < 40) then
         var_break = var_break + 1
         var_showbreak = true
       else
         var_showbreak = false
      end
    else
      if var_ro == 41 or (var_ro == 40 and var_rm < 40) then
         var_break = var_break + 1
         var_showbreak = true
       else
         var_showbreak = false
      end
   end
 else
   var_showbreak = false
end
end


-- Switch service

function switch_service()
if var_service > 0 then
   var_service = var_service%2 + 1 end
var_deuce = 0
var_break = 0
end



-- ===============================
--                          Undo
-- ===============================

function set_old()
rm_old = var_rm
ro_old = var_ro
adv_m_old = var_adv_m
adv_o_old = var_adv_o
gm_old = var_gm
go_old = var_go
sm_old = var_sm
so_old = var_so
tiebreak_old = tiebreak
service_old = var_service
var_deuce_old = var_deuce
var_break_old = var_break
var_showbreak_old = var_showbreak
var_reset = 3
var_start = false
var_setpoint = false
var_matchpoint = false
end



function var_undo()
var_rm = rm_old
var_ro = ro_old
var_adv_m = adv_m_old
var_adv_o = adv_o_old
var_gm = gm_old
var_go = go_old
var_sm = sm_old
var_so = so_old
tiebreak = tiebreak_old
var_service = service_old
var_deuce = var_deuce_old
var_break = var_break_old
var_showbreak = var_showbreak_old
check_point()
end




-- ===============================
--                          Game timer
-- ===============================


function var_smin()
return var_zero((math.floor({swsst}/60000)%60))
end

function var_shr()
return var_zero((math.floor({swsst}/3600000)%3600))
end


function var_zero(n)
if n < 10 then
   return ("0" .. n)
 else
   return (n)
end
end