summaryrefslogtreecommitdiff
path: root/app/Libraries/Helper.php
blob: 9f0ca2d1a44aa2f67254e5a67569d33cad58a5ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
namespace App\Libraries;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

use Carbon\Carbon;

use App\Libraries\AnimeSchedule;
use App\Libraries\AnimeSeason;
use App\Anime;
use App\AnimeStats;
use App\MALUser;
use App\Calendar;
use App\Airing;

use Eluceo\iCal\Component\Calendar as iCalendar; 
use Eluceo\iCal\Component\Event;

class Helper {
	public function getCalendar( $username ) {
		$user = MALUser::where('username', $username)->get()->first();

		$vCalendar = new iCalendar('animes.iamfabulous.de');
		$vCalendar->setName('Anime Schedule');

		foreach( $user->calendar()->get() as $anime ) {

			if ( is_null($anime->episodes_complete) ) {
				$episodes_complete = '?';	
			} else {
				$episodes_complete = $anime->episodes_complete;
			}

			$vEvent = new Event();

			$vEvent
				->setDtStart(new \DateTime($anime->airing_at))
                                ->setDtEnd( new \DateTime(Carbon::create($anime->airing_at)->add($anime->duration, 'minutes')) )
                                ->setNoTime(false)
                                ->setSummary( $anime->title . " (" . $anime->episode . "/" . $episodes_complete . ")" )
                                ->setUrl( env("APP_URL") )
				->setDescription(
					"(Episode " . $anime->episode . "/" . $episodes_complete . ")
					You have watched " . $anime->episodes_watched . " episodes of " . $anime->title . " and scored it with " . $anime->score_user . ".
					(Public score: ". fF($anime->score) .")\n\n"
					. $anime->mal_url
					. "\n" . route('anime', ["mal_id" => $anime->mal_id, "slug" => Str::slug($anime->title)])
				);

			$vCalendar->addComponent($vEvent);
		}

		return $vCalendar->render();

	}

        public function setCalendar($username) {
                $user = MALUser::where('username', $username)->get()->first();

		if ( is_null($user) ) {
			echo "setCalendar: User (" .  $username . ") does not exists. Skipping\n";
			return;
		}

		if ( is_null($user->IsWatching) ) {
			echo "setCalendar: User (" .  $username . ") does not watch any anime. Skipping\n";
			return;
		}

                foreach( $user->IsWatching as $anime ) {
                        $stats = $anime->getStats()->get()->last();
                        $airing = $anime->getAiring()->get()->last();

			if ( is_null($airing) ) {
				echo "setCalendar: This is a bug! No airing date found for: " . $anime->title_pref . " (" . $anime->mal_id . ")\n";
				continue;
			}
			/**
			 * Check for duplicate entry.
			 */
			$check = Calendar::where('mal_id', $anime->mal_id)->where('user_id', $user->id)->where(
				function($q) use ($airing){
					$q->where('episode', $airing->episode)->orWhere('airing_at', $airing->aired_at);
				})->get()->first();

			/**
			 * Check if we already have an entry. If we don't continue it raises an sql integrity error.
			 */
			if ( ! is_null( $check ) ) {

				$check_watched_anime = DB::table('is_watching')
							    ->where('mal_id', $anime->mal_id)
							    ->where('user_id', $user->id)
							    ->get()->first();

				/**
				 * Update watched episodes in calendar view.
				 */
				if ( $check_watched_anime->episodes_watched != $check->episodes_watched ) {
					echo "setCalendar: update episodes_watched from " . $check->episodes_watched . " to " . $check_watched_anime->episodes_watched . "\n";

					$check->episodes_watched = $check_watched_anime->episodes_watched;
					$check->save();
				}

				/**
				 * Update user set score in calendar view.
				 */
				if ( $check_watched_anime->score_user != $check->score_user ){
					echo "setCalendar: update user score from " . $check->score_user . " to " . $check_watched_anime->score_user . " on anime " . $anime->mal_id . "\n";
					$check->score_user = $check_watched_anime->score_user;
					$check->save();
				}

				/**
				 * Update airing date in calendar view.
				 */
				if ( $airing->aired_at != $check->airing_at ) {
					echo "setCalendar: Missmatch airing schedule and calendar. (" . $anime->mal_id .") Updating calendar...\n";
					$check->airing_at = $airing->aired_at;
					$check->save();
				}

				/**
				 * Update public score in calendar view.
				 */
				if ( $check->score != $stats->score ) {
					echo "setCalendar: Update public score. (" . $anime->mal_id .")\n";
					$check->score = $stats->score;
					$check->save();
				}

				continue;
			}

                        $calendar = new Calendar();     
                        $calendar->mal_id = $anime->mal_id;
                        $calendar->user_id = $anime->Watching->user_id;
                        $calendar->username = $username;
                        $calendar->airing_at  = $airing->aired_at;
                        $calendar->duration = $airing->duration;            
                        $calendar->episode = $airing->episode;             
                        $calendar->episodes_watched = $anime->Watching->episodes_watched;
                        $calendar->episodes_complete = $anime->episodes;
                        $calendar->score = $stats->score;
                        $calendar->score_user = $anime->Watching->score_user;
                        $calendar->title = $anime->title_pref;
                        $calendar->mal_url = $anime->url;

                        $calendar->save();
                }
        }

