回精華區
作者weiyu.bbs@weiyu.twbbs.org (Andy) 看板: itoc
標題Re: 請問一下關於國庫系統
時間王者之家 (2005/03/31 Thu 08:00:00) Updated: 2005/03/31
※ 引述《jal.bbs@bbs.jal.idv.tw (小瓜呆~)》之銘言:
> 把使用者爆掉多出來的錢,跟點歌 or 玩遊戲之類所扣的錢
> 想個辦法存到一個地方去。
> 然後就會有個國庫出現了...
> 最後當國庫 = 零 的時候..玩遊戲贏了也會沒錢給...:p

: acct.c:addmoney() 和 addgold() 換成以下

/*
    程式假設:
        record 0 存放銀幣
        record 1 存放金幣
    共用檔案:run/treasury
    剛開始國庫的錢設為
    INITIAL_MONEY
    國庫的金幣一開始有
    INITIAL_GOLD

    把addmoney和addgold兩個換成下面那樣
    不過前提是 扣錢也要用到addmoney才有效:p
    不只是溢位 如果扣錢也用這個函數的話
    也會存到國庫吧:p (沒試過)
 */


#define FN_TREASURY     "run/treasury"
#define INITIAL_MONEY   1000000
#define INITIAL_GOLD    1000


void
addmoney(addend)
  int addend;
{
  int treasury;
  int overflow;

  if (rec_get(FN_TREASURY, &treasury, sizeof(int), 0))
    treasury = INITIAL_MONEY;

  if (addend < 0)
  {
    treasury -= addend;
    cuser.money += addend;
    rec_put(FN_TREASURY, &treasury, sizeof(int), 0, NULL);
  }
  else if (addend < (INT_MAX - cuser.money))      /* 避免溢位 */
  {
    if (treasury < addend)
    {
      vmsg("國庫沒銀幣了,沒辦法支付您");
      return;
    }
    cuser.money += addend;
  }
  else
  {
    /* 將溢出的部分存入國庫 */
    overflow = INT_MAX - cuser.money;
    overflow = addend - overflow;
    treasury += overflow;
    rec_put(FN_TREASURY, &treasury, sizeof(int), 0, NULL);
    cuser.money = INT_MAX;
  }
}


void
addgold(addend)
  int addend;
{
  int treasury;
  int overflow;

  if (rec_get(FN_TREASURY, &treasury, sizeof(int), 1))
    treasury = INITIAL_GOLD;

  if (addend < 0)
  {
    treasury -= addend;
    cuser.money += addend;
    rec_put(FN_TREASURY, &treasury, sizeof(int), 1, NULL);
  }
  else if (addend < (INT_MAX - cuser.gold))       /* 避免溢位 */
  {
    if (treasury < addend)
    {
      vmsg("國庫沒金幣了,沒辦法支付您");
      return;
    }
    cuser.gold += addend;
  }
  else
  {
    /* 將溢出的部分存入國庫 */
    overflow = INT_MAX - cuser.gold;
    overflow = addend - overflow;
    treasury += overflow;
    rec_put(FN_TREASURY, &treasury, sizeof(int), 1, NULL);
    cuser.gold = INT_MAX;
  }
}

: bank.c:x_bank() 在銀行的地方顯示國庫還有多少錢

int
x_bank()
{
  char ans[3];
+ int treasury;

  ...

    "  ╰═════════════════════════════╯\033[m");

+ outs("\n國庫還有");
+ if (rec_get("run/treasury_money", &treasury, sizeof(int), 0))
+   prints(" %d 銀", treasury);
+ if (rec_get("run/treasury_gold", &treasury, sizeof(int), 0))
+   prints(" %d 金", treasury);

  vget(11, 0, "請輸入您需要的服務:", ans, 3, DOECHO);


--
                        生▂�R▃有▁了▃苦▆痛
                        何不把眼睛的餘光留給別人
                            你會看到更多

--
╭────╮ ╬ weiyu.twbbs.org ☆☆☆☆☆☆☆☆☆☆☆☆
家│ ╬ weiyu  ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
╰────╯ ╬ 發表於 weiyu.mshome.net  ☆☆☆☆☆☆☆☆☆
精華選讀←離開[主題上]主題下(k)上篇(j)下篇S/a搜尋 G串列 TAB精華 ↑↓捲 Pg/Space翻