2021-02-22 17:55:53 +08:00
< ? php
class Attendance
{
protected $userid ;
protected $curdate ;
public function __construct ( $userid ){
$this -> userid = $userid ;
$this -> curdate = date ( 'Y-m-d' );
$this -> cachename = sprintf ( 'attendance_%u_%s' , $this -> userid , $this -> curdate );
}
public function check ( $flush = false )
{
global $Cache ;
if ( $flush || ( $row = $Cache -> get_value ( $this -> cachename )) === false ){
$res = sql_query ( sprintf ( 'SELECT * FROM `attendance` WHERE `uid` = %u AND DATE(`added`) = %s' , $this -> userid , sqlesc ( $this -> curdate . ' 00:00:00' ))) or sqlerr ( __FILE__ , __LINE__ );
$row = mysql_num_rows ( $res ) ? mysql_fetch_assoc ( $res ) : array ();
2022-02-25 23:13:34 +08:00
$Cache -> cache_value ( $this -> cachename , $row , 600 );
2021-02-22 17:55:53 +08:00
}
return empty ( $row ) ? false : $row ;
}
public function attend ( $initial = 10 , $step = 5 , $maximum = 2000 , $continous = array ())
{
2021-06-14 02:12:21 +08:00
do_log ( json_encode ( func_get_args ()));
2021-02-22 17:55:53 +08:00
if ( $this -> check ( true )) return false ;
2021-06-14 12:49:16 +08:00
$res = sql_query ( sprintf ( 'SELECT id, DATEDIFF(%s, `added`) AS diff, `days`, `total_days` FROM `attendance` WHERE `uid` = %u ORDER BY `id` DESC LIMIT 1' , sqlesc ( $this -> curdate ), $this -> userid )) or sqlerr ( __FILE__ , __LINE__ );
2021-06-10 21:07:20 +08:00
$doUpdate = mysql_num_rows ( $res );
if ( $doUpdate ) {
$row = mysql_fetch_row ( $res );
do_log ( " uid: { $this -> userid } , row: " . json_encode ( $row ));
} else {
2021-06-14 12:49:16 +08:00
$row = [ 0 , 0 , 0 , 0 ];
2021-06-10 21:07:20 +08:00
}
2021-06-14 12:49:16 +08:00
list ( $id , $datediff , $days , $totalDays ) = $row ;
2022-02-25 23:13:34 +08:00
$points = min ( $initial + $step * $days , $maximum );
2021-02-22 17:55:53 +08:00
$cdays = $datediff == 1 ? ++ $days : 1 ;
if ( $cdays > 1 ){
krsort ( $continous );
foreach ( $continous as $sday => $svalue ){
if ( $cdays >= $sday ){
$points += $svalue ;
break ;
}
}
}
2021-06-10 21:07:20 +08:00
// sql_query(sprintf('INSERT INTO `attendance` (`uid`,`added`,`points`,`days`) VALUES (%u, %s, %u, %u)', $this->userid, sqlesc(date('Y-m-d H:i:s')), $points, $cdays)) or sqlerr(__FILE__, __LINE__);
if ( $doUpdate ) {
$sql = sprintf (
2021-06-14 12:49:16 +08:00
'UPDATE `attendance` set added = %s, points = %s, days = %s, total_days= %s where id = %s limit 1' ,
sqlesc ( date ( 'Y-m-d H:i:s' )), $points , $cdays , $totalDays + 1 , $id
2021-06-10 21:07:20 +08:00
);
} else {
$sql = sprintf (
2021-06-15 23:11:27 +08:00
'INSERT INTO `attendance` (`uid`, `added`, `points`, `days`, `total_days`) VALUES (%u, %s, %u, %u, %u)' ,
2021-06-13 22:21:42 +08:00
$this -> userid , sqlesc ( date ( 'Y-m-d H:i:s' )), $points , $cdays , $totalDays + 1
2021-06-10 21:07:20 +08:00
);
}
do_log ( sprintf ( 'uid: %s, date: %s, doUpdate: %s, sql: %s' , $this -> userid , $this -> curdate , $doUpdate , $sql ), 'notice' );
sql_query ( $sql ) or sqlerr ( __FILE__ , __LINE__ );
2021-02-22 17:55:53 +08:00
KPS ( '+' , $points , $this -> userid );
global $Cache ;
$Cache -> delete_value ( $this -> cachename );
2021-06-10 21:07:20 +08:00
return array ( ++ $totalDays , $cdays , $points );
2021-02-22 17:55:53 +08:00
}
2021-06-10 21:07:20 +08:00
}