	public function setIsWatching($username) {
		$user = MALUser::where('username', $username)->get()->first();

		if ( is_null($user) ) {
			echo "setIsWatching: User (" .  $username . ") does not exists. Skipping\n";
			return;
		}
		
		$actually_watching = array();

		foreach( $user->getIsWatching() as $anime_details ) {
			$check = DB::table('is_watching')
				->where('mal_id', $anime_details["mal_id"])
				->where('user_id', $user->id)
				->exists();

			if ( $check ) {
				$actually_watching[] = $anime_details["mal_id"];

				/**
				 * Update watched episodes.
				 */
				$check_watched_anime = DB::table('is_watching')
							    ->where('mal_id', $anime_details["mal_id"])
							    ->where('user_id', $user->id)
							    ->get()->first();
				if ( $check_watched_anime->episodes_watched != $anime_details["episodes_watched"] ) {
					DB::update('UPDATE is_watching SET episodes_watched = ? WHERE mal_id = ? AND user_id = ?',
						[ $anime_details["episodes_watched"], $anime_details["mal_id"], $user->id] );
				}

				/**
				 * Update user given score.
				 */
				if ( $check_watched_anime->score_user != $anime_details["score_user"] ) {
					DB::update('UPDATE is_watching SET score_user = ? WHERE mal_id = ? AND user_id = ?',
						[ $anime_details["score_user"], $anime_details["mal_id"], $user->id] );
				}

				continue;
			}
			$anime = Anime::where('mal_id', $anime_details["mal_id"])->get()->first();
			$user->IsWatching()->save( $anime, $anime_details );

			$actually_watching[] = $anime_details["mal_id"];
		}

		/**
		 * Check for anime which aren't watched anymore and delete them.
		 */
		$not_watched = DB::table('is_watching')
				->where('user_id', $user->id)
				->whereNotIn('mal_id', $actually_watching)
				->delete();
	}

	public function createUser( $username ) {
		$user = MALUser::where('username', $username)->get()->first();

		if ( is_null($user) ) {
			$user = new MALUser();
			$user->set( $username );

			$user->save();
		}

		return $user;
	}

	public function setIsWatchingForAll() {
		$users = MALUser::all();
		foreach( $users as $user ) {
			$this->setIsWatching( $user->username );
		}
	}
	public function setCalendarForAll() {
		$users = MALUser::all();
		foreach( $users as $user ) {
			$this->setCalendar( $user->username );
		}
	}

}