]> git.r.bdr.sh - rbdr/r.bdr.sh/blob - jekyll/gaby/nyawnyawnyaw.php
f8a96fe70d707f03af10110e2411366ba85dcb96
[rbdr/r.bdr.sh] / jekyll / gaby / nyawnyawnyaw.php
1 <?php
2
3 $db = mysql_connect("localhost","abuguet","CambiamE!!");
4 mysql_set_charset ( 'utf8', $db );
5 mysql_select_db("abuguet");
6
7 //Prepare random Query
8 mysql_query('SELECT @count := COUNT(*) FROM `gab_ascii`');
9 mysql_query('SET @offset = CONVERT(FLOOR(RAND() * @count), SIGNED)');
10 mysql_query('PREPARE `randquery` FROM "SELECT * FROM `gab_ascii` LIMIT ?, 1"');
11
12 //get 20
13 $rows = array();
14 for($i = 0; $i < 20; $i++){
15 mysql_query('SET @offset = CONVERT(FLOOR(RAND() * @count), SIGNED)');
16 $res = mysql_query('EXECUTE `randquery` USING @offset');
17 $rows[$i] = mysql_fetch_assoc($res);
18 }
19
20 $area = array();
21 $chars = array();
22 $counter = 0;
23 //empty array.
24 for($i = 0; $i < 25; $i++){
25 for ($j = 0; $j < 80; $j++){
26 $area[$i][$j] = 0;
27 }
28 }
29
30 function getCharArray ($jstring)
31 {
32 if (mb_strlen ($jstring, 'UTF-8') == 0)
33 return array();
34
35 $ret = array ();
36 $alen = strlen ($jstring);
37 $char = '';
38 for ($i = 0; $i < $alen; $i++) {
39 $char .= $jstring[$i];
40 if (mb_check_encoding ($char, 'UTF-8')) {
41 array_push ($ret, $char);
42 $char = '';
43 }
44 }
45
46 return $ret;
47 }
48
49 function can_place($w,$h,$x,$y){
50 global $area;
51 for($i = $y; $i < $y+$h; $i++){
52 for ($j = $x; $j < $x+$w; $j++){
53 if($area[$i][$j] != 0){
54 return false;
55 }
56 }
57 }
58 return true;
59 }
60
61 function do_place($row,$x,$y){
62 global $area, $chars, $counter;
63 for($i = $y,$ch = 0; $i < $y+$row['height']; $i++,$ch++){
64 for ($j = $x,$cw = 0; $j < $x+$row['width']; $j++,$cw++){
65 if($ch == 0 && $cw == 0){ $area[$i][$j] = 2; }else{ $area[$i][$j] = 1;}
66 $text = getCharArray($row['text']);
67 $chars[$i][$j] = $text[($row['width']*$ch)+$cw];
68 $counter++;
69 }
70 }
71 }
72
73 foreach($rows as $row){
74 if($counter < 660){ //try to keep it at 33% fill.
75 $tries = 0; //lets do only 10 tries.
76 $placed = false;
77 while(!$placed && $tries < 10){ //while it isn't placed and we still have tries
78 $x = rand (0, 80-$row['width'] ); //get a valid root point
79 $y = rand (0, 25-$row['height'] );
80
81 if(can_place($row['width'],$row['height'],$x,$y)){
82 do_place($row,$x,$y);
83 $placed = true;
84 }else{
85 $tries++;
86 }
87 }
88 }
89 }
90
91 ?>
92 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
93 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
94 <head>
95 <title>nyaw nyaw nyaw :3</title>
96 <style type="text/css">
97
98 @font-face {
99 font-family: 'Mona';
100 src: url('mona.ttf') format('truetype');
101 font-weight: normal;
102 font-style: normal;
103
104 }
105
106 *{
107 margin: 0;
108 padding: 0;
109 }
110
111 body{
112 text-align: center;
113 font-size: 0.625em;
114 background-color: #fff;
115 }
116
117 #wrapper{
118 margin: 0 auto;
119 width: 960px;
120 margin-top: 100px;
121 text-align: left;
122 }
123
124 #wrapper #term{
125 font-size: 1.2em;
126 font-family: Mona,'MS PGothic', monospace;
127 margin-top: 100px;
128 }
129
130 .letter{
131 color: #999;
132 display: block;
133 float: left;
134 width: 12px;
135 height: 16px;
136 }
137
138 #clear{
139 clear: both;
140 height: 0;
141 width: 0;
142 }
143
144 .letter.test{
145 background-color: #c00;
146 color: #fff;
147 }
148
149 </style>
150 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
151 <script src="http://www.google.com/jsapi" type="text/javascript"></script>
152 <script type="text/javascript">
153 // Load jQuery
154 google.load("jquery", "1.4.0");
155 </script>
156 </head>
157 <body>
158 <div id="wrapper">
159 <div id="term"><?php
160 for($i = 0; $i < 25; $i++){
161 for ($j = 0; $j < 80; $j++){
162 if($area[$i][$j] != 0){
163 echo "<span class=\"letter test\" id=\"letter-".((80*$i)+$j)."\">".htmlspecialchars($chars[$i][$j])."</span>";
164 }else{
165 echo "<span class=\"letter\" id=\"letter-".((80*$i)+$j)."\">".htmlspecialchars(chr(rand(33,126)))."</span>";
166 }
167 }
168 echo "<div id=\"clear\"/>&nbsp;</div>\n";
169 }
170 ?></div>
171 </div>
172 </body>
173 </html>