Advertisement
Guest User

Untitled

a guest
Mar 14th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.19 KB | None | 0 0
  1. #include <YSI\y_timers>
  2. #include <YSI\y_iterate>
  3. #include <YSI\y_hooks>
  4.  
  5. #define MAX_TEAMS_FOOTBALL 10 // maximum football teams
  6.  
  7. enum fotbal_inf {
  8. mid,
  9. name[56],
  10. bool:onmatch,
  11. Float:cota1,
  12. Float:cotaX,
  13. Float:cota2,
  14. starting,
  15. home[26],
  16. away[26]
  17. };
  18. new matchVariables[MAX_TEAMS_FOOTBALL][fotbal_inf];
  19.  
  20. new Iterator:currentTeams<MAX_TEAMS_FOOTBALL>;
  21. new Iterator:betTeam[MAX_TEAMS_FOOTBALL]<MAX_PLAYERS>;
  22.  
  23. new Timer:matchTimer[MAX_TEAMS_FOOTBALL] = Timer:-1;
  24. new Timer:playingMatch[MAX_TEAMS_FOOTBALL] = Timer:-1;
  25.  
  26. new goalshome[MAX_TEAMS_FOOTBALL];
  27. new goalsaway[MAX_TEAMS_FOOTBALL];
  28.  
  29. hook OnGameModeInit() {
  30. Iter_Init(betTeam);
  31. return 1;
  32. }
  33.  
  34. hook OnPlayerDisconnect(playerid, reason) {
  35. deleteBetPVars(playerid);
  36. return 1;
  37. }
  38.  
  39. hook OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) {
  40. switch(dialogid) {
  41. case DIALOG_FOOTBALL: {
  42. if(!response) hideDialog(playerid);
  43. else {
  44. new x = listitem, szMessage[56];
  45.  
  46. if(matchVariables[x][onmatch] == true) return SCM(playerid, -1, "You can't place a bet on this match because it already started.");
  47. if(GetPVarInt(playerid, "betPlacedOn")) return SCM(playerid, -1, "You've already placed a bet.");
  48.  
  49. SetPVarInt(playerid, "betAuxPlacedOn", x);
  50. format(szMessage, 56, "1 - %0.2f\nX - %0.2f\n2 - %0.2f", matchVariables[x][cota1], matchVariables[x][cotaX], matchVariables[x][cota2]);
  51. ShowPlayerDialog(playerid, DIALOG_FOOTBALL2, DIALOG_STYLE_LIST, "SERVER: Choose bet", szMessage, "Select", "Cancel");
  52. }
  53. }
  54. case DIALOG_FOOTBALL2: {
  55. if(!response) {
  56. hideDialog(playerid);
  57. deleteBetPVars(playerid);
  58. }
  59. else {
  60. switch(listitem) {
  61. case 0: SetPVarFloat(playerid, "betPossibleWin", matchVariables[GetPVarInt(playerid, "betPlacedOn")][cota1]);
  62. case 1: SetPVarFloat(playerid, "betPossibleWin", matchVariables[GetPVarInt(playerid, "betPlacedOn")][cotaX]);
  63. case 2: SetPVarFloat(playerid, "betPossibleWin", matchVariables[GetPVarInt(playerid, "betPlacedOn")][cota2]);
  64. }
  65. SetPVarInt(playerid, "betCota", listitem);
  66. ShowPlayerDialog(playerid, DIALOG_FOOTBALL3, DIALOG_STYLE_INPUT, "SERVER: Enter your bet", "Enter the amount you wish to bet.", "Done", "Cancel");
  67. }
  68. }
  69. case DIALOG_FOOTBALL3: {
  70. if(!response) {
  71. hideDialog(playerid);
  72. deleteBetPVars(playerid);
  73. }
  74. else {
  75. if(GetPlayerMoney(playerid) < strval(inputtext)) return SCM(playerid, -1, "You don't have enough money.");
  76.  
  77. SetPVarInt(playerid, "betMoney", strval(inputtext));
  78. SetPVarInt(playerid, "betPlacedOn", GetPVarInt(playerid, "betAuxPlacedOn"));
  79. Iter_Add(betTeam[GetPVarInt(playerid, "betAuxPlacedOn")], playerid);
  80. GivePlayerMoney(playerid, -strval(inputtext));
  81.  
  82. new string[256];
  83. format(string, 256, "(-) You've placed a bet on %s of $%s. Your possible winnings: $%s.", matchVariables[GetPVarInt(playerid, "betPlacedOn")][name], FormatNumber(strval(inputtext)), FormatNumber(floatround(GetPVarFloat(playerid, "betPossibleWin")*strval(inputtext))));
  84. SCM(playerid, COLOR_ATTACK, string);
  85. }
  86. }
  87. }
  88. return 1;
  89. }
  90.  
  91. stock deleteBetPVars(playerid) {
  92.  
  93. if(GetPVarInt(playerid, "betCota")) DeletePVar(playerid, "betCota");
  94. if(GetPVarInt(playerid, "betPossibleWin")) DeletePVar(playerid, "betPossibleWin");
  95. if(GetPVarInt(playerid, "betMoney")) DeletePVar(playerid, "betMoney");
  96. if(GetPVarInt(playerid, "betPlacedOn")) DeletePVar(playerid, "betPlacedOn");
  97. if(GetPVarInt(playerid, "betAuxPlacedOn")) DeletePVar(playerid, "betAuxPlacedOn");
  98.  
  99. return 1;
  100. }
  101.  
  102. stock teamHome(teamname[]) {
  103. new chars = strfind(teamname, "vs", true);
  104. new hhome[56];
  105. strmid(hhome, teamname, 0, chars-1);
  106. return hhome;
  107. }
  108.  
  109. stock teamAway(teamname[]) {
  110. new chars = strfind(teamname, "vs", true);
  111. new aaway[56];
  112. strmid(aaway, teamname, chars+2, strlen(teamname));
  113. return aaway;
  114. }
  115.  
  116. stock getMyBet(playerid) {
  117. new small[5];
  118.  
  119. switch(GetPVarInt(playerid, "betCota")) {
  120. case 0: small = "1";
  121. case 1: small = "X";
  122. case 2: small = "2";
  123. }
  124.  
  125. return small;
  126. }
  127.  
  128. new numberLopped[MAX_TEAMS_FOOTBALL];
  129. timer onMatch[30*1000](id) {
  130. if(numberLopped[id] == 0 || numberLopped[id] == 1 || numberLopped[id] == 2) {
  131. new szMessage[256];
  132. szMessage[0] = EOS;
  133.  
  134. new sansaHome = random(100) - floatround(matchVariables[id][cota1]);
  135. new sansaAway = random(100) - floatround(matchVariables[id][cota2]);
  136. new sansaEqual = random(100) - floatround(matchVariables[id][cotaX]);
  137.  
  138. new cases;
  139. if(sansaHome > sansaAway > sansaEqual) cases = 1;
  140. else if(sansaAway > sansaHome > sansaEqual) cases = 3;
  141. else if((sansaEqual > sansaHome > sansaAway) && numberLopped[id] != 0) cases = 2;
  142. else if(numberLopped[id] != 0) cases = 2;
  143. else cases = 1;
  144.  
  145. switch(cases) {
  146. case 1: {
  147. goalshome[id]++;
  148. foreach(new x : betTeam[id]) {
  149. format(szMessage, 256, "{52d467}[BET UPDATE] %s just scored! The score is now %d - %d (%s). Your bet: %s.", matchVariables[id][home], goalshome[id], goalsaway[id], matchVariables[id][name], getMyBet(x));
  150. SCM(x, -1, szMessage);
  151. }
  152. }
  153. case 2: {
  154. foreach(new x : betTeam[id]) {
  155. format(szMessage, 256, "{52d467}[BET UPDATE] The score is still %d - %d (%s). Your bet: %s.", goalshome[id], goalsaway[id], matchVariables[id][name], getMyBet(x));
  156. SCM(x, -1, szMessage);
  157. }
  158. }
  159. case 3: {
  160. goalsaway[id]++;
  161. foreach(new x : betTeam[id]) {
  162. format(szMessage, 256, "{52d467}[BET UPDATE] %s just scored! The score is now %d - %d (%s). Your bet: %s.", matchVariables[id][away], goalshome[id], goalsaway[id], matchVariables[id][name], getMyBet(x));
  163. SCM(x, -1, szMessage);
  164. }
  165. }
  166. }
  167. }
  168. else if(numberLopped[id] == 3) {
  169.  
  170. new szFinal[256];
  171. new win = -1;
  172.  
  173. szFinal[0] = EOS;
  174.  
  175. if(goalsaway[id] > goalshome[id]) {
  176. win = 2;
  177. format(szFinal, 256, "[MATCH OVER]{dad953} %s won against %s. Final score: %d - %d. ", matchVariables[id][away], matchVariables[id][home], goalshome[id], goalsaway[id]);
  178. }
  179. else if(goalsaway[id] < goalshome[id]) {
  180. win = 0;
  181. format(szFinal, 256, "[MATCH OVER]{dad953} %s won against %s. Final score: %d - %d. ", matchVariables[id][home], matchVariables[id][away], goalshome[id], goalsaway[id]);
  182. }
  183. else {
  184. win = 1;
  185. format(szFinal, 256, "[MATCH OVER]{dad953} %s has ended equally. Final score: %d - %d. ", matchVariables[id][name], goalshome[id], goalsaway[id]);
  186. }
  187.  
  188. foreach(new x : betTeam[id]) {
  189. new szMessage[20];
  190. szMessage[0] = EOS;
  191.  
  192. format(szMessage, 20, "Your bet: %s.", getMyBet(x));
  193. strcat(szFinal, szMessage);
  194.  
  195. SCM(x, COLOR_ATTACK, szFinal);
  196.  
  197. if(win == GetPVarInt(x, "betCota")) {
  198. new payment = floatround(GetPVarFloat(x, "betPossibleWin")*GetPVarInt(x, "betMoney"));
  199. GivePlayerMoney(x, payment);
  200.  
  201. new string[256];
  202. format(string, 256, "(+) You've won $%s.", FormatNumber(floatround(GetPVarFloat(x, "betPossibleWin")*GetPVarInt(x, "betMoney"))));
  203. SCM(x, COLOR_ATTACK, string);
  204. }
  205. else SCM(x, COLOR_ATTACK, "(-) You've lost. Try again next time!");
  206.  
  207. deleteBetPVars(x);
  208. Iter_SafeRemove(betTeam[id], x, x);
  209. }
  210. stop playingMatch[id];
  211. playingMatch[id] = Timer:-1;
  212.  
  213. prepareTeams(id);
  214. }
  215. numberLopped[id]++;
  216. }
  217.  
  218. timer startMatch[1000](id) {
  219. if(matchVariables[id][starting] == 0) {
  220. stop matchTimer[id];
  221. matchTimer[id] = Timer:-1;
  222. numberLopped[id] = -1;
  223. playingMatch[id] = repeat onMatch(id);
  224. matchVariables[id][onmatch] = true;
  225. }
  226. else matchVariables[id][starting]--;
  227. }
  228.  
  229. stock saveMatch(x) {
  230. if(strlen(matchVariables[x][name])) {
  231. new string[356];
  232. mysql_format(con_handle, string, 256, "UPDATE `football_matches` SET `matchName` = '%e' WHERE `matchID` = '%d'", matchVariables[x][name], matchVariables[x][mid]);
  233. mysql_tquery(con_handle, string);
  234. }
  235. return 1;
  236. }
  237.  
  238. function getMatchesData() {
  239. Iter_Clear(currentTeams);
  240.  
  241. for(new x, y = cache_num_rows(); x != y; x++) {
  242. cache_get_value_name_int(x, "matchID", matchVariables[x][mid]);
  243. cache_get_value_name(x, "matchName", matchVariables[x][name], 56);
  244.  
  245. prepareTeams(x);
  246. if(!Iter_Contains(currentTeams, x)) Iter_Add(currentTeams, x);
  247. }
  248.  
  249. printf("- %d matches loaded.", Iter_Count(currentTeams));
  250. return 1;
  251. }
  252.  
  253. function addMatch(text[]) {
  254. foreach(new x : currentTeams)
  255. if(!strcmp(text, matchVariables[x][name])) return 1;
  256.  
  257. new string[256];
  258. mysql_format(con_handle, string, 256, "INSERT INTO `football_matches` (`matchName`) VALUES ('%e')", text);
  259. mysql_tquery(con_handle, string);
  260.  
  261.  
  262. new x = Iter_Alloc(currentTeams);
  263. matchVariables[x][mid] = cache_insert_id();
  264. format(matchVariables[x][name], 56, text);
  265.  
  266. prepareTeams(x);
  267. saveMatch(x);
  268.  
  269. return 1;
  270. }
  271.  
  272. function prepareTeams(x) {
  273. new Float:c1, Float:cX, Float:c2;
  274.  
  275. c1 = frandom(5.5, 1.0);
  276. cX = frandom(10.0, 2.0);
  277. c2 = frandom(5.5, 1.0);
  278.  
  279. goalsaway[x] = 0;
  280. goalshome[x] = 0;
  281.  
  282. matchVariables[x][cota1] = c1;
  283. matchVariables[x][cotaX] = cX;
  284. matchVariables[x][cota2] = c2;
  285.  
  286. matchVariables[x][starting] = 60 + random(40);
  287. matchVariables[x][onmatch] = false;
  288.  
  289. format(matchVariables[x][home], 26, teamHome(matchVariables[x][name]));
  290. format(matchVariables[x][away], 26, teamAway(matchVariables[x][name]));
  291.  
  292. matchTimer[x] = repeat startMatch(x);
  293. return 1;
  294. }
  295.  
  296. YCMD:matches(playerid, params[], help) {
  297. new largeString[526], count = 0;
  298. strcat(largeString, "STATUS-TEAMS\t1\tX\t2\n");
  299.  
  300. foreach(new x : currentTeams) {
  301. count++;
  302. new szString[256];
  303. new szSmall[56];
  304.  
  305. switch(matchVariables[x][onmatch]) {
  306. case false: format(szSmall, 56, "{e3b42b}[STARTING IN %d]{FFFFFF}", matchVariables[x][starting]);
  307. case true: format(szSmall, 56, "{e3b42b}[IN MATCH]{FFFFFF}");
  308. }
  309.  
  310. format(szString, 256, "%s - %s\t%0.2f\t%0.2f\t%0.2f\n", szSmall, matchVariables[x][name], matchVariables[x][cota1], matchVariables[x][cotaX], matchVariables[x][cota2]);
  311. strcat(largeString, szString);
  312. }
  313.  
  314. if(count != 0) ShowPlayerDialog(playerid, DIALOG_FOOTBALL, DIALOG_STYLE_TABLIST_HEADERS, "SERVER: Betting football", largeString, "Select", "Cancel");
  315. else SCM(playerid, -1, "No active matches.");
  316. return 1;
  317. }
  318.  
  319. YCMD:addmatch(playerid, params[], help) {
  320. new team[56];
  321.  
  322. if(sscanf(params, "s[56]", team)) return SCM(playerid, -1, "/debugmatches [name]");
  323. addMatch(team);
  324.  
  325. return 1;
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement