Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. <?php
  2. class MyDB extends SQLite3 {
  3. function __construct() {
  4. $this->open('Rankings.db');
  5. }
  6. }
  7.  
  8. //FUNCTIONS
  9. function usage(){
  10. echo("SYNOPSIS\n\t php ranking.php [OPTION]... [TABLE]internal/external");
  11. echo("\nDESCRIPTION");
  12. echo("\n\t-i\n\t\t Insert new participant [NAME]");
  13. echo("\n\t-a\n\t\t add points to specified id of a participant [ID] [POINTS]");
  14. echo("\n\t-d\n\t\t Delete a row given an ID [ID]");
  15. echo("\n\t-g\n\t\t Returns score and position of a participant for a given id [ID]");
  16. echo("\n\t-G\n\t\t Returns current ranking(internal/external)");
  17. echo("\n\t-ni\n\t\t Returns id by a given name");
  18. echo("\n\t-r\n\t\t Resets the score of a specified id. [ID]");
  19. echo("\n\t-R\n\t\t Reset the score of everyone at the table.");
  20. echo("\n");
  21.  
  22. }
  23. //gets all values from all rows
  24. function getRanking($db, $table){
  25. $sql =<<<EOF
  26. SELECT * FROM $table;
  27. EOF;
  28.  
  29. $ret = $db->query($sql);
  30.  
  31. while($row = $ret->fetchArray(SQLITE3_ASSOC) ) {
  32. echo "ID: ". $row['ID'] . "\n";
  33. echo "NAME: ". $row['NAME'] . "\n";
  34. echo "SCORE: ". $row['SCORE'] . "\n";
  35. echo "POSITION: ". $row['POSITION'] . "\n\n";
  36. }
  37. echo("Operation done successfully\n");
  38.  
  39. }
  40.  
  41. //get score and position given an id
  42. function getRankingId($db, $table, $id){
  43. $sql =<<<EOF
  44. SELECT SCORE, POSITION FROM $table WHERE ID = $id;
  45. EOF;
  46. $ret = $db->query($sql);
  47. $row = $ret->fetchArray(SQLITE3_ASSOC);
  48. echo($row['SCORE'] . "\n");
  49. echo($row['POSITION'] . "\n");
  50. echo("Operation done successfully\n");
  51.  
  52. }
  53.  
  54. //get id by providen name
  55. function getNameId($db, $table, $name){
  56. $sql =<<<EOF
  57. SELECT ID FROM $table WHERE NAME = '$name';
  58. EOF;
  59. $ret = $db->query($sql);
  60. $row = $ret->fetchArray(SQLITE3_ASSOC);
  61. echo($row['ID'] . "\n");
  62. echo("Operation done successfully\n");
  63. }
  64.  
  65. //insert new row on specified table
  66. function insertRow($db, $table, $name){
  67. $db->exec("INSERT INTO $table (NAME, SCORE, POSITION) VALUES ('$name', 0,9999)");
  68. }
  69.  
  70. //delete row by id
  71. function deleteRow($db, $table, $id){
  72.  
  73. $db->exec("DELETE FROM $table WHERE ID=$id");
  74. }
  75. //add points to current score by id
  76. function addScore($db, $table, $id, $points){
  77. $sql =<<<EOF
  78. UPDATE $table set SCORE = SCORE + $points WHERE ID=$id;
  79. EOF;
  80. $ret = $db->exec($sql);
  81. if(!$ret){
  82. echo $db->lastErrorMsg();
  83. } else {
  84. echo $db->changes();
  85. }
  86.  
  87. }
  88.  
  89. //Reset the score of all current users
  90. function resetAllScore($db, $table){
  91. $sql =<<<EOF
  92. UPDATE $table set SCORE=0;
  93. EOF;
  94. $ret = $db->exec($sql);
  95. if(!$ret){
  96. echo $db->lastErrorMsg();
  97. } else {
  98. echo $db->changes();
  99. }
  100.  
  101. }
  102.  
  103. //Reset the score of a concrent user
  104. function resetConcretScore($db, $table, $id){
  105. $sql =<<<EOF
  106. UPDATE $table set SCORE=0 WHERE ID = $id;
  107. EOF;
  108. $ret = $db->exec($sql);
  109. if(!$ret){
  110. echo $db->lastErrorMsg();
  111. } else {
  112. echo $db->changes();
  113. }
  114.  
  115. }
  116.  
  117. function showArgumentError(){
  118. echo("Something went wrong with arguments.");
  119. }
  120.  
  121. //FUNCTIONS END
  122.  
  123.  
  124. //principal arguments
  125. if(isset($argv[1])){
  126. $command = $argv[1];
  127. if($argv[1] == "--help"){
  128. usage();
  129. exit;
  130. }
  131. }
  132. else{
  133. echo "You must specify a command. Type --help for help\n";
  134. exit;
  135. }
  136.  
  137. if( isset($argv[2]) ) {
  138. if($argv[2] == "external")
  139. $table = "PUBLICSCORE";
  140. else if($argv[2] == "internal")
  141. $table = "PRIVATESCORE";
  142. }
  143. else
  144. echo "Bad arguments number.";
  145.  
  146. //Database
  147. $db = new MyDB();
  148. if(!$db){
  149. echo $db->lastErrorMsg();
  150. } else {
  151. echo("Opened database succesfully\n");
  152. }
  153.  
  154. $name = 'maria';
  155.  
  156. //function calls and arguments assigns
  157. switch($argv[1]){
  158. //insert
  159. case "-i":
  160. //name
  161. if( isset($argv[3]) )
  162. $name = $argv[3];
  163. else{
  164. showArgumentError();
  165. exit;
  166. }
  167. insertRow($db, $table, $name);
  168. break;
  169.  
  170. //ADD
  171. case "-a":
  172. //id
  173. if( isset($argv[3]) )
  174. $id = $argv[3];
  175. else{
  176. showArgumentError();
  177. exit;
  178. }
  179. //points to add
  180. if( isset($argv[4]) )
  181. $points = $argv[4];
  182. else{
  183. showArgumentError();
  184. exit;
  185. }
  186.  
  187. addScore($db, $table, $id, $points);
  188. break;
  189.  
  190. //delete row by id
  191. case "-d":
  192. if( isset($argv[3]) )
  193. $id = $argv[3];
  194. else{
  195. showArgumentError();
  196. exit;
  197. }
  198. deleteRow($db, $table, $id);
  199. break;
  200.  
  201. //get ranking by id
  202. case "-g":
  203. //id
  204. if( isset($argv[3]) )
  205. $id = $argv[3];
  206. else{
  207. showArgumentError();
  208. exit;
  209. }
  210. getRankingId($db, $table, $id);
  211. break;
  212.  
  213. //getranking
  214. case "-G":
  215. getRanking($db, $table);
  216. break;
  217.  
  218. case "-ni":
  219. if( isset($argv[3]) )
  220. $name = $argv[3];
  221. else{
  222. showArgumentError();
  223. exit;
  224. }
  225. getNameId($db, $table, $name);
  226.  
  227. break;
  228. //reset by id
  229. case "-r":
  230. //id
  231. if( isset($argv[3]) )
  232. $id = $argv[3];
  233. else{
  234. showArgumentError();
  235. exit;
  236. }
  237. resetConcretScore($db, $table, $id);
  238. break;
  239.  
  240. //reset all rows
  241. case "-R":
  242. resetAllScore($db, $table);
  243. break;
  244.  
  245.  
  246.  
  247. }
  248.  
  249.  
  250. $db->close();
  251.  
  252. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement