/* * pHatt - PalmOS(TM) Hattrick (http://www.hattrick.org) Manager Assistant * * function.c - various often-needed functions * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #include "phat.h" /* Returns the ObjectPtr (this is needed sooooo often...) */ void *getOPtr (UInt16 o) { return FrmGetObjectPtr(gpForm, FrmGetObjectIndex(gpForm, o)); } /* Update the Player Popup List */ void updatePlayerList() { struct player *p; Int16 i, j; if ( playerNameList ) MemPtrFree(playerNameList); p = players; i = 0; while ( p ) { p = p->next; i++; } if ( i ) { if ( prefs.selectedPlayer >= i ) prefs.selectedPlayer = i - 1; p = players; playerNameList = MemPtrNew(i * sizeof(Char *)); for ( j = 0 ; j < i ; j++ ) { if ( p->name ) playerNameList[j] = p->name; else playerNameList[j] = "Nameless"; p = p->next; } } LstSetListChoices(getOPtr(PlayerSelection), playerNameList, i); LstSetHeight(getOPtr(PlayerSelection), ( i <= 9 ? i : 9 )); LstDrawList(getOPtr(PlayerSelection)); SetEditValue(PlayerSelection, PlayerTrigger, prefs.selectedPlayer); } /* Helper for Fields with Numbers */ void fillField(Char *s, UInt16 f, Int8 i) { StrIToA(s, i + 1); FldSetTextPtr(getOPtr(f), s); } /* Helper for Fields with Decimal Numbers */ void fillValue(Char *s, UInt16 f, Int16 i) { Char t[3]; StrIToA(s, i / 100); StrIToA(t, (i / 10) % 10); StrCat(s, "."); StrCat(s, t); FldSetTextPtr(getOPtr(f), s); FldDrawField(getOPtr(f)); } /* Helper for Debugging */ void beepwait() { UInt32 w; SndPlaySystemSound(sndInfo); w = TimGetSeconds(); while ( w + 2 > TimGetSeconds()); } /* Allocates Memory for Name-Swapping */ Char *allocName(Char *s) { Char *a; a = MemPtrNew(StrLen(s) + 2); StrPrintF(a, "%s", s); return a; } /* Returns Player from his ID in the list */ struct player *getPlayer(Int16 p) { struct player *l = players; struct player *n = players; Int8 i = 0; while ( i++ < p && n ) { l = n; n = n->next; } if ( n ) return n; if ( l ) return l; return NULL; } /* Changes Screen according to chosen Menu */ Boolean switchScreen(UInt16 id) { UInt16 really; /* We don't want to switch to ourselves */ prefs.prefsPrevScreen = FrmGetActiveFormID(); switch (id) { case HelpMenuAbout: FrmAlert(AboutAlert); return true; case HelpMenuDisclaimer: FrmAlert(DisclaimerAlert); return true; case ScreenMenuArena: if ( prefs.prefsPrevScreen == ArenaScreen ) return true; FrmGotoForm(ArenaScreen); return true; case ScreenMenuPlayer: if ( prefs.prefsPrevScreen == PlayerScreen ) return true; FrmGotoForm(PlayerScreen); return true; case ScreenMenuPosition: if ( prefs.prefsPrevScreen == PositionScreen ) return true; FrmGotoForm(whichPositionPage()); return true; case PrefsMenuArena: FrmGotoForm(PrefsArenaScreen); return true; case PrefsMenuKeeper: prefs.prefsPlayerScreen = PrefsKeeper; prefs.prefsAttitude = PosNormalButton; FrmGotoForm(PrefsPlayerScreen); return true; case PrefsMenuDefender: prefs.prefsPlayerScreen = PrefsDefender; prefs.prefsAttitude = PosNormalButton; FrmGotoForm(PrefsPlayerScreen); return true; case PrefsMenuWingBack: prefs.prefsPlayerScreen = PrefsWingBack; prefs.prefsAttitude = PosNormalButton; FrmGotoForm(PrefsPlayerScreen); return true; case PrefsMenuMidfield: prefs.prefsPlayerScreen = PrefsMidfield; prefs.prefsAttitude = PosNormalButton; FrmGotoForm(PrefsPlayerScreen); return true; case PrefsMenuWinger: prefs.prefsPlayerScreen = PrefsWinger; prefs.prefsAttitude = PosNormalButton; FrmGotoForm(PrefsPlayerScreen); return true; case PrefsMenuForward: prefs.prefsPlayerScreen = PrefsForward; prefs.prefsAttitude = PosNormalButton; FrmGotoForm(PrefsPlayerScreen); return true; case PlayerMenuNew: EditPlayer(-1); return true; case PlayerMenuEdit: EditPlayer(prefs.selectedPlayer); return true; case PlayerMenuDelete: if ( NofPlayers == 1 ) FrmAlert(DeleteLastPlayerAlert); else { really = FrmAlert(DeletePlayerAlert); if ( ! really ) { removePlayer(prefs.selectedPlayer); FrmGotoForm(prefs.selectedScreen); } } return true; case PlayerMenuAlarm: bgForm = gpForm; FrmGotoForm(TransferScreen); return true; case PlayerMenuChanges: bgForm = gpForm; FrmGotoForm(ChangesScreen); return true; case PrefsMenuVarious: bgForm = gpForm; FrmGotoForm(PrefsVariousScreen); return true; default: return false; } } Int8 posID(UInt16 p) { switch (p) { case Pos_Keeper: return 0; case Pos_Def: return 1; case Pos_DefWing: return 2; case Pos_DefOff: return 3; case Pos_WBack: return 4; case Pos_WBackOff: return 5; case Pos_WBackMid: return 6; case Pos_WBackDef: return 7; case Pos_Mid: return 8; case Pos_MidOff: return 9; case Pos_MidWing: return 10; case Pos_MidDef: return 11; case Pos_Wing: return 12; case Pos_WingOff: return 13; case Pos_WingMid: return 14; case Pos_WingDef: return 15; case Pos_For: return 16; case Pos_ForDef: return 17; case Pos_Captain: return 18; case Pos_Setter: return 19; default: return -1; } } /* Set Value in List */ void SetEditValue (UInt16 list, UInt16 trigger, Int8 value) { LstSetSelection(getOPtr(list), value); LstMakeItemVisible(getOPtr(list), value); CtlSetLabel(getOPtr(trigger), LstGetSelectionText(getOPtr(list), value)); } /* Set next alaram */ void SetNextAlarm() { struct player *p = players; UInt32 now; UInt32 first = 0xffffffff; Int8 count = 0, which = NofPlayers; UInt16 cardNo; LocalID dbID; now = TimGetSeconds(); while (p) { if ( p->transfer > now && p->transfer < first ) { first = p->transfer; which = count; } count++; p = p->next; } if ( which < NofPlayers ) { if ( firstStart ) { saveDB(); firstStart = false; } SysCurAppDatabase(&cardNo, &dbID); AlmSetAlarm(cardNo, dbID, which, first, true); } } /* Return the correct Value */ Char *getCurrency() { switch (prefs.currency) { case Country_Oceania: return "AU$"; case Country_Thai: return "Bah"; case Country_Venezuela: return "bol"; case Country_Schweiz: return "CHF"; case Country_Canada: return "C$"; case Country_Srbija: return "Din"; case Country_Vietnam: return "Don"; case Country_Hungary: return "eFt"; case Country_Deutschland: case Country_Italia: case Country_France: case Country_Suomi: case Country_Nederland: case Country_Ireland: case Country_Espana: case Country_Oesterreich: case Country_Belgie: case Country_Portugal: case Country_Slovenija: case Country_Hellas: return "EUR"; case Country_Egypt: return "E£"; case Country_Island: return "Ikr"; case Country_Slovakia: return "Kor"; case Country_Ceska: return "Kor"; case Country_Eesti: case Country_Sverige: case Country_Norge: case Country_Danmark: return "Kro"; case Country_Romania: return "Lei"; case Country_China: return "Yua"; case Country_Hrvatska: return "Kun"; case Country_HongKong: return "HKD"; case Country_Israel: return "NIS"; case Country_Ukraina: return "Hry"; case Country_Latvija: return "Lat"; case Country_Bulgaria: return "Lev"; case Country_Turkiye: return "Lir"; case Country_Lietuva: return "Lt"; case Country_Bosnia: return "mar"; case Country_Malaysia: return "MYR"; case Country_Chile: case Country_Argentina: case Country_Uruguay: case Country_Pilipinas: case Country_Mexico: return "Pes"; case Country_SthAfrica: return "Ran"; case Country_Brasil: return "Rea"; case Country_Rossiya: return "Rou"; case Country_Indonesia: return "Rp"; case Country_Pakistan: case Country_India: return "Rup"; case Country_Singapore: return "SG$"; case Country_Peru: case Country_USA: return "US$"; case Country_Korea: return "Won"; case Country_Nippon: return "Yen"; case Country_Polska: return "Zlo"; case Country_Taiwan: case Country_Colombia: return "$"; case Country_Scotland: case Country_Wales: case Country_England: return "£"; default: return "???"; } } Int32 valueInCurrency(Int32 v) { switch (prefs.currency) { case Country_Chile: return 20 * v; case Country_Nippon: case Country_Island: return 10 * v; case Country_Pakistan: case Country_Slovakia: return 5 * v; case Country_India: case Country_Thai: case Country_Rossiya: case Country_Ceska: case Country_Pilipinas: return 4 * v; case Country_Romania: case Country_Eesti: return 2 * v; case Country_Sverige: case Country_Norge: case Country_Danmark: case Country_Colombia: case Country_China: case Country_Uruguay: case Country_Mexico: case Country_Indonesia: case Country_Srbija: case Country_Hrvatska: case Country_HongKong: case Country_Vietnam: return v; case Country_SthAfrica: return 4 * v / 5; case Country_Israel: case Country_Ukraina: return v / 2; case Country_Lietuva: case Country_Polska: case Country_Egypt: case Country_Malaysia: return 2 * v / 5; case Country_Oceania: case Country_Brasil: case Country_Canada: case Country_Schweiz: case Country_Singapore: case Country_Bulgaria: case Country_Bosnia: return v / 5; case Country_Deutschland: case Country_Italia: case Country_France: case Country_Suomi: case Country_Nederland: case Country_Ireland: case Country_Espana: case Country_Oesterreich: case Country_Belgie: case Country_Portugal: case Country_Slovenija: case Country_Hellas: case Country_Argentina: case Country_Peru: case Country_USA: case Country_Taiwan: case Country_Venezuela: case Country_Korea: case Country_Turkiye: return v / 10; case Country_Scotland: case Country_Wales: case Country_England: return v / 15; case Country_Latvija: return v / 20; case Country_Hungary: return v / 50; default: return v; } } Char *getInCurrency(Int32 v, Boolean issigned) { Int8 i; Int32 b; Int16 ste[6]; Char *s; Char dummy[4]; NumberFormatType numFor; Char tho, dec; numFor = (NumberFormatType) PrefGetPreference(prefNumberFormat); LocGetNumberSeparators(numFor, &tho, &dec); s = MemPtrNew(maxStrIToALen + 12); b = valueInCurrency(v); i = 0; while ( b != 0 ) { ste[i++] = b % 1000; b /= 1000; } if ( i > 0 ) { if ( issigned ) StrPrintF(s, "%+i", ste[--i]); else StrPrintF(s, "%i", ste[--i]); while ( --i >= 0 ) { StrCat(s, ","); if ( ste[i] < 100 ) StrCat(s, "0"); if ( ste[i] < 10 ) StrCat(s, "0"); StrPrintF(dummy, "%i", ste[i]); StrCat(s, dummy); } } else StrPrintF(s, "0"); StrLocalizeNumber(s, tho, dec); StrCat(s, " "); StrCat(s, getCurrency()); return s; } Int32 currencyToKroner(Int32 v) { switch (prefs.currency) { case Country_Chile: return v / 20; case Country_Nippon: case Country_Island: return v / 10; case Country_Pakistan: case Country_Slovakia: return v / 5; case Country_India: case Country_Thai: case Country_Rossiya: case Country_Ceska: case Country_Pilipinas: return v / 4; case Country_Romania: case Country_Eesti: return v / 2; case Country_Sverige: case Country_Norge: case Country_Danmark: case Country_Colombia: case Country_China: case Country_Uruguay: case Country_Mexico: case Country_Indonesia: case Country_Srbija: case Country_Hrvatska: case Country_HongKong: case Country_Vietnam: return v; case Country_SthAfrica: return 5 * v / 4; case Country_Israel: case Country_Ukraina: return 2 * v; case Country_Lietuva: case Country_Polska: case Country_Egypt: case Country_Malaysia: return 5 * v / 2; case Country_Oceania: case Country_Brasil: case Country_Canada: case Country_Schweiz: case Country_Singapore: case Country_Bulgaria: case Country_Bosnia: return 5 * v; case Country_Deutschland: case Country_Italia: case Country_France: case Country_Suomi: case Country_Nederland: case Country_Ireland: case Country_Espana: case Country_Oesterreich: case Country_Belgie: case Country_Portugal: case Country_Slovenija: case Country_Hellas: case Country_Argentina: case Country_Peru: case Country_USA: case Country_Taiwan: case Country_Venezuela: case Country_Korea: case Country_Turkiye: return 10 * v; case Country_Scotland: case Country_Wales: case Country_England: return 15 * v; case Country_Latvija: return 20 * v; case Country_Hungary: return 50 * v; default: return v; } } /* Returns the right string for a number */ Char *getStrength(Int8 s) { switch ( s ) { case 0: return "disastrous"; case 1: return "wretched"; case 2: return "poor"; case 3: return "weak"; case 4: return "inadequate"; case 5: return "passable"; case 6: return "solid"; case 7: return "excellent"; case 8: return "formidable"; case 9: return "outstanding"; case 10: return "brilliant"; case 11: return "magnificient"; case 12: return "world class"; case 13: return "supernatural"; case 14: return "titanic"; case 15: return "extra-terrestrial"; case 16: return "mythical"; case 17: return "magical"; case 18: return "utopian"; default: return "divine"; } }